blob: 2dafde398395ead806a6025c6d8f043b2c19ee39 [file] [log] [blame]
Jonathan Hartdb3af892015-01-26 13:19:07 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jonathan Hartdb3af892015-01-26 13:19:07 -08003 *
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 */
Madan Jampania090a112016-01-18 16:38:17 -080042public interface EventuallyConsistentMap<K, V> extends DistributedPrimitive {
43
44 @Override
Madan Jampani39fff102016-02-14 13:17:28 -080045 default DistributedPrimitive.Type primitiveType() {
Madan Jampania090a112016-01-18 16:38:17 -080046 return DistributedPrimitive.Type.EVENTUALLY_CONSISTENT_MAP;
47 }
Jonathan Hartdb3af892015-01-26 13:19:07 -080048
49 /**
50 * Returns the number of key-value mappings in this map.
51 *
52 * @return number of key-value mappings
53 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070054 int size();
Jonathan Hartdb3af892015-01-26 13:19:07 -080055
56 /**
57 * Returns true if this map is empty.
58 *
59 * @return true if this map is empty, otherwise false
60 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070061 boolean isEmpty();
Jonathan Hartdb3af892015-01-26 13:19:07 -080062
63 /**
64 * Returns true if the map contains a mapping for the specified key.
65 *
66 * @param key the key to check if this map contains
67 * @return true if this map has a mapping for the key, otherwise false
68 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070069 boolean containsKey(K key);
Jonathan Hartdb3af892015-01-26 13:19:07 -080070
71 /**
72 * Returns true if the map contains a mapping from any key to the specified
73 * value.
74 *
75 * @param value the value to check if this map has a mapping for
76 * @return true if this map has a mapping to this value, otherwise false
77 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070078 boolean containsValue(V value);
Jonathan Hartdb3af892015-01-26 13:19:07 -080079
80 /**
81 * Returns the value mapped to the specified key.
82 *
83 * @param key the key to look up in this map
84 * @return the value mapped to the key, or null if no mapping is found
85 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -070086 V get(K key);
Jonathan Hartdb3af892015-01-26 13:19:07 -080087
88 /**
89 * Associates the specified value to the specified key in this map.
90 * <p>
91 * Note: this differs from the specification of {@link java.util.Map}
92 * because it does not return the previous value associated with the key.
93 * Clients are expected to register an
Jonathan Hart77bdd262015-02-03 09:07:48 -080094 * {@link EventuallyConsistentMapListener} if
Jonathan Hartdb3af892015-01-26 13:19:07 -080095 * they are interested in receiving notification of updates to the map.
Jonathan Hart4f397e82015-02-04 09:10:41 -080096 * </p><p>
97 * Null values are not allowed in the map.
Jonathan Hartdb3af892015-01-26 13:19:07 -080098 * </p>
99 *
100 * @param key the key to add a mapping for in this map
101 * @param value the value to associate with the key in this map
102 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700103 void put(K key, V value);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800104
105 /**
106 * Removes the mapping associated with the specified key from the map.
107 * <p>
Madan Jampani43e9c9c2015-06-26 14:16:46 -0700108 * Clients are expected to register an {@link EventuallyConsistentMapListener} if
Jonathan Hartdb3af892015-01-26 13:19:07 -0800109 * they are interested in receiving notification of updates to the map.
110 * </p>
111 *
112 * @param key the key to remove the mapping for
Madan Jampani43e9c9c2015-06-26 14:16:46 -0700113 * @return previous value associated with key, or null if there was no mapping for key.
Jonathan Hartdb3af892015-01-26 13:19:07 -0800114 */
Madan Jampani43e9c9c2015-06-26 14:16:46 -0700115 V remove(K key);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800116
117 /**
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800118 * Removes the given key-value mapping from the map, if it exists.
119 * <p>
120 * This actually means remove any values up to and including the timestamp
Madan Jampanibcf1a482015-06-24 19:05:56 -0700121 * given by the map's timestampProvider.
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800122 * Any mappings that produce an earlier timestamp than this given key-value
123 * pair will be removed, and any mappings that produce a later timestamp
124 * will supersede this remove.
125 * </p><p>
126 * Note: this differs from the specification of {@link java.util.Map}
127 * because it does not return a boolean indication whether a value was removed.
128 * Clients are expected to register an
Jonathan Hart77bdd262015-02-03 09:07:48 -0800129 * {@link EventuallyConsistentMapListener} if
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800130 * they are interested in receiving notification of updates to the map.
131 * </p>
132 *
133 * @param key the key to remove the mapping for
134 * @param value the value mapped to the key
135 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700136 void remove(K key, V value);
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800137
138 /**
Madan Jampani4727a112015-07-16 12:12:58 -0700139 * Attempts to compute a mapping for the specified key and its current mapped
140 * value (or null if there is no current mapping).
141 * <p>
142 * If the function returns null, the mapping is removed (or remains absent if initially absent).
143 * @param key map key
144 * @param recomputeFunction function to recompute a new value
145 * @return new value
146 */
147 V compute(K key, BiFunction<K, V, V> recomputeFunction);
148
149 /**
Jonathan Hartdb3af892015-01-26 13:19:07 -0800150 * Adds mappings for all key-value pairs in the specified map to this map.
151 * <p>
152 * This will be more efficient in communication than calling individual put
153 * operations.
154 * </p>
155 *
156 * @param m a map of values to add to this map
157 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700158 void putAll(Map<? extends K, ? extends V> m);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800159
160 /**
161 * Removes all mappings from this map.
162 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700163 void clear();
Jonathan Hartdb3af892015-01-26 13:19:07 -0800164
165 /**
166 * Returns a set of the keys in this map. Changes to the set are not
167 * reflected back to the map.
168 *
169 * @return set of keys in the map
170 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700171 Set<K> keySet();
Jonathan Hartdb3af892015-01-26 13:19:07 -0800172
173 /**
174 * Returns a collections of values in this map. Changes to the collection
175 * are not reflected back to the map.
176 *
177 * @return collection of values in the map
178 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700179 Collection<V> values();
Jonathan Hartdb3af892015-01-26 13:19:07 -0800180
181 /**
182 * Returns a set of mappings contained in this map. Changes to the set are
183 * not reflected back to the map.
184 *
185 * @return set of key-value mappings in this map
186 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700187 Set<Map.Entry<K, V>> entrySet();
Jonathan Hartdb3af892015-01-26 13:19:07 -0800188
189 /**
190 * Adds the specified listener to the map which will be notified whenever
191 * the mappings in the map are changed.
192 *
193 * @param listener listener to register for events
194 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700195 void addListener(EventuallyConsistentMapListener<K, V> listener);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800196
197 /**
198 * Removes the specified listener from the map such that it will no longer
199 * receive change notifications.
200 *
201 * @param listener listener to deregister for events
202 */
Sho SHIMIZU3310a342015-05-13 12:14:05 -0700203 void removeListener(EventuallyConsistentMapListener<K, V> listener);
Jonathan Hartdb3af892015-01-26 13:19:07 -0800204}