Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -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 | b6ec9cd | 2016-07-25 16:01:10 -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 | |
| 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 | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 21 | |
| 22 | import java.util.Collection; |
| 23 | import java.util.Map; |
| 24 | import java.util.Set; |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame] | 25 | import java.util.concurrent.Executor; |
Jordan Halterman | 5e88435 | 2018-05-21 22:11:07 -0700 | [diff] [blame] | 26 | import java.util.stream.Stream; |
| 27 | import java.util.stream.StreamSupport; |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * This provides a synchronous version of the functionality provided by |
| 31 | * {@link AsyncConsistentMultimap}. Instead of returning futures this map |
| 32 | * blocks until the future completes then returns the result. |
| 33 | */ |
Jordan Halterman | 5e88435 | 2018-05-21 22:11:07 -0700 | [diff] [blame] | 34 | public interface ConsistentMultimap<K, V> extends DistributedPrimitive, Iterable<Map.Entry<K, V>> { |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Returns the number of key-value pairs in this multimap. |
| 37 | * @return the number of key-value pairs |
| 38 | */ |
| 39 | int size(); |
| 40 | |
| 41 | /** |
| 42 | * Returns if this multimap contains no key-value pairs. |
Yuta HIGUCHI | faa7c67 | 2016-07-28 13:42:07 -0700 | [diff] [blame] | 43 | * |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 44 | * @return true if no key-value pairs exist, false otherwise |
| 45 | */ |
| 46 | boolean isEmpty(); |
| 47 | |
| 48 | /** |
| 49 | * Returns true if there is at lease one key-value pair with a key equal to |
| 50 | * key. |
Yuta HIGUCHI | faa7c67 | 2016-07-28 13:42:07 -0700 | [diff] [blame] | 51 | * |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 52 | * @param key the key to query |
| 53 | * @return true if the map contains a |
| 54 | * key-value pair with key false otherwise |
| 55 | */ |
| 56 | boolean containsKey(K key); |
| 57 | |
| 58 | /** |
| 59 | * Returns true if this map contains at lease one key-value pair with a |
| 60 | * value equal to value. |
Yuta HIGUCHI | faa7c67 | 2016-07-28 13:42:07 -0700 | [diff] [blame] | 61 | * |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 62 | * @param value the value to query |
| 63 | * @return true if there is a key-value pair with the specified value, |
| 64 | * false otherwise. |
| 65 | */ |
| 66 | boolean containsValue(V value); |
| 67 | |
| 68 | /** |
| 69 | * Returns true if this map contains at least one key-value pair with key |
| 70 | * and value specified. |
Yuta HIGUCHI | faa7c67 | 2016-07-28 13:42:07 -0700 | [diff] [blame] | 71 | * |
| 72 | * @param key the key to query |
| 73 | * @param value the value to query |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 74 | * @return true if there is a key-value pair with the specified key and |
| 75 | * value, false otherwise. |
| 76 | */ |
| 77 | boolean containsEntry(K key, V value); |
| 78 | |
| 79 | /** |
| 80 | * If the key-value pair does not already exist adds either the key value |
| 81 | * pair or the value to the set of values associated with the key and |
| 82 | * returns true, if the key-value pair already exists then behavior is |
| 83 | * implementation specific with some implementations allowing duplicates |
| 84 | * and others ignoring put requests for existing entries. |
Yuta HIGUCHI | faa7c67 | 2016-07-28 13:42:07 -0700 | [diff] [blame] | 85 | * |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 86 | * @param key the key to add |
| 87 | * @param value the value to add |
| 88 | * @return true if the map has changed because of this call, |
| 89 | * false otherwise |
| 90 | */ |
| 91 | boolean put(K key, V value); |
| 92 | |
| 93 | /** |
Jordan Halterman | 8c57a09 | 2018-06-04 14:53:06 -0700 | [diff] [blame] | 94 | * If the key-value pair does not already exist adds either the key value |
| 95 | * pair or the value to the set of values associated with the key and |
| 96 | * returns the updated value, if the key-value pair already exists then behavior |
| 97 | * is implementation specific with some implementations allowing duplicates |
| 98 | * and others ignoring put requests for existing entries. |
| 99 | * |
| 100 | * @param key the key to add |
| 101 | * @param value the value to add |
| 102 | * @return the updated values |
| 103 | */ |
| 104 | Versioned<Collection<? extends V>> putAndGet(K key, V value); |
| 105 | |
| 106 | /** |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 107 | * Removes the key-value pair with the specified values if it exists. In |
| 108 | * implementations that allow duplicates which matching entry will be |
| 109 | * removed is undefined. |
Yuta HIGUCHI | faa7c67 | 2016-07-28 13:42:07 -0700 | [diff] [blame] | 110 | * |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 111 | * @param key the key of the pair to be removed |
| 112 | * @param value the value of the pair to be removed |
| 113 | * @return true if the map changed because of this call, false otherwise. |
| 114 | */ |
| 115 | boolean remove(K key, V value); |
| 116 | |
| 117 | /** |
Jordan Halterman | 8c57a09 | 2018-06-04 14:53:06 -0700 | [diff] [blame] | 118 | * Removes the key-value pair with the specified values if it exists. In |
| 119 | * implementations that allow duplicates which matching entry will be |
| 120 | * removed is undefined. |
| 121 | * |
| 122 | * @param key the key of the pair to be removed |
| 123 | * @param value the value of the pair to be removed |
| 124 | * @return the updated values |
| 125 | */ |
| 126 | Versioned<Collection<? extends V>> removeAndGet(K key, V value); |
| 127 | |
| 128 | /** |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 129 | * Removes the key-value pairs with the specified key and values if they |
| 130 | * exist. In implementations that allow duplicates each instance of a key |
| 131 | * will remove one matching entry, which one is not defined. Equivalent to |
| 132 | * repeated calls to {@code remove()} for each key value pair but more |
| 133 | * efficient. |
Yuta HIGUCHI | faa7c67 | 2016-07-28 13:42:07 -0700 | [diff] [blame] | 134 | * |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 135 | * @param key the key of the pair to be removed |
| 136 | * @param values the set of values to be removed |
| 137 | * @return true if the map changes because of this call, false otherwise. |
| 138 | */ |
| 139 | boolean removeAll(K key, Collection<? extends V> values); |
| 140 | |
| 141 | /** |
| 142 | * Removes all values associated with the specified key as well as the key |
| 143 | * itself. |
Yuta HIGUCHI | faa7c67 | 2016-07-28 13:42:07 -0700 | [diff] [blame] | 144 | * |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 145 | * @param key the key whose key-value pairs will be removed |
| 146 | * @return the set of values that were removed, which may be empty, if the |
| 147 | * values did not exist the version will be less than one. |
| 148 | */ |
| 149 | Versioned<Collection<? extends V>> removeAll(K key); |
| 150 | |
| 151 | /** |
| 152 | * Adds the set of key-value pairs of the specified key with each of the |
| 153 | * values in the iterable if each key-value pair does not already exist, |
| 154 | * if the pair does exist the behavior is implementation specific. |
| 155 | * (Same as repeated puts but with efficiency gains.) |
Yuta HIGUCHI | faa7c67 | 2016-07-28 13:42:07 -0700 | [diff] [blame] | 156 | * |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 157 | * @param key the key to use for all pairs to be added |
| 158 | * @param values the set of values to be added in pairs with the key |
| 159 | * @return true if any change in the map results from this call, |
| 160 | * false otherwise |
| 161 | */ |
| 162 | boolean putAll(K key, Collection<? extends V> values); |
| 163 | |
| 164 | /** |
| 165 | * Stores all the values in values associated with the key specified, |
| 166 | * removes all preexisting values and returns a collection of the removed |
| 167 | * values which may be empty if the entry did not exist. |
Yuta HIGUCHI | faa7c67 | 2016-07-28 13:42:07 -0700 | [diff] [blame] | 168 | * |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 169 | * @param key the key for all entries to be added |
| 170 | * @param values the values to be associated with the key |
| 171 | * @return the collection of removed values, which may be empty |
| 172 | */ |
| 173 | Versioned<Collection<? extends V>> replaceValues(K key, |
| 174 | Collection<V> values); |
| 175 | |
| 176 | /** |
| 177 | * Removes all key-value pairs, after which it will be empty. |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 178 | */ |
| 179 | void clear(); |
| 180 | |
| 181 | /** |
| 182 | * Returns a collection of values associated with the specified key, if the |
| 183 | * key is not in the map it will return an empty collection. |
Yuta HIGUCHI | faa7c67 | 2016-07-28 13:42:07 -0700 | [diff] [blame] | 184 | * |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 185 | * @param key the key whose associated values will be returned |
| 186 | * @return the collection of the values |
| 187 | * associated with the specified key, the collection may be empty |
| 188 | */ |
| 189 | Versioned<Collection<? extends V>> get(K key); |
| 190 | |
| 191 | /** |
| 192 | * Returns a set of the keys contained in this multimap with one or more |
| 193 | * associated values. |
Yuta HIGUCHI | faa7c67 | 2016-07-28 13:42:07 -0700 | [diff] [blame] | 194 | * |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 195 | * @return the collection of all keys with one or more associated values, |
| 196 | * this may be empty |
| 197 | */ |
| 198 | Set<K> keySet(); |
| 199 | |
| 200 | /** |
| 201 | * Returns a multiset of the keys present in this multimap with one or more |
| 202 | * associated values each. Keys will appear once for each key-value pair |
| 203 | * in which they participate. |
Yuta HIGUCHI | faa7c67 | 2016-07-28 13:42:07 -0700 | [diff] [blame] | 204 | * |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 205 | * @return a multiset of the keys, this may be empty |
| 206 | */ |
| 207 | Multiset<K> keys(); |
| 208 | |
| 209 | /** |
| 210 | * Returns a collection of values in the set with duplicates permitted, the |
| 211 | * size of this collection will equal the size of the map at the time of |
| 212 | * creation. |
Yuta HIGUCHI | faa7c67 | 2016-07-28 13:42:07 -0700 | [diff] [blame] | 213 | * |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 214 | * @return a collection of values, this may be empty |
| 215 | */ |
| 216 | Multiset<V> values(); |
| 217 | |
| 218 | /** |
| 219 | * Returns a collection of each key-value pair in this map. |
Jordan Halterman | 5e88435 | 2018-05-21 22:11:07 -0700 | [diff] [blame] | 220 | * <p> |
| 221 | * Do not use this method to read large maps. Use an {@link #iterator()} or {@link #stream()} instead. |
Yuta HIGUCHI | faa7c67 | 2016-07-28 13:42:07 -0700 | [diff] [blame] | 222 | * |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 223 | * @return a collection of all entries in the map, this may be empty |
| 224 | */ |
| 225 | Collection<Map.Entry<K, V>> entries(); |
| 226 | |
| 227 | /** |
Jordan Halterman | 5e88435 | 2018-05-21 22:11:07 -0700 | [diff] [blame] | 228 | * Streams entries from the map. |
| 229 | * <p> |
| 230 | * This method is optimized for large maps. |
| 231 | * |
| 232 | * @return the map entry stream |
| 233 | */ |
| 234 | default Stream<Map.Entry<K, V>> stream() { |
| 235 | return StreamSupport.stream(spliterator(), false); |
| 236 | } |
| 237 | |
| 238 | /** |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 239 | * Returns a map of keys to collections of values that reflect the set of |
| 240 | * key-value pairs contained in the multimap, where the key value pairs |
| 241 | * would be the key paired with each of the values in the collection. |
Yuta HIGUCHI | faa7c67 | 2016-07-28 13:42:07 -0700 | [diff] [blame] | 242 | * |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 243 | * @return a map of keys to collections of values, the returned map may be |
| 244 | * empty. |
| 245 | */ |
| 246 | Map<K, Collection<V>> asMap(); |
Jonathan Hart | 46bf89b | 2017-02-27 15:56:42 -0800 | [diff] [blame] | 247 | |
| 248 | /** |
| 249 | * Registers the specified listener to be notified whenever the map is updated. |
| 250 | * |
| 251 | * @param listener listener to notify about map events |
| 252 | */ |
| 253 | default void addListener(MultimapEventListener<K, V> listener) { |
| 254 | addListener(listener, MoreExecutors.directExecutor()); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Registers the specified listener to be notified whenever the map is updated. |
| 259 | * |
| 260 | * @param listener listener to notify about map events |
| 261 | * @param executor executor to use for handling incoming map events |
| 262 | */ |
| 263 | void addListener(MultimapEventListener<K, V> listener, Executor executor); |
| 264 | |
| 265 | /** |
| 266 | * Unregisters the specified listener such that it will no longer |
| 267 | * receive map change notifications. |
| 268 | * |
| 269 | * @param listener listener to unregister |
| 270 | */ |
| 271 | void removeListener(MultimapEventListener<K, V> listener); |
Aaron Kruglikov | b6ec9cd | 2016-07-25 16:01:10 -0700 | [diff] [blame] | 272 | } |