Madan Jampani | 2546111 | 2015-02-17 14:17:29 -0800 | [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 | 2546111 | 2015-02-17 14:17:29 -0800 | [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 | |
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 | 0463cf9 | 2016-05-04 14:46:08 -0700 | [diff] [blame] | 23 | import java.util.concurrent.Executor; |
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; |
Jordan Halterman | dae1160 | 2018-07-03 00:00:47 -0700 | [diff] [blame] | 27 | import java.util.stream.Stream; |
| 28 | import java.util.stream.StreamSupport; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 29 | |
Madan Jampani | 0463cf9 | 2016-05-04 14:46:08 -0700 | [diff] [blame] | 30 | import com.google.common.util.concurrent.MoreExecutors; |
| 31 | |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 32 | /** |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 33 | * {@code ConsistentMap} provides the same functionality as {@link AsyncConsistentMap} with |
| 34 | * the only difference that all its methods block until the corresponding operation completes. |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 35 | * |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 36 | * @param <K> type of key |
| 37 | * @param <V> type of value |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 38 | */ |
Jordan Halterman | dae1160 | 2018-07-03 00:00:47 -0700 | [diff] [blame] | 39 | public interface ConsistentMap<K, V> extends DistributedPrimitive, Iterable<Map.Entry<K, Versioned<V>>> { |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Returns the number of entries in the map. |
| 43 | * |
| 44 | * @return map size. |
| 45 | */ |
| 46 | int size(); |
| 47 | |
| 48 | /** |
| 49 | * Returns true if the map is empty. |
| 50 | * |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 51 | * @return true if map has no entries, false otherwise |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 52 | */ |
| 53 | boolean isEmpty(); |
| 54 | |
| 55 | /** |
| 56 | * Returns true if this map contains a mapping for the specified key. |
| 57 | * |
| 58 | * @param key key |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 59 | * @return true if map contains key, false otherwise |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 60 | */ |
| 61 | boolean containsKey(K key); |
| 62 | |
| 63 | /** |
| 64 | * Returns true if this map contains the specified value. |
| 65 | * |
| 66 | * @param value value |
| 67 | * @return true if map contains value, false otherwise. |
| 68 | */ |
| 69 | boolean containsValue(V value); |
| 70 | |
| 71 | /** |
| 72 | * Returns the value (and version) to which the specified key is mapped, or null if this |
| 73 | * map contains no mapping for the key. |
| 74 | * |
| 75 | * @param key the key whose associated value (and version) is to be returned |
| 76 | * @return the value (and version) to which the specified key is mapped, or null if |
| 77 | * this map contains no mapping for the key |
| 78 | */ |
| 79 | Versioned<V> get(K key); |
| 80 | |
| 81 | /** |
Jordan Halterman | f627244 | 2017-04-20 02:18:08 -0700 | [diff] [blame] | 82 | * Returns the value (and version) to which the specified key is mapped, or the provided |
| 83 | * default value if this map contains no mapping for the key. |
| 84 | * <p> |
| 85 | * Note: a non-null {@link Versioned} value will be returned even if the {@code defaultValue} |
| 86 | * is {@code null}. |
| 87 | * |
| 88 | * @param key the key whose associated value (and version) is to be returned |
| 89 | * @param defaultValue the default value to return if the key is not set |
| 90 | * @return the value (and version) to which the specified key is mapped, or null if |
| 91 | * this map contains no mapping for the key |
| 92 | */ |
| 93 | Versioned<V> getOrDefault(K key, V defaultValue); |
| 94 | |
| 95 | /** |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 96 | * If the specified key is not already associated with a value (or is mapped to null), |
| 97 | * attempts to compute its value using the given mapping function and enters it into |
| 98 | * this map unless null. |
| 99 | * |
| 100 | * @param key key with which the specified value is to be associated |
| 101 | * @param mappingFunction the function to compute a value |
| 102 | * @return the current (existing or computed) value associated with the specified key, |
| 103 | * or null if the computed value is null. Method throws {@code ConsistentMapException.ConcurrentModification} |
| 104 | * if a concurrent modification of map is detected |
| 105 | */ |
| 106 | Versioned<V> computeIfAbsent(K key, |
| 107 | Function<? super K, ? extends V> mappingFunction); |
| 108 | |
| 109 | /** |
| 110 | * Attempts to compute a mapping for the specified key and its current mapped value (or |
| 111 | * null if there is no current mapping). |
| 112 | * If the computed value is null, the current mapping will be removed from the map. |
| 113 | * |
| 114 | * @param key key with which the specified value is to be associated |
| 115 | * @param remappingFunction the function to compute a value |
| 116 | * @return the new value associated with the specified key, or null if none. |
| 117 | * This method throws {@code ConsistentMapException.ConcurrentModification} |
| 118 | * if a concurrent modification of map is detected |
| 119 | */ |
| 120 | Versioned<V> compute(K key, |
| 121 | BiFunction<? super K, ? super V, ? extends V> remappingFunction); |
| 122 | |
| 123 | /** |
| 124 | * If the value for the specified key is present and non-null, attempts to compute a new |
| 125 | * mapping given the key and its current mapped value. |
| 126 | * If the computed value is null, the current mapping will be removed from the map. |
| 127 | * |
| 128 | * @param key key with which the specified value is to be associated |
| 129 | * @param remappingFunction the function to compute a value |
| 130 | * @return the new value associated with the specified key, or null if none. |
| 131 | * This method throws {@code ConsistentMapException.ConcurrentModification} |
| 132 | * if a concurrent modification of map is detected |
| 133 | */ |
| 134 | Versioned<V> computeIfPresent(K key, |
| 135 | BiFunction<? super K, ? super V, ? extends V> remappingFunction); |
| 136 | |
| 137 | /** |
| 138 | * If the value for the specified key satisfies a condition, attempts to compute a new |
| 139 | * mapping given the key and its current mapped value. |
| 140 | * If the computed value is null, the current mapping will be removed from the map. |
| 141 | * |
| 142 | * @param key key with which the specified value is to be associated |
| 143 | * @param condition condition that should evaluate to true for the computation to proceed |
| 144 | * @param remappingFunction the function to compute a value |
| 145 | * @return the new value associated with the specified key, or the old value if condition evaluates to false. |
| 146 | * This method throws {@code ConsistentMapException.ConcurrentModification} if a concurrent |
| 147 | * modification of map is detected |
| 148 | */ |
| 149 | Versioned<V> computeIf(K key, |
| 150 | Predicate<? super V> condition, |
| 151 | BiFunction<? super K, ? super V, ? extends V> remappingFunction); |
| 152 | |
| 153 | /** |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 154 | * Associates the specified value with the specified key in this map (optional operation). |
| 155 | * If the map previously contained a mapping for the key, the old value is replaced by the |
| 156 | * specified value. |
| 157 | * |
| 158 | * @param key key with which the specified value is to be associated |
| 159 | * @param value value to be associated with the specified key |
| 160 | * @return the previous value (and version) associated with key, or null if there was |
| 161 | * no mapping for key. |
| 162 | */ |
| 163 | Versioned<V> put(K key, V value); |
| 164 | |
| 165 | /** |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 166 | * Associates the specified value with the specified key in this map (optional operation). |
| 167 | * If the map previously contained a mapping for the key, the old value is replaced by the |
| 168 | * specified value. |
| 169 | * |
| 170 | * @param key key with which the specified value is to be associated |
| 171 | * @param value value to be associated with the specified key |
| 172 | * @return new value. |
| 173 | */ |
| 174 | Versioned<V> putAndGet(K key, V value); |
| 175 | |
| 176 | /** |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 177 | * Removes the mapping for a key from this map if it is present (optional operation). |
| 178 | * |
| 179 | * @param key key whose value is to be removed from the map |
| 180 | * @return the value (and version) to which this map previously associated the key, |
| 181 | * or null if the map contained no mapping for the key. |
| 182 | */ |
| 183 | Versioned<V> remove(K key); |
| 184 | |
| 185 | /** |
| 186 | * Removes all of the mappings from this map (optional operation). |
| 187 | * The map will be empty after this call returns. |
| 188 | */ |
| 189 | void clear(); |
| 190 | |
| 191 | /** |
| 192 | * Returns a Set view of the keys contained in this map. |
| 193 | * This method differs from the behavior of java.util.Map.keySet() in that |
| 194 | * what is returned is a unmodifiable snapshot view of the keys in the ConsistentMap. |
| 195 | * Attempts to modify the returned set, whether direct or via its iterator, |
| 196 | * result in an UnsupportedOperationException. |
| 197 | * |
| 198 | * @return a set of the keys contained in this map |
| 199 | */ |
| 200 | Set<K> keySet(); |
| 201 | |
| 202 | /** |
| 203 | * Returns the collection of values (and associated versions) contained in this map. |
| 204 | * This method differs from the behavior of java.util.Map.values() in that |
| 205 | * what is returned is a unmodifiable snapshot view of the values in the ConsistentMap. |
| 206 | * Attempts to modify the returned collection, whether direct or via its iterator, |
| 207 | * result in an UnsupportedOperationException. |
| 208 | * |
| 209 | * @return a collection of the values (and associated versions) contained in this map |
| 210 | */ |
| 211 | Collection<Versioned<V>> values(); |
| 212 | |
| 213 | /** |
| 214 | * Returns the set of entries contained in this map. |
| 215 | * This method differs from the behavior of java.util.Map.entrySet() in that |
| 216 | * what is returned is a unmodifiable snapshot view of the entries in the ConsistentMap. |
| 217 | * Attempts to modify the returned set, whether direct or via its iterator, |
| 218 | * result in an UnsupportedOperationException. |
| 219 | * |
| 220 | * @return set of entries contained in this map. |
| 221 | */ |
| 222 | Set<Entry<K, Versioned<V>>> entrySet(); |
| 223 | |
| 224 | /** |
| 225 | * If the specified key is not already associated with a value |
| 226 | * associates it with the given value and returns null, else returns the current value. |
| 227 | * |
| 228 | * @param key key with which the specified value is to be associated |
| 229 | * @param value value to be associated with the specified key |
| 230 | * @return the previous value associated with the specified key or null |
| 231 | * if key does not already mapped to a value. |
| 232 | */ |
| 233 | Versioned<V> putIfAbsent(K key, V value); |
| 234 | |
| 235 | /** |
| 236 | * Removes the entry for the specified key only if it is currently |
| 237 | * mapped to the specified value. |
| 238 | * |
| 239 | * @param key key with which the specified value is associated |
| 240 | * @param value value expected to be associated with the specified key |
| 241 | * @return true if the value was removed |
| 242 | */ |
| 243 | boolean remove(K key, V value); |
| 244 | |
| 245 | /** |
| 246 | * Removes the entry for the specified key only if its current |
| 247 | * version in the map is equal to the specified version. |
| 248 | * |
| 249 | * @param key key with which the specified version is associated |
| 250 | * @param version version expected to be associated with the specified key |
| 251 | * @return true if the value was removed |
| 252 | */ |
| 253 | boolean remove(K key, long version); |
| 254 | |
| 255 | /** |
Jihwan Kim | 9887ad9 | 2015-12-12 00:23:57 +0900 | [diff] [blame] | 256 | * Replaces the entry for the specified key only if there is any value |
| 257 | * which associated with specified key. |
| 258 | * |
| 259 | * @param key key with which the specified value is associated |
| 260 | * @param value value expected to be associated with the specified key |
| 261 | * @return the previous value associated with the specified key or null |
| 262 | */ |
| 263 | Versioned<V> replace(K key, V value); |
| 264 | |
| 265 | /** |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 266 | * Replaces the entry for the specified key only if currently mapped |
| 267 | * to the specified value. |
| 268 | * |
| 269 | * @param key key with which the specified value is associated |
| 270 | * @param oldValue value expected to be associated with the specified key |
| 271 | * @param newValue value to be associated with the specified key |
| 272 | * @return true if the value was replaced |
| 273 | */ |
| 274 | boolean replace(K key, V oldValue, V newValue); |
| 275 | |
| 276 | /** |
| 277 | * Replaces the entry for the specified key only if it is currently mapped to the |
| 278 | * specified version. |
| 279 | * |
| 280 | * @param key key key with which the specified value is associated |
| 281 | * @param oldVersion version expected to be associated with the specified key |
| 282 | * @param newValue value to be associated with the specified key |
| 283 | * @return true if the value was replaced |
| 284 | */ |
| 285 | boolean replace(K key, long oldVersion, V newValue); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 286 | |
| 287 | /** |
Jordan Halterman | dae1160 | 2018-07-03 00:00:47 -0700 | [diff] [blame] | 288 | * Streams entries from the map. |
| 289 | * <p> |
| 290 | * This method is optimized for large maps. |
| 291 | * |
| 292 | * @return the map entry stream |
| 293 | */ |
| 294 | default Stream<Entry<K, Versioned<V>>> stream() { |
| 295 | return StreamSupport.stream(spliterator(), false); |
| 296 | } |
| 297 | |
| 298 | /** |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 299 | * Registers the specified listener to be notified whenever the map is updated. |
| 300 | * |
| 301 | * @param listener listener to notify about map events |
| 302 | */ |
Madan Jampani | 0463cf9 | 2016-05-04 14:46:08 -0700 | [diff] [blame] | 303 | default void addListener(MapEventListener<K, V> listener) { |
| 304 | addListener(listener, MoreExecutors.directExecutor()); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Registers the specified listener to be notified whenever the map is updated. |
| 309 | * |
| 310 | * @param listener listener to notify about map events |
| 311 | * @param executor executor to use for handling incoming map events |
| 312 | */ |
| 313 | void addListener(MapEventListener<K, V> listener, Executor executor); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 314 | |
| 315 | /** |
| 316 | * Unregisters the specified listener such that it will no longer |
| 317 | * receive map change notifications. |
| 318 | * |
| 319 | * @param listener listener to unregister |
| 320 | */ |
| 321 | void removeListener(MapEventListener<K, V> listener); |
Madan Jampani | e106522 | 2015-08-17 16:21:51 -0700 | [diff] [blame] | 322 | |
| 323 | /** |
| 324 | * Returns a java.util.Map instance backed by this ConsistentMap. |
| 325 | * @return java.util.Map |
| 326 | */ |
| 327 | Map<K, V> asJavaMap(); |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 328 | } |