Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.store.service; |
| 18 | |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Multiset; |
| 20 | |
| 21 | import java.util.Collection; |
| 22 | import java.util.Map; |
| 23 | import java.util.Set; |
| 24 | import 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 | */ |
| 34 | public 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 Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 80 | * |
| 81 | * @param key key |
| 82 | * @param value value |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 83 | * @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 Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 95 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 96 | * @param key the key to add |
| 97 | * @param value the value to add |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 98 | * @return a future whose value will be true if the map has changed because |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 99 | * 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 Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 107 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 108 | * @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 Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 116 | * 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 Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 121 | * |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 122 | * @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 Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 127 | CompletableFuture<Boolean> removeAll(K key, |
| 128 | Collection<? extends V> values); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Removes all values associated with the specified key as well as the key |
| 132 | * itself. |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 133 | * |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 134 | * @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 Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 136 | * which may be empty, if the values did not exist the version will be |
| 137 | * less than one. |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 138 | */ |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 139 | CompletableFuture<Versioned<Collection<? extends V>>> removeAll(K key); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 140 | |
| 141 | /** |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 142 | * 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 Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 146 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 147 | * @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 Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 152 | CompletableFuture<Boolean> putAll(K key, |
| 153 | Collection<? extends V> values); |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 154 | |
| 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 Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 159 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 160 | * @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 Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 165 | CompletableFuture<Versioned<Collection<? extends V>>> replaceValues( |
| 166 | K key, Collection<V> values); |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 167 | |
| 168 | /** |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 169 | * Removes all key-value pairs, after which it will be empty. |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 170 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 171 | * @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 Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 179 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 180 | * @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 Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 184 | CompletableFuture<Versioned<Collection<? extends V>>> get(K key); |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 185 | |
| 186 | /** |
| 187 | * Returns a set of the keys contained in this multimap with one or more |
| 188 | * associated values. |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 189 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 190 | * @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 Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 199 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 200 | * @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 Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 209 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 210 | * @return a future whose value will be a collection of values, this may be |
| 211 | * empty |
| 212 | */ |
Aaron Kruglikov | 44a1fef | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 213 | CompletableFuture<Multiset<V>> values(); |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 214 | |
| 215 | /** |
| 216 | * Returns a collection of each key-value pair in this map. |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 217 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 218 | * @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 Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 227 | * |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 228 | * @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 | } |