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