blob: 28935098551fdc54d5b278f33db85075ae547398 [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 Jampani346d4f52015-05-04 11:09:39 -070021import java.util.Optional;
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/**
28 * A distributed, strongly consistent map.
29 * <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 *
59 * @return true if map has no entries, false otherwise.
60 */
61 boolean isEmpty();
62
63 /**
64 * Returns true if this map contains a mapping for the specified key.
65 *
66 * @param key key
67 * @return true if map contains key, false otherwise.
68 */
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 /**
171 * Associates the specified value with the specified key in this map (optional operation).
172 * If the map previously contained a mapping for the key, the old value is replaced by the
173 * specified value.
174 *
175 * @param key key with which the specified value is to be associated
176 * @param value value to be associated with the specified key
177 * @return optional updated value. Will be empty if update did not happen
178 */
179 Optional<Versioned<V>> putIfAbsentAndGet(K key, V value);
180
181 /**
Madan Jampani09342702015-02-05 23:32:40 -0800182 * Removes the mapping for a key from this map if it is present (optional operation).
183 *
184 * @param key key whose value is to be removed from the map
185 * @return the value (and version) to which this map previously associated the key,
186 * or null if the map contained no mapping for the key.
187 */
188 Versioned<V> remove(K key);
189
190 /**
191 * Removes all of the mappings from this map (optional operation).
192 * The map will be empty after this call returns.
193 */
194 void clear();
195
196 /**
197 * Returns a Set view of the keys contained in this map.
198 * This method differs from the behavior of java.util.Map.keySet() in that
199 * what is returned is a unmodifiable snapshot view of the keys in the ConsistentMap.
200 * Attempts to modify the returned set, whether direct or via its iterator,
201 * result in an UnsupportedOperationException.
202 *
203 * @return a set of the keys contained in this map
204 */
205 Set<K> keySet();
206
207 /**
208 * Returns the collection of values (and associated versions) contained in this map.
209 * This method differs from the behavior of java.util.Map.values() in that
210 * what is returned is a unmodifiable snapshot view of the values in the ConsistentMap.
211 * Attempts to modify the returned collection, whether direct or via its iterator,
212 * result in an UnsupportedOperationException.
213 *
214 * @return a collection of the values (and associated versions) contained in this map
215 */
216 Collection<Versioned<V>> values();
217
218 /**
219 * Returns the set of entries contained in this map.
220 * This method differs from the behavior of java.util.Map.entrySet() in that
221 * what is returned is a unmodifiable snapshot view of the entries in the ConsistentMap.
222 * Attempts to modify the returned set, whether direct or via its iterator,
223 * result in an UnsupportedOperationException.
224 *
225 * @return set of entries contained in this map.
226 */
227 Set<Entry<K, Versioned<V>>> entrySet();
228
229 /**
230 * If the specified key is not already associated with a value
231 * associates it with the given value and returns null, else returns the current value.
232 *
233 * @param key key with which the specified value is to be associated
234 * @param value value to be associated with the specified key
235 * @return the previous value associated with the specified key or null
236 * if key does not already mapped to a value.
237 */
238 Versioned<V> putIfAbsent(K key, V value);
239
240 /**
241 * Removes the entry for the specified key only if it is currently
242 * mapped to the specified value.
243 *
244 * @param key key with which the specified value is associated
245 * @param value value expected to be associated with the specified key
246 * @return true if the value was removed
247 */
248 boolean remove(K key, V value);
249
250 /**
251 * Removes the entry for the specified key only if its current
252 * version in the map is equal to the specified version.
253 *
254 * @param key key with which the specified version is associated
255 * @param version version expected to be associated with the specified key
256 * @return true if the value was removed
257 */
258 boolean remove(K key, long version);
259
260 /**
261 * Replaces the entry for the specified key only if currently mapped
262 * to the specified value.
263 *
264 * @param key key with which the specified value is associated
265 * @param oldValue value expected to be associated with the specified key
266 * @param newValue value to be associated with the specified key
267 * @return true if the value was replaced
268 */
269 boolean replace(K key, V oldValue, V newValue);
270
271 /**
272 * Replaces the entry for the specified key only if it is currently mapped to the
273 * specified version.
274 *
275 * @param key key key with which the specified value is associated
276 * @param oldVersion version expected to be associated with the specified key
277 * @param newValue value to be associated with the specified key
278 * @return true if the value was replaced
279 */
280 boolean replace(K key, long oldVersion, V newValue);
Madan Jampani346d4f52015-05-04 11:09:39 -0700281
282 /**
283 * Replaces the entry for the specified key only if it is currently mapped to the
284 * specified version.
285 *
286 * @param key key key with which the specified value is associated
287 * @param oldVersion version expected to be associated with the specified key
288 * @param newValue value to be associated with the specified key
289 * @return optional new value. Will be empty if replace did not happen
290 */
291 Optional<Versioned<V>> replaceAndGet(K key, long oldVersion, V newValue);
Madan Jampani09342702015-02-05 23:32:40 -0800292}