blob: f25fadc8931abff176697e26ccbac26464e4dc08 [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 /**
Aaron Kruglikov3e29f662016-07-13 10:18:10 -0700246 * If the specified key is not already associated with a value associates
247 * it with the given value and returns null, else behaves as a get
248 * returning the existing mapping without making any changes.
Madan Jampani7c521002015-03-23 12:23:01 -0700249 *
250 * @param key key with which the specified value is to be associated
251 * @param value value to be associated with the specified key
252 * @return the previous value associated with the specified key or null
253 * if key does not already mapped to a value.
254 */
255 CompletableFuture<Versioned<V>> putIfAbsent(K key, V value);
256
257 /**
258 * Removes the entry for the specified key only if it is currently
259 * mapped to the specified value.
260 *
261 * @param key key with which the specified value is associated
262 * @param value value expected to be associated with the specified key
263 * @return true if the value was removed
264 */
265 CompletableFuture<Boolean> remove(K key, V value);
266
267 /**
268 * Removes the entry for the specified key only if its current
269 * version in the map is equal to the specified version.
270 *
271 * @param key key with which the specified version is associated
272 * @param version version expected to be associated with the specified key
273 * @return true if the value was removed
274 */
275 CompletableFuture<Boolean> remove(K key, long version);
276
277 /**
Jihwan Kim9887ad92015-12-12 00:23:57 +0900278 * Replaces the entry for the specified key only if there is any value
279 * which associated with specified key.
280 *
281 * @param key key with which the specified value is associated
282 * @param value value expected to be associated with the specified key
283 * @return the previous value associated with the specified key or null
284 */
285 CompletableFuture<Versioned<V>> replace(K key, V value);
286
287 /**
Madan Jampani7c521002015-03-23 12:23:01 -0700288 * Replaces the entry for the specified key only if currently mapped
289 * to the specified value.
290 *
291 * @param key key with which the specified value is associated
292 * @param oldValue value expected to be associated with the specified key
293 * @param newValue value to be associated with the specified key
294 * @return true if the value was replaced
295 */
296 CompletableFuture<Boolean> replace(K key, V oldValue, V newValue);
297
298 /**
299 * Replaces the entry for the specified key only if it is currently mapped to the
300 * specified version.
301 *
302 * @param key key key with which the specified value is associated
303 * @param oldVersion version expected to be associated with the specified key
304 * @param newValue value to be associated with the specified key
305 * @return true if the value was replaced
306 */
307 CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue);
Madan Jampani346d4f52015-05-04 11:09:39 -0700308
309 /**
Madan Jampani50589ac2015-06-08 11:38:46 -0700310 * Registers the specified listener to be notified whenever the map is updated.
311 *
312 * @param listener listener to notify about map events
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800313 * @return future that will be completed when the operation finishes
Madan Jampani50589ac2015-06-08 11:38:46 -0700314 */
Madan Jampani0463cf92016-05-04 14:46:08 -0700315 default CompletableFuture<Void> addListener(MapEventListener<K, V> listener) {
316 return addListener(listener, MoreExecutors.directExecutor());
317 }
318
319 /**
320 * Registers the specified listener to be notified whenever the map is updated.
321 *
322 * @param listener listener to notify about map events
323 * @param executor executor to use for handling incoming map events
324 * @return future that will be completed when the operation finishes
325 */
326 CompletableFuture<Void> addListener(MapEventListener<K, V> listener, Executor executor);
Madan Jampani50589ac2015-06-08 11:38:46 -0700327
328 /**
329 * Unregisters the specified listener such that it will no longer
330 * receive map change notifications.
331 *
332 * @param listener listener to unregister
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800333 * @return future that will be completed when the operation finishes
Madan Jampani50589ac2015-06-08 11:38:46 -0700334 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800335 CompletableFuture<Void> removeListener(MapEventListener<K, V> listener);
Madan Jampani28b2cd62016-02-02 10:49:52 -0800336
337 /**
Madan Jampani74da78b2016-02-09 21:18:36 -0800338 * Prepares a transaction for commitment.
339 * @param transaction transaction
340 * @return {@code true} if prepare is successful and transaction is ready to be committed;
341 * {@code false} otherwise
342 */
343 CompletableFuture<Boolean> prepare(MapTransaction<K, V> transaction);
344
345 /**
346 * Commits a previously prepared transaction.
347 * @param transactionId transaction identifier
348 * @return future that will be completed when the operation finishes
349 */
350 CompletableFuture<Void> commit(TransactionId transactionId);
351
352 /**
353 * Aborts a previously prepared transaction.
354 * @param transactionId transaction identifier
355 * @return future that will be completed when the operation finishes
356 */
357 CompletableFuture<Void> rollback(TransactionId transactionId);
358
359 /**
Madan Jampani542d9e22016-04-05 15:39:55 -0700360 * Prepares a transaction and commits it in one go.
361 * @param transaction transaction
362 * @return {@code true} if operation is successful and updates are committed
363 * {@code false} otherwise
364 */
365 CompletableFuture<Boolean> prepareAndCommit(MapTransaction<K, V> transaction);
366
367 /**
Madan Jampani74da78b2016-02-09 21:18:36 -0800368 * Returns a new {@link ConsistentMap} that is backed by this instance.
369 *
370 * @return new {@code ConsistentMap} instance
371 */
372 default ConsistentMap<K, V> asConsistentMap() {
373 return asConsistentMap(DistributedPrimitive.DEFAULT_OPERTATION_TIMEOUT_MILLIS);
374 }
375
376 /**
Madan Jampani28b2cd62016-02-02 10:49:52 -0800377 * Returns a new {@link ConsistentMap} that is backed by this instance.
378 *
379 * @param timeoutMillis timeout duration for the returned ConsistentMap operations
380 * @return new {@code ConsistentMap} instance
381 */
382 default ConsistentMap<K, V> asConsistentMap(long timeoutMillis) {
383 return new DefaultConsistentMap<>(this, timeoutMillis);
384 }
Madan Jampani7c521002015-03-23 12:23:01 -0700385}