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