blob: 9a432e09a6925eac6ad1a72d6105731f8057c27a [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 Jampanidfde6ba2016-01-13 21:36:09 -080020import java.util.Objects;
Madan Jampani7c521002015-03-23 12:23:01 -070021import java.util.Map.Entry;
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 Jampani28b2cd62016-02-02 10:49:52 -080029import org.onosproject.store.primitives.DefaultConsistentMap;
Madan Jampani74da78b2016-02-09 21:18:36 -080030import org.onosproject.store.primitives.TransactionId;
Madan Jampani28b2cd62016-02-02 10:49:52 -080031
Madan Jampani0463cf92016-05-04 14:46:08 -070032import com.google.common.util.concurrent.MoreExecutors;
33
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 */
Madan Jampania090a112016-01-18 16:38:17 -080058public interface AsyncConsistentMap<K, V> extends DistributedPrimitive {
59
60 @Override
Madan Jampani39fff102016-02-14 13:17:28 -080061 default DistributedPrimitive.Type primitiveType() {
Madan Jampania090a112016-01-18 16:38:17 -080062 return DistributedPrimitive.Type.CONSISTENT_MAP;
63 }
Madan Jampani7c521002015-03-23 12:23:01 -070064
Madan Jampanifa242182016-01-22 13:42:54 -080065 @Override
66 default CompletableFuture<Void> destroy() {
67 return clear();
68 }
69
Madan Jampani7c521002015-03-23 12:23:01 -070070 /**
71 * Returns the number of entries in the map.
72 *
73 * @return a future for map size.
74 */
75 CompletableFuture<Integer> size();
76
77 /**
78 * Returns true if the map is empty.
79 *
80 * @return a future whose value will be true if map has no entries, false otherwise.
81 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -080082 default CompletableFuture<Boolean> isEmpty() {
83 return size().thenApply(s -> s == 0);
84 }
Madan Jampani7c521002015-03-23 12:23:01 -070085
86 /**
87 * Returns true if this map contains a mapping for the specified key.
88 *
89 * @param key key
90 * @return a future whose value will be true if map contains key, false otherwise.
91 */
92 CompletableFuture<Boolean> containsKey(K key);
93
94 /**
95 * Returns true if this map contains the specified value.
96 *
97 * @param value value
98 * @return a future whose value will be true if map contains value, false otherwise.
99 */
100 CompletableFuture<Boolean> containsValue(V value);
101
102 /**
103 * Returns the value (and version) to which the specified key is mapped, or null if this
104 * map contains no mapping for the key.
105 *
106 * @param key the key whose associated value (and version) is to be returned
107 * @return a future value (and version) to which the specified key is mapped, or null if
108 * this map contains no mapping for the key
109 */
110 CompletableFuture<Versioned<V>> get(K key);
111
112 /**
Madan Jampani346d4f52015-05-04 11:09:39 -0700113 * If the specified key is not already associated with a value (or is mapped to null),
114 * attempts to compute its value using the given mapping function and enters it into
115 * this map unless null.
116 * If a conflicting concurrent modification attempt is detected, the returned future
117 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
118 * @param key key with which the specified value is to be associated
119 * @param mappingFunction the function to compute a value
120 * @return the current (existing or computed) value associated with the specified key,
121 * or null if the computed value is null
122 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800123 default CompletableFuture<Versioned<V>> computeIfAbsent(K key,
124 Function<? super K, ? extends V> mappingFunction) {
125 return computeIf(key, Objects::isNull, (k, v) -> mappingFunction.apply(k));
126 }
Madan Jampani346d4f52015-05-04 11:09:39 -0700127
128 /**
129 * If the value for the specified key is present and non-null, attempts to compute a new
130 * mapping given the key and its current mapped value.
131 * If the computed value is null, the current mapping will be removed from the map.
132 * If a conflicting concurrent modification attempt is detected, the returned future
133 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
134 * @param key key with which the specified value is to be associated
135 * @param remappingFunction the function to compute a value
136 * @return the new value associated with the specified key, or null if computed value is null
137 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800138 default CompletableFuture<Versioned<V>> computeIfPresent(K key,
139 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
140 return computeIf(key, Objects::nonNull, remappingFunction);
141 }
Madan Jampani346d4f52015-05-04 11:09:39 -0700142
143 /**
144 * Attempts to compute a mapping for the specified key and its current mapped value (or
145 * null if there is no current mapping).
146 * If the computed value is null, the current mapping (if one exists) will be removed from the map.
147 * If a conflicting concurrent modification attempt is detected, the returned future
148 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
149 * @param key key with which the specified value is to be associated
150 * @param remappingFunction the function to compute a value
151 * @return the new value associated with the specified key, or null if computed value is null
152 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800153 default CompletableFuture<Versioned<V>> compute(K key,
154 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
155 return computeIf(key, v -> true, remappingFunction);
156 }
Madan Jampani346d4f52015-05-04 11:09:39 -0700157
158 /**
159 * If the value for the specified key satisfies a condition, attempts to compute a new
160 * mapping given the key and its current mapped value.
161 * If the computed value is null, the current mapping will be removed from the map.
162 * If a conflicting concurrent modification attempt is detected, the returned future
163 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
164 * @param key key with which the specified value is to be associated
165 * @param condition condition that should evaluate to true for the computation to proceed
166 * @param remappingFunction the function to compute a value
167 * @return the new value associated with the specified key, or the old value if condition evaluates to false
168 */
169 CompletableFuture<Versioned<V>> computeIf(K key,
170 Predicate<? super V> condition,
171 BiFunction<? super K, ? super V, ? extends V> remappingFunction);
172
173 /**
Madan Jampani7c521002015-03-23 12:23:01 -0700174 * Associates the specified value with the specified key in this map (optional operation).
175 * If the map previously contained a mapping for the key, the old value is replaced by the
176 * specified value.
177 *
178 * @param key key with which the specified value is to be associated
179 * @param value value to be associated with the specified key
180 * @return the previous value (and version) associated with key, or null if there was
181 * no mapping for key.
182 */
183 CompletableFuture<Versioned<V>> put(K key, V value);
184
185 /**
Madan Jampani346d4f52015-05-04 11:09:39 -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 new value.
193 */
194 CompletableFuture<Versioned<V>> putAndGet(K key, V value);
195
196 /**
Madan Jampani7c521002015-03-23 12:23:01 -0700197 * Removes the mapping for a key from this map if it is present (optional operation).
198 *
199 * @param key key whose value is to be removed from the map
200 * @return the value (and version) to which this map previously associated the key,
201 * or null if the map contained no mapping for the key.
202 */
203 CompletableFuture<Versioned<V>> remove(K key);
204
205 /**
206 * Removes all of the mappings from this map (optional operation).
207 * The map will be empty after this call returns.
Madan Jampani6e0d1472015-03-24 12:13:44 -0700208 * @return future that will be successfully completed when the map is cleared
Madan Jampani7c521002015-03-23 12:23:01 -0700209 */
210 CompletableFuture<Void> clear();
211
212 /**
213 * Returns a Set view of the keys contained in this map.
214 * This method differs from the behavior of java.util.Map.keySet() in that
215 * what is returned is a unmodifiable snapshot view of the keys in the ConsistentMap.
216 * Attempts to modify the returned set, whether direct or via its iterator,
217 * result in an UnsupportedOperationException.
218 *
219 * @return a set of the keys contained in this map
220 */
221 CompletableFuture<Set<K>> keySet();
222
223 /**
224 * Returns the collection of values (and associated versions) contained in this map.
225 * This method differs from the behavior of java.util.Map.values() in that
226 * what is returned is a unmodifiable snapshot view of the values in the ConsistentMap.
227 * Attempts to modify the returned collection, whether direct or via its iterator,
228 * result in an UnsupportedOperationException.
229 *
230 * @return a collection of the values (and associated versions) contained in this map
231 */
232 CompletableFuture<Collection<Versioned<V>>> values();
233
234 /**
235 * Returns the set of entries contained in this map.
236 * This method differs from the behavior of java.util.Map.entrySet() in that
237 * what is returned is a unmodifiable snapshot view of the entries in the ConsistentMap.
238 * Attempts to modify the returned set, whether direct or via its iterator,
239 * result in an UnsupportedOperationException.
240 *
241 * @return set of entries contained in this map.
242 */
243 CompletableFuture<Set<Entry<K, Versioned<V>>>> entrySet();
244
245 /**
246 * If the specified key is not already associated with a value
247 * associates it with the given value and returns null, else returns the current value.
248 *
249 * @param key key with which the specified value is to be associated
250 * @param value value to be associated with the specified key
251 * @return the previous value associated with the specified key or null
252 * if key does not already mapped to a value.
253 */
254 CompletableFuture<Versioned<V>> putIfAbsent(K key, V value);
255
256 /**
257 * Removes the entry for the specified key only if it is currently
258 * mapped to the specified value.
259 *
260 * @param key key with which the specified value is associated
261 * @param value value expected to be associated with the specified key
262 * @return true if the value was removed
263 */
264 CompletableFuture<Boolean> remove(K key, V value);
265
266 /**
267 * Removes the entry for the specified key only if its current
268 * version in the map is equal to the specified version.
269 *
270 * @param key key with which the specified version is associated
271 * @param version version expected to be associated with the specified key
272 * @return true if the value was removed
273 */
274 CompletableFuture<Boolean> remove(K key, long version);
275
276 /**
Jihwan Kim9887ad92015-12-12 00:23:57 +0900277 * Replaces the entry for the specified key only if there is any value
278 * which associated with specified key.
279 *
280 * @param key key with which the specified value is associated
281 * @param value value expected to be associated with the specified key
282 * @return the previous value associated with the specified key or null
283 */
284 CompletableFuture<Versioned<V>> replace(K key, V value);
285
286 /**
Madan Jampani7c521002015-03-23 12:23:01 -0700287 * Replaces the entry for the specified key only if currently mapped
288 * to the specified value.
289 *
290 * @param key key with which the specified value is associated
291 * @param oldValue value expected to be associated with the specified key
292 * @param newValue value to be associated with the specified key
293 * @return true if the value was replaced
294 */
295 CompletableFuture<Boolean> replace(K key, V oldValue, V newValue);
296
297 /**
298 * Replaces the entry for the specified key only if it is currently mapped to the
299 * specified version.
300 *
301 * @param key key key with which the specified value is associated
302 * @param oldVersion version 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, long oldVersion, V newValue);
Madan Jampani346d4f52015-05-04 11:09:39 -0700307
308 /**
Madan Jampani50589ac2015-06-08 11:38:46 -0700309 * Registers the specified listener to be notified whenever the map is updated.
310 *
311 * @param listener listener to notify about map events
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800312 * @return future that will be completed when the operation finishes
Madan Jampani50589ac2015-06-08 11:38:46 -0700313 */
Madan Jampani0463cf92016-05-04 14:46:08 -0700314 default CompletableFuture<Void> addListener(MapEventListener<K, V> listener) {
315 return addListener(listener, MoreExecutors.directExecutor());
316 }
317
318 /**
319 * Registers the specified listener to be notified whenever the map is updated.
320 *
321 * @param listener listener to notify about map events
322 * @param executor executor to use for handling incoming map events
323 * @return future that will be completed when the operation finishes
324 */
325 CompletableFuture<Void> addListener(MapEventListener<K, V> listener, Executor executor);
Madan Jampani50589ac2015-06-08 11:38:46 -0700326
327 /**
328 * Unregisters the specified listener such that it will no longer
329 * receive map change notifications.
330 *
331 * @param listener listener to unregister
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800332 * @return future that will be completed when the operation finishes
Madan Jampani50589ac2015-06-08 11:38:46 -0700333 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800334 CompletableFuture<Void> removeListener(MapEventListener<K, V> listener);
Madan Jampani28b2cd62016-02-02 10:49:52 -0800335
336 /**
Madan Jampani74da78b2016-02-09 21:18:36 -0800337 * Prepares a transaction for commitment.
338 * @param transaction transaction
339 * @return {@code true} if prepare is successful and transaction is ready to be committed;
340 * {@code false} otherwise
341 */
342 CompletableFuture<Boolean> prepare(MapTransaction<K, V> transaction);
343
344 /**
345 * Commits a previously prepared transaction.
346 * @param transactionId transaction identifier
347 * @return future that will be completed when the operation finishes
348 */
349 CompletableFuture<Void> commit(TransactionId transactionId);
350
351 /**
352 * Aborts a previously prepared transaction.
353 * @param transactionId transaction identifier
354 * @return future that will be completed when the operation finishes
355 */
356 CompletableFuture<Void> rollback(TransactionId transactionId);
357
358 /**
Madan Jampani542d9e22016-04-05 15:39:55 -0700359 * Prepares a transaction and commits it in one go.
360 * @param transaction transaction
361 * @return {@code true} if operation is successful and updates are committed
362 * {@code false} otherwise
363 */
364 CompletableFuture<Boolean> prepareAndCommit(MapTransaction<K, V> transaction);
365
366 /**
Madan Jampani74da78b2016-02-09 21:18:36 -0800367 * Returns a new {@link ConsistentMap} that is backed by this instance.
368 *
369 * @return new {@code ConsistentMap} instance
370 */
371 default ConsistentMap<K, V> asConsistentMap() {
372 return asConsistentMap(DistributedPrimitive.DEFAULT_OPERTATION_TIMEOUT_MILLIS);
373 }
374
375 /**
Madan Jampani28b2cd62016-02-02 10:49:52 -0800376 * Returns a new {@link ConsistentMap} that is backed by this instance.
377 *
378 * @param timeoutMillis timeout duration for the returned ConsistentMap operations
379 * @return new {@code ConsistentMap} instance
380 */
381 default ConsistentMap<K, V> asConsistentMap(long timeoutMillis) {
382 return new DefaultConsistentMap<>(this, timeoutMillis);
383 }
Madan Jampani7c521002015-03-23 12:23:01 -0700384}