blob: b86845e44680dec6637bd2a82edc768da8162460 [file] [log] [blame]
Aaron Kruglikov99702652016-04-11 16:24:18 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Aaron Kruglikov99702652016-04-11 16:24:18 -07003 *
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
Aaron Kruglikov99702652016-04-11 16:24:18 -070019import com.google.common.collect.Multiset;
Jonathan Hart46bf89b2017-02-27 15:56:42 -080020import com.google.common.util.concurrent.MoreExecutors;
Aaron Kruglikov61582a02016-09-06 13:18:58 -070021import org.onosproject.store.primitives.DefaultConsistentMultimap;
Aaron Kruglikov99702652016-04-11 16:24:18 -070022
23import java.util.Collection;
24import java.util.Map;
25import java.util.Set;
26import java.util.concurrent.CompletableFuture;
Jonathan Hart46bf89b2017-02-27 15:56:42 -080027import java.util.concurrent.Executor;
Aaron Kruglikov99702652016-04-11 16:24:18 -070028
29/**
30 * Interface for a distributed multimap.
31 *
32 * NOTE: Editing any returned collection will NOT effect the map itself and
33 * changes in the map will NOT be reflected in the returned collections.
34 * Certain operations may be too expensive when backed by a distributed data
35 * structure and have been labeled as such.
36 */
Jordan Halterman5e884352018-05-21 22:11:07 -070037public interface AsyncConsistentMultimap<K, V> extends DistributedPrimitive, AsyncIterable<Map.Entry<K, V>> {
Aaron Kruglikov99702652016-04-11 16:24:18 -070038
39 @Override
40 default DistributedPrimitive.Type primitiveType() {
41 return Type.CONSISTENT_MULTIMAP;
42 }
43
44 @Override
45 default CompletableFuture<Void> destroy() {
46 return clear();
47 }
48
49 /**
50 * Returns the number of key-value pairs in this multimap.
51 * @return the number of key-value pairs
52 */
53 CompletableFuture<Integer> size();
54
55 /**
56 * Returns if this multimap contains no key-value pairs.
57 * @return completable future that will be true if no key-value pairs
58 * exist, false otherwise
59 */
60 CompletableFuture<Boolean> isEmpty();
61
62 /**
63 * Returns true if there is at lease one key-value pair with a key equal to
64 * key.
65 * @param key the key to query
66 * @return a future whose value will be true if the map contains a
67 * key-value pair with key false otherwise
68 */
69 CompletableFuture<Boolean> containsKey(K key);
70
71 /**
72 * Returns true if this map contains at lease one key-value pair with a
73 * value equal to value.
74 * @param value the value to query
75 * @return a future whose value will be true if there is a key-value pair
76 * with the specified value, false otherwise.
77 */
78 CompletableFuture<Boolean> containsValue(V value);
79
80 /**
81 * Returns true if this map contains at least one key-value pair with key
82 * and value specified.
Ray Milkeybb23e0b2016-08-02 17:00:21 -070083 *
84 * @param key key
85 * @param value value
Aaron Kruglikov99702652016-04-11 16:24:18 -070086 * @return a future whose value will be true if there is a key-value pair
87 * with the specified key and value,
88 * false otherwise.
89 */
90 CompletableFuture<Boolean> containsEntry(K key, V value);
91
92 /**
93 * If the key-value pair does not already exist adds either the key value
94 * pair or the value to the set of values associated with the key and
95 * returns true, if the key-value pair already exists then behavior is
96 * implementation specific with some implementations allowing duplicates
97 * and others ignoring put requests for existing entries.
Ray Milkeybb23e0b2016-08-02 17:00:21 -070098 *
Aaron Kruglikov99702652016-04-11 16:24:18 -070099 * @param key the key to add
100 * @param value the value to add
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700101 * @return a future whose value will be true if the map has changed because
Aaron Kruglikov99702652016-04-11 16:24:18 -0700102 * of this call, false otherwise
103 */
104 CompletableFuture<Boolean> put(K key, V value);
105
106 /**
Jordan Halterman8c57a092018-06-04 14:53:06 -0700107 * If the key-value pair does not already exist adds either the key value
108 * pair or the value to the set of values associated with the key and
109 * returns the updated value, if the key-value pair already exists then behavior
110 * is implementation specific with some implementations allowing duplicates
111 * and others ignoring put requests for existing entries.
112 *
113 * @param key the key to add
114 * @param value the value to add
115 * @return a future to be completed with the updated values
116 */
117 CompletableFuture<Versioned<Collection<? extends V>>> putAndGet(K key, V value);
118
119 /**
Aaron Kruglikov99702652016-04-11 16:24:18 -0700120 * Removes the key-value pair with the specified values if it exists. In
121 * implementations that allow duplicates which matching entry will be
122 * removed is undefined.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700123 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700124 * @param key the key of the pair to be removed
125 * @param value the value of the pair to be removed
126 * @return a future whose value will be true if the map changed because of
127 * this call, false otherwise.
128 */
129 CompletableFuture<Boolean> remove(K key, V value);
130
131 /**
Jordan Halterman8c57a092018-06-04 14:53:06 -0700132 * Removes the key-value pair with the specified values if it exists. In
133 * implementations that allow duplicates which matching entry will be
134 * removed is undefined.
135 *
136 * @param key the key of the pair to be removed
137 * @param value the value of the pair to be removed
138 * @return a future to be completed with the updated values
139 */
140 CompletableFuture<Versioned<Collection<? extends V>>> removeAndGet(K key, V value);
141
142 /**
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700143 * Removes the key-value pairs with the specified key and values if they
144 * exist. In implementations that allow duplicates each instance of a key
145 * will remove one matching entry, which one is not defined. Equivalent to
146 * repeated calls to {@code remove()} for each key value pair but more
147 * efficient.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700148 *
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700149 * @param key the key of the pair to be removed
150 * @param values the set of values to be removed
151 * @return a future whose value will be true if the map changes because of
152 * this call, false otherwise.
153 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700154 CompletableFuture<Boolean> removeAll(K key,
155 Collection<? extends V> values);
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700156
157 /**
158 * Removes all values associated with the specified key as well as the key
159 * itself.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700160 *
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700161 * @param key the key whose key-value pairs will be removed
162 * @return a future whose value is the set of values that were removed,
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700163 * which may be empty, if the values did not exist the version will be
164 * less than one.
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700165 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700166 CompletableFuture<Versioned<Collection<? extends V>>> removeAll(K key);
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700167
168 /**
pier4fcb4b22019-10-11 18:19:59 +0200169 * Removes the set of key-value pairs with the specified key and values if they
170 * exist. In implementations that allow duplicates each instance of a key
171 * will remove one matching entry, which one is not defined. Equivalent to
172 * repeated calls to {@code remove()} for each key value pair but more
173 * efficient.
174 *
175 * @param mapping the keys-values to be removed
176 * @return a future whose value will be true if the map changes because of
177 * this call, false otherwise.
178 */
179 CompletableFuture<Boolean> removeAll(Map<K, Collection<? extends V>> mapping);
180
181 /**
Aaron Kruglikov99702652016-04-11 16:24:18 -0700182 * Adds the set of key-value pairs of the specified key with each of the
183 * values in the iterable if each key-value pair does not already exist,
184 * if the pair does exist the behavior is implementation specific.
185 * (Same as repeated puts but with efficiency gains.)
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700186 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700187 * @param key the key to use for all pairs to be added
188 * @param values the set of values to be added in pairs with the key
189 * @return a future whose value will be true if any change in the map
190 * results from this call, false otherwise
191 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700192 CompletableFuture<Boolean> putAll(K key,
193 Collection<? extends V> values);
Aaron Kruglikov99702652016-04-11 16:24:18 -0700194
195 /**
pier4fcb4b22019-10-11 18:19:59 +0200196 * Adds the set of key-value pairs of the specified mapping with each of
197 * the values in the iterable if each key-value pair does not already exist,
198 * if the pair does exist the behavior is implementation specific.
199 * (Same as repeated puts but with efficiency gains.)
200 *
201 * @param mapping the keys-values to be added
202 * @return a future whose value will be true if any change in the map
203 * results from this call, false otherwise
204 */
205 CompletableFuture<Boolean> putAll(Map<K, Collection<? extends V>> mapping);
206
207 /**
Aaron Kruglikov99702652016-04-11 16:24:18 -0700208 * Stores all the values in values associated with the key specified,
209 * removes all preexisting values and returns a collection of the removed
210 * values which may be empty if the entry did not exist.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700211 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700212 * @param key the key for all entries to be added
213 * @param values the values to be associated with the key
214 * @return a future whose value will be the collection of removed values,
215 * which may be empty
216 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700217 CompletableFuture<Versioned<Collection<? extends V>>> replaceValues(
218 K key, Collection<V> values);
Aaron Kruglikov99702652016-04-11 16:24:18 -0700219
220 /**
Aaron Kruglikov99702652016-04-11 16:24:18 -0700221 * Removes all key-value pairs, after which it will be empty.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700222 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700223 * @return a future whose value is irrelevant, simply used to determine if
224 * the call has completed
225 */
226 CompletableFuture<Void> clear();
227
228 /**
229 * Returns a collection of values associated with the specified key, if the
230 * key is not in the map it will return an empty collection.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700231 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700232 * @param key the key whose associated values will be returned
233 * @return a future whose value will be the collection of the values
234 * associated with the specified key, the collection may be empty
235 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700236 CompletableFuture<Versioned<Collection<? extends V>>> get(K key);
Aaron Kruglikov99702652016-04-11 16:24:18 -0700237
238 /**
239 * Returns a set of the keys contained in this multimap with one or more
240 * associated values.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700241 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700242 * @return a future whose value will be the collection of all keys with one
243 * or more associated values, this may be empty
244 */
245 CompletableFuture<Set<K>> keySet();
246
247 /**
248 * Returns a multiset of the keys present in this multimap with one or more
249 * associated values each. Keys will appear once for each key-value pair
250 * in which they participate.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700251 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700252 * @return a future whose value will be a multiset of the keys, this may
253 * be empty
254 */
255 CompletableFuture<Multiset<K>> keys();
256
257 /**
258 * Returns a collection of values in the set with duplicates permitted, the
259 * size of this collection will equal the size of the map at the time of
260 * creation.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700261 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700262 * @return a future whose value will be a collection of values, this may be
263 * empty
264 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700265 CompletableFuture<Multiset<V>> values();
Aaron Kruglikov99702652016-04-11 16:24:18 -0700266
267 /**
268 * Returns a collection of each key-value pair in this map.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700269 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700270 * @return a future whose value will be a collection of all entries in the
271 * map, this may be empty
272 */
273 CompletableFuture<Collection<Map.Entry<K, V>>> entries();
274
275 /**
Jonathan Hart46bf89b2017-02-27 15:56:42 -0800276 * Registers the specified listener to be notified whenever the map is updated.
277 *
278 * @param listener listener to notify about map events
279 * @return future that will be completed when the operation finishes
280 */
281 default CompletableFuture<Void> addListener(MultimapEventListener<K, V> listener) {
282 return addListener(listener, MoreExecutors.directExecutor());
283 }
284
285 /**
286 * Registers the specified listener to be notified whenever the map is updated.
287 *
288 * @param listener listener to notify about map events
289 * @param executor executor to use for handling incoming map events
290 * @return future that will be completed when the operation finishes
291 */
292 CompletableFuture<Void> addListener(MultimapEventListener<K, V> listener, Executor executor);
293
294 /**
295 * Unregisters the specified listener such that it will no longer
296 * receive map change notifications.
297 *
298 * @param listener listener to unregister
299 * @return future that will be completed when the operation finishes
300 */
301 CompletableFuture<Void> removeListener(MultimapEventListener<K, V> listener);
302
303 /**
Aaron Kruglikov99702652016-04-11 16:24:18 -0700304 * Returns a map of keys to collections of values that reflect the set of
305 * key-value pairs contained in the multimap, where the key value pairs
306 * would be the key paired with each of the values in the collection.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700307 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700308 * @return a future whose value will be a map of keys to collections of
309 * values, the returned map may be empty.
310 */
311 CompletableFuture<Map<K, Collection<V>>> asMap();
Aaron Kruglikov61582a02016-09-06 13:18:58 -0700312
313 /**
314 * Returns a {@code ConsistentMultimap} instance that wraps this map. All
315 * calls will have the same behavior as this map but will be blocking
316 * instead of asynchronous. If a call does not complete within the
317 * default timeout an exception will be produced.
318 *
319 * @return a {@code ConsistentMultimap} which wraps this map, providing
320 * synchronous access to this map
321 */
322 default ConsistentMultimap<K, V> asMultimap() {
Jordan Halterman6440b092017-05-24 17:48:08 -0700323 return asMultimap(DEFAULT_OPERATION_TIMEOUT_MILLIS);
Aaron Kruglikov61582a02016-09-06 13:18:58 -0700324 }
325
326 /**
327 * Returns a {@code ConsistentMultimap} instance that wraps this map. All
328 * calls will have the same behavior as this map but will be blocking
329 * instead of asynchronous. If a call does not complete within the
330 * specified timeout an exception will be produced.
331 *
332 * @param timeoutMillis the number of millis to block while waiting for a
333 * call to return
334 * @return a {@code ConsistentMultimap} which wraps this map, providing
335 * synchronous access to this map
336 */
337 default ConsistentMultimap<K, V> asMultimap(long timeoutMillis) {
338 return new DefaultConsistentMultimap(this, timeoutMillis);
339 }
Aaron Kruglikov99702652016-04-11 16:24:18 -0700340}