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