Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.store.service; |
| 18 | |
| 19 | import java.util.Collection; |
Jordan Halterman | 8c47358 | 2018-07-03 00:00:47 -0700 | [diff] [blame] | 20 | import java.util.Map; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 21 | import java.util.Map.Entry; |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 22 | import java.util.Objects; |
Madan Jampani | 6e0d147 | 2015-03-24 12:13:44 -0700 | [diff] [blame] | 23 | import java.util.Set; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 24 | import java.util.concurrent.CompletableFuture; |
Madan Jampani | 0463cf9 | 2016-05-04 14:46:08 -0700 | [diff] [blame] | 25 | import java.util.concurrent.Executor; |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 26 | import java.util.function.BiFunction; |
| 27 | import java.util.function.Function; |
| 28 | import java.util.function.Predicate; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 29 | |
Madan Jampani | 0463cf9 | 2016-05-04 14:46:08 -0700 | [diff] [blame] | 30 | import com.google.common.util.concurrent.MoreExecutors; |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 31 | import org.onosproject.store.primitives.DefaultConsistentMap; |
| 32 | import org.onosproject.store.primitives.MapUpdate; |
Madan Jampani | 0463cf9 | 2016-05-04 14:46:08 -0700 | [diff] [blame] | 33 | |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 34 | /** |
| 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 Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 46 | * 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 Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 49 | * </p><p> |
| 50 | * This map does not allow null values. All methods can throw a ConsistentMapException |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 51 | * (which extends {@code RuntimeException}) to indicate failures. |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 52 | * <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 Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 57 | */ |
Jordan Halterman | 8c47358 | 2018-07-03 00:00:47 -0700 | [diff] [blame] | 58 | public interface AsyncConsistentMap<K, V> |
| 59 | extends DistributedPrimitive, Transactional<MapUpdate<K, V>>, AsyncIterable<Map.Entry<K, Versioned<V>>> { |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 60 | |
| 61 | @Override |
Madan Jampani | 39fff10 | 2016-02-14 13:17:28 -0800 | [diff] [blame] | 62 | default DistributedPrimitive.Type primitiveType() { |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 63 | return DistributedPrimitive.Type.CONSISTENT_MAP; |
| 64 | } |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 65 | |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 66 | @Override |
| 67 | default CompletableFuture<Void> destroy() { |
| 68 | return clear(); |
| 69 | } |
| 70 | |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 71 | /** |
| 72 | * Returns the number of entries in the map. |
| 73 | * |
| 74 | * @return a future for map size. |
| 75 | */ |
| 76 | CompletableFuture<Integer> size(); |
| 77 | |
| 78 | /** |
| 79 | * Returns true if the map is empty. |
| 80 | * |
| 81 | * @return a future whose value will be true if map has no entries, false otherwise. |
| 82 | */ |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 83 | default CompletableFuture<Boolean> isEmpty() { |
| 84 | return size().thenApply(s -> s == 0); |
| 85 | } |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Returns true if this map contains a mapping for the specified key. |
| 89 | * |
| 90 | * @param key key |
| 91 | * @return a future whose value will be true if map contains key, false otherwise. |
| 92 | */ |
| 93 | CompletableFuture<Boolean> containsKey(K key); |
| 94 | |
| 95 | /** |
| 96 | * Returns true if this map contains the specified value. |
| 97 | * |
| 98 | * @param value value |
| 99 | * @return a future whose value will be true if map contains value, false otherwise. |
| 100 | */ |
| 101 | CompletableFuture<Boolean> containsValue(V value); |
| 102 | |
| 103 | /** |
| 104 | * Returns the value (and version) to which the specified key is mapped, or null if this |
| 105 | * map contains no mapping for the key. |
| 106 | * |
| 107 | * @param key the key whose associated value (and version) is to be returned |
| 108 | * @return a future value (and version) to which the specified key is mapped, or null if |
| 109 | * this map contains no mapping for the key |
| 110 | */ |
| 111 | CompletableFuture<Versioned<V>> get(K key); |
| 112 | |
| 113 | /** |
Jordan Halterman | f627244 | 2017-04-20 02:18:08 -0700 | [diff] [blame] | 114 | * Returns the value (and version) to which the specified key is mapped, or the provided |
| 115 | * default value if this map contains no mapping for the key. |
| 116 | * |
| 117 | * @param key the key whose associated value (and version) is to be returned |
| 118 | * @param defaultValue the default value to return if the key is not set |
| 119 | * @return a future value (and version) to which the specified key is mapped, or null if |
| 120 | * this map contains no mapping for the key |
| 121 | */ |
| 122 | CompletableFuture<Versioned<V>> getOrDefault(K key, V defaultValue); |
| 123 | |
| 124 | /** |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 125 | * If the specified key is not already associated with a value (or is mapped to null), |
| 126 | * attempts to compute its value using the given mapping function and enters it into |
| 127 | * this map unless null. |
| 128 | * If a conflicting concurrent modification attempt is detected, the returned future |
| 129 | * will be completed exceptionally with ConsistentMapException.ConcurrentModification. |
| 130 | * @param key key with which the specified value is to be associated |
| 131 | * @param mappingFunction the function to compute a value |
| 132 | * @return the current (existing or computed) value associated with the specified key, |
| 133 | * or null if the computed value is null |
| 134 | */ |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 135 | default CompletableFuture<Versioned<V>> computeIfAbsent(K key, |
| 136 | Function<? super K, ? extends V> mappingFunction) { |
| 137 | return computeIf(key, Objects::isNull, (k, v) -> mappingFunction.apply(k)); |
| 138 | } |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * If the value for the specified key is present and non-null, attempts to compute a new |
| 142 | * mapping given the key and its current mapped value. |
| 143 | * If the computed value is null, the current mapping will be removed from the map. |
| 144 | * If a conflicting concurrent modification attempt is detected, the returned future |
| 145 | * will be completed exceptionally with ConsistentMapException.ConcurrentModification. |
| 146 | * @param key key with which the specified value is to be associated |
| 147 | * @param remappingFunction the function to compute a value |
| 148 | * @return the new value associated with the specified key, or null if computed value is null |
| 149 | */ |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 150 | default CompletableFuture<Versioned<V>> computeIfPresent(K key, |
| 151 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
| 152 | return computeIf(key, Objects::nonNull, remappingFunction); |
| 153 | } |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * Attempts to compute a mapping for the specified key and its current mapped value (or |
| 157 | * null if there is no current mapping). |
| 158 | * If the computed value is null, the current mapping (if one exists) will be removed from the map. |
| 159 | * If a conflicting concurrent modification attempt is detected, the returned future |
| 160 | * will be completed exceptionally with ConsistentMapException.ConcurrentModification. |
| 161 | * @param key key with which the specified value is to be associated |
| 162 | * @param remappingFunction the function to compute a value |
| 163 | * @return the new value associated with the specified key, or null if computed value is null |
| 164 | */ |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 165 | default CompletableFuture<Versioned<V>> compute(K key, |
| 166 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
| 167 | return computeIf(key, v -> true, remappingFunction); |
| 168 | } |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | * If the value for the specified key satisfies a condition, attempts to compute a new |
| 172 | * mapping given the key and its current mapped value. |
| 173 | * If the computed value is null, the current mapping will be removed from the map. |
| 174 | * If a conflicting concurrent modification attempt is detected, the returned future |
| 175 | * will be completed exceptionally with ConsistentMapException.ConcurrentModification. |
| 176 | * @param key key with which the specified value is to be associated |
| 177 | * @param condition condition that should evaluate to true for the computation to proceed |
| 178 | * @param remappingFunction the function to compute a value |
| 179 | * @return the new value associated with the specified key, or the old value if condition evaluates to false |
| 180 | */ |
| 181 | CompletableFuture<Versioned<V>> computeIf(K key, |
| 182 | Predicate<? super V> condition, |
| 183 | BiFunction<? super K, ? super V, ? extends V> remappingFunction); |
| 184 | |
| 185 | /** |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 186 | * 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 the previous value (and version) associated with key, or null if there was |
| 193 | * no mapping for key. |
| 194 | */ |
| 195 | CompletableFuture<Versioned<V>> put(K key, V value); |
| 196 | |
| 197 | /** |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 198 | * Associates the specified value with the specified key in this map (optional operation). |
| 199 | * If the map previously contained a mapping for the key, the old value is replaced by the |
| 200 | * specified value. |
| 201 | * |
| 202 | * @param key key with which the specified value is to be associated |
| 203 | * @param value value to be associated with the specified key |
| 204 | * @return new value. |
| 205 | */ |
| 206 | CompletableFuture<Versioned<V>> putAndGet(K key, V value); |
| 207 | |
| 208 | /** |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 209 | * Removes the mapping for a key from this map if it is present (optional operation). |
| 210 | * |
| 211 | * @param key key whose value is to be removed from the map |
| 212 | * @return the value (and version) to which this map previously associated the key, |
| 213 | * or null if the map contained no mapping for the key. |
| 214 | */ |
| 215 | CompletableFuture<Versioned<V>> remove(K key); |
| 216 | |
| 217 | /** |
| 218 | * Removes all of the mappings from this map (optional operation). |
| 219 | * The map will be empty after this call returns. |
Madan Jampani | 6e0d147 | 2015-03-24 12:13:44 -0700 | [diff] [blame] | 220 | * @return future that will be successfully completed when the map is cleared |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 221 | */ |
| 222 | CompletableFuture<Void> clear(); |
| 223 | |
| 224 | /** |
| 225 | * Returns a Set view of the keys contained in this map. |
| 226 | * This method differs from the behavior of java.util.Map.keySet() in that |
| 227 | * what is returned is a unmodifiable snapshot view of the keys in the ConsistentMap. |
| 228 | * Attempts to modify the returned set, whether direct or via its iterator, |
| 229 | * result in an UnsupportedOperationException. |
| 230 | * |
| 231 | * @return a set of the keys contained in this map |
| 232 | */ |
| 233 | CompletableFuture<Set<K>> keySet(); |
| 234 | |
| 235 | /** |
| 236 | * Returns the collection of values (and associated versions) contained in this map. |
| 237 | * This method differs from the behavior of java.util.Map.values() in that |
| 238 | * what is returned is a unmodifiable snapshot view of the values in the ConsistentMap. |
| 239 | * Attempts to modify the returned collection, whether direct or via its iterator, |
| 240 | * result in an UnsupportedOperationException. |
| 241 | * |
| 242 | * @return a collection of the values (and associated versions) contained in this map |
| 243 | */ |
| 244 | CompletableFuture<Collection<Versioned<V>>> values(); |
| 245 | |
| 246 | /** |
| 247 | * Returns the set of entries contained in this map. |
| 248 | * This method differs from the behavior of java.util.Map.entrySet() in that |
| 249 | * what is returned is a unmodifiable snapshot view of the entries in the ConsistentMap. |
| 250 | * Attempts to modify the returned set, whether direct or via its iterator, |
| 251 | * result in an UnsupportedOperationException. |
| 252 | * |
| 253 | * @return set of entries contained in this map. |
| 254 | */ |
| 255 | CompletableFuture<Set<Entry<K, Versioned<V>>>> entrySet(); |
| 256 | |
| 257 | /** |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 258 | * If the specified key is not already associated with a value associates |
| 259 | * it with the given value and returns null, else behaves as a get |
| 260 | * returning the existing mapping without making any changes. |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 261 | * |
| 262 | * @param key key with which the specified value is to be associated |
| 263 | * @param value value to be associated with the specified key |
| 264 | * @return the previous value associated with the specified key or null |
| 265 | * if key does not already mapped to a value. |
| 266 | */ |
| 267 | CompletableFuture<Versioned<V>> putIfAbsent(K key, V value); |
| 268 | |
| 269 | /** |
| 270 | * Removes the entry for the specified key only if it is currently |
| 271 | * mapped to the specified value. |
| 272 | * |
| 273 | * @param key key with which the specified value is associated |
| 274 | * @param value value expected to be associated with the specified key |
| 275 | * @return true if the value was removed |
| 276 | */ |
| 277 | CompletableFuture<Boolean> remove(K key, V value); |
| 278 | |
| 279 | /** |
| 280 | * Removes the entry for the specified key only if its current |
| 281 | * version in the map is equal to the specified version. |
| 282 | * |
| 283 | * @param key key with which the specified version is associated |
| 284 | * @param version version expected to be associated with the specified key |
| 285 | * @return true if the value was removed |
| 286 | */ |
| 287 | CompletableFuture<Boolean> remove(K key, long version); |
| 288 | |
| 289 | /** |
Jihwan Kim | 9887ad9 | 2015-12-12 00:23:57 +0900 | [diff] [blame] | 290 | * Replaces the entry for the specified key only if there is any value |
| 291 | * which associated with specified key. |
| 292 | * |
| 293 | * @param key key with which the specified value is associated |
| 294 | * @param value value expected to be associated with the specified key |
| 295 | * @return the previous value associated with the specified key or null |
| 296 | */ |
| 297 | CompletableFuture<Versioned<V>> replace(K key, V value); |
| 298 | |
| 299 | /** |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 300 | * Replaces the entry for the specified key only if currently mapped |
| 301 | * to the specified value. |
| 302 | * |
| 303 | * @param key key with which the specified value is associated |
| 304 | * @param oldValue value expected to be associated with the specified key |
| 305 | * @param newValue value to be associated with the specified key |
| 306 | * @return true if the value was replaced |
| 307 | */ |
| 308 | CompletableFuture<Boolean> replace(K key, V oldValue, V newValue); |
| 309 | |
| 310 | /** |
| 311 | * Replaces the entry for the specified key only if it is currently mapped to the |
| 312 | * specified version. |
| 313 | * |
| 314 | * @param key key key with which the specified value is associated |
| 315 | * @param oldVersion version expected to be associated with the specified key |
| 316 | * @param newValue value to be associated with the specified key |
| 317 | * @return true if the value was replaced |
| 318 | */ |
| 319 | CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 320 | |
| 321 | /** |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 322 | * Registers the specified listener to be notified whenever the map is updated. |
| 323 | * |
| 324 | * @param listener listener to notify about map events |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 325 | * @return future that will be completed when the operation finishes |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 326 | */ |
Madan Jampani | 0463cf9 | 2016-05-04 14:46:08 -0700 | [diff] [blame] | 327 | default CompletableFuture<Void> addListener(MapEventListener<K, V> listener) { |
| 328 | return addListener(listener, MoreExecutors.directExecutor()); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Registers the specified listener to be notified whenever the map is updated. |
| 333 | * |
| 334 | * @param listener listener to notify about map events |
| 335 | * @param executor executor to use for handling incoming map events |
| 336 | * @return future that will be completed when the operation finishes |
| 337 | */ |
| 338 | CompletableFuture<Void> addListener(MapEventListener<K, V> listener, Executor executor); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 339 | |
| 340 | /** |
| 341 | * Unregisters the specified listener such that it will no longer |
| 342 | * receive map change notifications. |
| 343 | * |
| 344 | * @param listener listener to unregister |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 345 | * @return future that will be completed when the operation finishes |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 346 | */ |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 347 | CompletableFuture<Void> removeListener(MapEventListener<K, V> listener); |
Madan Jampani | 28b2cd6 | 2016-02-02 10:49:52 -0800 | [diff] [blame] | 348 | |
| 349 | /** |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 350 | * Returns a new {@link ConsistentMap} that is backed by this instance. |
| 351 | * |
| 352 | * @return new {@code ConsistentMap} instance |
| 353 | */ |
| 354 | default ConsistentMap<K, V> asConsistentMap() { |
Jordan Halterman | 6440b09 | 2017-05-24 17:48:08 -0700 | [diff] [blame] | 355 | return asConsistentMap(DistributedPrimitive.DEFAULT_OPERATION_TIMEOUT_MILLIS); |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /** |
Madan Jampani | 28b2cd6 | 2016-02-02 10:49:52 -0800 | [diff] [blame] | 359 | * Returns a new {@link ConsistentMap} that is backed by this instance. |
| 360 | * |
| 361 | * @param timeoutMillis timeout duration for the returned ConsistentMap operations |
| 362 | * @return new {@code ConsistentMap} instance |
| 363 | */ |
| 364 | default ConsistentMap<K, V> asConsistentMap(long timeoutMillis) { |
| 365 | return new DefaultConsistentMap<>(this, timeoutMillis); |
| 366 | } |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 367 | } |