Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 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 | */ |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 16 | package org.onosproject.store.service; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 17 | |
| 18 | import java.util.Collection; |
| 19 | import java.util.Map; |
| 20 | import java.util.Set; |
Madan Jampani | 4727a11 | 2015-07-16 12:12:58 -0700 | [diff] [blame] | 21 | import java.util.function.BiFunction; |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * A distributed, eventually consistent map. |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 25 | * <p> |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 26 | * This map does not offer read after writes consistency. Operations are |
| 27 | * serialized via the timestamps issued by the clock service. If two updates |
| 28 | * are in conflict, the update with the more recent timestamp will endure. |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 29 | * </p><p> |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 30 | * The interface is mostly similar to {@link java.util.Map} with some minor |
| 31 | * semantic changes and the addition of a listener framework (because the map |
| 32 | * can be mutated by clients on other instances, not only through the local Java |
| 33 | * API). |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 34 | * </p><p> |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 35 | * Clients are expected to register an |
Jonathan Hart | 77bdd26 | 2015-02-03 09:07:48 -0800 | [diff] [blame] | 36 | * {@link EventuallyConsistentMapListener} if they |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 37 | * are interested in receiving notifications of update to the map. |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 38 | * </p><p> |
| 39 | * Null values are not allowed in this map. |
| 40 | * </p> |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 41 | */ |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 42 | public interface EventuallyConsistentMap<K, V> extends DistributedPrimitive { |
| 43 | |
| 44 | @Override |
Madan Jampani | 39fff10 | 2016-02-14 13:17:28 -0800 | [diff] [blame] | 45 | default DistributedPrimitive.Type primitiveType() { |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 46 | return DistributedPrimitive.Type.EVENTUALLY_CONSISTENT_MAP; |
| 47 | } |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Returns the number of key-value mappings in this map. |
| 51 | * |
| 52 | * @return number of key-value mappings |
| 53 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 54 | int size(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Returns true if this map is empty. |
| 58 | * |
| 59 | * @return true if this map is empty, otherwise false |
| 60 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 61 | boolean isEmpty(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Returns true if the map contains a mapping for the specified key. |
| 65 | * |
| 66 | * @param key the key to check if this map contains |
| 67 | * @return true if this map has a mapping for the key, otherwise false |
| 68 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 69 | boolean containsKey(K key); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Returns true if the map contains a mapping from any key to the specified |
| 73 | * value. |
| 74 | * |
| 75 | * @param value the value to check if this map has a mapping for |
| 76 | * @return true if this map has a mapping to this value, otherwise false |
| 77 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 78 | boolean containsValue(V value); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * Returns the value mapped to the specified key. |
| 82 | * |
| 83 | * @param key the key to look up in this map |
| 84 | * @return the value mapped to the key, or null if no mapping is found |
| 85 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 86 | V get(K key); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * Associates the specified value to the specified key in this map. |
| 90 | * <p> |
| 91 | * Note: this differs from the specification of {@link java.util.Map} |
| 92 | * because it does not return the previous value associated with the key. |
| 93 | * Clients are expected to register an |
Jonathan Hart | 77bdd26 | 2015-02-03 09:07:48 -0800 | [diff] [blame] | 94 | * {@link EventuallyConsistentMapListener} if |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 95 | * they are interested in receiving notification of updates to the map. |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 96 | * </p><p> |
| 97 | * Null values are not allowed in the map. |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 98 | * </p> |
| 99 | * |
| 100 | * @param key the key to add a mapping for in this map |
| 101 | * @param value the value to associate with the key in this map |
| 102 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 103 | void put(K key, V value); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * Removes the mapping associated with the specified key from the map. |
| 107 | * <p> |
Madan Jampani | 43e9c9c | 2015-06-26 14:16:46 -0700 | [diff] [blame] | 108 | * Clients are expected to register an {@link EventuallyConsistentMapListener} if |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 109 | * they are interested in receiving notification of updates to the map. |
| 110 | * </p> |
| 111 | * |
| 112 | * @param key the key to remove the mapping for |
Madan Jampani | 43e9c9c | 2015-06-26 14:16:46 -0700 | [diff] [blame] | 113 | * @return previous value associated with key, or null if there was no mapping for key. |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 114 | */ |
Madan Jampani | 43e9c9c | 2015-06-26 14:16:46 -0700 | [diff] [blame] | 115 | V remove(K key); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 116 | |
| 117 | /** |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 118 | * Removes the given key-value mapping from the map, if it exists. |
| 119 | * <p> |
| 120 | * This actually means remove any values up to and including the timestamp |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 121 | * given by the map's timestampProvider. |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 122 | * Any mappings that produce an earlier timestamp than this given key-value |
| 123 | * pair will be removed, and any mappings that produce a later timestamp |
| 124 | * will supersede this remove. |
| 125 | * </p><p> |
| 126 | * Note: this differs from the specification of {@link java.util.Map} |
| 127 | * because it does not return a boolean indication whether a value was removed. |
| 128 | * Clients are expected to register an |
Jonathan Hart | 77bdd26 | 2015-02-03 09:07:48 -0800 | [diff] [blame] | 129 | * {@link EventuallyConsistentMapListener} if |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 130 | * they are interested in receiving notification of updates to the map. |
| 131 | * </p> |
| 132 | * |
| 133 | * @param key the key to remove the mapping for |
| 134 | * @param value the value mapped to the key |
| 135 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 136 | void remove(K key, V value); |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 137 | |
| 138 | /** |
Madan Jampani | 4727a11 | 2015-07-16 12:12:58 -0700 | [diff] [blame] | 139 | * Attempts to compute a mapping for the specified key and its current mapped |
| 140 | * value (or null if there is no current mapping). |
| 141 | * <p> |
| 142 | * If the function returns null, the mapping is removed (or remains absent if initially absent). |
| 143 | * @param key map key |
| 144 | * @param recomputeFunction function to recompute a new value |
| 145 | * @return new value |
| 146 | */ |
| 147 | V compute(K key, BiFunction<K, V, V> recomputeFunction); |
| 148 | |
| 149 | /** |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 150 | * Adds mappings for all key-value pairs in the specified map to this map. |
| 151 | * <p> |
| 152 | * This will be more efficient in communication than calling individual put |
| 153 | * operations. |
| 154 | * </p> |
| 155 | * |
| 156 | * @param m a map of values to add to this map |
| 157 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 158 | void putAll(Map<? extends K, ? extends V> m); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 159 | |
| 160 | /** |
| 161 | * Removes all mappings from this map. |
| 162 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 163 | void clear(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * Returns a set of the keys in this map. Changes to the set are not |
| 167 | * reflected back to the map. |
| 168 | * |
| 169 | * @return set of keys in the map |
| 170 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 171 | Set<K> keySet(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * Returns a collections of values in this map. Changes to the collection |
| 175 | * are not reflected back to the map. |
| 176 | * |
| 177 | * @return collection of values in the map |
| 178 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 179 | Collection<V> values(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 180 | |
| 181 | /** |
| 182 | * Returns a set of mappings contained in this map. Changes to the set are |
| 183 | * not reflected back to the map. |
| 184 | * |
| 185 | * @return set of key-value mappings in this map |
| 186 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 187 | Set<Map.Entry<K, V>> entrySet(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 188 | |
| 189 | /** |
| 190 | * Adds the specified listener to the map which will be notified whenever |
| 191 | * the mappings in the map are changed. |
| 192 | * |
| 193 | * @param listener listener to register for events |
| 194 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 195 | void addListener(EventuallyConsistentMapListener<K, V> listener); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 196 | |
| 197 | /** |
| 198 | * Removes the specified listener from the map such that it will no longer |
| 199 | * receive change notifications. |
| 200 | * |
| 201 | * @param listener listener to deregister for events |
| 202 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 203 | void removeListener(EventuallyConsistentMapListener<K, V> listener); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 204 | } |