blob: 8f35c33723cf94a2c383c73b30882cf135974686 [file] [log] [blame]
Aaron Kruglikov99702652016-04-11 16:24:18 -07001/*
2 * Copyright 2016 Open Networking Laboratory
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
17package org.onosproject.store.service;
18
Aaron Kruglikov99702652016-04-11 16:24:18 -070019import com.google.common.collect.Multiset;
20
21import java.util.Collection;
22import java.util.Map;
23import java.util.Set;
24import java.util.concurrent.CompletableFuture;
25
26/**
27 * Interface for a distributed multimap.
28 *
29 * NOTE: Editing any returned collection will NOT effect the map itself and
30 * changes in the map will NOT be reflected in the returned collections.
31 * Certain operations may be too expensive when backed by a distributed data
32 * structure and have been labeled as such.
33 */
34public interface AsyncConsistentMultimap<K, V> extends DistributedPrimitive {
35
36 @Override
37 default DistributedPrimitive.Type primitiveType() {
38 return Type.CONSISTENT_MULTIMAP;
39 }
40
41 @Override
42 default CompletableFuture<Void> destroy() {
43 return clear();
44 }
45
46 /**
47 * Returns the number of key-value pairs in this multimap.
48 * @return the number of key-value pairs
49 */
50 CompletableFuture<Integer> size();
51
52 /**
53 * Returns if this multimap contains no key-value pairs.
54 * @return completable future that will be true if no key-value pairs
55 * exist, false otherwise
56 */
57 CompletableFuture<Boolean> isEmpty();
58
59 /**
60 * Returns true if there is at lease one key-value pair with a key equal to
61 * key.
62 * @param key the key to query
63 * @return a future whose value will be true if the map contains a
64 * key-value pair with key false otherwise
65 */
66 CompletableFuture<Boolean> containsKey(K key);
67
68 /**
69 * Returns true if this map contains at lease one key-value pair with a
70 * value equal to value.
71 * @param value the value to query
72 * @return a future whose value will be true if there is a key-value pair
73 * with the specified value, false otherwise.
74 */
75 CompletableFuture<Boolean> containsValue(V value);
76
77 /**
78 * Returns true if this map contains at least one key-value pair with key
79 * and value specified.
Ray Milkeybb23e0b2016-08-02 17:00:21 -070080 *
81 * @param key key
82 * @param value value
Aaron Kruglikov99702652016-04-11 16:24:18 -070083 * @return a future whose value will be true if there is a key-value pair
84 * with the specified key and value,
85 * false otherwise.
86 */
87 CompletableFuture<Boolean> containsEntry(K key, V value);
88
89 /**
90 * If the key-value pair does not already exist adds either the key value
91 * pair or the value to the set of values associated with the key and
92 * returns true, if the key-value pair already exists then behavior is
93 * implementation specific with some implementations allowing duplicates
94 * and others ignoring put requests for existing entries.
Ray Milkeybb23e0b2016-08-02 17:00:21 -070095 *
Aaron Kruglikov99702652016-04-11 16:24:18 -070096 * @param key the key to add
97 * @param value the value to add
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -070098 * @return a future whose value will be true if the map has changed because
Aaron Kruglikov99702652016-04-11 16:24:18 -070099 * of this call, false otherwise
100 */
101 CompletableFuture<Boolean> put(K key, V value);
102
103 /**
104 * Removes the key-value pair with the specified values if it exists. In
105 * implementations that allow duplicates which matching entry will be
106 * removed is undefined.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700107 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700108 * @param key the key of the pair to be removed
109 * @param value the value of the pair to be removed
110 * @return a future whose value will be true if the map changed because of
111 * this call, false otherwise.
112 */
113 CompletableFuture<Boolean> remove(K key, V value);
114
115 /**
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700116 * Removes the key-value pairs with the specified key and values if they
117 * exist. In implementations that allow duplicates each instance of a key
118 * will remove one matching entry, which one is not defined. Equivalent to
119 * repeated calls to {@code remove()} for each key value pair but more
120 * efficient.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700121 *
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700122 * @param key the key of the pair to be removed
123 * @param values the set of values to be removed
124 * @return a future whose value will be true if the map changes because of
125 * this call, false otherwise.
126 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700127 CompletableFuture<Boolean> removeAll(K key,
128 Collection<? extends V> values);
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700129
130 /**
131 * Removes all values associated with the specified key as well as the key
132 * itself.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700133 *
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700134 * @param key the key whose key-value pairs will be removed
135 * @return a future whose value is the set of values that were removed,
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700136 * which may be empty, if the values did not exist the version will be
137 * less than one.
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700138 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700139 CompletableFuture<Versioned<Collection<? extends V>>> removeAll(K key);
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700140
141 /**
Aaron Kruglikov99702652016-04-11 16:24:18 -0700142 * Adds the set of key-value pairs of the specified key with each of the
143 * values in the iterable if each key-value pair does not already exist,
144 * if the pair does exist the behavior is implementation specific.
145 * (Same as repeated puts but with efficiency gains.)
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700146 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700147 * @param key the key to use for all pairs to be added
148 * @param values the set of values to be added in pairs with the key
149 * @return a future whose value will be true if any change in the map
150 * results from this call, false otherwise
151 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700152 CompletableFuture<Boolean> putAll(K key,
153 Collection<? extends V> values);
Aaron Kruglikov99702652016-04-11 16:24:18 -0700154
155 /**
156 * Stores all the values in values associated with the key specified,
157 * removes all preexisting values and returns a collection of the removed
158 * values which may be empty if the entry did not exist.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700159 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700160 * @param key the key for all entries to be added
161 * @param values the values to be associated with the key
162 * @return a future whose value will be the collection of removed values,
163 * which may be empty
164 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700165 CompletableFuture<Versioned<Collection<? extends V>>> replaceValues(
166 K key, Collection<V> values);
Aaron Kruglikov99702652016-04-11 16:24:18 -0700167
168 /**
Aaron Kruglikov99702652016-04-11 16:24:18 -0700169 * Removes all key-value pairs, after which it will be empty.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700170 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700171 * @return a future whose value is irrelevant, simply used to determine if
172 * the call has completed
173 */
174 CompletableFuture<Void> clear();
175
176 /**
177 * Returns a collection of values associated with the specified key, if the
178 * key is not in the map it will return an empty collection.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700179 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700180 * @param key the key whose associated values will be returned
181 * @return a future whose value will be the collection of the values
182 * associated with the specified key, the collection may be empty
183 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700184 CompletableFuture<Versioned<Collection<? extends V>>> get(K key);
Aaron Kruglikov99702652016-04-11 16:24:18 -0700185
186 /**
187 * Returns a set of the keys contained in this multimap with one or more
188 * associated values.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700189 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700190 * @return a future whose value will be the collection of all keys with one
191 * or more associated values, this may be empty
192 */
193 CompletableFuture<Set<K>> keySet();
194
195 /**
196 * Returns a multiset of the keys present in this multimap with one or more
197 * associated values each. Keys will appear once for each key-value pair
198 * in which they participate.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700199 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700200 * @return a future whose value will be a multiset of the keys, this may
201 * be empty
202 */
203 CompletableFuture<Multiset<K>> keys();
204
205 /**
206 * Returns a collection of values in the set with duplicates permitted, the
207 * size of this collection will equal the size of the map at the time of
208 * creation.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700209 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700210 * @return a future whose value will be a collection of values, this may be
211 * empty
212 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700213 CompletableFuture<Multiset<V>> values();
Aaron Kruglikov99702652016-04-11 16:24:18 -0700214
215 /**
216 * Returns a collection of each key-value pair in this map.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700217 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700218 * @return a future whose value will be a collection of all entries in the
219 * map, this may be empty
220 */
221 CompletableFuture<Collection<Map.Entry<K, V>>> entries();
222
223 /**
224 * Returns a map of keys to collections of values that reflect the set of
225 * key-value pairs contained in the multimap, where the key value pairs
226 * would be the key paired with each of the values in the collection.
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700227 *
Aaron Kruglikov99702652016-04-11 16:24:18 -0700228 * @return a future whose value will be a map of keys to collections of
229 * values, the returned map may be empty.
230 */
231 CompletableFuture<Map<K, Collection<V>>> asMap();
232}