blob: a661cc6866f0d1af83309d90f1c3e718f26c30f4 [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.consistent.impl;
18
Flavio Castro41b1f3a2015-07-31 13:51:32 -070019import com.google.common.cache.CacheBuilder;
20import com.google.common.cache.CacheLoader;
21import com.google.common.cache.LoadingCache;
22import com.google.common.collect.Maps;
Madan Jampani7c521002015-03-23 12:23:01 -070023import org.onlab.util.HexString;
Madan Jampani648451f2015-07-21 22:09:05 -070024import org.onlab.util.SharedExecutors;
Madan Jampani346d4f52015-05-04 11:09:39 -070025import org.onlab.util.Tools;
Madan Jampanie8af1cc2015-06-23 14:23:31 -070026import org.onosproject.core.ApplicationId;
Madan Jampani7c521002015-03-23 12:23:01 -070027import org.onosproject.store.service.AsyncConsistentMap;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070028import org.onosproject.store.service.ConsistentMapException;
Madan Jampani50589ac2015-06-08 11:38:46 -070029import org.onosproject.store.service.MapEvent;
30import org.onosproject.store.service.MapEventListener;
Madan Jampani7c521002015-03-23 12:23:01 -070031import org.onosproject.store.service.Serializer;
32import org.onosproject.store.service.Versioned;
Madan Jampani50589ac2015-06-08 11:38:46 -070033import org.slf4j.Logger;
Madan Jampani7c521002015-03-23 12:23:01 -070034
Flavio Castro41b1f3a2015-07-31 13:51:32 -070035import java.util.Collection;
36import java.util.Map;
37import java.util.Map.Entry;
38import java.util.Objects;
39import java.util.Set;
40import java.util.concurrent.CompletableFuture;
41import java.util.concurrent.CopyOnWriteArraySet;
42import java.util.concurrent.atomic.AtomicReference;
43import java.util.function.BiFunction;
44import java.util.function.Function;
45import java.util.function.Predicate;
46import java.util.stream.Collectors;
47
48import static com.google.common.base.Preconditions.checkNotNull;
Madan Jampania6d787b2015-08-11 11:02:02 -070049import static org.onosproject.store.consistent.impl.StateMachineUpdate.Target.MAP_UPDATE;
Madan Jampanibab51a42015-08-10 13:53:35 -070050import static org.onosproject.store.consistent.impl.StateMachineUpdate.Target.TX_COMMIT;
Flavio Castro41b1f3a2015-07-31 13:51:32 -070051import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani7c521002015-03-23 12:23:01 -070052
53/**
54 * AsyncConsistentMap implementation that is backed by a Raft consensus
55 * based database.
56 *
57 * @param <K> type of key.
58 * @param <V> type of value.
59 */
Flavio Castro41b1f3a2015-07-31 13:51:32 -070060public class DefaultAsyncConsistentMap<K, V> implements AsyncConsistentMap<K, V> {
Madan Jampani7c521002015-03-23 12:23:01 -070061
62 private final String name;
Madan Jampanie8af1cc2015-06-23 14:23:31 -070063 private final ApplicationId applicationId;
Madan Jampanif1b8e172015-03-23 11:42:02 -070064 private final Database database;
Madan Jampani7c521002015-03-23 12:23:01 -070065 private final Serializer serializer;
Madan Jampani02b7fb82015-05-01 13:01:20 -070066 private final boolean readOnly;
Madan Jampanie8af1cc2015-06-23 14:23:31 -070067 private final boolean purgeOnUninstall;
Madan Jampani50589ac2015-06-08 11:38:46 -070068
Flavio Castro41b1f3a2015-07-31 13:51:32 -070069 private static final String PRIMITIVE_NAME = "consistentMap";
Flavio Castro5e1f1292015-07-23 17:14:09 -070070 private static final String SIZE = "size";
71 private static final String IS_EMPTY = "isEmpty";
72 private static final String CONTAINS_KEY = "containsKey";
73 private static final String CONTAINS_VALUE = "containsValue";
74 private static final String GET = "get";
75 private static final String COMPUTE_IF = "computeIf";
76 private static final String PUT = "put";
77 private static final String PUT_AND_GET = "putAndGet";
78 private static final String PUT_IF_ABSENT = "putIfAbsent";
79 private static final String REMOVE = "remove";
80 private static final String CLEAR = "clear";
81 private static final String KEY_SET = "keySet";
82 private static final String VALUES = "values";
83 private static final String ENTRY_SET = "entrySet";
84 private static final String REPLACE = "replace";
85 private static final String COMPUTE_IF_ABSENT = "computeIfAbsent";
86
Madan Jampani50589ac2015-06-08 11:38:46 -070087 private final Set<MapEventListener<K, V>> listeners = new CopyOnWriteArraySet<>();
88
89 private final Logger log = getLogger(getClass());
Flavio Castro41b1f3a2015-07-31 13:51:32 -070090 private final MeteringAgent monitor;
Madan Jampani7c521002015-03-23 12:23:01 -070091
92 private static final String ERROR_NULL_KEY = "Key cannot be null";
93 private static final String ERROR_NULL_VALUE = "Null values are not allowed";
94
95 private final LoadingCache<K, String> keyCache = CacheBuilder.newBuilder()
96 .softValues()
97 .build(new CacheLoader<K, String>() {
98
99 @Override
100 public String load(K key) {
101 return HexString.toHexString(serializer.encode(key));
102 }
103 });
104
105 protected K dK(String key) {
106 return serializer.decode(HexString.fromHexString(key));
107 }
108
109 public DefaultAsyncConsistentMap(String name,
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700110 ApplicationId applicationId,
111 Database database,
112 Serializer serializer,
113 boolean readOnly,
114 boolean purgeOnUninstall,
115 boolean meteringEnabled) {
Madan Jampani7c521002015-03-23 12:23:01 -0700116 this.name = checkNotNull(name, "map name cannot be null");
Madan Jampanie8af1cc2015-06-23 14:23:31 -0700117 this.applicationId = applicationId;
Madan Jampanif1b8e172015-03-23 11:42:02 -0700118 this.database = checkNotNull(database, "database cannot be null");
Madan Jampani7c521002015-03-23 12:23:01 -0700119 this.serializer = checkNotNull(serializer, "serializer cannot be null");
Madan Jampani02b7fb82015-05-01 13:01:20 -0700120 this.readOnly = readOnly;
Madan Jampanie8af1cc2015-06-23 14:23:31 -0700121 this.purgeOnUninstall = purgeOnUninstall;
Madan Jampani648451f2015-07-21 22:09:05 -0700122 this.database.registerConsumer(update -> {
123 SharedExecutors.getSingleThreadExecutor().execute(() -> {
Madan Jampani9eb55d12015-08-14 07:47:56 -0700124 if (listeners.isEmpty()) {
125 return;
126 }
127 try {
128 if (update.target() == MAP_UPDATE) {
129 Result<UpdateResult<String, byte[]>> result = update.output();
130 if (result.success() && result.value().mapName().equals(name)) {
131 MapEvent<K, V> mapEvent = result.value()
132 .<K, V>map(this::dK,
133 v -> serializer.decode(Tools.copyOf(v)))
134 .toMapEvent();
135 notifyListeners(mapEvent);
136 }
137 } else if (update.target() == TX_COMMIT) {
138 CommitResponse response = update.output();
139 if (response.success()) {
140 response.updates().forEach(u -> {
141 if (u.mapName().equals(name)) {
142 MapEvent<K, V> mapEvent =
143 u.<K, V>map(this::dK,
144 v -> serializer.decode(Tools.copyOf(v)))
145 .toMapEvent();
146 notifyListeners(mapEvent);
147 }
148 });
149 }
Madan Jampani648451f2015-07-21 22:09:05 -0700150 }
Madan Jampani9eb55d12015-08-14 07:47:56 -0700151 } catch (Exception e) {
152 log.warn("Error notifying listeners", e);
Madan Jampani648451f2015-07-21 22:09:05 -0700153 }
154 });
155 });
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700156 this.monitor = new MeteringAgent(PRIMITIVE_NAME, name, meteringEnabled);
Madan Jampani50589ac2015-06-08 11:38:46 -0700157 }
158
159 /**
160 * Returns this map name.
161 * @return map name
162 */
163 public String name() {
164 return name;
165 }
166
167 /**
168 * Returns the serializer for map entries.
169 * @return map entry serializer
170 */
171 public Serializer serializer() {
172 return serializer;
Madan Jampani7c521002015-03-23 12:23:01 -0700173 }
174
Madan Jampanie8af1cc2015-06-23 14:23:31 -0700175 /**
176 * Returns the applicationId owning this map.
177 * @return application Id
178 */
179 public ApplicationId applicationId() {
180 return applicationId;
181 }
182
183 /**
184 * Returns whether the map entries should be purged when the application
185 * owning it is uninstalled.
186 * @return true is map needs to cleared on app uninstall; false otherwise
187 */
188 public boolean purgeOnUninstall() {
189 return purgeOnUninstall;
190 }
191
Madan Jampani7c521002015-03-23 12:23:01 -0700192 @Override
193 public CompletableFuture<Integer> size() {
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700194 final MeteringAgent.Context timer = monitor.startTimer(SIZE);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700195 return database.mapSize(name)
196 .whenComplete((r, e) -> timer.stop());
Madan Jampani7c521002015-03-23 12:23:01 -0700197 }
198
199 @Override
200 public CompletableFuture<Boolean> isEmpty() {
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700201 final MeteringAgent.Context timer = monitor.startTimer(IS_EMPTY);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700202 return database.mapIsEmpty(name)
203 .whenComplete((r, e) -> timer.stop());
Madan Jampani7c521002015-03-23 12:23:01 -0700204 }
205
206 @Override
207 public CompletableFuture<Boolean> containsKey(K key) {
208 checkNotNull(key, ERROR_NULL_KEY);
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700209 final MeteringAgent.Context timer = monitor.startTimer(CONTAINS_KEY);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700210 return database.mapContainsKey(name, keyCache.getUnchecked(key))
211 .whenComplete((r, e) -> timer.stop());
Madan Jampani7c521002015-03-23 12:23:01 -0700212 }
213
214 @Override
215 public CompletableFuture<Boolean> containsValue(V value) {
216 checkNotNull(value, ERROR_NULL_VALUE);
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700217 final MeteringAgent.Context timer = monitor.startTimer(CONTAINS_VALUE);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700218 return database.mapContainsValue(name, serializer.encode(value))
219 .whenComplete((r, e) -> timer.stop());
Madan Jampani7c521002015-03-23 12:23:01 -0700220 }
221
222 @Override
223 public CompletableFuture<Versioned<V>> get(K key) {
224 checkNotNull(key, ERROR_NULL_KEY);
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700225 final MeteringAgent.Context timer = monitor.startTimer(GET);
Madan Jampani7804c992015-07-20 13:20:19 -0700226 return database.mapGet(name, keyCache.getUnchecked(key))
Flavio Castro5e1f1292015-07-23 17:14:09 -0700227 .whenComplete((r, e) -> timer.stop())
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700228 .thenApply(v -> v != null ? v.map(serializer::decode) : null);
Madan Jampani7c521002015-03-23 12:23:01 -0700229 }
230
231 @Override
Madan Jampani346d4f52015-05-04 11:09:39 -0700232 public CompletableFuture<Versioned<V>> computeIfAbsent(K key,
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700233 Function<? super K, ? extends V> mappingFunction) {
Madan Jampani7804c992015-07-20 13:20:19 -0700234 checkNotNull(key, ERROR_NULL_KEY);
235 checkNotNull(mappingFunction, "Mapping function cannot be null");
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700236 final MeteringAgent.Context timer = monitor.startTimer(COMPUTE_IF_ABSENT);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700237 return updateAndGet(key, Match.ifNull(), Match.any(), mappingFunction.apply(key))
238 .whenComplete((r, e) -> timer.stop())
239 .thenApply(v -> v.newValue());
Madan Jampani346d4f52015-05-04 11:09:39 -0700240 }
241
242 @Override
243 public CompletableFuture<Versioned<V>> computeIfPresent(K key,
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700244 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Madan Jampani346d4f52015-05-04 11:09:39 -0700245 return computeIf(key, Objects::nonNull, remappingFunction);
246 }
247
248 @Override
249 public CompletableFuture<Versioned<V>> compute(K key,
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700250 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Madan Jampani346d4f52015-05-04 11:09:39 -0700251 return computeIf(key, v -> true, remappingFunction);
252 }
253
254 @Override
255 public CompletableFuture<Versioned<V>> computeIf(K key,
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700256 Predicate<? super V> condition,
257 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Madan Jampani346d4f52015-05-04 11:09:39 -0700258 checkNotNull(key, ERROR_NULL_KEY);
259 checkNotNull(condition, "predicate function cannot be null");
260 checkNotNull(remappingFunction, "Remapping function cannot be null");
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700261 final MeteringAgent.Context timer = monitor.startTimer(COMPUTE_IF);
Madan Jampani346d4f52015-05-04 11:09:39 -0700262 return get(key).thenCompose(r1 -> {
263 V existingValue = r1 == null ? null : r1.value();
264 // if the condition evaluates to false, return existing value.
265 if (!condition.test(existingValue)) {
266 return CompletableFuture.completedFuture(r1);
267 }
268
269 AtomicReference<V> computedValue = new AtomicReference<>();
270 // if remappingFunction throws an exception, return the exception.
271 try {
272 computedValue.set(remappingFunction.apply(key, existingValue));
273 } catch (Exception e) {
274 return Tools.exceptionalFuture(e);
275 }
Madan Jampani7804c992015-07-20 13:20:19 -0700276 if (computedValue.get() == null && r1 == null) {
277 return CompletableFuture.completedFuture(null);
Madan Jampani346d4f52015-05-04 11:09:39 -0700278 }
Madan Jampani7804c992015-07-20 13:20:19 -0700279 Match<V> valueMatcher = r1 == null ? Match.ifNull() : Match.any();
280 Match<Long> versionMatcher = r1 == null ? Match.any() : Match.ifValue(r1.version());
281 return updateAndGet(key, valueMatcher, versionMatcher, computedValue.get())
Flavio Castro5e1f1292015-07-23 17:14:09 -0700282 .whenComplete((r, e) -> timer.stop())
Madan Jampani7804c992015-07-20 13:20:19 -0700283 .thenApply(v -> {
284 if (v.updated()) {
285 return v.newValue();
286 } else {
287 throw new ConsistentMapException.ConcurrentModification();
288 }
289 });
290 });
Madan Jampani346d4f52015-05-04 11:09:39 -0700291 }
292
293 @Override
Madan Jampani7c521002015-03-23 12:23:01 -0700294 public CompletableFuture<Versioned<V>> put(K key, V value) {
295 checkNotNull(key, ERROR_NULL_KEY);
296 checkNotNull(value, ERROR_NULL_VALUE);
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700297 final MeteringAgent.Context timer = monitor.startTimer(PUT);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700298 return updateAndGet(key, Match.any(), Match.any(), value).thenApply(v -> v.oldValue())
299 .whenComplete((r, e) -> timer.stop());
Madan Jampani346d4f52015-05-04 11:09:39 -0700300 }
301
302 @Override
303 public CompletableFuture<Versioned<V>> putAndGet(K key, V value) {
304 checkNotNull(key, ERROR_NULL_KEY);
305 checkNotNull(value, ERROR_NULL_VALUE);
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700306 final MeteringAgent.Context timer = monitor.startTimer(PUT_AND_GET);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700307 return updateAndGet(key, Match.any(), Match.any(), value).thenApply(v -> v.newValue())
308 .whenComplete((r, e) -> timer.stop());
Madan Jampani7c521002015-03-23 12:23:01 -0700309 }
310
311 @Override
312 public CompletableFuture<Versioned<V>> remove(K key) {
313 checkNotNull(key, ERROR_NULL_KEY);
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700314 final MeteringAgent.Context timer = monitor.startTimer(REMOVE);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700315 return updateAndGet(key, Match.any(), Match.any(), null).thenApply(v -> v.oldValue())
316 .whenComplete((r, e) -> timer.stop());
Madan Jampani7c521002015-03-23 12:23:01 -0700317 }
318
319 @Override
320 public CompletableFuture<Void> clear() {
Madan Jampani02b7fb82015-05-01 13:01:20 -0700321 checkIfUnmodifiable();
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700322 final MeteringAgent.Context timer = monitor.startTimer(CLEAR);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700323 return database.mapClear(name).thenApply(this::unwrapResult)
324 .whenComplete((r, e) -> timer.stop());
Madan Jampani7c521002015-03-23 12:23:01 -0700325 }
326
327 @Override
328 public CompletableFuture<Set<K>> keySet() {
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700329 final MeteringAgent.Context timer = monitor.startTimer(KEY_SET);
Madan Jampani7804c992015-07-20 13:20:19 -0700330 return database.mapKeySet(name)
Madan Jampani7c521002015-03-23 12:23:01 -0700331 .thenApply(s -> s
Flavio Castro5e1f1292015-07-23 17:14:09 -0700332 .stream()
333 .map(this::dK)
334 .collect(Collectors.toSet()))
335 .whenComplete((r, e) -> timer.stop());
Madan Jampani7c521002015-03-23 12:23:01 -0700336 }
337
338 @Override
339 public CompletableFuture<Collection<Versioned<V>>> values() {
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700340 final MeteringAgent.Context timer = monitor.startTimer(VALUES);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700341 return database.mapValues(name)
342 .whenComplete((r, e) -> timer.stop())
343 .thenApply(c -> c
344 .stream()
345 .map(v -> v.<V>map(serializer::decode))
346 .collect(Collectors.toList()));
Madan Jampani7c521002015-03-23 12:23:01 -0700347 }
348
349 @Override
350 public CompletableFuture<Set<Entry<K, Versioned<V>>>> entrySet() {
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700351 final MeteringAgent.Context timer = monitor.startTimer(ENTRY_SET);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700352 return database.mapEntrySet(name)
353 .whenComplete((r, e) -> timer.stop())
354 .thenApply(s -> s
355 .stream()
356 .map(this::mapRawEntry)
357 .collect(Collectors.toSet()));
Madan Jampani7c521002015-03-23 12:23:01 -0700358 }
359
360 @Override
361 public CompletableFuture<Versioned<V>> putIfAbsent(K key, V value) {
362 checkNotNull(key, ERROR_NULL_KEY);
363 checkNotNull(value, ERROR_NULL_VALUE);
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700364 final MeteringAgent.Context timer = monitor.startTimer(PUT_IF_ABSENT);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700365 return updateAndGet(key, Match.ifNull(), Match.any(), value)
366 .whenComplete((r, e) -> timer.stop())
367 .thenApply(v -> v.oldValue());
Madan Jampani7c521002015-03-23 12:23:01 -0700368 }
369
370 @Override
371 public CompletableFuture<Boolean> remove(K key, V value) {
372 checkNotNull(key, ERROR_NULL_KEY);
373 checkNotNull(value, ERROR_NULL_VALUE);
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700374 final MeteringAgent.Context timer = monitor.startTimer(REMOVE);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700375 return updateAndGet(key, Match.ifValue(value), Match.any(), null)
376 .whenComplete((r, e) -> timer.stop())
377 .thenApply(v -> v.updated());
Madan Jampani7c521002015-03-23 12:23:01 -0700378 }
379
380 @Override
381 public CompletableFuture<Boolean> remove(K key, long version) {
382 checkNotNull(key, ERROR_NULL_KEY);
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700383 final MeteringAgent.Context timer = monitor.startTimer(REMOVE);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700384 return updateAndGet(key, Match.any(), Match.ifValue(version), null)
385 .whenComplete((r, e) -> timer.stop())
386 .thenApply(v -> v.updated());
Madan Jampani7c521002015-03-23 12:23:01 -0700387 }
388
389 @Override
390 public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) {
391 checkNotNull(key, ERROR_NULL_KEY);
Madan Jampani7804c992015-07-20 13:20:19 -0700392 checkNotNull(oldValue, ERROR_NULL_VALUE);
Madan Jampani7c521002015-03-23 12:23:01 -0700393 checkNotNull(newValue, ERROR_NULL_VALUE);
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700394 final MeteringAgent.Context timer = monitor.startTimer(REPLACE);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700395 return updateAndGet(key, Match.ifValue(oldValue), Match.any(), newValue)
396 .whenComplete((r, e) -> timer.stop())
397 .thenApply(v -> v.updated());
Madan Jampani7c521002015-03-23 12:23:01 -0700398 }
399
400 @Override
401 public CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue) {
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700402 final MeteringAgent.Context timer = monitor.startTimer(REPLACE);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700403 return updateAndGet(key, Match.any(), Match.ifValue(oldVersion), newValue)
404 .whenComplete((r, e) -> timer.stop())
405 .thenApply(v -> v.updated());
Madan Jampani346d4f52015-05-04 11:09:39 -0700406 }
407
Madan Jampani7804c992015-07-20 13:20:19 -0700408 private Map.Entry<K, Versioned<V>> mapRawEntry(Map.Entry<String, Versioned<byte[]>> e) {
409 return Maps.immutableEntry(dK(e.getKey()), e.getValue().<V>map(serializer::decode));
410 }
411
412 private CompletableFuture<UpdateResult<K, V>> updateAndGet(K key,
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700413 Match<V> oldValueMatch,
414 Match<Long> oldVersionMatch,
415 V value) {
Madan Jampani02b7fb82015-05-01 13:01:20 -0700416 checkIfUnmodifiable();
Madan Jampani7804c992015-07-20 13:20:19 -0700417 return database.mapUpdate(name,
Flavio Castro5e1f1292015-07-23 17:14:09 -0700418 keyCache.getUnchecked(key),
419 oldValueMatch.map(serializer::encode),
420 oldVersionMatch,
421 value == null ? null : serializer.encode(value))
Madan Jampani7804c992015-07-20 13:20:19 -0700422 .thenApply(this::unwrapResult)
Madan Jampani85668632015-08-10 10:46:40 -0700423 .thenApply(r -> r.<K, V>map(this::dK, serializer::decode));
Madan Jampani7c521002015-03-23 12:23:01 -0700424 }
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700425
426 private <T> T unwrapResult(Result<T> result) {
427 if (result.status() == Result.Status.LOCKED) {
428 throw new ConsistentMapException.ConcurrentModification();
429 } else if (result.success()) {
430 return result.value();
431 } else {
432 throw new IllegalStateException("Must not be here");
433 }
434 }
Madan Jampani02b7fb82015-05-01 13:01:20 -0700435
436 private void checkIfUnmodifiable() {
437 if (readOnly) {
438 throw new UnsupportedOperationException();
439 }
440 }
Madan Jampani50589ac2015-06-08 11:38:46 -0700441
442 @Override
443 public void addListener(MapEventListener<K, V> listener) {
444 listeners.add(listener);
445 }
446
447 @Override
448 public void removeListener(MapEventListener<K, V> listener) {
449 listeners.remove(listener);
450 }
451
452 protected void notifyListeners(MapEvent<K, V> event) {
Madan Jampani85668632015-08-10 10:46:40 -0700453 if (event == null) {
454 return;
455 }
456 listeners.forEach(listener -> {
457 try {
458 listener.event(event);
459 } catch (Exception e) {
460 log.warn("Failure notifying listener about {}", event, e);
Madan Jampani50589ac2015-06-08 11:38:46 -0700461 }
Madan Jampani85668632015-08-10 10:46:40 -0700462 });
Madan Jampani50589ac2015-06-08 11:38:46 -0700463 }
Flavio Castro5e1f1292015-07-23 17:14:09 -0700464
Madan Jampanibab51a42015-08-10 13:53:35 -0700465}