blob: d83c553bed3fe7f6880bd12f11924dc12a66ed99 [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>
40 * All values stored in this map are versioned and the API supports optimistic
41 * concurrency by allowing conditional updates that take into consideration
42 * the version or value that was previously read.
43 * </p><p>
44 * This map does not allow null values. All methods can throw a ConsistentMapException
45 * (which extends 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
59 /**
60 * Returns the number of entries in the map.
61 *
62 * @return a future for map size.
63 */
64 CompletableFuture<Integer> size();
65
66 /**
67 * Returns true if the map is empty.
68 *
69 * @return a future whose value will be true if map has no entries, false otherwise.
70 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -080071 default CompletableFuture<Boolean> isEmpty() {
72 return size().thenApply(s -> s == 0);
73 }
Madan Jampani7c521002015-03-23 12:23:01 -070074
75 /**
76 * Returns true if this map contains a mapping for the specified key.
77 *
78 * @param key key
79 * @return a future whose value will be true if map contains key, false otherwise.
80 */
81 CompletableFuture<Boolean> containsKey(K key);
82
83 /**
84 * Returns true if this map contains the specified value.
85 *
86 * @param value value
87 * @return a future whose value will be true if map contains value, false otherwise.
88 */
89 CompletableFuture<Boolean> containsValue(V value);
90
91 /**
92 * Returns the value (and version) to which the specified key is mapped, or null if this
93 * map contains no mapping for the key.
94 *
95 * @param key the key whose associated value (and version) is to be returned
96 * @return a future value (and version) to which the specified key is mapped, or null if
97 * this map contains no mapping for the key
98 */
99 CompletableFuture<Versioned<V>> get(K key);
100
101 /**
Madan Jampani346d4f52015-05-04 11:09:39 -0700102 * If the specified key is not already associated with a value (or is mapped to null),
103 * attempts to compute its value using the given mapping function and enters it into
104 * this map unless null.
105 * If a conflicting concurrent modification attempt is detected, the returned future
106 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
107 * @param key key with which the specified value is to be associated
108 * @param mappingFunction the function to compute a value
109 * @return the current (existing or computed) value associated with the specified key,
110 * or null if the computed value is null
111 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800112 default CompletableFuture<Versioned<V>> computeIfAbsent(K key,
113 Function<? super K, ? extends V> mappingFunction) {
114 return computeIf(key, Objects::isNull, (k, v) -> mappingFunction.apply(k));
115 }
Madan Jampani346d4f52015-05-04 11:09:39 -0700116
117 /**
118 * If the value for the specified key is present and non-null, attempts to compute a new
119 * mapping given the key and its current mapped value.
120 * If the computed value is null, the current mapping will be removed from the map.
121 * If a conflicting concurrent modification attempt is detected, the returned future
122 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
123 * @param key key with which the specified value is to be associated
124 * @param remappingFunction the function to compute a value
125 * @return the new value associated with the specified key, or null if computed value is null
126 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800127 default CompletableFuture<Versioned<V>> computeIfPresent(K key,
128 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
129 return computeIf(key, Objects::nonNull, remappingFunction);
130 }
Madan Jampani346d4f52015-05-04 11:09:39 -0700131
132 /**
133 * Attempts to compute a mapping for the specified key and its current mapped value (or
134 * null if there is no current mapping).
135 * If the computed value is null, the current mapping (if one exists) will be removed from the map.
136 * If a conflicting concurrent modification attempt is detected, the returned future
137 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
138 * @param key key with which the specified value is to be associated
139 * @param remappingFunction the function to compute a value
140 * @return the new value associated with the specified key, or null if computed value is null
141 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800142 default CompletableFuture<Versioned<V>> compute(K key,
143 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
144 return computeIf(key, v -> true, remappingFunction);
145 }
Madan Jampani346d4f52015-05-04 11:09:39 -0700146
147 /**
148 * If the value for the specified key satisfies a condition, attempts to compute a new
149 * mapping given the key and its current mapped value.
150 * If the computed value is null, the current mapping will be removed from the map.
151 * If a conflicting concurrent modification attempt is detected, the returned future
152 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
153 * @param key key with which the specified value is to be associated
154 * @param condition condition that should evaluate to true for the computation to proceed
155 * @param remappingFunction the function to compute a value
156 * @return the new value associated with the specified key, or the old value if condition evaluates to false
157 */
158 CompletableFuture<Versioned<V>> computeIf(K key,
159 Predicate<? super V> condition,
160 BiFunction<? super K, ? super V, ? extends V> remappingFunction);
161
162 /**
Madan Jampani7c521002015-03-23 12:23:01 -0700163 * Associates the specified value with the specified key in this map (optional operation).
164 * If the map previously contained a mapping for the key, the old value is replaced by the
165 * specified value.
166 *
167 * @param key key with which the specified value is to be associated
168 * @param value value to be associated with the specified key
169 * @return the previous value (and version) associated with key, or null if there was
170 * no mapping for key.
171 */
172 CompletableFuture<Versioned<V>> put(K key, V value);
173
174 /**
Madan Jampani346d4f52015-05-04 11:09:39 -0700175 * Associates the specified value with the specified key in this map (optional operation).
176 * If the map previously contained a mapping for the key, the old value is replaced by the
177 * specified value.
178 *
179 * @param key key with which the specified value is to be associated
180 * @param value value to be associated with the specified key
181 * @return new value.
182 */
183 CompletableFuture<Versioned<V>> putAndGet(K key, V value);
184
185 /**
Madan Jampani7c521002015-03-23 12:23:01 -0700186 * Removes the mapping for a key from this map if it is present (optional operation).
187 *
188 * @param key key whose value is to be removed from the map
189 * @return the value (and version) to which this map previously associated the key,
190 * or null if the map contained no mapping for the key.
191 */
192 CompletableFuture<Versioned<V>> remove(K key);
193
194 /**
195 * Removes all of the mappings from this map (optional operation).
196 * The map will be empty after this call returns.
Madan Jampani6e0d1472015-03-24 12:13:44 -0700197 * @return future that will be successfully completed when the map is cleared
Madan Jampani7c521002015-03-23 12:23:01 -0700198 */
199 CompletableFuture<Void> clear();
200
201 /**
202 * Returns a Set view of the keys contained in this map.
203 * This method differs from the behavior of java.util.Map.keySet() in that
204 * what is returned is a unmodifiable snapshot view of the keys in the ConsistentMap.
205 * Attempts to modify the returned set, whether direct or via its iterator,
206 * result in an UnsupportedOperationException.
207 *
208 * @return a set of the keys contained in this map
209 */
210 CompletableFuture<Set<K>> keySet();
211
212 /**
213 * Returns the collection of values (and associated versions) contained in this map.
214 * This method differs from the behavior of java.util.Map.values() in that
215 * what is returned is a unmodifiable snapshot view of the values in the ConsistentMap.
216 * Attempts to modify the returned collection, whether direct or via its iterator,
217 * result in an UnsupportedOperationException.
218 *
219 * @return a collection of the values (and associated versions) contained in this map
220 */
221 CompletableFuture<Collection<Versioned<V>>> values();
222
223 /**
224 * Returns the set of entries contained in this map.
225 * This method differs from the behavior of java.util.Map.entrySet() in that
226 * what is returned is a unmodifiable snapshot view of the entries in the ConsistentMap.
227 * Attempts to modify the returned set, whether direct or via its iterator,
228 * result in an UnsupportedOperationException.
229 *
230 * @return set of entries contained in this map.
231 */
232 CompletableFuture<Set<Entry<K, Versioned<V>>>> entrySet();
233
234 /**
235 * If the specified key is not already associated with a value
236 * associates it with the given value and returns null, else returns the current value.
237 *
238 * @param key key with which the specified value is to be associated
239 * @param value value to be associated with the specified key
240 * @return the previous value associated with the specified key or null
241 * if key does not already mapped to a value.
242 */
243 CompletableFuture<Versioned<V>> putIfAbsent(K key, V value);
244
245 /**
246 * Removes the entry for the specified key only if it is currently
247 * mapped to the specified value.
248 *
249 * @param key key with which the specified value is associated
250 * @param value value expected to be associated with the specified key
251 * @return true if the value was removed
252 */
253 CompletableFuture<Boolean> remove(K key, V value);
254
255 /**
256 * Removes the entry for the specified key only if its current
257 * version in the map is equal to the specified version.
258 *
259 * @param key key with which the specified version is associated
260 * @param version version expected to be associated with the specified key
261 * @return true if the value was removed
262 */
263 CompletableFuture<Boolean> remove(K key, long version);
264
265 /**
Jihwan Kim9887ad92015-12-12 00:23:57 +0900266 * Replaces the entry for the specified key only if there is any value
267 * which associated with specified key.
268 *
269 * @param key key with which the specified value is associated
270 * @param value value expected to be associated with the specified key
271 * @return the previous value associated with the specified key or null
272 */
273 CompletableFuture<Versioned<V>> replace(K key, V value);
274
275 /**
Madan Jampani7c521002015-03-23 12:23:01 -0700276 * Replaces the entry for the specified key only if currently mapped
277 * to the specified value.
278 *
279 * @param key key with which the specified value is associated
280 * @param oldValue value expected to be associated with the specified key
281 * @param newValue value to be associated with the specified key
282 * @return true if the value was replaced
283 */
284 CompletableFuture<Boolean> replace(K key, V oldValue, V newValue);
285
286 /**
287 * Replaces the entry for the specified key only if it is currently mapped to the
288 * specified version.
289 *
290 * @param key key key with which the specified value is associated
291 * @param oldVersion version 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, long oldVersion, V newValue);
Madan Jampani346d4f52015-05-04 11:09:39 -0700296
297 /**
Madan Jampani50589ac2015-06-08 11:38:46 -0700298 * Registers the specified listener to be notified whenever the map is updated.
299 *
300 * @param listener listener to notify about map events
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800301 * @return future that will be completed when the operation finishes
Madan Jampani50589ac2015-06-08 11:38:46 -0700302 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800303 CompletableFuture<Void> addListener(MapEventListener<K, V> listener);
Madan Jampani50589ac2015-06-08 11:38:46 -0700304
305 /**
306 * Unregisters the specified listener such that it will no longer
307 * receive map change notifications.
308 *
309 * @param listener listener to unregister
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800310 * @return future that will be completed when the operation finishes
Madan Jampani50589ac2015-06-08 11:38:46 -0700311 */
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800312 CompletableFuture<Void> removeListener(MapEventListener<K, V> listener);
Madan Jampani7c521002015-03-23 12:23:01 -0700313}