blob: 9082ba64ddd52bd6657ddcbe91a2ac7a88294f28 [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
Madan Jampanibab51a42015-08-10 13:53:35 -070035
Flavio Castro41b1f3a2015-07-31 13:51:32 -070036import java.util.Collection;
37import java.util.Map;
38import java.util.Map.Entry;
39import java.util.Objects;
40import java.util.Set;
41import java.util.concurrent.CompletableFuture;
42import java.util.concurrent.CopyOnWriteArraySet;
43import java.util.concurrent.atomic.AtomicReference;
44import java.util.function.BiFunction;
45import java.util.function.Function;
46import java.util.function.Predicate;
47import java.util.stream.Collectors;
48
49import static com.google.common.base.Preconditions.checkNotNull;
Madan Jampania6d787b2015-08-11 11:02:02 -070050import static org.onosproject.store.consistent.impl.StateMachineUpdate.Target.MAP_UPDATE;
Madan Jampanibab51a42015-08-10 13:53:35 -070051import static org.onosproject.store.consistent.impl.StateMachineUpdate.Target.TX_COMMIT;
Flavio Castro41b1f3a2015-07-31 13:51:32 -070052import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani7c521002015-03-23 12:23:01 -070053
54/**
55 * AsyncConsistentMap implementation that is backed by a Raft consensus
56 * based database.
57 *
58 * @param <K> type of key.
59 * @param <V> type of value.
60 */
Flavio Castro41b1f3a2015-07-31 13:51:32 -070061public class DefaultAsyncConsistentMap<K, V> implements AsyncConsistentMap<K, V> {
Madan Jampani7c521002015-03-23 12:23:01 -070062
63 private final String name;
Madan Jampanie8af1cc2015-06-23 14:23:31 -070064 private final ApplicationId applicationId;
Madan Jampanif1b8e172015-03-23 11:42:02 -070065 private final Database database;
Madan Jampani7c521002015-03-23 12:23:01 -070066 private final Serializer serializer;
Madan Jampani02b7fb82015-05-01 13:01:20 -070067 private final boolean readOnly;
Madan Jampanie8af1cc2015-06-23 14:23:31 -070068 private final boolean purgeOnUninstall;
Madan Jampani50589ac2015-06-08 11:38:46 -070069
Flavio Castro41b1f3a2015-07-31 13:51:32 -070070 private static final String PRIMITIVE_NAME = "consistentMap";
Flavio Castro5e1f1292015-07-23 17:14:09 -070071 private static final String SIZE = "size";
72 private static final String IS_EMPTY = "isEmpty";
73 private static final String CONTAINS_KEY = "containsKey";
74 private static final String CONTAINS_VALUE = "containsValue";
75 private static final String GET = "get";
76 private static final String COMPUTE_IF = "computeIf";
77 private static final String PUT = "put";
78 private static final String PUT_AND_GET = "putAndGet";
79 private static final String PUT_IF_ABSENT = "putIfAbsent";
80 private static final String REMOVE = "remove";
81 private static final String CLEAR = "clear";
82 private static final String KEY_SET = "keySet";
83 private static final String VALUES = "values";
84 private static final String ENTRY_SET = "entrySet";
85 private static final String REPLACE = "replace";
86 private static final String COMPUTE_IF_ABSENT = "computeIfAbsent";
87
Madan Jampani50589ac2015-06-08 11:38:46 -070088 private final Set<MapEventListener<K, V>> listeners = new CopyOnWriteArraySet<>();
89
90 private final Logger log = getLogger(getClass());
Flavio Castro41b1f3a2015-07-31 13:51:32 -070091 private final MeteringAgent monitor;
Madan Jampani7c521002015-03-23 12:23:01 -070092
93 private static final String ERROR_NULL_KEY = "Key cannot be null";
94 private static final String ERROR_NULL_VALUE = "Null values are not allowed";
95
96 private final LoadingCache<K, String> keyCache = CacheBuilder.newBuilder()
97 .softValues()
98 .build(new CacheLoader<K, String>() {
99
100 @Override
101 public String load(K key) {
102 return HexString.toHexString(serializer.encode(key));
103 }
104 });
105
106 protected K dK(String key) {
107 return serializer.decode(HexString.fromHexString(key));
108 }
109
110 public DefaultAsyncConsistentMap(String name,
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700111 ApplicationId applicationId,
112 Database database,
113 Serializer serializer,
114 boolean readOnly,
115 boolean purgeOnUninstall,
116 boolean meteringEnabled) {
Madan Jampani7c521002015-03-23 12:23:01 -0700117 this.name = checkNotNull(name, "map name cannot be null");
Madan Jampanie8af1cc2015-06-23 14:23:31 -0700118 this.applicationId = applicationId;
Madan Jampanif1b8e172015-03-23 11:42:02 -0700119 this.database = checkNotNull(database, "database cannot be null");
Madan Jampani7c521002015-03-23 12:23:01 -0700120 this.serializer = checkNotNull(serializer, "serializer cannot be null");
Madan Jampani02b7fb82015-05-01 13:01:20 -0700121 this.readOnly = readOnly;
Madan Jampanie8af1cc2015-06-23 14:23:31 -0700122 this.purgeOnUninstall = purgeOnUninstall;
Madan Jampani648451f2015-07-21 22:09:05 -0700123 this.database.registerConsumer(update -> {
124 SharedExecutors.getSingleThreadExecutor().execute(() -> {
Madan Jampania6d787b2015-08-11 11:02:02 -0700125 if (update.target() == MAP_UPDATE) {
Madan Jampani648451f2015-07-21 22:09:05 -0700126 Result<UpdateResult<String, byte[]>> result = update.output();
127 if (result.success() && result.value().mapName().equals(name)) {
128 MapEvent<K, V> mapEvent = result.value().<K, V>map(this::dK, serializer::decode).toMapEvent();
Madan Jampani85668632015-08-10 10:46:40 -0700129 notifyListeners(mapEvent);
Madan Jampani648451f2015-07-21 22:09:05 -0700130 }
Madan Jampanibab51a42015-08-10 13:53:35 -0700131 } else if (update.target() == TX_COMMIT) {
132 CommitResponse response = update.output();
133 if (response.success()) {
134 response.updates().forEach(u -> {
135 if (u.mapName().equals(name)) {
136 MapEvent<K, V> mapEvent = u.<K, V>map(this::dK, serializer::decode).toMapEvent();
137 notifyListeners(mapEvent);
138 }
139 });
140 }
Madan Jampani648451f2015-07-21 22:09:05 -0700141 }
142 });
143 });
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700144 this.monitor = new MeteringAgent(PRIMITIVE_NAME, name, meteringEnabled);
Madan Jampani50589ac2015-06-08 11:38:46 -0700145 }
146
147 /**
148 * Returns this map name.
149 * @return map name
150 */
151 public String name() {
152 return name;
153 }
154
155 /**
156 * Returns the serializer for map entries.
157 * @return map entry serializer
158 */
159 public Serializer serializer() {
160 return serializer;
Madan Jampani7c521002015-03-23 12:23:01 -0700161 }
162
Madan Jampanie8af1cc2015-06-23 14:23:31 -0700163 /**
164 * Returns the applicationId owning this map.
165 * @return application Id
166 */
167 public ApplicationId applicationId() {
168 return applicationId;
169 }
170
171 /**
172 * Returns whether the map entries should be purged when the application
173 * owning it is uninstalled.
174 * @return true is map needs to cleared on app uninstall; false otherwise
175 */
176 public boolean purgeOnUninstall() {
177 return purgeOnUninstall;
178 }
179
Madan Jampani7c521002015-03-23 12:23:01 -0700180 @Override
181 public CompletableFuture<Integer> size() {
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700182 final MeteringAgent.Context timer = monitor.startTimer(SIZE);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700183 return database.mapSize(name)
184 .whenComplete((r, e) -> timer.stop());
Madan Jampani7c521002015-03-23 12:23:01 -0700185 }
186
187 @Override
188 public CompletableFuture<Boolean> isEmpty() {
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700189 final MeteringAgent.Context timer = monitor.startTimer(IS_EMPTY);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700190 return database.mapIsEmpty(name)
191 .whenComplete((r, e) -> timer.stop());
Madan Jampani7c521002015-03-23 12:23:01 -0700192 }
193
194 @Override
195 public CompletableFuture<Boolean> containsKey(K key) {
196 checkNotNull(key, ERROR_NULL_KEY);
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700197 final MeteringAgent.Context timer = monitor.startTimer(CONTAINS_KEY);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700198 return database.mapContainsKey(name, keyCache.getUnchecked(key))
199 .whenComplete((r, e) -> timer.stop());
Madan Jampani7c521002015-03-23 12:23:01 -0700200 }
201
202 @Override
203 public CompletableFuture<Boolean> containsValue(V value) {
204 checkNotNull(value, ERROR_NULL_VALUE);
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700205 final MeteringAgent.Context timer = monitor.startTimer(CONTAINS_VALUE);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700206 return database.mapContainsValue(name, serializer.encode(value))
207 .whenComplete((r, e) -> timer.stop());
Madan Jampani7c521002015-03-23 12:23:01 -0700208 }
209
210 @Override
211 public CompletableFuture<Versioned<V>> get(K key) {
212 checkNotNull(key, ERROR_NULL_KEY);
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700213 final MeteringAgent.Context timer = monitor.startTimer(GET);
Madan Jampani7804c992015-07-20 13:20:19 -0700214 return database.mapGet(name, keyCache.getUnchecked(key))
Flavio Castro5e1f1292015-07-23 17:14:09 -0700215 .whenComplete((r, e) -> timer.stop())
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700216 .thenApply(v -> v != null ? v.map(serializer::decode) : null);
Madan Jampani7c521002015-03-23 12:23:01 -0700217 }
218
219 @Override
Madan Jampani346d4f52015-05-04 11:09:39 -0700220 public CompletableFuture<Versioned<V>> computeIfAbsent(K key,
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700221 Function<? super K, ? extends V> mappingFunction) {
Madan Jampani7804c992015-07-20 13:20:19 -0700222 checkNotNull(key, ERROR_NULL_KEY);
223 checkNotNull(mappingFunction, "Mapping function cannot be null");
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700224 final MeteringAgent.Context timer = monitor.startTimer(COMPUTE_IF_ABSENT);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700225 return updateAndGet(key, Match.ifNull(), Match.any(), mappingFunction.apply(key))
226 .whenComplete((r, e) -> timer.stop())
227 .thenApply(v -> v.newValue());
Madan Jampani346d4f52015-05-04 11:09:39 -0700228 }
229
230 @Override
231 public CompletableFuture<Versioned<V>> computeIfPresent(K key,
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700232 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Madan Jampani346d4f52015-05-04 11:09:39 -0700233 return computeIf(key, Objects::nonNull, remappingFunction);
234 }
235
236 @Override
237 public CompletableFuture<Versioned<V>> compute(K key,
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700238 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Madan Jampani346d4f52015-05-04 11:09:39 -0700239 return computeIf(key, v -> true, remappingFunction);
240 }
241
242 @Override
243 public CompletableFuture<Versioned<V>> computeIf(K key,
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700244 Predicate<? super V> condition,
245 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Madan Jampani346d4f52015-05-04 11:09:39 -0700246 checkNotNull(key, ERROR_NULL_KEY);
247 checkNotNull(condition, "predicate function cannot be null");
248 checkNotNull(remappingFunction, "Remapping function cannot be null");
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700249 final MeteringAgent.Context timer = monitor.startTimer(COMPUTE_IF);
Madan Jampani346d4f52015-05-04 11:09:39 -0700250 return get(key).thenCompose(r1 -> {
251 V existingValue = r1 == null ? null : r1.value();
252 // if the condition evaluates to false, return existing value.
253 if (!condition.test(existingValue)) {
254 return CompletableFuture.completedFuture(r1);
255 }
256
257 AtomicReference<V> computedValue = new AtomicReference<>();
258 // if remappingFunction throws an exception, return the exception.
259 try {
260 computedValue.set(remappingFunction.apply(key, existingValue));
261 } catch (Exception e) {
262 return Tools.exceptionalFuture(e);
263 }
Madan Jampani7804c992015-07-20 13:20:19 -0700264 if (computedValue.get() == null && r1 == null) {
265 return CompletableFuture.completedFuture(null);
Madan Jampani346d4f52015-05-04 11:09:39 -0700266 }
Madan Jampani7804c992015-07-20 13:20:19 -0700267 Match<V> valueMatcher = r1 == null ? Match.ifNull() : Match.any();
268 Match<Long> versionMatcher = r1 == null ? Match.any() : Match.ifValue(r1.version());
269 return updateAndGet(key, valueMatcher, versionMatcher, computedValue.get())
Flavio Castro5e1f1292015-07-23 17:14:09 -0700270 .whenComplete((r, e) -> timer.stop())
Madan Jampani7804c992015-07-20 13:20:19 -0700271 .thenApply(v -> {
272 if (v.updated()) {
273 return v.newValue();
274 } else {
275 throw new ConsistentMapException.ConcurrentModification();
276 }
277 });
278 });
Madan Jampani346d4f52015-05-04 11:09:39 -0700279 }
280
281 @Override
Madan Jampani7c521002015-03-23 12:23:01 -0700282 public CompletableFuture<Versioned<V>> put(K key, V value) {
283 checkNotNull(key, ERROR_NULL_KEY);
284 checkNotNull(value, ERROR_NULL_VALUE);
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700285 final MeteringAgent.Context timer = monitor.startTimer(PUT);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700286 return updateAndGet(key, Match.any(), Match.any(), value).thenApply(v -> v.oldValue())
287 .whenComplete((r, e) -> timer.stop());
Madan Jampani346d4f52015-05-04 11:09:39 -0700288 }
289
290 @Override
291 public CompletableFuture<Versioned<V>> putAndGet(K key, V value) {
292 checkNotNull(key, ERROR_NULL_KEY);
293 checkNotNull(value, ERROR_NULL_VALUE);
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700294 final MeteringAgent.Context timer = monitor.startTimer(PUT_AND_GET);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700295 return updateAndGet(key, Match.any(), Match.any(), value).thenApply(v -> v.newValue())
296 .whenComplete((r, e) -> timer.stop());
Madan Jampani7c521002015-03-23 12:23:01 -0700297 }
298
299 @Override
300 public CompletableFuture<Versioned<V>> remove(K key) {
301 checkNotNull(key, ERROR_NULL_KEY);
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700302 final MeteringAgent.Context timer = monitor.startTimer(REMOVE);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700303 return updateAndGet(key, Match.any(), Match.any(), null).thenApply(v -> v.oldValue())
304 .whenComplete((r, e) -> timer.stop());
Madan Jampani7c521002015-03-23 12:23:01 -0700305 }
306
307 @Override
308 public CompletableFuture<Void> clear() {
Madan Jampani02b7fb82015-05-01 13:01:20 -0700309 checkIfUnmodifiable();
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700310 final MeteringAgent.Context timer = monitor.startTimer(CLEAR);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700311 return database.mapClear(name).thenApply(this::unwrapResult)
312 .whenComplete((r, e) -> timer.stop());
Madan Jampani7c521002015-03-23 12:23:01 -0700313 }
314
315 @Override
316 public CompletableFuture<Set<K>> keySet() {
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700317 final MeteringAgent.Context timer = monitor.startTimer(KEY_SET);
Madan Jampani7804c992015-07-20 13:20:19 -0700318 return database.mapKeySet(name)
Madan Jampani7c521002015-03-23 12:23:01 -0700319 .thenApply(s -> s
Flavio Castro5e1f1292015-07-23 17:14:09 -0700320 .stream()
321 .map(this::dK)
322 .collect(Collectors.toSet()))
323 .whenComplete((r, e) -> timer.stop());
Madan Jampani7c521002015-03-23 12:23:01 -0700324 }
325
326 @Override
327 public CompletableFuture<Collection<Versioned<V>>> values() {
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700328 final MeteringAgent.Context timer = monitor.startTimer(VALUES);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700329 return database.mapValues(name)
330 .whenComplete((r, e) -> timer.stop())
331 .thenApply(c -> c
332 .stream()
333 .map(v -> v.<V>map(serializer::decode))
334 .collect(Collectors.toList()));
Madan Jampani7c521002015-03-23 12:23:01 -0700335 }
336
337 @Override
338 public CompletableFuture<Set<Entry<K, Versioned<V>>>> entrySet() {
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700339 final MeteringAgent.Context timer = monitor.startTimer(ENTRY_SET);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700340 return database.mapEntrySet(name)
341 .whenComplete((r, e) -> timer.stop())
342 .thenApply(s -> s
343 .stream()
344 .map(this::mapRawEntry)
345 .collect(Collectors.toSet()));
Madan Jampani7c521002015-03-23 12:23:01 -0700346 }
347
348 @Override
349 public CompletableFuture<Versioned<V>> putIfAbsent(K key, V value) {
350 checkNotNull(key, ERROR_NULL_KEY);
351 checkNotNull(value, ERROR_NULL_VALUE);
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700352 final MeteringAgent.Context timer = monitor.startTimer(PUT_IF_ABSENT);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700353 return updateAndGet(key, Match.ifNull(), Match.any(), value)
354 .whenComplete((r, e) -> timer.stop())
355 .thenApply(v -> v.oldValue());
Madan Jampani7c521002015-03-23 12:23:01 -0700356 }
357
358 @Override
359 public CompletableFuture<Boolean> remove(K key, V value) {
360 checkNotNull(key, ERROR_NULL_KEY);
361 checkNotNull(value, ERROR_NULL_VALUE);
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700362 final MeteringAgent.Context timer = monitor.startTimer(REMOVE);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700363 return updateAndGet(key, Match.ifValue(value), Match.any(), null)
364 .whenComplete((r, e) -> timer.stop())
365 .thenApply(v -> v.updated());
Madan Jampani7c521002015-03-23 12:23:01 -0700366 }
367
368 @Override
369 public CompletableFuture<Boolean> remove(K key, long version) {
370 checkNotNull(key, ERROR_NULL_KEY);
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700371 final MeteringAgent.Context timer = monitor.startTimer(REMOVE);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700372 return updateAndGet(key, Match.any(), Match.ifValue(version), null)
373 .whenComplete((r, e) -> timer.stop())
374 .thenApply(v -> v.updated());
Madan Jampani7c521002015-03-23 12:23:01 -0700375 }
376
377 @Override
378 public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) {
379 checkNotNull(key, ERROR_NULL_KEY);
Madan Jampani7804c992015-07-20 13:20:19 -0700380 checkNotNull(oldValue, ERROR_NULL_VALUE);
Madan Jampani7c521002015-03-23 12:23:01 -0700381 checkNotNull(newValue, ERROR_NULL_VALUE);
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700382 final MeteringAgent.Context timer = monitor.startTimer(REPLACE);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700383 return updateAndGet(key, Match.ifValue(oldValue), Match.any(), newValue)
384 .whenComplete((r, e) -> timer.stop())
385 .thenApply(v -> v.updated());
Madan Jampani7c521002015-03-23 12:23:01 -0700386 }
387
388 @Override
389 public CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue) {
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700390 final MeteringAgent.Context timer = monitor.startTimer(REPLACE);
Flavio Castro5e1f1292015-07-23 17:14:09 -0700391 return updateAndGet(key, Match.any(), Match.ifValue(oldVersion), newValue)
392 .whenComplete((r, e) -> timer.stop())
393 .thenApply(v -> v.updated());
Madan Jampani346d4f52015-05-04 11:09:39 -0700394 }
395
Madan Jampani7804c992015-07-20 13:20:19 -0700396 private Map.Entry<K, Versioned<V>> mapRawEntry(Map.Entry<String, Versioned<byte[]>> e) {
397 return Maps.immutableEntry(dK(e.getKey()), e.getValue().<V>map(serializer::decode));
398 }
399
400 private CompletableFuture<UpdateResult<K, V>> updateAndGet(K key,
Flavio Castro41b1f3a2015-07-31 13:51:32 -0700401 Match<V> oldValueMatch,
402 Match<Long> oldVersionMatch,
403 V value) {
Madan Jampani02b7fb82015-05-01 13:01:20 -0700404 checkIfUnmodifiable();
Madan Jampani7804c992015-07-20 13:20:19 -0700405 return database.mapUpdate(name,
Flavio Castro5e1f1292015-07-23 17:14:09 -0700406 keyCache.getUnchecked(key),
407 oldValueMatch.map(serializer::encode),
408 oldVersionMatch,
409 value == null ? null : serializer.encode(value))
Madan Jampani7804c992015-07-20 13:20:19 -0700410 .thenApply(this::unwrapResult)
Madan Jampani85668632015-08-10 10:46:40 -0700411 .thenApply(r -> r.<K, V>map(this::dK, serializer::decode));
Madan Jampani7c521002015-03-23 12:23:01 -0700412 }
Madan Jampanibff6d8f2015-03-31 16:53:47 -0700413
414 private <T> T unwrapResult(Result<T> result) {
415 if (result.status() == Result.Status.LOCKED) {
416 throw new ConsistentMapException.ConcurrentModification();
417 } else if (result.success()) {
418 return result.value();
419 } else {
420 throw new IllegalStateException("Must not be here");
421 }
422 }
Madan Jampani02b7fb82015-05-01 13:01:20 -0700423
424 private void checkIfUnmodifiable() {
425 if (readOnly) {
426 throw new UnsupportedOperationException();
427 }
428 }
Madan Jampani50589ac2015-06-08 11:38:46 -0700429
430 @Override
431 public void addListener(MapEventListener<K, V> listener) {
432 listeners.add(listener);
433 }
434
435 @Override
436 public void removeListener(MapEventListener<K, V> listener) {
437 listeners.remove(listener);
438 }
439
440 protected void notifyListeners(MapEvent<K, V> event) {
Madan Jampani85668632015-08-10 10:46:40 -0700441 if (event == null) {
442 return;
443 }
444 listeners.forEach(listener -> {
445 try {
446 listener.event(event);
447 } catch (Exception e) {
448 log.warn("Failure notifying listener about {}", event, e);
Madan Jampani50589ac2015-06-08 11:38:46 -0700449 }
Madan Jampani85668632015-08-10 10:46:40 -0700450 });
Madan Jampani50589ac2015-06-08 11:38:46 -0700451 }
Flavio Castro5e1f1292015-07-23 17:14:09 -0700452
Madan Jampanibab51a42015-08-10 13:53:35 -0700453}