Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.store.consistent.impl; |
| 18 | |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 19 | import com.google.common.cache.CacheBuilder; |
| 20 | import com.google.common.cache.CacheLoader; |
| 21 | import com.google.common.cache.LoadingCache; |
| 22 | import com.google.common.collect.Maps; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 23 | import org.onlab.util.HexString; |
Madan Jampani | 648451f | 2015-07-21 22:09:05 -0700 | [diff] [blame] | 24 | import org.onlab.util.SharedExecutors; |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 25 | import org.onlab.util.Tools; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 26 | import org.onosproject.core.ApplicationId; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 27 | import org.onosproject.store.service.AsyncConsistentMap; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 28 | import org.onosproject.store.service.ConsistentMapException; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 29 | import org.onosproject.store.service.MapEvent; |
| 30 | import org.onosproject.store.service.MapEventListener; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 31 | import org.onosproject.store.service.Serializer; |
| 32 | import org.onosproject.store.service.Versioned; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 33 | import org.slf4j.Logger; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 34 | |
Madan Jampani | bab51a4 | 2015-08-10 13:53:35 -0700 | [diff] [blame] | 35 | |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 36 | import java.util.Collection; |
| 37 | import java.util.Map; |
| 38 | import java.util.Map.Entry; |
| 39 | import java.util.Objects; |
| 40 | import java.util.Set; |
| 41 | import java.util.concurrent.CompletableFuture; |
| 42 | import java.util.concurrent.CopyOnWriteArraySet; |
| 43 | import java.util.concurrent.atomic.AtomicReference; |
| 44 | import java.util.function.BiFunction; |
| 45 | import java.util.function.Function; |
| 46 | import java.util.function.Predicate; |
| 47 | import java.util.stream.Collectors; |
| 48 | |
| 49 | import static com.google.common.base.Preconditions.checkNotNull; |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 50 | import static org.onosproject.store.consistent.impl.StateMachineUpdate.Target.MAP_UPDATE; |
Madan Jampani | bab51a4 | 2015-08-10 13:53:35 -0700 | [diff] [blame] | 51 | import static org.onosproject.store.consistent.impl.StateMachineUpdate.Target.TX_COMMIT; |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 52 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 53 | |
| 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 Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 61 | public class DefaultAsyncConsistentMap<K, V> implements AsyncConsistentMap<K, V> { |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 62 | |
| 63 | private final String name; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 64 | private final ApplicationId applicationId; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 65 | private final Database database; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 66 | private final Serializer serializer; |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 67 | private final boolean readOnly; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 68 | private final boolean purgeOnUninstall; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 69 | |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 70 | private static final String PRIMITIVE_NAME = "consistentMap"; |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 71 | 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 Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 88 | private final Set<MapEventListener<K, V>> listeners = new CopyOnWriteArraySet<>(); |
| 89 | |
| 90 | private final Logger log = getLogger(getClass()); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 91 | private final MeteringAgent monitor; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 92 | |
| 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 Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 111 | ApplicationId applicationId, |
| 112 | Database database, |
| 113 | Serializer serializer, |
| 114 | boolean readOnly, |
| 115 | boolean purgeOnUninstall, |
| 116 | boolean meteringEnabled) { |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 117 | this.name = checkNotNull(name, "map name cannot be null"); |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 118 | this.applicationId = applicationId; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 119 | this.database = checkNotNull(database, "database cannot be null"); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 120 | this.serializer = checkNotNull(serializer, "serializer cannot be null"); |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 121 | this.readOnly = readOnly; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 122 | this.purgeOnUninstall = purgeOnUninstall; |
Madan Jampani | 648451f | 2015-07-21 22:09:05 -0700 | [diff] [blame] | 123 | this.database.registerConsumer(update -> { |
| 124 | SharedExecutors.getSingleThreadExecutor().execute(() -> { |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 125 | if (update.target() == MAP_UPDATE) { |
Madan Jampani | 648451f | 2015-07-21 22:09:05 -0700 | [diff] [blame] | 126 | 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 Jampani | 8566863 | 2015-08-10 10:46:40 -0700 | [diff] [blame] | 129 | notifyListeners(mapEvent); |
Madan Jampani | 648451f | 2015-07-21 22:09:05 -0700 | [diff] [blame] | 130 | } |
Madan Jampani | bab51a4 | 2015-08-10 13:53:35 -0700 | [diff] [blame] | 131 | } 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 Jampani | 648451f | 2015-07-21 22:09:05 -0700 | [diff] [blame] | 141 | } |
| 142 | }); |
| 143 | }); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 144 | this.monitor = new MeteringAgent(PRIMITIVE_NAME, name, meteringEnabled); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 145 | } |
| 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 Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 163 | /** |
| 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 Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 180 | @Override |
| 181 | public CompletableFuture<Integer> size() { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 182 | final MeteringAgent.Context timer = monitor.startTimer(SIZE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 183 | return database.mapSize(name) |
| 184 | .whenComplete((r, e) -> timer.stop()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public CompletableFuture<Boolean> isEmpty() { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 189 | final MeteringAgent.Context timer = monitor.startTimer(IS_EMPTY); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 190 | return database.mapIsEmpty(name) |
| 191 | .whenComplete((r, e) -> timer.stop()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public CompletableFuture<Boolean> containsKey(K key) { |
| 196 | checkNotNull(key, ERROR_NULL_KEY); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 197 | final MeteringAgent.Context timer = monitor.startTimer(CONTAINS_KEY); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 198 | return database.mapContainsKey(name, keyCache.getUnchecked(key)) |
| 199 | .whenComplete((r, e) -> timer.stop()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public CompletableFuture<Boolean> containsValue(V value) { |
| 204 | checkNotNull(value, ERROR_NULL_VALUE); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 205 | final MeteringAgent.Context timer = monitor.startTimer(CONTAINS_VALUE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 206 | return database.mapContainsValue(name, serializer.encode(value)) |
| 207 | .whenComplete((r, e) -> timer.stop()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public CompletableFuture<Versioned<V>> get(K key) { |
| 212 | checkNotNull(key, ERROR_NULL_KEY); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 213 | final MeteringAgent.Context timer = monitor.startTimer(GET); |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 214 | return database.mapGet(name, keyCache.getUnchecked(key)) |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 215 | .whenComplete((r, e) -> timer.stop()) |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 216 | .thenApply(v -> v != null ? v.map(serializer::decode) : null); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | @Override |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 220 | public CompletableFuture<Versioned<V>> computeIfAbsent(K key, |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 221 | Function<? super K, ? extends V> mappingFunction) { |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 222 | checkNotNull(key, ERROR_NULL_KEY); |
| 223 | checkNotNull(mappingFunction, "Mapping function cannot be null"); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 224 | final MeteringAgent.Context timer = monitor.startTimer(COMPUTE_IF_ABSENT); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 225 | return updateAndGet(key, Match.ifNull(), Match.any(), mappingFunction.apply(key)) |
| 226 | .whenComplete((r, e) -> timer.stop()) |
| 227 | .thenApply(v -> v.newValue()); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public CompletableFuture<Versioned<V>> computeIfPresent(K key, |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 232 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 233 | return computeIf(key, Objects::nonNull, remappingFunction); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public CompletableFuture<Versioned<V>> compute(K key, |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 238 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 239 | return computeIf(key, v -> true, remappingFunction); |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public CompletableFuture<Versioned<V>> computeIf(K key, |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 244 | Predicate<? super V> condition, |
| 245 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 246 | checkNotNull(key, ERROR_NULL_KEY); |
| 247 | checkNotNull(condition, "predicate function cannot be null"); |
| 248 | checkNotNull(remappingFunction, "Remapping function cannot be null"); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 249 | final MeteringAgent.Context timer = monitor.startTimer(COMPUTE_IF); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 250 | 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 Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 264 | if (computedValue.get() == null && r1 == null) { |
| 265 | return CompletableFuture.completedFuture(null); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 266 | } |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 267 | 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 Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 270 | .whenComplete((r, e) -> timer.stop()) |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 271 | .thenApply(v -> { |
| 272 | if (v.updated()) { |
| 273 | return v.newValue(); |
| 274 | } else { |
| 275 | throw new ConsistentMapException.ConcurrentModification(); |
| 276 | } |
| 277 | }); |
| 278 | }); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | @Override |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 282 | public CompletableFuture<Versioned<V>> put(K key, V value) { |
| 283 | checkNotNull(key, ERROR_NULL_KEY); |
| 284 | checkNotNull(value, ERROR_NULL_VALUE); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 285 | final MeteringAgent.Context timer = monitor.startTimer(PUT); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 286 | return updateAndGet(key, Match.any(), Match.any(), value).thenApply(v -> v.oldValue()) |
| 287 | .whenComplete((r, e) -> timer.stop()); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 288 | } |
| 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 Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 294 | final MeteringAgent.Context timer = monitor.startTimer(PUT_AND_GET); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 295 | return updateAndGet(key, Match.any(), Match.any(), value).thenApply(v -> v.newValue()) |
| 296 | .whenComplete((r, e) -> timer.stop()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public CompletableFuture<Versioned<V>> remove(K key) { |
| 301 | checkNotNull(key, ERROR_NULL_KEY); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 302 | final MeteringAgent.Context timer = monitor.startTimer(REMOVE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 303 | return updateAndGet(key, Match.any(), Match.any(), null).thenApply(v -> v.oldValue()) |
| 304 | .whenComplete((r, e) -> timer.stop()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public CompletableFuture<Void> clear() { |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 309 | checkIfUnmodifiable(); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 310 | final MeteringAgent.Context timer = monitor.startTimer(CLEAR); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 311 | return database.mapClear(name).thenApply(this::unwrapResult) |
| 312 | .whenComplete((r, e) -> timer.stop()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public CompletableFuture<Set<K>> keySet() { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 317 | final MeteringAgent.Context timer = monitor.startTimer(KEY_SET); |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 318 | return database.mapKeySet(name) |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 319 | .thenApply(s -> s |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 320 | .stream() |
| 321 | .map(this::dK) |
| 322 | .collect(Collectors.toSet())) |
| 323 | .whenComplete((r, e) -> timer.stop()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public CompletableFuture<Collection<Versioned<V>>> values() { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 328 | final MeteringAgent.Context timer = monitor.startTimer(VALUES); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 329 | 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 Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | @Override |
| 338 | public CompletableFuture<Set<Entry<K, Versioned<V>>>> entrySet() { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 339 | final MeteringAgent.Context timer = monitor.startTimer(ENTRY_SET); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 340 | 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 Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 346 | } |
| 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 Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 352 | final MeteringAgent.Context timer = monitor.startTimer(PUT_IF_ABSENT); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 353 | return updateAndGet(key, Match.ifNull(), Match.any(), value) |
| 354 | .whenComplete((r, e) -> timer.stop()) |
| 355 | .thenApply(v -> v.oldValue()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 356 | } |
| 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 Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 362 | final MeteringAgent.Context timer = monitor.startTimer(REMOVE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 363 | return updateAndGet(key, Match.ifValue(value), Match.any(), null) |
| 364 | .whenComplete((r, e) -> timer.stop()) |
| 365 | .thenApply(v -> v.updated()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public CompletableFuture<Boolean> remove(K key, long version) { |
| 370 | checkNotNull(key, ERROR_NULL_KEY); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 371 | final MeteringAgent.Context timer = monitor.startTimer(REMOVE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 372 | return updateAndGet(key, Match.any(), Match.ifValue(version), null) |
| 373 | .whenComplete((r, e) -> timer.stop()) |
| 374 | .thenApply(v -> v.updated()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | @Override |
| 378 | public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) { |
| 379 | checkNotNull(key, ERROR_NULL_KEY); |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 380 | checkNotNull(oldValue, ERROR_NULL_VALUE); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 381 | checkNotNull(newValue, ERROR_NULL_VALUE); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 382 | final MeteringAgent.Context timer = monitor.startTimer(REPLACE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 383 | return updateAndGet(key, Match.ifValue(oldValue), Match.any(), newValue) |
| 384 | .whenComplete((r, e) -> timer.stop()) |
| 385 | .thenApply(v -> v.updated()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue) { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 390 | final MeteringAgent.Context timer = monitor.startTimer(REPLACE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 391 | return updateAndGet(key, Match.any(), Match.ifValue(oldVersion), newValue) |
| 392 | .whenComplete((r, e) -> timer.stop()) |
| 393 | .thenApply(v -> v.updated()); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 396 | 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 Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 401 | Match<V> oldValueMatch, |
| 402 | Match<Long> oldVersionMatch, |
| 403 | V value) { |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 404 | checkIfUnmodifiable(); |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 405 | return database.mapUpdate(name, |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 406 | keyCache.getUnchecked(key), |
| 407 | oldValueMatch.map(serializer::encode), |
| 408 | oldVersionMatch, |
| 409 | value == null ? null : serializer.encode(value)) |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 410 | .thenApply(this::unwrapResult) |
Madan Jampani | 8566863 | 2015-08-10 10:46:40 -0700 | [diff] [blame] | 411 | .thenApply(r -> r.<K, V>map(this::dK, serializer::decode)); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 412 | } |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 413 | |
| 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 Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 423 | |
| 424 | private void checkIfUnmodifiable() { |
| 425 | if (readOnly) { |
| 426 | throw new UnsupportedOperationException(); |
| 427 | } |
| 428 | } |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 429 | |
| 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 Jampani | 8566863 | 2015-08-10 10:46:40 -0700 | [diff] [blame] | 441 | 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 Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 449 | } |
Madan Jampani | 8566863 | 2015-08-10 10:46:40 -0700 | [diff] [blame] | 450 | }); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 451 | } |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 452 | |
Madan Jampani | bab51a4 | 2015-08-10 13:53:35 -0700 | [diff] [blame] | 453 | } |