blob: 8aabca7412933ec4b83c561776254137396a84a6 [file] [log] [blame]
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -07003 *
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
17package org.onosproject.store.service;
18
19import com.google.common.collect.Multiset;
Jonathan Hart46bf89b2017-02-27 15:56:42 -080020import com.google.common.util.concurrent.MoreExecutors;
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -070021
22import java.util.Collection;
23import java.util.Map;
24import java.util.Set;
Jonathan Hart46bf89b2017-02-27 15:56:42 -080025import java.util.concurrent.Executor;
Jordan Halterman5e884352018-05-21 22:11:07 -070026import java.util.stream.Stream;
27import java.util.stream.StreamSupport;
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -070028
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 Halterman5e884352018-05-21 22:11:07 -070034public interface ConsistentMultimap<K, V> extends DistributedPrimitive, Iterable<Map.Entry<K, V>> {
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -070035 /**
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 HIGUCHIfaa7c672016-07-28 13:42:07 -070043 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -070044 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -070051 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -070052 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -070061 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -070062 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -070071 *
72 * @param key the key to query
73 * @param value the value to query
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -070074 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -070085 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -070086 * @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 Halterman8c57a092018-06-04 14:53:06 -070094 * 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 Kruglikovb6ec9cd2016-07-25 16:01:10 -0700107 * 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 HIGUCHIfaa7c672016-07-28 13:42:07 -0700110 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700111 * @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 Halterman8c57a092018-06-04 14:53:06 -0700118 * 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 Kruglikovb6ec9cd2016-07-25 16:01:10 -0700129 * 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 HIGUCHIfaa7c672016-07-28 13:42:07 -0700134 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700135 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -0700144 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700145 * @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 /**
pier4fcb4b22019-10-11 18:19:59 +0200152 * Removes the set of key-value pairs with the specified key and values if they
153 * exist. In implementations that allow duplicates each instance of a key
154 * will remove one matching entry, which one is not defined. Equivalent to
155 * repeated calls to {@code remove()} for each key value pair but more
156 * efficient.
157 *
158 * @param mapping the keys-values to be removed
159 * @return true if the map changes because of this call, false otherwise.
160 */
161 boolean removeAll(Map<K, Collection<? extends V>> mapping);
162
163 /**
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700164 * Adds the set of key-value pairs of the specified key with each of the
165 * values in the iterable if each key-value pair does not already exist,
166 * if the pair does exist the behavior is implementation specific.
167 * (Same as repeated puts but with efficiency gains.)
Yuta HIGUCHIfaa7c672016-07-28 13:42:07 -0700168 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700169 * @param key the key to use for all pairs to be added
170 * @param values the set of values to be added in pairs with the key
171 * @return true if any change in the map results from this call,
172 * false otherwise
173 */
174 boolean putAll(K key, Collection<? extends V> values);
175
176 /**
pier4fcb4b22019-10-11 18:19:59 +0200177 * Adds the set of key-value pairs of the specified mapping with each of
178 * the values in the iterable if each key-value pair does not already exist,
179 * if the pair does exist the behavior is implementation specific.
180 * (Same as repeated puts but with efficiency gains.)
181 *
182 * @param mapping the keys-values to be added
183 * @return true if any change in the map results from this call,
184 * false otherwise
185 */
186 boolean putAll(Map<K, Collection<? extends V>> mapping);
187
188 /**
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700189 * Stores all the values in values associated with the key specified,
190 * removes all preexisting values and returns a collection of the removed
191 * values which may be empty if the entry did not exist.
Yuta HIGUCHIfaa7c672016-07-28 13:42:07 -0700192 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700193 * @param key the key for all entries to be added
194 * @param values the values to be associated with the key
195 * @return the collection of removed values, which may be empty
196 */
197 Versioned<Collection<? extends V>> replaceValues(K key,
198 Collection<V> values);
199
200 /**
201 * Removes all key-value pairs, after which it will be empty.
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700202 */
203 void clear();
204
205 /**
206 * Returns a collection of values associated with the specified key, if the
207 * key is not in the map it will return an empty collection.
Yuta HIGUCHIfaa7c672016-07-28 13:42:07 -0700208 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700209 * @param key the key whose associated values will be returned
210 * @return the collection of the values
211 * associated with the specified key, the collection may be empty
212 */
213 Versioned<Collection<? extends V>> get(K key);
214
215 /**
216 * Returns a set of the keys contained in this multimap with one or more
217 * associated values.
Yuta HIGUCHIfaa7c672016-07-28 13:42:07 -0700218 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700219 * @return the collection of all keys with one or more associated values,
220 * this may be empty
221 */
222 Set<K> keySet();
223
224 /**
225 * Returns a multiset of the keys present in this multimap with one or more
226 * associated values each. Keys will appear once for each key-value pair
227 * in which they participate.
Yuta HIGUCHIfaa7c672016-07-28 13:42:07 -0700228 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700229 * @return a multiset of the keys, this may be empty
230 */
231 Multiset<K> keys();
232
233 /**
234 * Returns a collection of values in the set with duplicates permitted, the
235 * size of this collection will equal the size of the map at the time of
236 * creation.
Yuta HIGUCHIfaa7c672016-07-28 13:42:07 -0700237 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700238 * @return a collection of values, this may be empty
239 */
240 Multiset<V> values();
241
242 /**
243 * Returns a collection of each key-value pair in this map.
Jordan Halterman5e884352018-05-21 22:11:07 -0700244 * <p>
245 * Do not use this method to read large maps. Use an {@link #iterator()} or {@link #stream()} instead.
Yuta HIGUCHIfaa7c672016-07-28 13:42:07 -0700246 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700247 * @return a collection of all entries in the map, this may be empty
248 */
249 Collection<Map.Entry<K, V>> entries();
250
251 /**
Jordan Halterman5e884352018-05-21 22:11:07 -0700252 * Streams entries from the map.
253 * <p>
254 * This method is optimized for large maps.
255 *
256 * @return the map entry stream
257 */
258 default Stream<Map.Entry<K, V>> stream() {
259 return StreamSupport.stream(spliterator(), false);
260 }
261
262 /**
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700263 * Returns a map of keys to collections of values that reflect the set of
264 * key-value pairs contained in the multimap, where the key value pairs
265 * would be the key paired with each of the values in the collection.
Yuta HIGUCHIfaa7c672016-07-28 13:42:07 -0700266 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700267 * @return a map of keys to collections of values, the returned map may be
268 * empty.
269 */
270 Map<K, Collection<V>> asMap();
Jonathan Hart46bf89b2017-02-27 15:56:42 -0800271
272 /**
273 * Registers the specified listener to be notified whenever the map is updated.
274 *
275 * @param listener listener to notify about map events
276 */
277 default void addListener(MultimapEventListener<K, V> listener) {
278 addListener(listener, MoreExecutors.directExecutor());
279 }
280
281 /**
282 * Registers the specified listener to be notified whenever the map is updated.
283 *
284 * @param listener listener to notify about map events
285 * @param executor executor to use for handling incoming map events
286 */
287 void addListener(MultimapEventListener<K, V> listener, Executor executor);
288
289 /**
290 * Unregisters the specified listener such that it will no longer
291 * receive map change notifications.
292 *
293 * @param listener listener to unregister
294 */
295 void removeListener(MultimapEventListener<K, V> listener);
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700296}