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