blob: 51e31f390e970a6c8aa91546007877dacc134c85 [file] [log] [blame]
Madan Jampani7c521002015-03-23 12:23:01 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
Madan Jampani7c521002015-03-23 12:23:01 -070020import java.util.Map.Entry;
Jordan Halterman948d6592017-04-20 17:18:24 -070021import java.util.Objects;
Madan Jampani6e0d1472015-03-24 12:13:44 -070022import java.util.Set;
Madan Jampani7c521002015-03-23 12:23:01 -070023import java.util.concurrent.CompletableFuture;
Madan Jampani0463cf92016-05-04 14:46:08 -070024import java.util.concurrent.Executor;
Madan Jampani346d4f52015-05-04 11:09:39 -070025import java.util.function.BiFunction;
26import java.util.function.Function;
27import java.util.function.Predicate;
Madan Jampani7c521002015-03-23 12:23:01 -070028
Madan Jampani0463cf92016-05-04 14:46:08 -070029import com.google.common.util.concurrent.MoreExecutors;
Jordan Halterman948d6592017-04-20 17:18:24 -070030import org.onosproject.store.primitives.DefaultConsistentMap;
31import org.onosproject.store.primitives.MapUpdate;
Madan Jampani0463cf92016-05-04 14:46:08 -070032
Madan Jampani7c521002015-03-23 12:23:01 -070033/**
34 * A distributed, strongly consistent map whose methods are all executed asynchronously.
35 * <p>
36 * This map offers strong read-after-update (where update == create/update/delete)
37 * consistency. All operations to the map are serialized and applied in a consistent
38 * manner.
39 * <p>
40 * The stronger consistency comes at the expense of availability in
41 * the event of a network partition. A network partition can be either due to
42 * a temporary disruption in network connectivity between participating nodes
43 * or due to a node being temporarily down.
44 * </p><p>
Madan Jampanifa242182016-01-22 13:42:54 -080045 * All values stored in this map are {@link Versioned versioned} and the API
46 * supports optimistic concurrency by allowing conditional updates that take into
47 * consideration the version or value that was previously read.
Madan Jampani7c521002015-03-23 12:23:01 -070048 * </p><p>
49 * This map does not allow null values. All methods can throw a ConsistentMapException
Madan Jampanifa242182016-01-22 13:42:54 -080050 * (which extends {@code RuntimeException}) to indicate failures.
Madan Jampanidfde6ba2016-01-13 21:36:09 -080051 * <p>
52 * All methods of this interface return a {@link CompletableFuture future} immediately
53 * after a successful invocation. The operation itself is executed asynchronous and
54 * the returned future will be {@link CompletableFuture#complete completed} when the
55 * operation finishes.
Madan Jampani7c521002015-03-23 12:23:01 -070056 */
Jordan Halterman948d6592017-04-20 17:18:24 -070057public interface AsyncConsistentMap<K, V> extends DistributedPrimitive, Transactional<MapUpdate<K, V>> {
Madan Jampania090a112016-01-18 16:38:17 -080058
59 @Override
Madan Jampani39fff102016-02-14 13:17:28 -080060 default DistributedPrimitive.Type primitiveType() {
Madan Jampania090a112016-01-18 16:38:17 -080061 return DistributedPrimitive.Type.CONSISTENT_MAP;
62 }
Madan Jampani7c521002015-03-23 12:23:01 -070063
Madan Jampanifa242182016-01-22 13:42:54 -080064 @Override
65 default CompletableFuture<Void> destroy() {
66 return clear();
67 }
68
Madan Jampani7c521002015-03-23 12:23:01 -070069 /**
70 * Returns the number of entries in the map.
71 *
72 * @return a future for map size.
73 */
74 CompletableFuture<Integer> size();
75
76 /**
77 * Returns true if the map is empty.
78 *
79 * @return a future whose value will be true if map has no entries, false otherwise.
80 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -080081 default CompletableFuture<Boolean> isEmpty() {
82 return size().thenApply(s -> s == 0);
83 }
Madan Jampani7c521002015-03-23 12:23:01 -070084
85 /**
86 * Returns true if this map contains a mapping for the specified key.
87 *
88 * @param key key
89 * @return a future whose value will be true if map contains key, false otherwise.
90 */
91 CompletableFuture<Boolean> containsKey(K key);
92
93 /**
94 * Returns true if this map contains the specified value.
95 *
96 * @param value value
97 * @return a future whose value will be true if map contains value, false otherwise.
98 */
99 CompletableFuture<Boolean> containsValue(V value);
100
101 /**
102 * Returns the value (and version) to which the specified key is mapped, or null if this
103 * map contains no mapping for the key.
104 *
105 * @param key the key whose associated value (and version) is to be returned
106 * @return a future value (and version) to which the specified key is mapped, or null if
107 * this map contains no mapping for the key
108 */
109 CompletableFuture<Versioned<V>> get(K key);
110
111 /**
Jordan Haltermanf6272442017-04-20 02:18:08 -0700112 * Returns the value (and version) to which the specified key is mapped, or the provided
113 * default value if this map contains no mapping for the key.
114 *
115 * @param key the key whose associated value (and version) is to be returned
116 * @param defaultValue the default value to return if the key is not set
117 * @return a future value (and version) to which the specified key is mapped, or null if
118 * this map contains no mapping for the key
119 */
120 CompletableFuture<Versioned<V>> getOrDefault(K key, V defaultValue);
121
122 /**
Madan Jampani346d4f52015-05-04 11:09:39 -0700123 * If the specified key is not already associated with a value (or is mapped to null),
124 * attempts to compute its value using the given mapping function and enters it into
125 * this map unless null.
126 * If a conflicting concurrent modification attempt is detected, the returned future
127 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
128 * @param key key with which the specified value is to be associated
129 * @param mappingFunction the function to compute a value
130 * @return the current (existing or computed) value associated with the specified key,
131 * or null if the computed value is null
132 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800133 default CompletableFuture<Versioned<V>> computeIfAbsent(K key,
134 Function<? super K, ? extends V> mappingFunction) {
135 return computeIf(key, Objects::isNull, (k, v) -> mappingFunction.apply(k));
136 }
Madan Jampani346d4f52015-05-04 11:09:39 -0700137
138 /**
139 * If the value for the specified key is present and non-null, attempts to compute a new
140 * mapping given the key and its current mapped value.
141 * If the computed value is null, the current mapping will be removed from the map.
142 * If a conflicting concurrent modification attempt is detected, the returned future
143 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
144 * @param key key with which the specified value is to be associated
145 * @param remappingFunction the function to compute a value
146 * @return the new value associated with the specified key, or null if computed value is null
147 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800148 default CompletableFuture<Versioned<V>> computeIfPresent(K key,
149 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
150 return computeIf(key, Objects::nonNull, remappingFunction);
151 }
Madan Jampani346d4f52015-05-04 11:09:39 -0700152
153 /**
154 * Attempts to compute a mapping for the specified key and its current mapped value (or
155 * null if there is no current mapping).
156 * If the computed value is null, the current mapping (if one exists) will be removed from the map.
157 * If a conflicting concurrent modification attempt is detected, the returned future
158 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
159 * @param key key with which the specified value is to be associated
160 * @param remappingFunction the function to compute a value
161 * @return the new value associated with the specified key, or null if computed value is null
162 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800163 default CompletableFuture<Versioned<V>> compute(K key,
164 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
165 return computeIf(key, v -> true, remappingFunction);
166 }
Madan Jampani346d4f52015-05-04 11:09:39 -0700167
168 /**
169 * If the value for the specified key satisfies a condition, attempts to compute a new
170 * mapping given the key and its current mapped value.
171 * If the computed value is null, the current mapping will be removed from the map.
172 * If a conflicting concurrent modification attempt is detected, the returned future
173 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
174 * @param key key with which the specified value is to be associated
175 * @param condition condition that should evaluate to true for the computation to proceed
176 * @param remappingFunction the function to compute a value
177 * @return the new value associated with the specified key, or the old value if condition evaluates to false
178 */
179 CompletableFuture<Versioned<V>> computeIf(K key,
180 Predicate<? super V> condition,
181 BiFunction<? super K, ? super V, ? extends V> remappingFunction);
182
183 /**
Madan Jampani7c521002015-03-23 12:23:01 -0700184 * Associates the specified value with the specified key in this map (optional operation).
185 * If the map previously contained a mapping for the key, the old value is replaced by the
186 * specified value.
187 *
188 * @param key key with which the specified value is to be associated
189 * @param value value to be associated with the specified key
190 * @return the previous value (and version) associated with key, or null if there was
191 * no mapping for key.
192 */
193 CompletableFuture<Versioned<V>> put(K key, V value);
194
195 /**
Madan Jampani346d4f52015-05-04 11:09:39 -0700196 * Associates the specified value with the specified key in this map (optional operation).
197 * If the map previously contained a mapping for the key, the old value is replaced by the
198 * specified value.
199 *
200 * @param key key with which the specified value is to be associated
201 * @param value value to be associated with the specified key
202 * @return new value.
203 */
204 CompletableFuture<Versioned<V>> putAndGet(K key, V value);
205
206 /**
Madan Jampani7c521002015-03-23 12:23:01 -0700207 * Removes the mapping for a key from this map if it is present (optional operation).
208 *
209 * @param key key whose value is to be removed from the map
210 * @return the value (and version) to which this map previously associated the key,
211 * or null if the map contained no mapping for the key.
212 */
213 CompletableFuture<Versioned<V>> remove(K key);
214
215 /**
216 * Removes all of the mappings from this map (optional operation).
217 * The map will be empty after this call returns.
Madan Jampani6e0d1472015-03-24 12:13:44 -0700218 * @return future that will be successfully completed when the map is cleared
Madan Jampani7c521002015-03-23 12:23:01 -0700219 */
220 CompletableFuture<Void> clear();
221
222 /**
223 * Returns a Set view of the keys contained in this map.
224 * This method differs from the behavior of java.util.Map.keySet() in that
225 * what is returned is a unmodifiable snapshot view of the keys in the ConsistentMap.
226 * Attempts to modify the returned set, whether direct or via its iterator,
227 * result in an UnsupportedOperationException.
228 *
229 * @return a set of the keys contained in this map
230 */
231 CompletableFuture<Set<K>> keySet();
232
233 /**
234 * Returns the collection of values (and associated versions) contained in this map.
235 * This method differs from the behavior of java.util.Map.values() in that
236 * what is returned is a unmodifiable snapshot view of the values in the ConsistentMap.
237 * Attempts to modify the returned collection, whether direct or via its iterator,
238 * result in an UnsupportedOperationException.
239 *
240 * @return a collection of the values (and associated versions) contained in this map
241 */
242 CompletableFuture<Collection<Versioned<V>>> values();
243
244 /**
245 * Returns the set of entries contained in this map.
246 * This method differs from the behavior of java.util.Map.entrySet() in that
247 * what is returned is a unmodifiable snapshot view of the entries in the ConsistentMap.
248 * Attempts to modify the returned set, whether direct or via its iterator,
249 * result in an UnsupportedOperationException.
250 *
251 * @return set of entries contained in this map.
252 */
253 CompletableFuture<Set<Entry<K, Versioned<V>>>> entrySet();
254
255 /**
Aaron Kruglikov3e29f662016-07-13 10:18:10 -0700256 * If the specified key is not already associated with a value associates
257 * it with the given value and returns null, else behaves as a get
258 * returning the existing mapping without making any changes.
Madan Jampani7c521002015-03-23 12:23:01 -0700259 *
260 * @param key key with which the specified value is to be associated
261 * @param value value to be associated with the specified key
262 * @return the previous value associated with the specified key or null
263 * if key does not already mapped to a value.
264 */
265 CompletableFuture<Versioned<V>> putIfAbsent(K key, V value);
266
267 /**
268 * Removes the entry for the specified key only if it is currently
269 * mapped to the specified value.
270 *
271 * @param key key with which the specified value is associated
272 * @param value value expected to be associated with the specified key
273 * @return true if the value was removed
274 */
275 CompletableFuture<Boolean> remove(K key, V value);
276
277 /**
278 * Removes the entry for the specified key only if its current
279 * version in the map is equal to the specified version.
280 *
281 * @param key key with which the specified version is associated
282 * @param version version expected to be associated with the specified key
283 * @return true if the value was removed
284 */
285 CompletableFuture<Boolean> remove(K key, long version);
286
287 /**
Jihwan Kim9887ad92015-12-12 00:23:57 +0900288 * Replaces the entry for the specified key only if there is any value
289 * which associated with specified key.
290 *
291 * @param key key with which the specified value is associated
292 * @param value value expected to be associated with the specified key
293 * @return the previous value associated with the specified key or null
294 */
295 CompletableFuture<Versioned<V>> replace(K key, V value);
296
297 /**
Madan Jampani7c521002015-03-23 12:23:01 -0700298 * Replaces the entry for the specified key only if currently mapped
299 * to the specified value.
300 *
301 * @param key key with which the specified value is associated
302 * @param oldValue value expected to be associated with the specified key
303 * @param newValue value to be associated with the specified key
304 * @return true if the value was replaced
305 */
306 CompletableFuture<Boolean> replace(K key, V oldValue, V newValue);
307
308 /**
309 * Replaces the entry for the specified key only if it is currently mapped to the
310 * specified version.
311 *
312 * @param key key key with which the specified value is associated
313 * @param oldVersion version expected to be associated with the specified key
314 * @param newValue value to be associated with the specified key
315 * @return true if the value was replaced
316 */
317 CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue);
Madan Jampani346d4f52015-05-04 11:09:39 -0700318
319 /**
Madan Jampani50589ac2015-06-08 11:38:46 -0700320 * Registers the specified listener to be notified whenever the map is updated.
321 *
322 * @param listener listener to notify about map events
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800323 * @return future that will be completed when the operation finishes
Madan Jampani50589ac2015-06-08 11:38:46 -0700324 */
Madan Jampani0463cf92016-05-04 14:46:08 -0700325 default CompletableFuture<Void> addListener(MapEventListener<K, V> listener) {
326 return addListener(listener, MoreExecutors.directExecutor());
327 }
328
329 /**
330 * Registers the specified listener to be notified whenever the map is updated.
331 *
332 * @param listener listener to notify about map events
333 * @param executor executor to use for handling incoming map events
334 * @return future that will be completed when the operation finishes
335 */
336 CompletableFuture<Void> addListener(MapEventListener<K, V> listener, Executor executor);
Madan Jampani50589ac2015-06-08 11:38:46 -0700337
338 /**
339 * Unregisters the specified listener such that it will no longer
340 * receive map change notifications.
341 *
342 * @param listener listener to unregister
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800343 * @return future that will be completed when the operation finishes
Madan Jampani50589ac2015-06-08 11:38:46 -0700344 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800345 CompletableFuture<Void> removeListener(MapEventListener<K, V> listener);
Madan Jampani28b2cd62016-02-02 10:49:52 -0800346
347 /**
Madan Jampani74da78b2016-02-09 21:18:36 -0800348 * Returns a new {@link ConsistentMap} that is backed by this instance.
349 *
350 * @return new {@code ConsistentMap} instance
351 */
352 default ConsistentMap<K, V> asConsistentMap() {
Jordan Halterman6440b092017-05-24 17:48:08 -0700353 return asConsistentMap(DistributedPrimitive.DEFAULT_OPERATION_TIMEOUT_MILLIS);
Madan Jampani74da78b2016-02-09 21:18:36 -0800354 }
355
356 /**
Madan Jampani28b2cd62016-02-02 10:49:52 -0800357 * Returns a new {@link ConsistentMap} that is backed by this instance.
358 *
359 * @param timeoutMillis timeout duration for the returned ConsistentMap operations
360 * @return new {@code ConsistentMap} instance
361 */
362 default ConsistentMap<K, V> asConsistentMap(long timeoutMillis) {
363 return new DefaultConsistentMap<>(this, timeoutMillis);
364 }
Madan Jampani7c521002015-03-23 12:23:01 -0700365}