blob: 1779256f7f3a164c5b5790b70bf5af830e20f98f [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 Jampani7c521002015-03-23 12:23:01 -070020import java.util.Map.Entry;
Madan Jampani346d4f52015-05-04 11:09:39 -070021import java.util.Optional;
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.
46 *
47 */
48public interface AsyncConsistentMap<K, V> {
49
50 /**
51 * Returns the number of entries in the map.
52 *
53 * @return a future for map size.
54 */
55 CompletableFuture<Integer> size();
56
57 /**
58 * Returns true if the map is empty.
59 *
60 * @return a future whose value will be true if map has no entries, false otherwise.
61 */
62 CompletableFuture<Boolean> isEmpty();
63
64 /**
65 * Returns true if this map contains a mapping for the specified key.
66 *
67 * @param key key
68 * @return a future whose value will be true if map contains key, false otherwise.
69 */
70 CompletableFuture<Boolean> containsKey(K key);
71
72 /**
73 * Returns true if this map contains the specified value.
74 *
75 * @param value value
76 * @return a future whose value will be true if map contains value, false otherwise.
77 */
78 CompletableFuture<Boolean> containsValue(V value);
79
80 /**
81 * Returns the value (and version) to which the specified key is mapped, or null if this
82 * map contains no mapping for the key.
83 *
84 * @param key the key whose associated value (and version) is to be returned
85 * @return a future value (and version) to which the specified key is mapped, or null if
86 * this map contains no mapping for the key
87 */
88 CompletableFuture<Versioned<V>> get(K key);
89
90 /**
Madan Jampani346d4f52015-05-04 11:09:39 -070091 * If the specified key is not already associated with a value (or is mapped to null),
92 * attempts to compute its value using the given mapping function and enters it into
93 * this map unless null.
94 * If a conflicting concurrent modification attempt is detected, the returned future
95 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
96 * @param key key with which the specified value is to be associated
97 * @param mappingFunction the function to compute a value
98 * @return the current (existing or computed) value associated with the specified key,
99 * or null if the computed value is null
100 */
101 CompletableFuture<Versioned<V>> computeIfAbsent(K key,
102 Function<? super K, ? extends V> mappingFunction);
103
104 /**
105 * If the value for the specified key is present and non-null, attempts to compute a new
106 * mapping given the key and its current mapped value.
107 * If the computed value is null, the current mapping will be removed from the map.
108 * If a conflicting concurrent modification attempt is detected, the returned future
109 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
110 * @param key key with which the specified value is to be associated
111 * @param remappingFunction the function to compute a value
112 * @return the new value associated with the specified key, or null if computed value is null
113 */
114 CompletableFuture<Versioned<V>> computeIfPresent(K key,
115 BiFunction<? super K, ? super V, ? extends V> remappingFunction);
116
117 /**
118 * Attempts to compute a mapping for the specified key and its current mapped value (or
119 * null if there is no current mapping).
120 * If the computed value is null, the current mapping (if one exists) 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 */
127 CompletableFuture<Versioned<V>> compute(K key,
128 BiFunction<? super K, ? super V, ? extends V> remappingFunction);
129
130 /**
131 * If the value for the specified key satisfies a condition, attempts to compute a new
132 * mapping given the key and its current mapped value.
133 * If the computed value is null, the current mapping will be removed from the map.
134 * If a conflicting concurrent modification attempt is detected, the returned future
135 * will be completed exceptionally with ConsistentMapException.ConcurrentModification.
136 * @param key key with which the specified value is to be associated
137 * @param condition condition that should evaluate to true for the computation to proceed
138 * @param remappingFunction the function to compute a value
139 * @return the new value associated with the specified key, or the old value if condition evaluates to false
140 */
141 CompletableFuture<Versioned<V>> computeIf(K key,
142 Predicate<? super V> condition,
143 BiFunction<? super K, ? super V, ? extends V> remappingFunction);
144
145 /**
Madan Jampani7c521002015-03-23 12:23:01 -0700146 * Associates the specified value with the specified key in this map (optional operation).
147 * If the map previously contained a mapping for the key, the old value is replaced by the
148 * specified value.
149 *
150 * @param key key with which the specified value is to be associated
151 * @param value value to be associated with the specified key
152 * @return the previous value (and version) associated with key, or null if there was
153 * no mapping for key.
154 */
155 CompletableFuture<Versioned<V>> put(K key, V value);
156
157 /**
Madan Jampani346d4f52015-05-04 11:09:39 -0700158 * Associates the specified value with the specified key in this map (optional operation).
159 * If the map previously contained a mapping for the key, the old value is replaced by the
160 * specified value.
161 *
162 * @param key key with which the specified value is to be associated
163 * @param value value to be associated with the specified key
164 * @return new value.
165 */
166 CompletableFuture<Versioned<V>> putAndGet(K key, V value);
167
168 /**
169 * Associates the specified value with the specified key in this map (optional operation).
170 * If the map previously contained a mapping for the key, the old value is replaced by the
171 * specified value.
172 *
173 * @param key key with which the specified value is to be associated
174 * @param value value to be associated with the specified key
175 * @return optional updated value. Will be empty if update did not happen
176 */
177 CompletableFuture<Optional<Versioned<V>>> putIfAbsentAndGet(K key, V value);
178
179 /**
Madan Jampani7c521002015-03-23 12:23:01 -0700180 * Removes the mapping for a key from this map if it is present (optional operation).
181 *
182 * @param key key whose value is to be removed from the map
183 * @return the value (and version) to which this map previously associated the key,
184 * or null if the map contained no mapping for the key.
185 */
186 CompletableFuture<Versioned<V>> remove(K key);
187
188 /**
189 * Removes all of the mappings from this map (optional operation).
190 * The map will be empty after this call returns.
Madan Jampani6e0d1472015-03-24 12:13:44 -0700191 * @return future that will be successfully completed when the map is cleared
Madan Jampani7c521002015-03-23 12:23:01 -0700192 */
193 CompletableFuture<Void> clear();
194
195 /**
196 * Returns a Set view of the keys contained in this map.
197 * This method differs from the behavior of java.util.Map.keySet() in that
198 * what is returned is a unmodifiable snapshot view of the keys in the ConsistentMap.
199 * Attempts to modify the returned set, whether direct or via its iterator,
200 * result in an UnsupportedOperationException.
201 *
202 * @return a set of the keys contained in this map
203 */
204 CompletableFuture<Set<K>> keySet();
205
206 /**
207 * Returns the collection of values (and associated versions) contained in this map.
208 * This method differs from the behavior of java.util.Map.values() in that
209 * what is returned is a unmodifiable snapshot view of the values in the ConsistentMap.
210 * Attempts to modify the returned collection, whether direct or via its iterator,
211 * result in an UnsupportedOperationException.
212 *
213 * @return a collection of the values (and associated versions) contained in this map
214 */
215 CompletableFuture<Collection<Versioned<V>>> values();
216
217 /**
218 * Returns the set of entries contained in this map.
219 * This method differs from the behavior of java.util.Map.entrySet() in that
220 * what is returned is a unmodifiable snapshot view of the entries in the ConsistentMap.
221 * Attempts to modify the returned set, whether direct or via its iterator,
222 * result in an UnsupportedOperationException.
223 *
224 * @return set of entries contained in this map.
225 */
226 CompletableFuture<Set<Entry<K, Versioned<V>>>> entrySet();
227
228 /**
229 * If the specified key is not already associated with a value
230 * associates it with the given value and returns null, else returns the current value.
231 *
232 * @param key key with which the specified value is to be associated
233 * @param value value to be associated with the specified key
234 * @return the previous value associated with the specified key or null
235 * if key does not already mapped to a value.
236 */
237 CompletableFuture<Versioned<V>> putIfAbsent(K key, V value);
238
239 /**
240 * Removes the entry for the specified key only if it is currently
241 * mapped to the specified value.
242 *
243 * @param key key with which the specified value is associated
244 * @param value value expected to be associated with the specified key
245 * @return true if the value was removed
246 */
247 CompletableFuture<Boolean> remove(K key, V value);
248
249 /**
250 * Removes the entry for the specified key only if its current
251 * version in the map is equal to the specified version.
252 *
253 * @param key key with which the specified version is associated
254 * @param version version expected to be associated with the specified key
255 * @return true if the value was removed
256 */
257 CompletableFuture<Boolean> remove(K key, long version);
258
259 /**
260 * Replaces the entry for the specified key only if currently mapped
261 * to the specified value.
262 *
263 * @param key key with which the specified value is associated
264 * @param oldValue value expected to be associated with the specified key
265 * @param newValue value to be associated with the specified key
266 * @return true if the value was replaced
267 */
268 CompletableFuture<Boolean> replace(K key, V oldValue, V newValue);
269
270 /**
271 * Replaces the entry for the specified key only if it is currently mapped to the
272 * specified version.
273 *
274 * @param key key key with which the specified value is associated
275 * @param oldVersion version expected to be associated with the specified key
276 * @param newValue value to be associated with the specified key
277 * @return true if the value was replaced
278 */
279 CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue);
Madan Jampani346d4f52015-05-04 11:09:39 -0700280
281 /**
282 * Replaces the entry for the specified key only if it is currently mapped to the
283 * specified version.
284 *
285 * @param key key key with which the specified value is associated
286 * @param oldVersion version expected to be associated with the specified key
287 * @param newValue value to be associated with the specified key
288 * @return optional updated value. Will be empty if update did not happen.
289 */
290 CompletableFuture<Optional<Versioned<V>>> replaceAndGet(K key, long oldVersion, V newValue);
Madan Jampani50589ac2015-06-08 11:38:46 -0700291
292 /**
293 * Registers the specified listener to be notified whenever the map is updated.
294 *
295 * @param listener listener to notify about map events
296 */
297 void addListener(MapEventListener<K, V> listener);
298
299 /**
300 * Unregisters the specified listener such that it will no longer
301 * receive map change notifications.
302 *
303 * @param listener listener to unregister
304 */
305 void removeListener(MapEventListener<K, V> listener);
Madan Jampani7c521002015-03-23 12:23:01 -0700306}