Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -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 | |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Multiset; |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame] | 20 | import com.google.common.util.concurrent.MoreExecutors; |
Aaron Kruglikov | 61582a0 | 2016-09-06 13:18:58 -0700 | [diff] [blame] | 21 | import org.onosproject.store.primitives.DefaultConsistentMultimap; |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 22 | |
| 23 | import java.util.Collection; |
| 24 | import java.util.Map; |
| 25 | import java.util.Set; |
| 26 | import java.util.concurrent.CompletableFuture; |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame] | 27 | import java.util.concurrent.Executor; |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Interface for a distributed multimap. |
| 31 | * |
| 32 | * NOTE: Editing any returned collection will NOT effect the map itself and |
| 33 | * changes in the map will NOT be reflected in the returned collections. |
| 34 | * Certain operations may be too expensive when backed by a distributed data |
| 35 | * structure and have been labeled as such. |
| 36 | */ |
Jordan Halterman | 5e88435 | 2018-05-21 22:11:07 -0700 | [diff] [blame] | 37 | public interface AsyncConsistentMultimap<K, V> extends DistributedPrimitive, AsyncIterable<Map.Entry<K, V>> { |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 38 | |
| 39 | @Override |
| 40 | default DistributedPrimitive.Type primitiveType() { |
| 41 | return Type.CONSISTENT_MULTIMAP; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | default CompletableFuture<Void> destroy() { |
| 46 | return clear(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns the number of key-value pairs in this multimap. |
| 51 | * @return the number of key-value pairs |
| 52 | */ |
| 53 | CompletableFuture<Integer> size(); |
| 54 | |
| 55 | /** |
| 56 | * Returns if this multimap contains no key-value pairs. |
| 57 | * @return completable future that will be true if no key-value pairs |
| 58 | * exist, false otherwise |
| 59 | */ |
| 60 | CompletableFuture<Boolean> isEmpty(); |
| 61 | |
| 62 | /** |
| 63 | * Returns true if there is at lease one key-value pair with a key equal to |
| 64 | * key. |
| 65 | * @param key the key to query |
| 66 | * @return a future whose value will be true if the map contains a |
| 67 | * key-value pair with key false otherwise |
| 68 | */ |
| 69 | CompletableFuture<Boolean> containsKey(K key); |
| 70 | |
| 71 | /** |
| 72 | * Returns true if this map contains at lease one key-value pair with a |
| 73 | * value equal to value. |
| 74 | * @param value the value to query |
| 75 | * @return a future whose value will be true if there is a key-value pair |
| 76 | * with the specified value, false otherwise. |
| 77 | */ |
| 78 | CompletableFuture<Boolean> containsValue(V value); |
| 79 | |
| 80 | /** |
| 81 | * Returns true if this map contains at least one key-value pair with key |
| 82 | * and value specified. |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 83 | * |
| 84 | * @param key key |
| 85 | * @param value value |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 86 | * @return a future whose value will be true if there is a key-value pair |
| 87 | * with the specified key and value, |
| 88 | * false otherwise. |
| 89 | */ |
| 90 | CompletableFuture<Boolean> containsEntry(K key, V value); |
| 91 | |
| 92 | /** |
| 93 | * If the key-value pair does not already exist adds either the key value |
| 94 | * pair or the value to the set of values associated with the key and |
| 95 | * returns true, if the key-value pair already exists then behavior is |
| 96 | * implementation specific with some implementations allowing duplicates |
| 97 | * and others ignoring put requests for existing entries. |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 98 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 99 | * @param key the key to add |
| 100 | * @param value the value to add |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 101 | * @return a future whose value will be true if the map has changed because |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 102 | * of this call, false otherwise |
| 103 | */ |
| 104 | CompletableFuture<Boolean> put(K key, V value); |
| 105 | |
| 106 | /** |
Jordan Halterman | 8c57a09 | 2018-06-04 14:53:06 -0700 | [diff] [blame] | 107 | * If the key-value pair does not already exist adds either the key value |
| 108 | * pair or the value to the set of values associated with the key and |
| 109 | * returns the updated value, if the key-value pair already exists then behavior |
| 110 | * is implementation specific with some implementations allowing duplicates |
| 111 | * and others ignoring put requests for existing entries. |
| 112 | * |
| 113 | * @param key the key to add |
| 114 | * @param value the value to add |
| 115 | * @return a future to be completed with the updated values |
| 116 | */ |
| 117 | CompletableFuture<Versioned<Collection<? extends V>>> putAndGet(K key, V value); |
| 118 | |
| 119 | /** |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 120 | * Removes the key-value pair with the specified values if it exists. In |
| 121 | * implementations that allow duplicates which matching entry will be |
| 122 | * removed is undefined. |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 123 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 124 | * @param key the key of the pair to be removed |
| 125 | * @param value the value of the pair to be removed |
| 126 | * @return a future whose value will be true if the map changed because of |
| 127 | * this call, false otherwise. |
| 128 | */ |
| 129 | CompletableFuture<Boolean> remove(K key, V value); |
| 130 | |
| 131 | /** |
Jordan Halterman | 8c57a09 | 2018-06-04 14:53:06 -0700 | [diff] [blame] | 132 | * Removes the key-value pair with the specified values if it exists. In |
| 133 | * implementations that allow duplicates which matching entry will be |
| 134 | * removed is undefined. |
| 135 | * |
| 136 | * @param key the key of the pair to be removed |
| 137 | * @param value the value of the pair to be removed |
| 138 | * @return a future to be completed with the updated values |
| 139 | */ |
| 140 | CompletableFuture<Versioned<Collection<? extends V>>> removeAndGet(K key, V value); |
| 141 | |
| 142 | /** |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 143 | * Removes the key-value pairs with the specified key and values if they |
| 144 | * exist. In implementations that allow duplicates each instance of a key |
| 145 | * will remove one matching entry, which one is not defined. Equivalent to |
| 146 | * repeated calls to {@code remove()} for each key value pair but more |
| 147 | * efficient. |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 148 | * |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 149 | * @param key the key of the pair to be removed |
| 150 | * @param values the set of values to be removed |
| 151 | * @return a future whose value will be true if the map changes because of |
| 152 | * this call, false otherwise. |
| 153 | */ |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 154 | CompletableFuture<Boolean> removeAll(K key, |
| 155 | Collection<? extends V> values); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 156 | |
| 157 | /** |
| 158 | * Removes all values associated with the specified key as well as the key |
| 159 | * itself. |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 160 | * |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 161 | * @param key the key whose key-value pairs will be removed |
| 162 | * @return a future whose value is the set of values that were removed, |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 163 | * which may be empty, if the values did not exist the version will be |
| 164 | * less than one. |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 165 | */ |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 166 | CompletableFuture<Versioned<Collection<? extends V>>> removeAll(K key); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 167 | |
| 168 | /** |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 169 | * Adds the set of key-value pairs of the specified key with each of the |
| 170 | * values in the iterable if each key-value pair does not already exist, |
| 171 | * if the pair does exist the behavior is implementation specific. |
| 172 | * (Same as repeated puts but with efficiency gains.) |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 173 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 174 | * @param key the key to use for all pairs to be added |
| 175 | * @param values the set of values to be added in pairs with the key |
| 176 | * @return a future whose value will be true if any change in the map |
| 177 | * results from this call, false otherwise |
| 178 | */ |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 179 | CompletableFuture<Boolean> putAll(K key, |
| 180 | Collection<? extends V> values); |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 181 | |
| 182 | /** |
| 183 | * Stores all the values in values associated with the key specified, |
| 184 | * removes all preexisting values and returns a collection of the removed |
| 185 | * values which may be empty if the entry did not exist. |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 186 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 187 | * @param key the key for all entries to be added |
| 188 | * @param values the values to be associated with the key |
| 189 | * @return a future whose value will be the collection of removed values, |
| 190 | * which may be empty |
| 191 | */ |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 192 | CompletableFuture<Versioned<Collection<? extends V>>> replaceValues( |
| 193 | K key, Collection<V> values); |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 194 | |
| 195 | /** |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 196 | * Removes all key-value pairs, after which it will be empty. |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 197 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 198 | * @return a future whose value is irrelevant, simply used to determine if |
| 199 | * the call has completed |
| 200 | */ |
| 201 | CompletableFuture<Void> clear(); |
| 202 | |
| 203 | /** |
| 204 | * Returns a collection of values associated with the specified key, if the |
| 205 | * key is not in the map it will return an empty collection. |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 206 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 207 | * @param key the key whose associated values will be returned |
| 208 | * @return a future whose value will be the collection of the values |
| 209 | * associated with the specified key, the collection may be empty |
| 210 | */ |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 211 | CompletableFuture<Versioned<Collection<? extends V>>> get(K key); |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 212 | |
| 213 | /** |
| 214 | * Returns a set of the keys contained in this multimap with one or more |
| 215 | * associated values. |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 216 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 217 | * @return a future whose value will be the collection of all keys with one |
| 218 | * or more associated values, this may be empty |
| 219 | */ |
| 220 | CompletableFuture<Set<K>> keySet(); |
| 221 | |
| 222 | /** |
| 223 | * Returns a multiset of the keys present in this multimap with one or more |
| 224 | * associated values each. Keys will appear once for each key-value pair |
| 225 | * in which they participate. |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 226 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 227 | * @return a future whose value will be a multiset of the keys, this may |
| 228 | * be empty |
| 229 | */ |
| 230 | CompletableFuture<Multiset<K>> keys(); |
| 231 | |
| 232 | /** |
| 233 | * Returns a collection of values in the set with duplicates permitted, the |
| 234 | * size of this collection will equal the size of the map at the time of |
| 235 | * creation. |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 236 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 237 | * @return a future whose value will be a collection of values, this may be |
| 238 | * empty |
| 239 | */ |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 240 | CompletableFuture<Multiset<V>> values(); |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * Returns a collection of each key-value pair in this map. |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 244 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 245 | * @return a future whose value will be a collection of all entries in the |
| 246 | * map, this may be empty |
| 247 | */ |
| 248 | CompletableFuture<Collection<Map.Entry<K, V>>> entries(); |
| 249 | |
| 250 | /** |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame] | 251 | * Registers the specified listener to be notified whenever the map is updated. |
| 252 | * |
| 253 | * @param listener listener to notify about map events |
| 254 | * @return future that will be completed when the operation finishes |
| 255 | */ |
| 256 | default CompletableFuture<Void> addListener(MultimapEventListener<K, V> listener) { |
| 257 | return addListener(listener, MoreExecutors.directExecutor()); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Registers the specified listener to be notified whenever the map is updated. |
| 262 | * |
| 263 | * @param listener listener to notify about map events |
| 264 | * @param executor executor to use for handling incoming map events |
| 265 | * @return future that will be completed when the operation finishes |
| 266 | */ |
| 267 | CompletableFuture<Void> addListener(MultimapEventListener<K, V> listener, Executor executor); |
| 268 | |
| 269 | /** |
| 270 | * Unregisters the specified listener such that it will no longer |
| 271 | * receive map change notifications. |
| 272 | * |
| 273 | * @param listener listener to unregister |
| 274 | * @return future that will be completed when the operation finishes |
| 275 | */ |
| 276 | CompletableFuture<Void> removeListener(MultimapEventListener<K, V> listener); |
| 277 | |
| 278 | /** |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 279 | * Returns a map of keys to collections of values that reflect the set of |
| 280 | * key-value pairs contained in the multimap, where the key value pairs |
| 281 | * would be the key paired with each of the values in the collection. |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 282 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 283 | * @return a future whose value will be a map of keys to collections of |
| 284 | * values, the returned map may be empty. |
| 285 | */ |
| 286 | CompletableFuture<Map<K, Collection<V>>> asMap(); |
Aaron Kruglikov | 61582a0 | 2016-09-06 13:18:58 -0700 | [diff] [blame] | 287 | |
| 288 | /** |
| 289 | * Returns a {@code ConsistentMultimap} instance that wraps this map. All |
| 290 | * calls will have the same behavior as this map but will be blocking |
| 291 | * instead of asynchronous. If a call does not complete within the |
| 292 | * default timeout an exception will be produced. |
| 293 | * |
| 294 | * @return a {@code ConsistentMultimap} which wraps this map, providing |
| 295 | * synchronous access to this map |
| 296 | */ |
| 297 | default ConsistentMultimap<K, V> asMultimap() { |
Jordan Halterman | 6440b09 | 2017-05-24 17:48:08 -0700 | [diff] [blame] | 298 | return asMultimap(DEFAULT_OPERATION_TIMEOUT_MILLIS); |
Aaron Kruglikov | 61582a0 | 2016-09-06 13:18:58 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Returns a {@code ConsistentMultimap} instance that wraps this map. All |
| 303 | * calls will have the same behavior as this map but will be blocking |
| 304 | * instead of asynchronous. If a call does not complete within the |
| 305 | * specified timeout an exception will be produced. |
| 306 | * |
| 307 | * @param timeoutMillis the number of millis to block while waiting for a |
| 308 | * call to return |
| 309 | * @return a {@code ConsistentMultimap} which wraps this map, providing |
| 310 | * synchronous access to this map |
| 311 | */ |
| 312 | default ConsistentMultimap<K, V> asMultimap(long timeoutMillis) { |
| 313 | return new DefaultConsistentMultimap(this, timeoutMillis); |
| 314 | } |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 315 | } |