blob: 06395b8efd9b8ed899ba0f8572ccfc8d14d08b87 [file] [log] [blame]
Jonathan Hartdb3af892015-01-26 13:19:07 -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 */
Jonathan Hart6ec029a2015-03-24 17:12:35 -070016package org.onosproject.store.service;
Jonathan Hartdb3af892015-01-26 13:19:07 -080017
18import java.util.Collection;
19import java.util.Map;
20import java.util.Set;
Madan Jampani4727a112015-07-16 12:12:58 -070021import java.util.function.BiFunction;
Jonathan Hartdb3af892015-01-26 13:19:07 -080022
23/**
24 * A distributed, eventually consistent map.
Jonathan Hart4f397e82015-02-04 09:10:41 -080025 * <p>
Jonathan Hartdb3af892015-01-26 13:19:07 -080026 * This map does not offer read after writes consistency. Operations are
27 * serialized via the timestamps issued by the clock service. If two updates
28 * are in conflict, the update with the more recent timestamp will endure.
Jonathan Hart4f397e82015-02-04 09:10:41 -080029 * </p><p>
Jonathan Hartdb3af892015-01-26 13:19:07 -080030 * The interface is mostly similar to {@link java.util.Map} with some minor
31 * semantic changes and the addition of a listener framework (because the map
32 * can be mutated by clients on other instances, not only through the local Java
33 * API).
Jonathan Hart4f397e82015-02-04 09:10:41 -080034 * </p><p>
Jonathan Hartdb3af892015-01-26 13:19:07 -080035 * Clients are expected to register an
Jonathan Hart77bdd262015-02-03 09:07:48 -080036 * {@link EventuallyConsistentMapListener} if they
Jonathan Hartdb3af892015-01-26 13:19:07 -080037 * are interested in receiving notifications of update to the map.
Jonathan Hart4f397e82015-02-04 09:10:41 -080038 * </p><p>
39 * Null values are not allowed in this map.
40 * </p>
Jonathan Hartdb3af892015-01-26 13:19:07 -080041 */
42public interface EventuallyConsistentMap<K, V> {
43
44 /**
45 * Returns the number of key-value mappings in this map.
46 *
47 * @return number of key-value mappings
48 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070049 int size();
Jonathan Hartdb3af892015-01-26 13:19:07 -080050
51 /**
52 * Returns true if this map is empty.
53 *
54 * @return true if this map is empty, otherwise false
55 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070056 boolean isEmpty();
Jonathan Hartdb3af892015-01-26 13:19:07 -080057
58 /**
59 * Returns true if the map contains a mapping for the specified key.
60 *
61 * @param key the key to check if this map contains
62 * @return true if this map has a mapping for the key, otherwise false
63 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070064 boolean containsKey(K key);
Jonathan Hartdb3af892015-01-26 13:19:07 -080065
66 /**
67 * Returns true if the map contains a mapping from any key to the specified
68 * value.
69 *
70 * @param value the value to check if this map has a mapping for
71 * @return true if this map has a mapping to this value, otherwise false
72 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070073 boolean containsValue(V value);
Jonathan Hartdb3af892015-01-26 13:19:07 -080074
75 /**
76 * Returns the value mapped to the specified key.
77 *
78 * @param key the key to look up in this map
79 * @return the value mapped to the key, or null if no mapping is found
80 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070081 V get(K key);
Jonathan Hartdb3af892015-01-26 13:19:07 -080082
83 /**
84 * Associates the specified value to the specified key in this map.
85 * <p>
86 * Note: this differs from the specification of {@link java.util.Map}
87 * because it does not return the previous value associated with the key.
88 * Clients are expected to register an
Jonathan Hart77bdd262015-02-03 09:07:48 -080089 * {@link EventuallyConsistentMapListener} if
Jonathan Hartdb3af892015-01-26 13:19:07 -080090 * they are interested in receiving notification of updates to the map.
Jonathan Hart4f397e82015-02-04 09:10:41 -080091 * </p><p>
92 * Null values are not allowed in the map.
Jonathan Hartdb3af892015-01-26 13:19:07 -080093 * </p>
94 *
95 * @param key the key to add a mapping for in this map
96 * @param value the value to associate with the key in this map
97 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070098 void put(K key, V value);
Jonathan Hartdb3af892015-01-26 13:19:07 -080099
100 /**
101 * Removes the mapping associated with the specified key from the map.
102 * <p>
Madan Jampani43e9c9c2015-06-26 14:16:46 -0700103 * Clients are expected to register an {@link EventuallyConsistentMapListener} if
Jonathan Hartdb3af892015-01-26 13:19:07 -0800104 * they are interested in receiving notification of updates to the map.
105 * </p>
106 *
107 * @param key the key to remove the mapping for
Madan Jampani43e9c9c2015-06-26 14:16:46 -0700108 * @return previous value associated with key, or null if there was no mapping for key.
Jonathan Hartdb3af892015-01-26 13:19:07 -0800109 */
Madan Jampani43e9c9c2015-06-26 14:16:46 -0700110 V remove(K key);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800111
112 /**
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800113 * Removes the given key-value mapping from the map, if it exists.
114 * <p>
115 * This actually means remove any values up to and including the timestamp
Madan Jampanibcf1a482015-06-24 19:05:56 -0700116 * given by the map's timestampProvider.
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800117 * Any mappings that produce an earlier timestamp than this given key-value
118 * pair will be removed, and any mappings that produce a later timestamp
119 * will supersede this remove.
120 * </p><p>
121 * Note: this differs from the specification of {@link java.util.Map}
122 * because it does not return a boolean indication whether a value was removed.
123 * Clients are expected to register an
Jonathan Hart77bdd262015-02-03 09:07:48 -0800124 * {@link EventuallyConsistentMapListener} if
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800125 * they are interested in receiving notification of updates to the map.
126 * </p>
127 *
128 * @param key the key to remove the mapping for
129 * @param value the value mapped to the key
130 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700131 void remove(K key, V value);
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800132
133 /**
Madan Jampani4727a112015-07-16 12:12:58 -0700134 * Attempts to compute a mapping for the specified key and its current mapped
135 * value (or null if there is no current mapping).
136 * <p>
137 * If the function returns null, the mapping is removed (or remains absent if initially absent).
138 * @param key map key
139 * @param recomputeFunction function to recompute a new value
140 * @return new value
141 */
142 V compute(K key, BiFunction<K, V, V> recomputeFunction);
143
144 /**
Jonathan Hartdb3af892015-01-26 13:19:07 -0800145 * Adds mappings for all key-value pairs in the specified map to this map.
146 * <p>
147 * This will be more efficient in communication than calling individual put
148 * operations.
149 * </p>
150 *
151 * @param m a map of values to add to this map
152 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700153 void putAll(Map<? extends K, ? extends V> m);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800154
155 /**
156 * Removes all mappings from this map.
157 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700158 void clear();
Jonathan Hartdb3af892015-01-26 13:19:07 -0800159
160 /**
161 * Returns a set of the keys in this map. Changes to the set are not
162 * reflected back to the map.
163 *
164 * @return set of keys in the map
165 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700166 Set<K> keySet();
Jonathan Hartdb3af892015-01-26 13:19:07 -0800167
168 /**
169 * Returns a collections of values in this map. Changes to the collection
170 * are not reflected back to the map.
171 *
172 * @return collection of values in the map
173 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700174 Collection<V> values();
Jonathan Hartdb3af892015-01-26 13:19:07 -0800175
176 /**
177 * Returns a set of mappings contained in this map. Changes to the set are
178 * not reflected back to the map.
179 *
180 * @return set of key-value mappings in this map
181 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700182 Set<Map.Entry<K, V>> entrySet();
Jonathan Hartdb3af892015-01-26 13:19:07 -0800183
184 /**
185 * Adds the specified listener to the map which will be notified whenever
186 * the mappings in the map are changed.
187 *
188 * @param listener listener to register for events
189 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700190 void addListener(EventuallyConsistentMapListener<K, V> listener);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800191
192 /**
193 * Removes the specified listener from the map such that it will no longer
194 * receive change notifications.
195 *
196 * @param listener listener to deregister for events
197 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700198 void removeListener(EventuallyConsistentMapListener<K, V> listener);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800199
200 /**
201 * Shuts down the map and breaks communication between different instances.
202 * This allows the map objects to be cleaned up and garbage collected.
203 * Calls to any methods on the map subsequent to calling destroy() will
204 * throw a {@link java.lang.RuntimeException}.
205 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700206 void destroy();
Jonathan Hartdb3af892015-01-26 13:19:07 -0800207}