Madan Jampani | 2546111 | 2015-02-17 14:17:29 -0800 | [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 | |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 17 | package org.onosproject.store.service; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 18 | |
| 19 | import java.util.Collection; |
Madan Jampani | e106522 | 2015-08-17 16:21:51 -0700 | [diff] [blame] | 20 | import java.util.Map; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 21 | import java.util.Map.Entry; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 22 | import java.util.Set; |
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 | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 26 | |
| 27 | /** |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 28 | * A distributed, strongly consistent key-value map. |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 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> |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 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 ConsistentMap<K, V> { |
| 48 | |
| 49 | /** |
| 50 | * Returns the number of entries in the map. |
| 51 | * |
| 52 | * @return map size. |
| 53 | */ |
| 54 | int size(); |
| 55 | |
| 56 | /** |
| 57 | * Returns true if the map is empty. |
| 58 | * |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 59 | * @return true if map has no entries, false otherwise |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 60 | */ |
| 61 | boolean isEmpty(); |
| 62 | |
| 63 | /** |
| 64 | * Returns true if this map contains a mapping for the specified key. |
| 65 | * |
| 66 | * @param key key |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 67 | * @return true if map contains key, false otherwise |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 68 | */ |
| 69 | boolean containsKey(K key); |
| 70 | |
| 71 | /** |
| 72 | * Returns true if this map contains the specified value. |
| 73 | * |
| 74 | * @param value value |
| 75 | * @return true if map contains value, false otherwise. |
| 76 | */ |
| 77 | 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 the value (and version) to which the specified key is mapped, or null if |
| 85 | * this map contains no mapping for the key |
| 86 | */ |
| 87 | 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 | * |
| 94 | * @param key key with which the specified value is to be associated |
| 95 | * @param mappingFunction the function to compute a value |
| 96 | * @return the current (existing or computed) value associated with the specified key, |
| 97 | * or null if the computed value is null. Method throws {@code ConsistentMapException.ConcurrentModification} |
| 98 | * if a concurrent modification of map is detected |
| 99 | */ |
| 100 | Versioned<V> computeIfAbsent(K key, |
| 101 | Function<? super K, ? extends V> mappingFunction); |
| 102 | |
| 103 | /** |
| 104 | * Attempts to compute a mapping for the specified key and its current mapped value (or |
| 105 | * null if there is no current mapping). |
| 106 | * If the computed value is null, the current mapping will be removed from the map. |
| 107 | * |
| 108 | * @param key key with which the specified value is to be associated |
| 109 | * @param remappingFunction the function to compute a value |
| 110 | * @return the new value associated with the specified key, or null if none. |
| 111 | * This method throws {@code ConsistentMapException.ConcurrentModification} |
| 112 | * if a concurrent modification of map is detected |
| 113 | */ |
| 114 | Versioned<V> compute(K key, |
| 115 | BiFunction<? super K, ? super V, ? extends V> remappingFunction); |
| 116 | |
| 117 | /** |
| 118 | * If the value for the specified key is present and non-null, attempts to compute a new |
| 119 | * mapping given the key and its current mapped value. |
| 120 | * If the computed value is null, the current mapping will be removed from the map. |
| 121 | * |
| 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 none. |
| 125 | * This method throws {@code ConsistentMapException.ConcurrentModification} |
| 126 | * if a concurrent modification of map is detected |
| 127 | */ |
| 128 | Versioned<V> computeIfPresent(K key, |
| 129 | BiFunction<? super K, ? super V, ? extends V> remappingFunction); |
| 130 | |
| 131 | /** |
| 132 | * If the value for the specified key satisfies a condition, attempts to compute a new |
| 133 | * mapping given the key and its current mapped value. |
| 134 | * If the computed value is null, the current mapping will be removed from the map. |
| 135 | * |
| 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 | * This method throws {@code ConsistentMapException.ConcurrentModification} if a concurrent |
| 141 | * modification of map is detected |
| 142 | */ |
| 143 | Versioned<V> computeIf(K key, |
| 144 | Predicate<? super V> condition, |
| 145 | BiFunction<? super K, ? super V, ? extends V> remappingFunction); |
| 146 | |
| 147 | /** |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 148 | * Associates the specified value with the specified key in this map (optional operation). |
| 149 | * If the map previously contained a mapping for the key, the old value is replaced by the |
| 150 | * specified value. |
| 151 | * |
| 152 | * @param key key with which the specified value is to be associated |
| 153 | * @param value value to be associated with the specified key |
| 154 | * @return the previous value (and version) associated with key, or null if there was |
| 155 | * no mapping for key. |
| 156 | */ |
| 157 | Versioned<V> put(K key, V value); |
| 158 | |
| 159 | /** |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 160 | * Associates the specified value with the specified key in this map (optional operation). |
| 161 | * If the map previously contained a mapping for the key, the old value is replaced by the |
| 162 | * specified value. |
| 163 | * |
| 164 | * @param key key with which the specified value is to be associated |
| 165 | * @param value value to be associated with the specified key |
| 166 | * @return new value. |
| 167 | */ |
| 168 | Versioned<V> putAndGet(K key, V value); |
| 169 | |
| 170 | /** |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 171 | * Removes the mapping for a key from this map if it is present (optional operation). |
| 172 | * |
| 173 | * @param key key whose value is to be removed from the map |
| 174 | * @return the value (and version) to which this map previously associated the key, |
| 175 | * or null if the map contained no mapping for the key. |
| 176 | */ |
| 177 | Versioned<V> remove(K key); |
| 178 | |
| 179 | /** |
| 180 | * Removes all of the mappings from this map (optional operation). |
| 181 | * The map will be empty after this call returns. |
| 182 | */ |
| 183 | void clear(); |
| 184 | |
| 185 | /** |
| 186 | * Returns a Set view of the keys contained in this map. |
| 187 | * This method differs from the behavior of java.util.Map.keySet() in that |
| 188 | * what is returned is a unmodifiable snapshot view of the keys in the ConsistentMap. |
| 189 | * Attempts to modify the returned set, whether direct or via its iterator, |
| 190 | * result in an UnsupportedOperationException. |
| 191 | * |
| 192 | * @return a set of the keys contained in this map |
| 193 | */ |
| 194 | Set<K> keySet(); |
| 195 | |
| 196 | /** |
| 197 | * Returns the collection of values (and associated versions) contained in this map. |
| 198 | * This method differs from the behavior of java.util.Map.values() in that |
| 199 | * what is returned is a unmodifiable snapshot view of the values in the ConsistentMap. |
| 200 | * Attempts to modify the returned collection, whether direct or via its iterator, |
| 201 | * result in an UnsupportedOperationException. |
| 202 | * |
| 203 | * @return a collection of the values (and associated versions) contained in this map |
| 204 | */ |
| 205 | Collection<Versioned<V>> values(); |
| 206 | |
| 207 | /** |
| 208 | * Returns the set of entries contained in this map. |
| 209 | * This method differs from the behavior of java.util.Map.entrySet() in that |
| 210 | * what is returned is a unmodifiable snapshot view of the entries in the ConsistentMap. |
| 211 | * Attempts to modify the returned set, whether direct or via its iterator, |
| 212 | * result in an UnsupportedOperationException. |
| 213 | * |
| 214 | * @return set of entries contained in this map. |
| 215 | */ |
| 216 | Set<Entry<K, Versioned<V>>> entrySet(); |
| 217 | |
| 218 | /** |
| 219 | * If the specified key is not already associated with a value |
| 220 | * associates it with the given value and returns null, else returns the current value. |
| 221 | * |
| 222 | * @param key key with which the specified value is to be associated |
| 223 | * @param value value to be associated with the specified key |
| 224 | * @return the previous value associated with the specified key or null |
| 225 | * if key does not already mapped to a value. |
| 226 | */ |
| 227 | Versioned<V> putIfAbsent(K key, V value); |
| 228 | |
| 229 | /** |
| 230 | * Removes the entry for the specified key only if it is currently |
| 231 | * mapped to the specified value. |
| 232 | * |
| 233 | * @param key key with which the specified value is associated |
| 234 | * @param value value expected to be associated with the specified key |
| 235 | * @return true if the value was removed |
| 236 | */ |
| 237 | boolean remove(K key, V value); |
| 238 | |
| 239 | /** |
| 240 | * Removes the entry for the specified key only if its current |
| 241 | * version in the map is equal to the specified version. |
| 242 | * |
| 243 | * @param key key with which the specified version is associated |
| 244 | * @param version version expected to be associated with the specified key |
| 245 | * @return true if the value was removed |
| 246 | */ |
| 247 | boolean remove(K key, long version); |
| 248 | |
| 249 | /** |
| 250 | * Replaces the entry for the specified key only if currently mapped |
| 251 | * to the specified value. |
| 252 | * |
| 253 | * @param key key with which the specified value is associated |
| 254 | * @param oldValue value expected to be associated with the specified key |
| 255 | * @param newValue value to be associated with the specified key |
| 256 | * @return true if the value was replaced |
| 257 | */ |
| 258 | boolean replace(K key, V oldValue, V newValue); |
| 259 | |
| 260 | /** |
| 261 | * Replaces the entry for the specified key only if it is currently mapped to the |
| 262 | * specified version. |
| 263 | * |
| 264 | * @param key key key with which the specified value is associated |
| 265 | * @param oldVersion version expected to be associated with the specified key |
| 266 | * @param newValue value to be associated with the specified key |
| 267 | * @return true if the value was replaced |
| 268 | */ |
| 269 | boolean replace(K key, long oldVersion, V newValue); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 270 | |
| 271 | /** |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 272 | * Registers the specified listener to be notified whenever the map is updated. |
| 273 | * |
| 274 | * @param listener listener to notify about map events |
| 275 | */ |
| 276 | void addListener(MapEventListener<K, V> listener); |
| 277 | |
| 278 | /** |
| 279 | * Unregisters the specified listener such that it will no longer |
| 280 | * receive map change notifications. |
| 281 | * |
| 282 | * @param listener listener to unregister |
| 283 | */ |
| 284 | void removeListener(MapEventListener<K, V> listener); |
Madan Jampani | e106522 | 2015-08-17 16:21:51 -0700 | [diff] [blame] | 285 | |
| 286 | /** |
| 287 | * Returns a java.util.Map instance backed by this ConsistentMap. |
| 288 | * @return java.util.Map |
| 289 | */ |
| 290 | Map<K, V> asJavaMap(); |
| 291 | } |