Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
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 | */ |
| 42 | public interface EventuallyConsistentMap<K, V> { |
| 43 | |
| 44 | /** |
| 45 | * Returns the number of key-value mappings in this map. |
| 46 | * |
| 47 | * @return number of key-value mappings |
| 48 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 49 | int size(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Returns true if this map is empty. |
| 53 | * |
| 54 | * @return true if this map is empty, otherwise false |
| 55 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 56 | boolean isEmpty(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Returns true if the map contains a mapping for the specified key. |
| 60 | * |
| 61 | * @param key the key to check if this map contains |
| 62 | * @return true if this map has a mapping for the key, otherwise false |
| 63 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 64 | boolean containsKey(K key); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Returns true if the map contains a mapping from any key to the specified |
| 68 | * value. |
| 69 | * |
| 70 | * @param value the value to check if this map has a mapping for |
| 71 | * @return true if this map has a mapping to this value, otherwise false |
| 72 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 73 | boolean containsValue(V value); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Returns the value mapped to the specified key. |
| 77 | * |
| 78 | * @param key the key to look up in this map |
| 79 | * @return the value mapped to the key, or null if no mapping is found |
| 80 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 81 | V get(K key); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Associates the specified value to the specified key in this map. |
| 85 | * <p> |
| 86 | * Note: this differs from the specification of {@link java.util.Map} |
| 87 | * because it does not return the previous value associated with the key. |
| 88 | * Clients are expected to register an |
Jonathan Hart | 77bdd26 | 2015-02-03 09:07:48 -0800 | [diff] [blame] | 89 | * {@link EventuallyConsistentMapListener} if |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 90 | * they are interested in receiving notification of updates to the map. |
Jonathan Hart | 4f397e8 | 2015-02-04 09:10:41 -0800 | [diff] [blame] | 91 | * </p><p> |
| 92 | * Null values are not allowed in the map. |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 93 | * </p> |
| 94 | * |
| 95 | * @param key the key to add a mapping for in this map |
| 96 | * @param value the value to associate with the key in this map |
| 97 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 98 | void put(K key, V value); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Removes the mapping associated with the specified key from the map. |
| 102 | * <p> |
Madan Jampani | 43e9c9c | 2015-06-26 14:16:46 -0700 | [diff] [blame] | 103 | * Clients are expected to register an {@link EventuallyConsistentMapListener} if |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 104 | * they are interested in receiving notification of updates to the map. |
| 105 | * </p> |
| 106 | * |
| 107 | * @param key the key to remove the mapping for |
Madan Jampani | 43e9c9c | 2015-06-26 14:16:46 -0700 | [diff] [blame] | 108 | * @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] | 109 | */ |
Madan Jampani | 43e9c9c | 2015-06-26 14:16:46 -0700 | [diff] [blame] | 110 | V remove(K key); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 111 | |
| 112 | /** |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 113 | * Removes the given key-value mapping from the map, if it exists. |
| 114 | * <p> |
| 115 | * This actually means remove any values up to and including the timestamp |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 116 | * given by the map's timestampProvider. |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 117 | * Any mappings that produce an earlier timestamp than this given key-value |
| 118 | * pair will be removed, and any mappings that produce a later timestamp |
| 119 | * will supersede this remove. |
| 120 | * </p><p> |
| 121 | * Note: this differs from the specification of {@link java.util.Map} |
| 122 | * because it does not return a boolean indication whether a value was removed. |
| 123 | * Clients are expected to register an |
Jonathan Hart | 77bdd26 | 2015-02-03 09:07:48 -0800 | [diff] [blame] | 124 | * {@link EventuallyConsistentMapListener} if |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 125 | * they are interested in receiving notification of updates to the map. |
| 126 | * </p> |
| 127 | * |
| 128 | * @param key the key to remove the mapping for |
| 129 | * @param value the value mapped to the key |
| 130 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 131 | void remove(K key, V value); |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 132 | |
| 133 | /** |
Madan Jampani | 4727a11 | 2015-07-16 12:12:58 -0700 | [diff] [blame] | 134 | * Attempts to compute a mapping for the specified key and its current mapped |
| 135 | * value (or null if there is no current mapping). |
| 136 | * <p> |
| 137 | * If the function returns null, the mapping is removed (or remains absent if initially absent). |
| 138 | * @param key map key |
| 139 | * @param recomputeFunction function to recompute a new value |
| 140 | * @return new value |
| 141 | */ |
| 142 | V compute(K key, BiFunction<K, V, V> recomputeFunction); |
| 143 | |
| 144 | /** |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 145 | * Adds mappings for all key-value pairs in the specified map to this map. |
| 146 | * <p> |
| 147 | * This will be more efficient in communication than calling individual put |
| 148 | * operations. |
| 149 | * </p> |
| 150 | * |
| 151 | * @param m a map of values to add to this map |
| 152 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 153 | void putAll(Map<? extends K, ? extends V> m); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * Removes all mappings from this map. |
| 157 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 158 | void clear(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 159 | |
| 160 | /** |
| 161 | * Returns a set of the keys in this map. Changes to the set are not |
| 162 | * reflected back to the map. |
| 163 | * |
| 164 | * @return set of keys in the map |
| 165 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 166 | Set<K> keySet(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 167 | |
| 168 | /** |
| 169 | * Returns a collections of values in this map. Changes to the collection |
| 170 | * are not reflected back to the map. |
| 171 | * |
| 172 | * @return collection of values in the map |
| 173 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 174 | Collection<V> values(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 175 | |
| 176 | /** |
| 177 | * Returns a set of mappings contained in this map. Changes to the set are |
| 178 | * not reflected back to the map. |
| 179 | * |
| 180 | * @return set of key-value mappings in this map |
| 181 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 182 | Set<Map.Entry<K, V>> entrySet(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 183 | |
| 184 | /** |
| 185 | * Adds the specified listener to the map which will be notified whenever |
| 186 | * the mappings in the map are changed. |
| 187 | * |
| 188 | * @param listener listener to register for events |
| 189 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 190 | void addListener(EventuallyConsistentMapListener<K, V> listener); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 191 | |
| 192 | /** |
| 193 | * Removes the specified listener from the map such that it will no longer |
| 194 | * receive change notifications. |
| 195 | * |
| 196 | * @param listener listener to deregister for events |
| 197 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 198 | void removeListener(EventuallyConsistentMapListener<K, V> listener); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 199 | |
| 200 | /** |
| 201 | * Shuts down the map and breaks communication between different instances. |
| 202 | * This allows the map objects to be cleaned up and garbage collected. |
| 203 | * Calls to any methods on the map subsequent to calling destroy() will |
| 204 | * throw a {@link java.lang.RuntimeException}. |
| 205 | */ |
Sho SHIMIZU | 3310a34 | 2015-05-13 12:14:05 -0700 | [diff] [blame] | 206 | void destroy(); |
Jonathan Hart | db3af89 | 2015-01-26 13:19:07 -0800 | [diff] [blame] | 207 | } |