blob: 45688c07e0a783524bfa56a62e32483a80c5b232 [file] [log] [blame]
Madan Jampani7c521002015-03-23 12:23:01 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Madan Jampani7c521002015-03-23 12:23:01 -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
19import java.util.Collection;
Jordan Haltermandae11602018-07-03 00:00:47 -070020import java.util.Map;
Madan Jampani7c521002015-03-23 12:23:01 -070021import java.util.Map.Entry;
Jordan Halterman948d6592017-04-20 17:18:24 -070022import java.util.Objects;
Madan Jampani6e0d1472015-03-24 12:13:44 -070023import java.util.Set;
Madan Jampani7c521002015-03-23 12:23:01 -070024import java.util.concurrent.CompletableFuture;
Madan Jampani0463cf92016-05-04 14:46:08 -070025import java.util.concurrent.Executor;
Madan Jampani346d4f52015-05-04 11:09:39 -070026import java.util.function.BiFunction;
27import java.util.function.Function;
28import java.util.function.Predicate;
Madan Jampani7c521002015-03-23 12:23:01 -070029
Madan Jampani0463cf92016-05-04 14:46:08 -070030import com.google.common.util.concurrent.MoreExecutors;
Jordan Halterman948d6592017-04-20 17:18:24 -070031import org.onosproject.store.primitives.DefaultConsistentMap;
32import org.onosproject.store.primitives.MapUpdate;
Madan Jampani0463cf92016-05-04 14:46:08 -070033
Madan Jampani7c521002015-03-23 12:23:01 -070034/**
35 * A distributed, strongly consistent map whose methods are all executed asynchronously.
36 * <p>
37 * This map offers strong read-after-update (where update == create/update/delete)
38 * consistency. All operations to the map are serialized and applied in a consistent
39 * manner.
40 * <p>
41 * The stronger consistency comes at the expense of availability in
42 * the event of a network partition. A network partition can be either due to
43 * a temporary disruption in network connectivity between participating nodes
44 * or due to a node being temporarily down.
45 * </p><p>
Madan Jampanifa242182016-01-22 13:42:54 -080046 * All values stored in this map are {@link Versioned versioned} and the API
47 * supports optimistic concurrency by allowing conditional updates that take into
48 * consideration the version or value that was previously read.
Madan Jampani7c521002015-03-23 12:23:01 -070049 * </p><p>
50 * This map does not allow null values. All methods can throw a ConsistentMapException
Madan Jampanifa242182016-01-22 13:42:54 -080051 * (which extends {@code RuntimeException}) to indicate failures.
Madan Jampanidfde6ba2016-01-13 21:36:09 -080052 * <p>
53 * All methods of this interface return a {@link CompletableFuture future} immediately
54 * after a successful invocation. The operation itself is executed asynchronous and
55 * the returned future will be {@link CompletableFuture#complete completed} when the
56 * operation finishes.
Madan Jampani7c521002015-03-23 12:23:01 -070057 */
Jordan Haltermandae11602018-07-03 00:00:47 -070058public interface AsyncConsistentMap<K, V>
59 extends DistributedPrimitive, Transactional<MapUpdate<K, V>>, AsyncIterable<Map.Entry<K, Versioned<V>>> {
Madan Jampania090a112016-01-18 16:38:17 -080060
61 @Override
Madan Jampani39fff102016-02-14 13:17:28 -080062 default DistributedPrimitive.Type primitiveType() {
Madan Jampania090a112016-01-18 16:38:17 -080063 return DistributedPrimitive.Type.CONSISTENT_MAP;
64 }
Madan Jampani7c521002015-03-23 12:23:01 -070065
Madan Jampanifa242182016-01-22 13:42:54 -080066 @Override
67 default CompletableFuture<Void> destroy() {
68 return clear();
69 }
70
Madan Jampani7c521002015-03-23 12:23:01 -070071 /**
72 * Returns the number of entries in the map.
73 *
74 * @return a future for map size.
75 */
76 CompletableFuture<Integer> size();
77
78 /**
79 * Returns true if the map is empty.
80 *
81 * @return a future whose value will be true if map has no entries, false otherwise.
82 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -080083 default CompletableFuture<Boolean> isEmpty() {
84 return size().thenApply(s -> s == 0);
85 }
Madan Jampani7c521002015-03-23 12:23:01 -070086
87 /**
88 * Returns true if this map contains a mapping for the specified key.
89 *
90 * @param key key
91 * @return a future whose value will be true if map contains key, false otherwise.
92 */
93 CompletableFuture<Boolean> containsKey(K key);
94
95 /**
96 * Returns true if this map contains the specified value.
97 *
98 * @param value value
99 * @return a future whose value will be true if map contains value, false otherwise.
100 */
101 CompletableFuture<Boolean> containsValue(V value);
102
103 /**
104 * Returns the value (and version) to which the specified key is mapped, or null if this
105 * map contains no mapping for the key.
106 *
107 * @param key the key whose associated value (and version) is to be returned
108 * @return a future value (and version) to which the specified key is mapped, or null if
109 * this map contains no mapping for the key
110 */
111 CompletableFuture<Versioned<V>> get(K key);
112
113 /**
Jordan Haltermanf6272442017-04-20 02:18:08 -0700114 * Returns the value (and version) to which the specified key is mapped, or the provided
115 * default value if this map contains no mapping for the key.
116 *
117 * @param key the key whose associated value (and version) is to be returned
118 * @param defaultValue the default value to return if the key is not set
119 * @return a future value (and version) to which the specified key is mapped, or null if
120 * this map contains no mapping for the key
121 */
122 CompletableFuture<Versioned<V>> getOrDefault(K key, V defaultValue);
123
124 /**
Madan Jampani346d4f52015-05-04 11:09:39 -0700125 * If the specified key is not already associated with a value (or is mapped to null),
126 * attempts to compute its value using the given mapping function and enters it into
127 * this map unless null.
128 * If a conflicting concurrent modification attempt is detected, the returned future
129 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
130 * @param key key with which the specified value is to be associated
131 * @param mappingFunction the function to compute a value
132 * @return the current (existing or computed) value associated with the specified key,
133 * or null if the computed value is null
134 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800135 default CompletableFuture<Versioned<V>> computeIfAbsent(K key,
136 Function<? super K, ? extends V> mappingFunction) {
137 return computeIf(key, Objects::isNull, (k, v) -> mappingFunction.apply(k));
138 }
Madan Jampani346d4f52015-05-04 11:09:39 -0700139
140 /**
141 * If the value for the specified key is present and non-null, attempts to compute a new
142 * mapping given the key and its current mapped value.
143 * If the computed value is null, the current mapping will be removed from the map.
144 * If a conflicting concurrent modification attempt is detected, the returned future
145 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
146 * @param key key with which the specified value is to be associated
147 * @param remappingFunction the function to compute a value
148 * @return the new value associated with the specified key, or null if computed value is null
149 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800150 default CompletableFuture<Versioned<V>> computeIfPresent(K key,
151 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
152 return computeIf(key, Objects::nonNull, remappingFunction);
153 }
Madan Jampani346d4f52015-05-04 11:09:39 -0700154
155 /**
156 * Attempts to compute a mapping for the specified key and its current mapped value (or
157 * null if there is no current mapping).
158 * If the computed value is null, the current mapping (if one exists) will be removed from the map.
159 * If a conflicting concurrent modification attempt is detected, the returned future
160 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
161 * @param key key with which the specified value is to be associated
162 * @param remappingFunction the function to compute a value
163 * @return the new value associated with the specified key, or null if computed value is null
164 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800165 default CompletableFuture<Versioned<V>> compute(K key,
166 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
167 return computeIf(key, v -> true, remappingFunction);
168 }
Madan Jampani346d4f52015-05-04 11:09:39 -0700169
170 /**
171 * If the value for the specified key satisfies a condition, attempts to compute a new
172 * mapping given the key and its current mapped value.
173 * If the computed value is null, the current mapping will be removed from the map.
174 * If a conflicting concurrent modification attempt is detected, the returned future
175 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
176 * @param key key with which the specified value is to be associated
177 * @param condition condition that should evaluate to true for the computation to proceed
178 * @param remappingFunction the function to compute a value
179 * @return the new value associated with the specified key, or the old value if condition evaluates to false
180 */
181 CompletableFuture<Versioned<V>> computeIf(K key,
182 Predicate<? super V> condition,
183 BiFunction<? super K, ? super V, ? extends V> remappingFunction);
184
185 /**
Madan Jampani7c521002015-03-23 12:23:01 -0700186 * Associates the specified value with the specified key in this map (optional operation).
187 * If the map previously contained a mapping for the key, the old value is replaced by the
188 * specified value.
189 *
190 * @param key key with which the specified value is to be associated
191 * @param value value to be associated with the specified key
192 * @return the previous value (and version) associated with key, or null if there was
193 * no mapping for key.
194 */
195 CompletableFuture<Versioned<V>> put(K key, V value);
196
197 /**
Madan Jampani346d4f52015-05-04 11:09:39 -0700198 * Associates the specified value with the specified key in this map (optional operation).
199 * If the map previously contained a mapping for the key, the old value is replaced by the
200 * specified value.
201 *
202 * @param key key with which the specified value is to be associated
203 * @param value value to be associated with the specified key
204 * @return new value.
205 */
206 CompletableFuture<Versioned<V>> putAndGet(K key, V value);
207
208 /**
Madan Jampani7c521002015-03-23 12:23:01 -0700209 * Removes the mapping for a key from this map if it is present (optional operation).
210 *
211 * @param key key whose value is to be removed from the map
212 * @return the value (and version) to which this map previously associated the key,
213 * or null if the map contained no mapping for the key.
214 */
215 CompletableFuture<Versioned<V>> remove(K key);
216
217 /**
218 * Removes all of the mappings from this map (optional operation).
219 * The map will be empty after this call returns.
Madan Jampani6e0d1472015-03-24 12:13:44 -0700220 * @return future that will be successfully completed when the map is cleared
Madan Jampani7c521002015-03-23 12:23:01 -0700221 */
222 CompletableFuture<Void> clear();
223
224 /**
225 * Returns a Set view of the keys contained in this map.
226 * This method differs from the behavior of java.util.Map.keySet() in that
227 * what is returned is a unmodifiable snapshot view of the keys in the ConsistentMap.
228 * Attempts to modify the returned set, whether direct or via its iterator,
229 * result in an UnsupportedOperationException.
230 *
231 * @return a set of the keys contained in this map
232 */
233 CompletableFuture<Set<K>> keySet();
234
235 /**
236 * Returns the collection of values (and associated versions) contained in this map.
237 * This method differs from the behavior of java.util.Map.values() in that
238 * what is returned is a unmodifiable snapshot view of the values in the ConsistentMap.
239 * Attempts to modify the returned collection, whether direct or via its iterator,
240 * result in an UnsupportedOperationException.
241 *
242 * @return a collection of the values (and associated versions) contained in this map
243 */
244 CompletableFuture<Collection<Versioned<V>>> values();
245
246 /**
247 * Returns the set of entries contained in this map.
248 * This method differs from the behavior of java.util.Map.entrySet() in that
249 * what is returned is a unmodifiable snapshot view of the entries in the ConsistentMap.
250 * Attempts to modify the returned set, whether direct or via its iterator,
251 * result in an UnsupportedOperationException.
252 *
253 * @return set of entries contained in this map.
254 */
255 CompletableFuture<Set<Entry<K, Versioned<V>>>> entrySet();
256
257 /**
Aaron Kruglikov3e29f662016-07-13 10:18:10 -0700258 * If the specified key is not already associated with a value associates
259 * it with the given value and returns null, else behaves as a get
260 * returning the existing mapping without making any changes.
Madan Jampani7c521002015-03-23 12:23:01 -0700261 *
262 * @param key key with which the specified value is to be associated
263 * @param value value to be associated with the specified key
264 * @return the previous value associated with the specified key or null
265 * if key does not already mapped to a value.
266 */
267 CompletableFuture<Versioned<V>> putIfAbsent(K key, V value);
268
269 /**
270 * Removes the entry for the specified key only if it is currently
271 * mapped to the specified value.
272 *
273 * @param key key with which the specified value is associated
274 * @param value value expected to be associated with the specified key
275 * @return true if the value was removed
276 */
277 CompletableFuture<Boolean> remove(K key, V value);
278
279 /**
280 * Removes the entry for the specified key only if its current
281 * version in the map is equal to the specified version.
282 *
283 * @param key key with which the specified version is associated
284 * @param version version expected to be associated with the specified key
285 * @return true if the value was removed
286 */
287 CompletableFuture<Boolean> remove(K key, long version);
288
289 /**
Jihwan Kim9887ad92015-12-12 00:23:57 +0900290 * Replaces the entry for the specified key only if there is any value
291 * which associated with specified key.
292 *
293 * @param key key with which the specified value is associated
294 * @param value value expected to be associated with the specified key
295 * @return the previous value associated with the specified key or null
296 */
297 CompletableFuture<Versioned<V>> replace(K key, V value);
298
299 /**
Madan Jampani7c521002015-03-23 12:23:01 -0700300 * Replaces the entry for the specified key only if currently mapped
301 * to the specified value.
302 *
303 * @param key key with which the specified value is associated
304 * @param oldValue value expected to be associated with the specified key
305 * @param newValue value to be associated with the specified key
306 * @return true if the value was replaced
307 */
308 CompletableFuture<Boolean> replace(K key, V oldValue, V newValue);
309
310 /**
311 * Replaces the entry for the specified key only if it is currently mapped to the
312 * specified version.
313 *
314 * @param key key key with which the specified value is associated
315 * @param oldVersion version expected to be associated with the specified key
316 * @param newValue value to be associated with the specified key
317 * @return true if the value was replaced
318 */
319 CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue);
Madan Jampani346d4f52015-05-04 11:09:39 -0700320
321 /**
Madan Jampani50589ac2015-06-08 11:38:46 -0700322 * Registers the specified listener to be notified whenever the map is updated.
323 *
324 * @param listener listener to notify about map events
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800325 * @return future that will be completed when the operation finishes
Madan Jampani50589ac2015-06-08 11:38:46 -0700326 */
Madan Jampani0463cf92016-05-04 14:46:08 -0700327 default CompletableFuture<Void> addListener(MapEventListener<K, V> listener) {
328 return addListener(listener, MoreExecutors.directExecutor());
329 }
330
331 /**
332 * Registers the specified listener to be notified whenever the map is updated.
333 *
334 * @param listener listener to notify about map events
335 * @param executor executor to use for handling incoming map events
336 * @return future that will be completed when the operation finishes
337 */
338 CompletableFuture<Void> addListener(MapEventListener<K, V> listener, Executor executor);
Madan Jampani50589ac2015-06-08 11:38:46 -0700339
340 /**
341 * Unregisters the specified listener such that it will no longer
342 * receive map change notifications.
343 *
344 * @param listener listener to unregister
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800345 * @return future that will be completed when the operation finishes
Madan Jampani50589ac2015-06-08 11:38:46 -0700346 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800347 CompletableFuture<Void> removeListener(MapEventListener<K, V> listener);
Madan Jampani28b2cd62016-02-02 10:49:52 -0800348
349 /**
Madan Jampani74da78b2016-02-09 21:18:36 -0800350 * Returns a new {@link ConsistentMap} that is backed by this instance.
351 *
352 * @return new {@code ConsistentMap} instance
353 */
354 default ConsistentMap<K, V> asConsistentMap() {
Jordan Halterman6440b092017-05-24 17:48:08 -0700355 return asConsistentMap(DistributedPrimitive.DEFAULT_OPERATION_TIMEOUT_MILLIS);
Madan Jampani74da78b2016-02-09 21:18:36 -0800356 }
357
358 /**
Madan Jampani28b2cd62016-02-02 10:49:52 -0800359 * Returns a new {@link ConsistentMap} that is backed by this instance.
360 *
361 * @param timeoutMillis timeout duration for the returned ConsistentMap operations
362 * @return new {@code ConsistentMap} instance
363 */
364 default ConsistentMap<K, V> asConsistentMap(long timeoutMillis) {
365 return new DefaultConsistentMap<>(this, timeoutMillis);
366 }
Madan Jampani7c521002015-03-23 12:23:01 -0700367}