blob: 4d7f33bb3505c639660ed6f0d4c6d765a51740db [file] [log] [blame]
Madan Jampani09342702015-02-05 23:32:40 -08001package org.onosproject.store.consistent.impl;
2
3import java.util.Collection;
4import java.util.List;
5import java.util.Set;
6import java.util.Map.Entry;
7
8/**
9 * A distributed, strongly consistent map.
10 * <p>
11 * This map offers strong read-after-update (where update == create/update/delete)
12 * consistency. All operations to the map are serialized and applied in a consistent
13 * manner.
14 * <p>
15 * The stronger consistency comes at the expense of availability in
16 * the event of a network partition. A network partition can be either due to
17 * a temporary disruption in network connectivity between participating nodes
18 * or due to a node being temporarily down.
19 * </p><p>
20 * All values stored in this map are versioned and the API supports optimistic
21 * concurrency by allowing conditional updates that take into consideration
22 * the version or value that was previously read.
23 * </p><p>
24 * The map also supports atomic batch updates (transactions). One can provide a list
25 * of updates to be applied atomically if and only if all the operations are guaranteed
26 * to succeed i.e. all their preconditions are met. For example, the precondition
27 * for a putIfAbsent API call is absence of a mapping for the key. Similarly, the
28 * precondition for a conditional replace operation is the presence of an expected
29 * version or value
30 * </p><p>
31 * This map does not allow null values. All methods can throw a ConsistentMapException
32 * (which extends RuntimeException) to indicate failures.
33 *
34 */
35public interface ConsistentMap<K, V> {
36
37 /**
38 * Returns the number of entries in the map.
39 *
40 * @return map size.
41 */
42 int size();
43
44 /**
45 * Returns true if the map is empty.
46 *
47 * @return true if map has no entries, false otherwise.
48 */
49 boolean isEmpty();
50
51 /**
52 * Returns true if this map contains a mapping for the specified key.
53 *
54 * @param key key
55 * @return true if map contains key, false otherwise.
56 */
57 boolean containsKey(K key);
58
59 /**
60 * Returns true if this map contains the specified value.
61 *
62 * @param value value
63 * @return true if map contains value, false otherwise.
64 */
65 boolean containsValue(V value);
66
67 /**
68 * Returns the value (and version) to which the specified key is mapped, or null if this
69 * map contains no mapping for the key.
70 *
71 * @param key the key whose associated value (and version) is to be returned
72 * @return the value (and version) to which the specified key is mapped, or null if
73 * this map contains no mapping for the key
74 */
75 Versioned<V> get(K key);
76
77 /**
78 * Associates the specified value with the specified key in this map (optional operation).
79 * If the map previously contained a mapping for the key, the old value is replaced by the
80 * specified value.
81 *
82 * @param key key with which the specified value is to be associated
83 * @param value value to be associated with the specified key
84 * @return the previous value (and version) associated with key, or null if there was
85 * no mapping for key.
86 */
87 Versioned<V> put(K key, V value);
88
89 /**
90 * Removes the mapping for a key from this map if it is present (optional operation).
91 *
92 * @param key key whose value is to be removed from the map
93 * @return the value (and version) to which this map previously associated the key,
94 * or null if the map contained no mapping for the key.
95 */
96 Versioned<V> remove(K key);
97
98 /**
99 * Removes all of the mappings from this map (optional operation).
100 * The map will be empty after this call returns.
101 */
102 void clear();
103
104 /**
105 * Returns a Set view of the keys contained in this map.
106 * This method differs from the behavior of java.util.Map.keySet() in that
107 * what is returned is a unmodifiable snapshot view of the keys in the ConsistentMap.
108 * Attempts to modify the returned set, whether direct or via its iterator,
109 * result in an UnsupportedOperationException.
110 *
111 * @return a set of the keys contained in this map
112 */
113 Set<K> keySet();
114
115 /**
116 * Returns the collection of values (and associated versions) contained in this map.
117 * This method differs from the behavior of java.util.Map.values() in that
118 * what is returned is a unmodifiable snapshot view of the values in the ConsistentMap.
119 * Attempts to modify the returned collection, whether direct or via its iterator,
120 * result in an UnsupportedOperationException.
121 *
122 * @return a collection of the values (and associated versions) contained in this map
123 */
124 Collection<Versioned<V>> values();
125
126 /**
127 * Returns the set of entries contained in this map.
128 * This method differs from the behavior of java.util.Map.entrySet() in that
129 * what is returned is a unmodifiable snapshot view of the entries in the ConsistentMap.
130 * Attempts to modify the returned set, whether direct or via its iterator,
131 * result in an UnsupportedOperationException.
132 *
133 * @return set of entries contained in this map.
134 */
135 Set<Entry<K, Versioned<V>>> entrySet();
136
137 /**
138 * If the specified key is not already associated with a value
139 * associates it with the given value and returns null, else returns the current value.
140 *
141 * @param key key with which the specified value is to be associated
142 * @param value value to be associated with the specified key
143 * @return the previous value associated with the specified key or null
144 * if key does not already mapped to a value.
145 */
146 Versioned<V> putIfAbsent(K key, V value);
147
148 /**
149 * Removes the entry for the specified key only if it is currently
150 * mapped to the specified value.
151 *
152 * @param key key with which the specified value is associated
153 * @param value value expected to be associated with the specified key
154 * @return true if the value was removed
155 */
156 boolean remove(K key, V value);
157
158 /**
159 * Removes the entry for the specified key only if its current
160 * version in the map is equal to the specified version.
161 *
162 * @param key key with which the specified version is associated
163 * @param version version expected to be associated with the specified key
164 * @return true if the value was removed
165 */
166 boolean remove(K key, long version);
167
168 /**
169 * Replaces the entry for the specified key only if currently mapped
170 * to the specified value.
171 *
172 * @param key key with which the specified value is associated
173 * @param oldValue value expected to be associated with the specified key
174 * @param newValue value to be associated with the specified key
175 * @return true if the value was replaced
176 */
177 boolean replace(K key, V oldValue, V newValue);
178
179 /**
180 * Replaces the entry for the specified key only if it is currently mapped to the
181 * specified version.
182 *
183 * @param key key key with which the specified value is associated
184 * @param oldVersion version expected to be associated with the specified key
185 * @param newValue value to be associated with the specified key
186 * @return true if the value was replaced
187 */
188 boolean replace(K key, long oldVersion, V newValue);
189
190 /**
191 * Atomically apply the specified list of updates to the map.
192 * If any of the updates cannot be applied due to a precondition
193 * violation, none of the updates will be applied and the state of
194 * the map remains unaltered.
195 *
196 * @param updates list of updates to apply atomically.
197 * @return true if the map was updated.
198 */
199 boolean batchUpdate(List<UpdateOperation<K, V>> updates);
200}