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