blob: 6a44e060e74ba2608e854a80561a96888255ca13 [file] [log] [blame]
Aaron Kruglikov99702652016-04-11 16:24:18 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
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 */
37public interface AsyncConsistentMultimap<K, V> extends DistributedPrimitive {
38
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 /**
107 * Removes the key-value pair with the specified values if it exists. In
108 * implementations that allow duplicates which matching entry will be
109 * removed is undefined.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700110 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700111 * @param key the key of the pair to be removed
112 * @param value the value of the pair to be removed
113 * @return a future whose value will be true if the map changed because of
114 * this call, false otherwise.
115 */
116 CompletableFuture<Boolean> remove(K key, V value);
117
118 /**
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700119 * Removes the key-value pairs with the specified key and values if they
120 * exist. In implementations that allow duplicates each instance of a key
121 * will remove one matching entry, which one is not defined. Equivalent to
122 * repeated calls to {@code remove()} for each key value pair but more
123 * efficient.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700124 *
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700125 * @param key the key of the pair to be removed
126 * @param values the set of values to be removed
127 * @return a future whose value will be true if the map changes because of
128 * this call, false otherwise.
129 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700130 CompletableFuture<Boolean> removeAll(K key,
131 Collection<? extends V> values);
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700132
133 /**
134 * Removes all values associated with the specified key as well as the key
135 * itself.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700136 *
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700137 * @param key the key whose key-value pairs will be removed
138 * @return a future whose value is the set of values that were removed,
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700139 * which may be empty, if the values did not exist the version will be
140 * less than one.
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700141 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700142 CompletableFuture<Versioned<Collection<? extends V>>> removeAll(K key);
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700143
144 /**
Aaron Kruglikov99702652016-04-11 16:24:18 -0700145 * Adds the set of key-value pairs of the specified key with each of the
146 * values in the iterable if each key-value pair does not already exist,
147 * if the pair does exist the behavior is implementation specific.
148 * (Same as repeated puts but with efficiency gains.)
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700149 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700150 * @param key the key to use for all pairs to be added
151 * @param values the set of values to be added in pairs with the key
152 * @return a future whose value will be true if any change in the map
153 * results from this call, false otherwise
154 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700155 CompletableFuture<Boolean> putAll(K key,
156 Collection<? extends V> values);
Aaron Kruglikov99702652016-04-11 16:24:18 -0700157
158 /**
159 * Stores all the values in values associated with the key specified,
160 * removes all preexisting values and returns a collection of the removed
161 * values which may be empty if the entry did not exist.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700162 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700163 * @param key the key for all entries to be added
164 * @param values the values to be associated with the key
165 * @return a future whose value will be the collection of removed values,
166 * which may be empty
167 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700168 CompletableFuture<Versioned<Collection<? extends V>>> replaceValues(
169 K key, Collection<V> values);
Aaron Kruglikov99702652016-04-11 16:24:18 -0700170
171 /**
Aaron Kruglikov99702652016-04-11 16:24:18 -0700172 * Removes all key-value pairs, after which it will be empty.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700173 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700174 * @return a future whose value is irrelevant, simply used to determine if
175 * the call has completed
176 */
177 CompletableFuture<Void> clear();
178
179 /**
180 * Returns a collection of values associated with the specified key, if the
181 * key is not in the map it will return an empty collection.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700182 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700183 * @param key the key whose associated values will be returned
184 * @return a future whose value will be the collection of the values
185 * associated with the specified key, the collection may be empty
186 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700187 CompletableFuture<Versioned<Collection<? extends V>>> get(K key);
Aaron Kruglikov99702652016-04-11 16:24:18 -0700188
189 /**
190 * Returns a set of the keys contained in this multimap with one or more
191 * associated values.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700192 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700193 * @return a future whose value will be the collection of all keys with one
194 * or more associated values, this may be empty
195 */
196 CompletableFuture<Set<K>> keySet();
197
198 /**
199 * Returns a multiset of the keys present in this multimap with one or more
200 * associated values each. Keys will appear once for each key-value pair
201 * in which they participate.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700202 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700203 * @return a future whose value will be a multiset of the keys, this may
204 * be empty
205 */
206 CompletableFuture<Multiset<K>> keys();
207
208 /**
209 * Returns a collection of values in the set with duplicates permitted, the
210 * size of this collection will equal the size of the map at the time of
211 * creation.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700212 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700213 * @return a future whose value will be a collection of values, this may be
214 * empty
215 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700216 CompletableFuture<Multiset<V>> values();
Aaron Kruglikov99702652016-04-11 16:24:18 -0700217
218 /**
219 * Returns a collection of each key-value pair in this map.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700220 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700221 * @return a future whose value will be a collection of all entries in the
222 * map, this may be empty
223 */
224 CompletableFuture<Collection<Map.Entry<K, V>>> entries();
225
226 /**
Jonathan Hart46bf89b2017-02-27 15:56:42 -0800227 * Registers the specified listener to be notified whenever the map is updated.
228 *
229 * @param listener listener to notify about map events
230 * @return future that will be completed when the operation finishes
231 */
232 default CompletableFuture<Void> addListener(MultimapEventListener<K, V> listener) {
233 return addListener(listener, MoreExecutors.directExecutor());
234 }
235
236 /**
237 * Registers the specified listener to be notified whenever the map is updated.
238 *
239 * @param listener listener to notify about map events
240 * @param executor executor to use for handling incoming map events
241 * @return future that will be completed when the operation finishes
242 */
243 CompletableFuture<Void> addListener(MultimapEventListener<K, V> listener, Executor executor);
244
245 /**
246 * Unregisters the specified listener such that it will no longer
247 * receive map change notifications.
248 *
249 * @param listener listener to unregister
250 * @return future that will be completed when the operation finishes
251 */
252 CompletableFuture<Void> removeListener(MultimapEventListener<K, V> listener);
253
254 /**
Aaron Kruglikov99702652016-04-11 16:24:18 -0700255 * Returns a map of keys to collections of values that reflect the set of
256 * key-value pairs contained in the multimap, where the key value pairs
257 * would be the key paired with each of the values in the collection.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700258 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700259 * @return a future whose value will be a map of keys to collections of
260 * values, the returned map may be empty.
261 */
262 CompletableFuture<Map<K, Collection<V>>> asMap();
Aaron Kruglikov61582a02016-09-06 13:18:58 -0700263
264 /**
265 * Returns a {@code ConsistentMultimap} instance that wraps this map. All
266 * calls will have the same behavior as this map but will be blocking
267 * instead of asynchronous. If a call does not complete within the
268 * default timeout an exception will be produced.
269 *
270 * @return a {@code ConsistentMultimap} which wraps this map, providing
271 * synchronous access to this map
272 */
273 default ConsistentMultimap<K, V> asMultimap() {
Jordan Halterman6440b092017-05-24 17:48:08 -0700274 return asMultimap(DEFAULT_OPERATION_TIMEOUT_MILLIS);
Aaron Kruglikov61582a02016-09-06 13:18:58 -0700275 }
276
277 /**
278 * Returns a {@code ConsistentMultimap} instance that wraps this map. All
279 * calls will have the same behavior as this map but will be blocking
280 * instead of asynchronous. If a call does not complete within the
281 * specified timeout an exception will be produced.
282 *
283 * @param timeoutMillis the number of millis to block while waiting for a
284 * call to return
285 * @return a {@code ConsistentMultimap} which wraps this map, providing
286 * synchronous access to this map
287 */
288 default ConsistentMultimap<K, V> asMultimap(long timeoutMillis) {
289 return new DefaultConsistentMultimap(this, timeoutMillis);
290 }
Aaron Kruglikov99702652016-04-11 16:24:18 -0700291}