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