blob: f1ef356120cd617b2981c17d6577a0534c50b455 [file] [log] [blame]
Madan Jampani25461112015-02-17 14:17:29 -08001/*
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 */
16
Madan Jampani393e0f02015-02-12 07:35:39 +053017package org.onosproject.store.service;
Madan Jampani09342702015-02-05 23:32:40 -080018
19import java.util.Collection;
Madan Jampani09342702015-02-05 23:32:40 -080020import java.util.Map.Entry;
Madan Jampanif1b8e172015-03-23 11:42:02 -070021import java.util.Set;
Madan Jampani346d4f52015-05-04 11:09:39 -070022import java.util.function.BiFunction;
23import java.util.function.Function;
24import java.util.function.Predicate;
Madan Jampani09342702015-02-05 23:32:40 -080025
26/**
Madan Jampani80984052015-07-23 13:11:36 -070027 * A distributed, strongly consistent key-value map.
Madan Jampani09342702015-02-05 23:32:40 -080028 * <p>
29 * This map offers strong read-after-update (where update == create/update/delete)
30 * consistency. All operations to the map are serialized and applied in a consistent
31 * manner.
32 * <p>
33 * The stronger consistency comes at the expense of availability in
34 * the event of a network partition. A network partition can be either due to
35 * a temporary disruption in network connectivity between participating nodes
36 * or due to a node being temporarily down.
37 * </p><p>
38 * All values stored in this map are versioned and the API supports optimistic
39 * concurrency by allowing conditional updates that take into consideration
40 * the version or value that was previously read.
41 * </p><p>
Madan Jampani09342702015-02-05 23:32:40 -080042 * This map does not allow null values. All methods can throw a ConsistentMapException
43 * (which extends RuntimeException) to indicate failures.
44 *
45 */
46public interface ConsistentMap<K, V> {
47
48 /**
49 * Returns the number of entries in the map.
50 *
51 * @return map size.
52 */
53 int size();
54
55 /**
56 * Returns true if the map is empty.
57 *
Madan Jampani80984052015-07-23 13:11:36 -070058 * @return true if map has no entries, false otherwise
Madan Jampani09342702015-02-05 23:32:40 -080059 */
60 boolean isEmpty();
61
62 /**
63 * Returns true if this map contains a mapping for the specified key.
64 *
65 * @param key key
Madan Jampani80984052015-07-23 13:11:36 -070066 * @return true if map contains key, false otherwise
Madan Jampani09342702015-02-05 23:32:40 -080067 */
68 boolean containsKey(K key);
69
70 /**
71 * Returns true if this map contains the specified value.
72 *
73 * @param value value
74 * @return true if map contains value, false otherwise.
75 */
76 boolean containsValue(V value);
77
78 /**
79 * Returns the value (and version) to which the specified key is mapped, or null if this
80 * map contains no mapping for the key.
81 *
82 * @param key the key whose associated value (and version) is to be returned
83 * @return the value (and version) to which the specified key is mapped, or null if
84 * this map contains no mapping for the key
85 */
86 Versioned<V> get(K key);
87
88 /**
Madan Jampani346d4f52015-05-04 11:09:39 -070089 * If the specified key is not already associated with a value (or is mapped to null),
90 * attempts to compute its value using the given mapping function and enters it into
91 * this map unless null.
92 *
93 * @param key key with which the specified value is to be associated
94 * @param mappingFunction the function to compute a value
95 * @return the current (existing or computed) value associated with the specified key,
96 * or null if the computed value is null. Method throws {@code ConsistentMapException.ConcurrentModification}
97 * if a concurrent modification of map is detected
98 */
99 Versioned<V> computeIfAbsent(K key,
100 Function<? super K, ? extends V> mappingFunction);
101
102 /**
103 * Attempts to compute a mapping for the specified key and its current mapped value (or
104 * null if there is no current mapping).
105 * If the computed value is null, the current mapping will be removed from the map.
106 *
107 * @param key key with which the specified value is to be associated
108 * @param remappingFunction the function to compute a value
109 * @return the new value associated with the specified key, or null if none.
110 * This method throws {@code ConsistentMapException.ConcurrentModification}
111 * if a concurrent modification of map is detected
112 */
113 Versioned<V> compute(K key,
114 BiFunction<? super K, ? super V, ? extends V> remappingFunction);
115
116 /**
117 * If the value for the specified key is present and non-null, attempts to compute a new
118 * mapping given the key and its current mapped value.
119 * If the computed value is null, the current mapping will be removed from the map.
120 *
121 * @param key key with which the specified value is to be associated
122 * @param remappingFunction the function to compute a value
123 * @return the new value associated with the specified key, or null if none.
124 * This method throws {@code ConsistentMapException.ConcurrentModification}
125 * if a concurrent modification of map is detected
126 */
127 Versioned<V> computeIfPresent(K key,
128 BiFunction<? super K, ? super V, ? extends V> remappingFunction);
129
130 /**
131 * If the value for the specified key satisfies a condition, attempts to compute a new
132 * mapping given the key and its current mapped value.
133 * If the computed value is null, the current mapping will be removed from the map.
134 *
135 * @param key key with which the specified value is to be associated
136 * @param condition condition that should evaluate to true for the computation to proceed
137 * @param remappingFunction the function to compute a value
138 * @return the new value associated with the specified key, or the old value if condition evaluates to false.
139 * This method throws {@code ConsistentMapException.ConcurrentModification} if a concurrent
140 * modification of map is detected
141 */
142 Versioned<V> computeIf(K key,
143 Predicate<? super V> condition,
144 BiFunction<? super K, ? super V, ? extends V> remappingFunction);
145
146 /**
Madan Jampani09342702015-02-05 23:32:40 -0800147 * Associates the specified value with the specified key in this map (optional operation).
148 * If the map previously contained a mapping for the key, the old value is replaced by the
149 * specified value.
150 *
151 * @param key key with which the specified value is to be associated
152 * @param value value to be associated with the specified key
153 * @return the previous value (and version) associated with key, or null if there was
154 * no mapping for key.
155 */
156 Versioned<V> put(K key, V value);
157
158 /**
Madan Jampani346d4f52015-05-04 11:09:39 -0700159 * Associates the specified value with the specified key in this map (optional operation).
160 * If the map previously contained a mapping for the key, the old value is replaced by the
161 * specified value.
162 *
163 * @param key key with which the specified value is to be associated
164 * @param value value to be associated with the specified key
165 * @return new value.
166 */
167 Versioned<V> putAndGet(K key, V value);
168
169 /**
Madan Jampani09342702015-02-05 23:32:40 -0800170 * Removes the mapping for a key from this map if it is present (optional operation).
171 *
172 * @param key key whose value is to be removed from the map
173 * @return the value (and version) to which this map previously associated the key,
174 * or null if the map contained no mapping for the key.
175 */
176 Versioned<V> remove(K key);
177
178 /**
179 * Removes all of the mappings from this map (optional operation).
180 * The map will be empty after this call returns.
181 */
182 void clear();
183
184 /**
185 * Returns a Set view of the keys contained in this map.
186 * This method differs from the behavior of java.util.Map.keySet() in that
187 * what is returned is a unmodifiable snapshot view of the keys in the ConsistentMap.
188 * Attempts to modify the returned set, whether direct or via its iterator,
189 * result in an UnsupportedOperationException.
190 *
191 * @return a set of the keys contained in this map
192 */
193 Set<K> keySet();
194
195 /**
196 * Returns the collection of values (and associated versions) contained in this map.
197 * This method differs from the behavior of java.util.Map.values() in that
198 * what is returned is a unmodifiable snapshot view of the values in the ConsistentMap.
199 * Attempts to modify the returned collection, whether direct or via its iterator,
200 * result in an UnsupportedOperationException.
201 *
202 * @return a collection of the values (and associated versions) contained in this map
203 */
204 Collection<Versioned<V>> values();
205
206 /**
207 * Returns the set of entries contained in this map.
208 * This method differs from the behavior of java.util.Map.entrySet() in that
209 * what is returned is a unmodifiable snapshot view of the entries in the ConsistentMap.
210 * Attempts to modify the returned set, whether direct or via its iterator,
211 * result in an UnsupportedOperationException.
212 *
213 * @return set of entries contained in this map.
214 */
215 Set<Entry<K, Versioned<V>>> entrySet();
216
217 /**
218 * If the specified key is not already associated with a value
219 * associates it with the given value and returns null, else returns the current value.
220 *
221 * @param key key with which the specified value is to be associated
222 * @param value value to be associated with the specified key
223 * @return the previous value associated with the specified key or null
224 * if key does not already mapped to a value.
225 */
226 Versioned<V> putIfAbsent(K key, V value);
227
228 /**
229 * Removes the entry for the specified key only if it is currently
230 * mapped to the specified value.
231 *
232 * @param key key with which the specified value is associated
233 * @param value value expected to be associated with the specified key
234 * @return true if the value was removed
235 */
236 boolean remove(K key, V value);
237
238 /**
239 * Removes the entry for the specified key only if its current
240 * version in the map is equal to the specified version.
241 *
242 * @param key key with which the specified version is associated
243 * @param version version expected to be associated with the specified key
244 * @return true if the value was removed
245 */
246 boolean remove(K key, long version);
247
248 /**
249 * Replaces the entry for the specified key only if currently mapped
250 * to the specified value.
251 *
252 * @param key key with which the specified value is associated
253 * @param oldValue value expected to be associated with the specified key
254 * @param newValue value to be associated with the specified key
255 * @return true if the value was replaced
256 */
257 boolean replace(K key, V oldValue, V newValue);
258
259 /**
260 * Replaces the entry for the specified key only if it is currently mapped to the
261 * specified version.
262 *
263 * @param key key key with which the specified value is associated
264 * @param oldVersion version expected to be associated with the specified key
265 * @param newValue value to be associated with the specified key
266 * @return true if the value was replaced
267 */
268 boolean replace(K key, long oldVersion, V newValue);
Madan Jampani346d4f52015-05-04 11:09:39 -0700269
270 /**
Madan Jampani50589ac2015-06-08 11:38:46 -0700271 * Registers the specified listener to be notified whenever the map is updated.
272 *
273 * @param listener listener to notify about map events
274 */
275 void addListener(MapEventListener<K, V> listener);
276
277 /**
278 * Unregisters the specified listener such that it will no longer
279 * receive map change notifications.
280 *
281 * @param listener listener to unregister
282 */
283 void removeListener(MapEventListener<K, V> listener);
Madan Jampani09342702015-02-05 23:32:40 -0800284}