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. |
| 80 | * @return a future whose value will be true if there is a key-value pair |
| 81 | * with the specified key and value, |
| 82 | * false otherwise. |
| 83 | */ |
| 84 | CompletableFuture<Boolean> containsEntry(K key, V value); |
| 85 | |
| 86 | /** |
| 87 | * If the key-value pair does not already exist adds either the key value |
| 88 | * pair or the value to the set of values associated with the key and |
| 89 | * returns true, if the key-value pair already exists then behavior is |
| 90 | * implementation specific with some implementations allowing duplicates |
| 91 | * and others ignoring put requests for existing entries. |
| 92 | * @param key the key to add |
| 93 | * @param value the value to add |
Aaron Kruglikov | d9d73a0 | 2016-04-27 11:29:23 -0700 | [diff] [blame^] | 94 | * @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] | 95 | * of this call, false otherwise |
| 96 | */ |
| 97 | CompletableFuture<Boolean> put(K key, V value); |
| 98 | |
| 99 | /** |
| 100 | * Removes the key-value pair with the specified values if it exists. In |
| 101 | * implementations that allow duplicates which matching entry will be |
| 102 | * removed is undefined. |
| 103 | * @param key the key of the pair to be removed |
| 104 | * @param value the value of the pair to be removed |
| 105 | * @return a future whose value will be true if the map changed because of |
| 106 | * this call, false otherwise. |
| 107 | */ |
| 108 | CompletableFuture<Boolean> remove(K key, V value); |
| 109 | |
| 110 | /** |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 111 | * Removes the key-value pairs with the specified key and values if they |
| 112 | * exist. In implementations that allow duplicates each instance of a key |
| 113 | * will remove one matching entry, which one is not defined. Equivalent to |
| 114 | * repeated calls to {@code remove()} for each key value pair but more |
| 115 | * efficient. |
| 116 | * @param key the key of the pair to be removed |
| 117 | * @param values the set of values to be removed |
| 118 | * @return a future whose value will be true if the map changes because of |
| 119 | * this call, false otherwise. |
| 120 | */ |
Aaron Kruglikov | d9d73a0 | 2016-04-27 11:29:23 -0700 | [diff] [blame^] | 121 | CompletableFuture<Boolean> removeAll(K key, |
| 122 | Collection<? extends V> values); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * Removes all values associated with the specified key as well as the key |
| 126 | * itself. |
| 127 | * @param key the key whose key-value pairs will be removed |
| 128 | * @return a future whose value is the set of values that were removed, |
Aaron Kruglikov | d9d73a0 | 2016-04-27 11:29:23 -0700 | [diff] [blame^] | 129 | * which may be empty, if the values did not exist the version will be |
| 130 | * less than one. |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 131 | */ |
Aaron Kruglikov | d9d73a0 | 2016-04-27 11:29:23 -0700 | [diff] [blame^] | 132 | CompletableFuture<Versioned<Collection<? extends V>>> removeAll(K key); |
Aaron Kruglikov | a26f654 | 2016-04-19 13:37:42 -0700 | [diff] [blame] | 133 | |
| 134 | /** |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 135 | * Adds the set of key-value pairs of the specified key with each of the |
| 136 | * values in the iterable if each key-value pair does not already exist, |
| 137 | * if the pair does exist the behavior is implementation specific. |
| 138 | * (Same as repeated puts but with efficiency gains.) |
| 139 | * @param key the key to use for all pairs to be added |
| 140 | * @param values the set of values to be added in pairs with the key |
| 141 | * @return a future whose value will be true if any change in the map |
| 142 | * results from this call, false otherwise |
| 143 | */ |
Aaron Kruglikov | d9d73a0 | 2016-04-27 11:29:23 -0700 | [diff] [blame^] | 144 | CompletableFuture<Boolean> putAll(K key, |
| 145 | Collection<? extends V> values); |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 146 | |
| 147 | /** |
| 148 | * Stores all the values in values associated with the key specified, |
| 149 | * removes all preexisting values and returns a collection of the removed |
| 150 | * values which may be empty if the entry did not exist. |
| 151 | * @param key the key for all entries to be added |
| 152 | * @param values the values to be associated with the key |
| 153 | * @return a future whose value will be the collection of removed values, |
| 154 | * which may be empty |
| 155 | */ |
Aaron Kruglikov | d9d73a0 | 2016-04-27 11:29:23 -0700 | [diff] [blame^] | 156 | CompletableFuture<Versioned<Collection<? extends V>>> replaceValues( |
| 157 | K key, Collection<V> values); |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 158 | |
| 159 | /** |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 160 | * Removes all key-value pairs, after which it will be empty. |
| 161 | * @return a future whose value is irrelevant, simply used to determine if |
| 162 | * the call has completed |
| 163 | */ |
| 164 | CompletableFuture<Void> clear(); |
| 165 | |
| 166 | /** |
| 167 | * Returns a collection of values associated with the specified key, if the |
| 168 | * key is not in the map it will return an empty collection. |
| 169 | * @param key the key whose associated values will be returned |
| 170 | * @return a future whose value will be the collection of the values |
| 171 | * associated with the specified key, the collection may be empty |
| 172 | */ |
Aaron Kruglikov | d9d73a0 | 2016-04-27 11:29:23 -0700 | [diff] [blame^] | 173 | CompletableFuture<Versioned<Collection<? extends V>>> get(K key); |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * Returns a set of the keys contained in this multimap with one or more |
| 177 | * associated values. |
| 178 | * @return a future whose value will be the collection of all keys with one |
| 179 | * or more associated values, this may be empty |
| 180 | */ |
| 181 | CompletableFuture<Set<K>> keySet(); |
| 182 | |
| 183 | /** |
| 184 | * Returns a multiset of the keys present in this multimap with one or more |
| 185 | * associated values each. Keys will appear once for each key-value pair |
| 186 | * in which they participate. |
| 187 | * @return a future whose value will be a multiset of the keys, this may |
| 188 | * be empty |
| 189 | */ |
| 190 | CompletableFuture<Multiset<K>> keys(); |
| 191 | |
| 192 | /** |
| 193 | * Returns a collection of values in the set with duplicates permitted, the |
| 194 | * size of this collection will equal the size of the map at the time of |
| 195 | * creation. |
| 196 | * @return a future whose value will be a collection of values, this may be |
| 197 | * empty |
| 198 | */ |
Aaron Kruglikov | d9d73a0 | 2016-04-27 11:29:23 -0700 | [diff] [blame^] | 199 | CompletableFuture<Multiset<V>> values(); |
Aaron Kruglikov | 9970265 | 2016-04-11 16:24:18 -0700 | [diff] [blame] | 200 | |
| 201 | /** |
| 202 | * Returns a collection of each key-value pair in this map. |
| 203 | * @return a future whose value will be a collection of all entries in the |
| 204 | * map, this may be empty |
| 205 | */ |
| 206 | CompletableFuture<Collection<Map.Entry<K, V>>> entries(); |
| 207 | |
| 208 | /** |
| 209 | * Returns a map of keys to collections of values that reflect the set of |
| 210 | * key-value pairs contained in the multimap, where the key value pairs |
| 211 | * would be the key paired with each of the values in the collection. |
| 212 | * @return a future whose value will be a map of keys to collections of |
| 213 | * values, the returned map may be empty. |
| 214 | */ |
| 215 | CompletableFuture<Map<K, Collection<V>>> asMap(); |
| 216 | } |