blob: 2bec77aec5579dbd0e0e3e3bbb2dc31371d33842 [file] [log] [blame]
Madan Jampani7c521002015-03-23 12:23:01 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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 Jampani346d4f52015-05-04 11:09:39 -070024import java.util.function.BiFunction;
25import java.util.function.Function;
26import java.util.function.Predicate;
Madan Jampani7c521002015-03-23 12:23:01 -070027
28/**
29 * A distributed, strongly consistent map whose methods are all executed asynchronously.
30 * <p>
31 * This map offers strong read-after-update (where update == create/update/delete)
32 * consistency. All operations to the map are serialized and applied in a consistent
33 * manner.
34 * <p>
35 * The stronger consistency comes at the expense of availability in
36 * the event of a network partition. A network partition can be either due to
37 * a temporary disruption in network connectivity between participating nodes
38 * or due to a node being temporarily down.
39 * </p><p>
Madan Jampanifa242182016-01-22 13:42:54 -080040 * All values stored in this map are {@link Versioned versioned} and the API
41 * supports optimistic concurrency by allowing conditional updates that take into
42 * consideration the version or value that was previously read.
Madan Jampani7c521002015-03-23 12:23:01 -070043 * </p><p>
44 * This map does not allow null values. All methods can throw a ConsistentMapException
Madan Jampanifa242182016-01-22 13:42:54 -080045 * (which extends {@code RuntimeException}) to indicate failures.
Madan Jampanidfde6ba2016-01-13 21:36:09 -080046 * <p>
47 * All methods of this interface return a {@link CompletableFuture future} immediately
48 * after a successful invocation. The operation itself is executed asynchronous and
49 * the returned future will be {@link CompletableFuture#complete completed} when the
50 * operation finishes.
Madan Jampani7c521002015-03-23 12:23:01 -070051 */
Madan Jampania090a112016-01-18 16:38:17 -080052public interface AsyncConsistentMap<K, V> extends DistributedPrimitive {
53
54 @Override
55 default DistributedPrimitive.Type type() {
56 return DistributedPrimitive.Type.CONSISTENT_MAP;
57 }
Madan Jampani7c521002015-03-23 12:23:01 -070058
Madan Jampanifa242182016-01-22 13:42:54 -080059 @Override
60 default CompletableFuture<Void> destroy() {
61 return clear();
62 }
63
Madan Jampani7c521002015-03-23 12:23:01 -070064 /**
65 * Returns the number of entries in the map.
66 *
67 * @return a future for map size.
68 */
69 CompletableFuture<Integer> size();
70
71 /**
72 * Returns true if the map is empty.
73 *
74 * @return a future whose value will be true if map has no entries, false otherwise.
75 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -080076 default CompletableFuture<Boolean> isEmpty() {
77 return size().thenApply(s -> s == 0);
78 }
Madan Jampani7c521002015-03-23 12:23:01 -070079
80 /**
81 * Returns true if this map contains a mapping for the specified key.
82 *
83 * @param key key
84 * @return a future whose value will be true if map contains key, false otherwise.
85 */
86 CompletableFuture<Boolean> containsKey(K key);
87
88 /**
89 * Returns true if this map contains the specified value.
90 *
91 * @param value value
92 * @return a future whose value will be true if map contains value, false otherwise.
93 */
94 CompletableFuture<Boolean> containsValue(V value);
95
96 /**
97 * Returns the value (and version) to which the specified key is mapped, or null if this
98 * map contains no mapping for the key.
99 *
100 * @param key the key whose associated value (and version) is to be returned
101 * @return a future value (and version) to which the specified key is mapped, or null if
102 * this map contains no mapping for the key
103 */
104 CompletableFuture<Versioned<V>> get(K key);
105
106 /**
Madan Jampani346d4f52015-05-04 11:09:39 -0700107 * If the specified key is not already associated with a value (or is mapped to null),
108 * attempts to compute its value using the given mapping function and enters it into
109 * this map unless null.
110 * If a conflicting concurrent modification attempt is detected, the returned future
111 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
112 * @param key key with which the specified value is to be associated
113 * @param mappingFunction the function to compute a value
114 * @return the current (existing or computed) value associated with the specified key,
115 * or null if the computed value is null
116 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800117 default CompletableFuture<Versioned<V>> computeIfAbsent(K key,
118 Function<? super K, ? extends V> mappingFunction) {
119 return computeIf(key, Objects::isNull, (k, v) -> mappingFunction.apply(k));
120 }
Madan Jampani346d4f52015-05-04 11:09:39 -0700121
122 /**
123 * If the value for the specified key is present and non-null, attempts to compute a new
124 * mapping given the key and its current mapped value.
125 * If the computed value is null, the current mapping will be removed from the map.
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 remappingFunction the function to compute a value
130 * @return the new value associated with the specified key, or null if computed value is null
131 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800132 default CompletableFuture<Versioned<V>> computeIfPresent(K key,
133 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
134 return computeIf(key, Objects::nonNull, remappingFunction);
135 }
Madan Jampani346d4f52015-05-04 11:09:39 -0700136
137 /**
138 * Attempts to compute a mapping for the specified key and its current mapped value (or
139 * null if there is no current mapping).
140 * If the computed value is null, the current mapping (if one exists) will be removed from the map.
141 * If a conflicting concurrent modification attempt is detected, the returned future
142 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
143 * @param key key with which the specified value is to be associated
144 * @param remappingFunction the function to compute a value
145 * @return the new value associated with the specified key, or null if computed value is null
146 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800147 default CompletableFuture<Versioned<V>> compute(K key,
148 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
149 return computeIf(key, v -> true, remappingFunction);
150 }
Madan Jampani346d4f52015-05-04 11:09:39 -0700151
152 /**
153 * If the value for the specified key satisfies a condition, attempts to compute a new
154 * mapping given the key and its current mapped value.
155 * If the computed value is null, the current mapping will be removed from the map.
156 * If a conflicting concurrent modification attempt is detected, the returned future
157 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
158 * @param key key with which the specified value is to be associated
159 * @param condition condition that should evaluate to true for the computation to proceed
160 * @param remappingFunction the function to compute a value
161 * @return the new value associated with the specified key, or the old value if condition evaluates to false
162 */
163 CompletableFuture<Versioned<V>> computeIf(K key,
164 Predicate<? super V> condition,
165 BiFunction<? super K, ? super V, ? extends V> remappingFunction);
166
167 /**
Madan Jampani7c521002015-03-23 12:23:01 -0700168 * Associates the specified value with the specified key in this map (optional operation).
169 * If the map previously contained a mapping for the key, the old value is replaced by the
170 * specified value.
171 *
172 * @param key key with which the specified value is to be associated
173 * @param value value to be associated with the specified key
174 * @return the previous value (and version) associated with key, or null if there was
175 * no mapping for key.
176 */
177 CompletableFuture<Versioned<V>> put(K key, V value);
178
179 /**
Madan Jampani346d4f52015-05-04 11:09:39 -0700180 * Associates the specified value with the specified key in this map (optional operation).
181 * If the map previously contained a mapping for the key, the old value is replaced by the
182 * specified value.
183 *
184 * @param key key with which the specified value is to be associated
185 * @param value value to be associated with the specified key
186 * @return new value.
187 */
188 CompletableFuture<Versioned<V>> putAndGet(K key, V value);
189
190 /**
Madan Jampani7c521002015-03-23 12:23:01 -0700191 * Removes the mapping for a key from this map if it is present (optional operation).
192 *
193 * @param key key whose value is to be removed from the map
194 * @return the value (and version) to which this map previously associated the key,
195 * or null if the map contained no mapping for the key.
196 */
197 CompletableFuture<Versioned<V>> remove(K key);
198
199 /**
200 * Removes all of the mappings from this map (optional operation).
201 * The map will be empty after this call returns.
Madan Jampani6e0d1472015-03-24 12:13:44 -0700202 * @return future that will be successfully completed when the map is cleared
Madan Jampani7c521002015-03-23 12:23:01 -0700203 */
204 CompletableFuture<Void> clear();
205
206 /**
207 * Returns a Set view of the keys contained in this map.
208 * This method differs from the behavior of java.util.Map.keySet() in that
209 * what is returned is a unmodifiable snapshot view of the keys in the ConsistentMap.
210 * Attempts to modify the returned set, whether direct or via its iterator,
211 * result in an UnsupportedOperationException.
212 *
213 * @return a set of the keys contained in this map
214 */
215 CompletableFuture<Set<K>> keySet();
216
217 /**
218 * Returns the collection of values (and associated versions) contained in this map.
219 * This method differs from the behavior of java.util.Map.values() in that
220 * what is returned is a unmodifiable snapshot view of the values in the ConsistentMap.
221 * Attempts to modify the returned collection, whether direct or via its iterator,
222 * result in an UnsupportedOperationException.
223 *
224 * @return a collection of the values (and associated versions) contained in this map
225 */
226 CompletableFuture<Collection<Versioned<V>>> values();
227
228 /**
229 * Returns the set of entries contained in this map.
230 * This method differs from the behavior of java.util.Map.entrySet() in that
231 * what is returned is a unmodifiable snapshot view of the entries in the ConsistentMap.
232 * Attempts to modify the returned set, whether direct or via its iterator,
233 * result in an UnsupportedOperationException.
234 *
235 * @return set of entries contained in this map.
236 */
237 CompletableFuture<Set<Entry<K, Versioned<V>>>> entrySet();
238
239 /**
240 * If the specified key is not already associated with a value
241 * associates it with the given value and returns null, else returns the current value.
242 *
243 * @param key key with which the specified value is to be associated
244 * @param value value to be associated with the specified key
245 * @return the previous value associated with the specified key or null
246 * if key does not already mapped to a value.
247 */
248 CompletableFuture<Versioned<V>> putIfAbsent(K key, V value);
249
250 /**
251 * Removes the entry for the specified key only if it is currently
252 * mapped to the specified value.
253 *
254 * @param key key with which the specified value is associated
255 * @param value value expected to be associated with the specified key
256 * @return true if the value was removed
257 */
258 CompletableFuture<Boolean> remove(K key, V value);
259
260 /**
261 * Removes the entry for the specified key only if its current
262 * version in the map is equal to the specified version.
263 *
264 * @param key key with which the specified version is associated
265 * @param version version expected to be associated with the specified key
266 * @return true if the value was removed
267 */
268 CompletableFuture<Boolean> remove(K key, long version);
269
270 /**
Jihwan Kim9887ad92015-12-12 00:23:57 +0900271 * Replaces the entry for the specified key only if there is any value
272 * which associated with specified key.
273 *
274 * @param key key with which the specified value is associated
275 * @param value value expected to be associated with the specified key
276 * @return the previous value associated with the specified key or null
277 */
278 CompletableFuture<Versioned<V>> replace(K key, V value);
279
280 /**
Madan Jampani7c521002015-03-23 12:23:01 -0700281 * Replaces the entry for the specified key only if currently mapped
282 * to the specified value.
283 *
284 * @param key key with which the specified value is associated
285 * @param oldValue value expected to be associated with the specified key
286 * @param newValue value to be associated with the specified key
287 * @return true if the value was replaced
288 */
289 CompletableFuture<Boolean> replace(K key, V oldValue, V newValue);
290
291 /**
292 * Replaces the entry for the specified key only if it is currently mapped to the
293 * specified version.
294 *
295 * @param key key key with which the specified value is associated
296 * @param oldVersion version expected to be associated with the specified key
297 * @param newValue value to be associated with the specified key
298 * @return true if the value was replaced
299 */
300 CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue);
Madan Jampani346d4f52015-05-04 11:09:39 -0700301
302 /**
Madan Jampani50589ac2015-06-08 11:38:46 -0700303 * Registers the specified listener to be notified whenever the map is updated.
304 *
305 * @param listener listener to notify about map events
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800306 * @return future that will be completed when the operation finishes
Madan Jampani50589ac2015-06-08 11:38:46 -0700307 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800308 CompletableFuture<Void> addListener(MapEventListener<K, V> listener);
Madan Jampani50589ac2015-06-08 11:38:46 -0700309
310 /**
311 * Unregisters the specified listener such that it will no longer
312 * receive map change notifications.
313 *
314 * @param listener listener to unregister
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800315 * @return future that will be completed when the operation finishes
Madan Jampani50589ac2015-06-08 11:38:46 -0700316 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800317 CompletableFuture<Void> removeListener(MapEventListener<K, V> listener);
Madan Jampani7c521002015-03-23 12:23:01 -0700318}