blob: 9d42baf55a8c57560d3a969f5f51721c55acdd12 [file] [log] [blame]
Aaron Kruglikov99702652016-04-11 16:24:18 -07001/*
2 * Copyright 2016 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.store.service;
18
19import com.google.common.collect.Multimap;
20import com.google.common.collect.Multiset;
21
22import java.util.Collection;
23import java.util.Map;
24import java.util.Set;
25import java.util.concurrent.CompletableFuture;
26
27/**
28 * Interface for a distributed multimap.
29 *
30 * NOTE: Editing any returned collection will NOT effect the map itself and
31 * changes in the map will NOT be reflected in the returned collections.
32 * Certain operations may be too expensive when backed by a distributed data
33 * structure and have been labeled as such.
34 */
35public interface AsyncConsistentMultimap<K, V> extends DistributedPrimitive {
36
37 @Override
38 default DistributedPrimitive.Type primitiveType() {
39 return Type.CONSISTENT_MULTIMAP;
40 }
41
42 @Override
43 default CompletableFuture<Void> destroy() {
44 return clear();
45 }
46
47 /**
48 * Returns the number of key-value pairs in this multimap.
49 * @return the number of key-value pairs
50 */
51 CompletableFuture<Integer> size();
52
53 /**
54 * Returns if this multimap contains no key-value pairs.
55 * @return completable future that will be true if no key-value pairs
56 * exist, false otherwise
57 */
58 CompletableFuture<Boolean> isEmpty();
59
60 /**
61 * Returns true if there is at lease one key-value pair with a key equal to
62 * key.
63 * @param key the key to query
64 * @return a future whose value will be true if the map contains a
65 * key-value pair with key false otherwise
66 */
67 CompletableFuture<Boolean> containsKey(K key);
68
69 /**
70 * Returns true if this map contains at lease one key-value pair with a
71 * value equal to value.
72 * @param value the value to query
73 * @return a future whose value will be true if there is a key-value pair
74 * with the specified value, false otherwise.
75 */
76 CompletableFuture<Boolean> containsValue(V value);
77
78 /**
79 * Returns true if this map contains at least one key-value pair with key
80 * and value specified.
81 * @return a future whose value will be true if there is a key-value pair
82 * with the specified key and value,
83 * false otherwise.
84 */
85 CompletableFuture<Boolean> containsEntry(K key, V value);
86
87 /**
88 * If the key-value pair does not already exist adds either the key value
89 * pair or the value to the set of values associated with the key and
90 * returns true, if the key-value pair already exists then behavior is
91 * implementation specific with some implementations allowing duplicates
92 * and others ignoring put requests for existing entries.
93 * @param key the key to add
94 * @param value the value to add
95 * @return a future whose value will betrue if the map has changed because
96 * of this call, false otherwise
97 */
98 CompletableFuture<Boolean> put(K key, V value);
99
100 /**
101 * Removes the key-value pair with the specified values if it exists. In
102 * implementations that allow duplicates which matching entry will be
103 * removed is undefined.
104 * @param key the key of the pair to be removed
105 * @param value the value of the pair to be removed
106 * @return a future whose value will be true if the map changed because of
107 * this call, false otherwise.
108 */
109 CompletableFuture<Boolean> remove(K key, V value);
110
111 /**
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700112 * Removes the key-value pairs with the specified key and values if they
113 * exist. In implementations that allow duplicates each instance of a key
114 * will remove one matching entry, which one is not defined. Equivalent to
115 * repeated calls to {@code remove()} for each key value pair but more
116 * efficient.
117 * @param key the key of the pair to be removed
118 * @param values the set of values to be removed
119 * @return a future whose value will be true if the map changes because of
120 * this call, false otherwise.
121 */
122 CompletableFuture<Boolean> removeAll(K key, Iterable<? extends V> values);
123
124 /**
125 * Removes all values associated with the specified key as well as the key
126 * itself.
127 * @param key the key whose key-value pairs will be removed
128 * @return a future whose value is the set of values that were removed,
129 * which may be empty
130 */
131 CompletableFuture<Versioned<Collection<byte[]>>> removeAll(K key);
132
133 /**
Aaron Kruglikov99702652016-04-11 16:24:18 -0700134 * Adds the set of key-value pairs of the specified key with each of the
135 * values in the iterable if each key-value pair does not already exist,
136 * if the pair does exist the behavior is implementation specific.
137 * (Same as repeated puts but with efficiency gains.)
138 * @param key the key to use for all pairs to be added
139 * @param values the set of values to be added in pairs with the key
140 * @return a future whose value will be true if any change in the map
141 * results from this call, false otherwise
142 */
143 CompletableFuture<Boolean> putAll(K key, Iterable<? extends V> values);
144
145 /**
146 * Adds all entries from this multimap that are not already present, and
147 * may or may not add duplicate entries depending on the implementation.
148 * @param multiMap the map whose entries should be added
149 * @return a future whose value will be true if any change results from
150 * this call, false otherwise
151 */
152 CompletableFuture<Boolean> putAll(
153 Multimap<? extends K, ? extends V> multiMap);
154
155 /**
156 * Stores all the values in values associated with the key specified,
157 * removes all preexisting values and returns a collection of the removed
158 * values which may be empty if the entry did not exist.
159 * @param key the key for all entries to be added
160 * @param values the values to be associated with the key
161 * @return a future whose value will be the collection of removed values,
162 * which may be empty
163 */
164 CompletableFuture<Collection<V>> replaceValues(K key, Iterable<V> values);
165
166 /**
Aaron Kruglikov99702652016-04-11 16:24:18 -0700167 * Removes all key-value pairs, after which it will be empty.
168 * @return a future whose value is irrelevant, simply used to determine if
169 * the call has completed
170 */
171 CompletableFuture<Void> clear();
172
173 /**
174 * Returns a collection of values associated with the specified key, if the
175 * key is not in the map it will return an empty collection.
176 * @param key the key whose associated values will be returned
177 * @return a future whose value will be the collection of the values
178 * associated with the specified key, the collection may be empty
179 */
180 CompletableFuture<Collection<V>> get(K key);
181
182 /**
183 * Returns a set of the keys contained in this multimap with one or more
184 * associated values.
185 * @return a future whose value will be the collection of all keys with one
186 * or more associated values, this may be empty
187 */
188 CompletableFuture<Set<K>> keySet();
189
190 /**
191 * Returns a multiset of the keys present in this multimap with one or more
192 * associated values each. Keys will appear once for each key-value pair
193 * in which they participate.
194 * @return a future whose value will be a multiset of the keys, this may
195 * be empty
196 */
197 CompletableFuture<Multiset<K>> keys();
198
199 /**
200 * Returns a collection of values in the set with duplicates permitted, the
201 * size of this collection will equal the size of the map at the time of
202 * creation.
203 * @return a future whose value will be a collection of values, this may be
204 * empty
205 */
206 CompletableFuture<Collection<V>> values();
207
208 /**
209 * Returns a collection of each key-value pair in this map.
210 * @return a future whose value will be a collection of all entries in the
211 * map, this may be empty
212 */
213 CompletableFuture<Collection<Map.Entry<K, V>>> entries();
214
215 /**
216 * Returns a map of keys to collections of values that reflect the set of
217 * key-value pairs contained in the multimap, where the key value pairs
218 * would be the key paired with each of the values in the collection.
219 * @return a future whose value will be a map of keys to collections of
220 * values, the returned map may be empty.
221 */
222 CompletableFuture<Map<K, Collection<V>>> asMap();
223}