blob: bba38bb28ccbac716ea1a177130dd629bec7f1e7 [file] [log] [blame]
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
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;
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -070026
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 */
32public 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 HIGUCHIfaa7c672016-07-28 13:42:07 -070041 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -070042 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -070049 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -070050 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -070059 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -070060 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -070069 *
70 * @param key the key to query
71 * @param value the value to query
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -070072 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -070083 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -070084 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -070095 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -070096 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -0700108 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700109 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -0700118 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700119 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -0700130 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700131 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -0700142 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700143 * @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 Kruglikovb6ec9cd2016-07-25 16:01:10 -0700152 */
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 HIGUCHIfaa7c672016-07-28 13:42:07 -0700158 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700159 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -0700168 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700169 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -0700178 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700179 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -0700187 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700188 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -0700194 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700195 * @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 HIGUCHIfaa7c672016-07-28 13:42:07 -0700203 *
Aaron Kruglikovb6ec9cd2016-07-25 16:01:10 -0700204 * @return a map of keys to collections of values, the returned map may be
205 * empty.
206 */
207 Map<K, Collection<V>> asMap();
Jonathan Hart46bf89b2017-02-27 15:56:42 -0800208
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 Kruglikovb6ec9cd2016-07-25 16:01:10 -0700233}