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