blob: 94943b8d33369f85f9c6e3da7f8d26e9a96007db [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 /**
Aaron Kruglikov99702652016-04-11 16:24:18 -0700169 * Adds the set of key-value pairs of the specified key with each of the
170 * values in the iterable if each key-value pair does not already exist,
171 * if the pair does exist the behavior is implementation specific.
172 * (Same as repeated puts but with efficiency gains.)
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700173 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700174 * @param key the key to use for all pairs to be added
175 * @param values the set of values to be added in pairs with the key
176 * @return a future whose value will be true if any change in the map
177 * results from this call, false otherwise
178 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700179 CompletableFuture<Boolean> putAll(K key,
180 Collection<? extends V> values);
Aaron Kruglikov99702652016-04-11 16:24:18 -0700181
182 /**
183 * Stores all the values in values associated with the key specified,
184 * removes all preexisting values and returns a collection of the removed
185 * values which may be empty if the entry did not exist.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700186 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700187 * @param key the key for all entries to be added
188 * @param values the values to be associated with the key
189 * @return a future whose value will be the collection of removed values,
190 * which may be empty
191 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700192 CompletableFuture<Versioned<Collection<? extends V>>> replaceValues(
193 K key, Collection<V> values);
Aaron Kruglikov99702652016-04-11 16:24:18 -0700194
195 /**
Aaron Kruglikov99702652016-04-11 16:24:18 -0700196 * Removes all key-value pairs, after which it will be empty.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700197 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700198 * @return a future whose value is irrelevant, simply used to determine if
199 * the call has completed
200 */
201 CompletableFuture<Void> clear();
202
203 /**
204 * Returns a collection of values associated with the specified key, if the
205 * key is not in the map it will return an empty collection.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700206 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700207 * @param key the key whose associated values will be returned
208 * @return a future whose value will be the collection of the values
209 * associated with the specified key, the collection may be empty
210 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700211 CompletableFuture<Versioned<Collection<? extends V>>> get(K key);
Aaron Kruglikov99702652016-04-11 16:24:18 -0700212
213 /**
214 * Returns a set of the keys contained in this multimap with one or more
215 * associated values.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700216 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700217 * @return a future whose value will be the collection of all keys with one
218 * or more associated values, this may be empty
219 */
220 CompletableFuture<Set<K>> keySet();
221
222 /**
223 * Returns a multiset of the keys present in this multimap with one or more
224 * associated values each. Keys will appear once for each key-value pair
225 * in which they participate.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700226 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700227 * @return a future whose value will be a multiset of the keys, this may
228 * be empty
229 */
230 CompletableFuture<Multiset<K>> keys();
231
232 /**
233 * Returns a collection of values in the set with duplicates permitted, the
234 * size of this collection will equal the size of the map at the time of
235 * creation.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700236 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700237 * @return a future whose value will be a collection of values, this may be
238 * empty
239 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700240 CompletableFuture<Multiset<V>> values();
Aaron Kruglikov99702652016-04-11 16:24:18 -0700241
242 /**
243 * Returns a collection of each key-value pair in this map.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700244 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700245 * @return a future whose value will be a collection of all entries in the
246 * map, this may be empty
247 */
248 CompletableFuture<Collection<Map.Entry<K, V>>> entries();
249
250 /**
Jonathan Hart46bf89b2017-02-27 15:56:42 -0800251 * Registers the specified listener to be notified whenever the map is updated.
252 *
253 * @param listener listener to notify about map events
254 * @return future that will be completed when the operation finishes
255 */
256 default CompletableFuture<Void> addListener(MultimapEventListener<K, V> listener) {
257 return addListener(listener, MoreExecutors.directExecutor());
258 }
259
260 /**
261 * Registers the specified listener to be notified whenever the map is updated.
262 *
263 * @param listener listener to notify about map events
264 * @param executor executor to use for handling incoming map events
265 * @return future that will be completed when the operation finishes
266 */
267 CompletableFuture<Void> addListener(MultimapEventListener<K, V> listener, Executor executor);
268
269 /**
270 * Unregisters the specified listener such that it will no longer
271 * receive map change notifications.
272 *
273 * @param listener listener to unregister
274 * @return future that will be completed when the operation finishes
275 */
276 CompletableFuture<Void> removeListener(MultimapEventListener<K, V> listener);
277
278 /**
Aaron Kruglikov99702652016-04-11 16:24:18 -0700279 * Returns a map of keys to collections of values that reflect the set of
280 * key-value pairs contained in the multimap, where the key value pairs
281 * would be the key paired with each of the values in the collection.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700282 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700283 * @return a future whose value will be a map of keys to collections of
284 * values, the returned map may be empty.
285 */
286 CompletableFuture<Map<K, Collection<V>>> asMap();
Aaron Kruglikov61582a02016-09-06 13:18:58 -0700287
288 /**
289 * Returns a {@code ConsistentMultimap} instance that wraps this map. All
290 * calls will have the same behavior as this map but will be blocking
291 * instead of asynchronous. If a call does not complete within the
292 * default timeout an exception will be produced.
293 *
294 * @return a {@code ConsistentMultimap} which wraps this map, providing
295 * synchronous access to this map
296 */
297 default ConsistentMultimap<K, V> asMultimap() {
Jordan Halterman6440b092017-05-24 17:48:08 -0700298 return asMultimap(DEFAULT_OPERATION_TIMEOUT_MILLIS);
Aaron Kruglikov61582a02016-09-06 13:18:58 -0700299 }
300
301 /**
302 * Returns a {@code ConsistentMultimap} instance that wraps this map. All
303 * calls will have the same behavior as this map but will be blocking
304 * instead of asynchronous. If a call does not complete within the
305 * specified timeout an exception will be produced.
306 *
307 * @param timeoutMillis the number of millis to block while waiting for a
308 * call to return
309 * @return a {@code ConsistentMultimap} which wraps this map, providing
310 * synchronous access to this map
311 */
312 default ConsistentMultimap<K, V> asMultimap(long timeoutMillis) {
313 return new DefaultConsistentMultimap(this, timeoutMillis);
314 }
Aaron Kruglikov99702652016-04-11 16:24:18 -0700315}