blob: 99045a867c558754fbae4cf373cfedf63d4cd508 [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 /**
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 HIGUCHIfaa7c672016-07-28 13:42:07 -0700156 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700157 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -0700168 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700169 * @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 Kruglikovb6ec9cd2016-07-25 16:01:10 -0700178 */
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 HIGUCHIfaa7c672016-07-28 13:42:07 -0700184 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700185 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -0700194 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700195 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -0700204 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700205 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -0700213 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700214 * @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 Halterman5e884352018-05-21 22:11:07 -0700220 * <p>
221 * Do not use this method to read large maps. Use an {@link #iterator()} or {@link #stream()} instead.
Yuta HIGUCHIfaa7c672016-07-28 13:42:07 -0700222 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700223 * @return a collection of all entries in the map, this may be empty
224 */
225 Collection<Map.Entry<K, V>> entries();
226
227 /**
Jordan Halterman5e884352018-05-21 22:11:07 -0700228 * 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 Kruglikovb6ec9cd2016-07-25 16:01:10 -0700239 * 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 HIGUCHIfaa7c672016-07-28 13:42:07 -0700242 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700243 * @return a map of keys to collections of values, the returned map may be
244 * empty.
245 */
246 Map<K, Collection<V>> asMap();
Jonathan Hart46bf89b2017-02-27 15:56:42 -0800247
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 Kruglikovb6ec9cd2016-07-25 16:01:10 -0700272}