blob: da121c938df6b918b58b5bd352dd5b1689db21ed [file] [log] [blame]
Aaron Kruglikov99702652016-04-11 16:24:18 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
Aaron Kruglikov99702652016-04-11 16:24:18 -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
Aaron Kruglikov99702652016-04-11 16:24:18 -070019import com.google.common.collect.Multiset;
Aaron Kruglikov61582a02016-09-06 13:18:58 -070020import org.onosproject.store.primitives.DefaultConsistentMultimap;
Aaron Kruglikov99702652016-04-11 16:24:18 -070021
22import java.util.Collection;
23import java.util.Map;
24import java.util.Set;
25import java.util.concurrent.CompletableFuture;
26
27/**
28 * Interface for a distributed multimap.
29 *
30 * NOTE: Editing any returned collection will NOT effect the map itself and
31 * changes in the map will NOT be reflected in the returned collections.
32 * Certain operations may be too expensive when backed by a distributed data
33 * structure and have been labeled as such.
34 */
35public interface AsyncConsistentMultimap<K, V> extends DistributedPrimitive {
36
37 @Override
38 default DistributedPrimitive.Type primitiveType() {
39 return Type.CONSISTENT_MULTIMAP;
40 }
41
42 @Override
43 default CompletableFuture<Void> destroy() {
44 return clear();
45 }
46
47 /**
48 * Returns the number of key-value pairs in this multimap.
49 * @return the number of key-value pairs
50 */
51 CompletableFuture<Integer> size();
52
53 /**
54 * Returns if this multimap contains no key-value pairs.
55 * @return completable future that will be true if no key-value pairs
56 * exist, false otherwise
57 */
58 CompletableFuture<Boolean> isEmpty();
59
60 /**
61 * Returns true if there is at lease one key-value pair with a key equal to
62 * key.
63 * @param key the key to query
64 * @return a future whose value will be true if the map contains a
65 * key-value pair with key false otherwise
66 */
67 CompletableFuture<Boolean> containsKey(K key);
68
69 /**
70 * Returns true if this map contains at lease one key-value pair with a
71 * value equal to value.
72 * @param value the value to query
73 * @return a future whose value will be true if there is a key-value pair
74 * with the specified value, false otherwise.
75 */
76 CompletableFuture<Boolean> containsValue(V value);
77
78 /**
79 * Returns true if this map contains at least one key-value pair with key
80 * and value specified.
Ray Milkeybb23e0b2016-08-02 17:00:21 -070081 *
82 * @param key key
83 * @param value value
Aaron Kruglikov99702652016-04-11 16:24:18 -070084 * @return a future whose value will be true if there is a key-value pair
85 * with the specified key and value,
86 * false otherwise.
87 */
88 CompletableFuture<Boolean> containsEntry(K key, V value);
89
90 /**
91 * If the key-value pair does not already exist adds either the key value
92 * pair or the value to the set of values associated with the key and
93 * returns true, if the key-value pair already exists then behavior is
94 * implementation specific with some implementations allowing duplicates
95 * and others ignoring put requests for existing entries.
Ray Milkeybb23e0b2016-08-02 17:00:21 -070096 *
Aaron Kruglikov99702652016-04-11 16:24:18 -070097 * @param key the key to add
98 * @param value the value to add
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -070099 * @return a future whose value will be true if the map has changed because
Aaron Kruglikov99702652016-04-11 16:24:18 -0700100 * of this call, false otherwise
101 */
102 CompletableFuture<Boolean> put(K key, V value);
103
104 /**
105 * Removes the key-value pair with the specified values if it exists. In
106 * implementations that allow duplicates which matching entry will be
107 * removed is undefined.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700108 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700109 * @param key the key of the pair to be removed
110 * @param value the value of the pair to be removed
111 * @return a future whose value will be true if the map changed because of
112 * this call, false otherwise.
113 */
114 CompletableFuture<Boolean> remove(K key, V value);
115
116 /**
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700117 * Removes the key-value pairs with the specified key and values if they
118 * exist. In implementations that allow duplicates each instance of a key
119 * will remove one matching entry, which one is not defined. Equivalent to
120 * repeated calls to {@code remove()} for each key value pair but more
121 * efficient.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700122 *
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700123 * @param key the key of the pair to be removed
124 * @param values the set of values to be removed
125 * @return a future whose value will be true if the map changes because of
126 * this call, false otherwise.
127 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700128 CompletableFuture<Boolean> removeAll(K key,
129 Collection<? extends V> values);
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700130
131 /**
132 * Removes all values associated with the specified key as well as the key
133 * itself.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700134 *
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700135 * @param key the key whose key-value pairs will be removed
136 * @return a future whose value is the set of values that were removed,
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700137 * which may be empty, if the values did not exist the version will be
138 * less than one.
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700139 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700140 CompletableFuture<Versioned<Collection<? extends V>>> removeAll(K key);
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700141
142 /**
Aaron Kruglikov99702652016-04-11 16:24:18 -0700143 * Adds the set of key-value pairs of the specified key with each of the
144 * values in the iterable if each key-value pair does not already exist,
145 * if the pair does exist the behavior is implementation specific.
146 * (Same as repeated puts but with efficiency gains.)
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700147 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700148 * @param key the key to use for all pairs to be added
149 * @param values the set of values to be added in pairs with the key
150 * @return a future whose value will be true if any change in the map
151 * results from this call, false otherwise
152 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700153 CompletableFuture<Boolean> putAll(K key,
154 Collection<? extends V> values);
Aaron Kruglikov99702652016-04-11 16:24:18 -0700155
156 /**
157 * Stores all the values in values associated with the key specified,
158 * removes all preexisting values and returns a collection of the removed
159 * values which may be empty if the entry did not exist.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700160 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700161 * @param key the key for all entries to be added
162 * @param values the values to be associated with the key
163 * @return a future whose value will be the collection of removed values,
164 * which may be empty
165 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700166 CompletableFuture<Versioned<Collection<? extends V>>> replaceValues(
167 K key, Collection<V> values);
Aaron Kruglikov99702652016-04-11 16:24:18 -0700168
169 /**
Aaron Kruglikov99702652016-04-11 16:24:18 -0700170 * Removes all key-value pairs, after which it will be empty.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700171 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700172 * @return a future whose value is irrelevant, simply used to determine if
173 * the call has completed
174 */
175 CompletableFuture<Void> clear();
176
177 /**
178 * Returns a collection of values associated with the specified key, if the
179 * key is not in the map it will return an empty collection.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700180 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700181 * @param key the key whose associated values will be returned
182 * @return a future whose value will be the collection of the values
183 * associated with the specified key, the collection may be empty
184 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700185 CompletableFuture<Versioned<Collection<? extends V>>> get(K key);
Aaron Kruglikov99702652016-04-11 16:24:18 -0700186
187 /**
188 * Returns a set of the keys contained in this multimap with one or more
189 * associated values.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700190 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700191 * @return a future whose value will be the collection of all keys with one
192 * or more associated values, this may be empty
193 */
194 CompletableFuture<Set<K>> keySet();
195
196 /**
197 * Returns a multiset of the keys present in this multimap with one or more
198 * associated values each. Keys will appear once for each key-value pair
199 * in which they participate.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700200 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700201 * @return a future whose value will be a multiset of the keys, this may
202 * be empty
203 */
204 CompletableFuture<Multiset<K>> keys();
205
206 /**
207 * Returns a collection of values in the set with duplicates permitted, the
208 * size of this collection will equal the size of the map at the time of
209 * creation.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700210 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700211 * @return a future whose value will be a collection of values, this may be
212 * empty
213 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700214 CompletableFuture<Multiset<V>> values();
Aaron Kruglikov99702652016-04-11 16:24:18 -0700215
216 /**
217 * Returns a collection of each key-value pair in this map.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700218 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700219 * @return a future whose value will be a collection of all entries in the
220 * map, this may be empty
221 */
222 CompletableFuture<Collection<Map.Entry<K, V>>> entries();
223
224 /**
225 * Returns a map of keys to collections of values that reflect the set of
226 * key-value pairs contained in the multimap, where the key value pairs
227 * would be the key paired with each of the values in the collection.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700228 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700229 * @return a future whose value will be a map of keys to collections of
230 * values, the returned map may be empty.
231 */
232 CompletableFuture<Map<K, Collection<V>>> asMap();
Aaron Kruglikov61582a02016-09-06 13:18:58 -0700233
234 /**
235 * Returns a {@code ConsistentMultimap} instance that wraps this map. All
236 * calls will have the same behavior as this map but will be blocking
237 * instead of asynchronous. If a call does not complete within the
238 * default timeout an exception will be produced.
239 *
240 * @return a {@code ConsistentMultimap} which wraps this map, providing
241 * synchronous access to this map
242 */
243 default ConsistentMultimap<K, V> asMultimap() {
244 return asMultimap(DEFAULT_OPERTATION_TIMEOUT_MILLIS);
245 }
246
247 /**
248 * Returns a {@code ConsistentMultimap} instance that wraps this map. All
249 * calls will have the same behavior as this map but will be blocking
250 * instead of asynchronous. If a call does not complete within the
251 * specified timeout an exception will be produced.
252 *
253 * @param timeoutMillis the number of millis to block while waiting for a
254 * call to return
255 * @return a {@code ConsistentMultimap} which wraps this map, providing
256 * synchronous access to this map
257 */
258 default ConsistentMultimap<K, V> asMultimap(long timeoutMillis) {
259 return new DefaultConsistentMultimap(this, timeoutMillis);
260 }
Aaron Kruglikov99702652016-04-11 16:24:18 -0700261}