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 | |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 35 | import java.util.Collection; |
| 36 | import java.util.Map; |
| 37 | import java.util.Map.Entry; |
| 38 | import java.util.Objects; |
| 39 | import java.util.Set; |
| 40 | import java.util.concurrent.CompletableFuture; |
| 41 | import java.util.concurrent.CopyOnWriteArraySet; |
| 42 | import java.util.concurrent.atomic.AtomicReference; |
| 43 | import java.util.function.BiFunction; |
| 44 | import java.util.function.Function; |
| 45 | import java.util.function.Predicate; |
| 46 | import java.util.stream.Collectors; |
| 47 | |
| 48 | import static com.google.common.base.Preconditions.checkNotNull; |
Madan Jampani | a6d787b | 2015-08-11 11:02:02 -0700 | [diff] [blame] | 49 | import static org.onosproject.store.consistent.impl.StateMachineUpdate.Target.MAP_UPDATE; |
Madan Jampani | bab51a4 | 2015-08-10 13:53:35 -0700 | [diff] [blame] | 50 | import static org.onosproject.store.consistent.impl.StateMachineUpdate.Target.TX_COMMIT; |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 51 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 52 | |
| 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 Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 60 | public class DefaultAsyncConsistentMap<K, V> implements AsyncConsistentMap<K, V> { |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 61 | |
| 62 | private final String name; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 63 | private final ApplicationId applicationId; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 64 | private final Database database; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 65 | private final Serializer serializer; |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 66 | private final boolean readOnly; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 67 | private final boolean purgeOnUninstall; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 68 | |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 69 | private static final String PRIMITIVE_NAME = "consistentMap"; |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 70 | 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 Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 87 | private final Set<MapEventListener<K, V>> listeners = new CopyOnWriteArraySet<>(); |
| 88 | |
| 89 | private final Logger log = getLogger(getClass()); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 90 | private final MeteringAgent monitor; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 91 | |
| 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 Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 110 | ApplicationId applicationId, |
| 111 | Database database, |
| 112 | Serializer serializer, |
| 113 | boolean readOnly, |
| 114 | boolean purgeOnUninstall, |
| 115 | boolean meteringEnabled) { |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 116 | this.name = checkNotNull(name, "map name cannot be null"); |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 117 | this.applicationId = applicationId; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 118 | this.database = checkNotNull(database, "database cannot be null"); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 119 | this.serializer = checkNotNull(serializer, "serializer cannot be null"); |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 120 | this.readOnly = readOnly; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 121 | this.purgeOnUninstall = purgeOnUninstall; |
Madan Jampani | 648451f | 2015-07-21 22:09:05 -0700 | [diff] [blame] | 122 | this.database.registerConsumer(update -> { |
| 123 | SharedExecutors.getSingleThreadExecutor().execute(() -> { |
Madan Jampani | 9eb55d1 | 2015-08-14 07:47:56 -0700 | [diff] [blame] | 124 | 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 Jampani | 648451f | 2015-07-21 22:09:05 -0700 | [diff] [blame] | 150 | } |
Madan Jampani | 9eb55d1 | 2015-08-14 07:47:56 -0700 | [diff] [blame] | 151 | } catch (Exception e) { |
| 152 | log.warn("Error notifying listeners", e); |
Madan Jampani | 648451f | 2015-07-21 22:09:05 -0700 | [diff] [blame] | 153 | } |
| 154 | }); |
| 155 | }); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 156 | this.monitor = new MeteringAgent(PRIMITIVE_NAME, name, meteringEnabled); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 157 | } |
| 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 Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 175 | /** |
| 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 Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 192 | @Override |
| 193 | public CompletableFuture<Integer> size() { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 194 | final MeteringAgent.Context timer = monitor.startTimer(SIZE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 195 | return database.mapSize(name) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 196 | .whenComplete((r, e) -> timer.stop(e)); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public CompletableFuture<Boolean> isEmpty() { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 201 | final MeteringAgent.Context timer = monitor.startTimer(IS_EMPTY); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 202 | return database.mapIsEmpty(name) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 203 | .whenComplete((r, e) -> timer.stop(e)); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public CompletableFuture<Boolean> containsKey(K key) { |
| 208 | checkNotNull(key, ERROR_NULL_KEY); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 209 | final MeteringAgent.Context timer = monitor.startTimer(CONTAINS_KEY); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 210 | return database.mapContainsKey(name, keyCache.getUnchecked(key)) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 211 | .whenComplete((r, e) -> timer.stop(e)); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public CompletableFuture<Boolean> containsValue(V value) { |
| 216 | checkNotNull(value, ERROR_NULL_VALUE); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 217 | final MeteringAgent.Context timer = monitor.startTimer(CONTAINS_VALUE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 218 | return database.mapContainsValue(name, serializer.encode(value)) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 219 | .whenComplete((r, e) -> timer.stop(e)); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public CompletableFuture<Versioned<V>> get(K key) { |
| 224 | checkNotNull(key, ERROR_NULL_KEY); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 225 | final MeteringAgent.Context timer = monitor.startTimer(GET); |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 226 | return database.mapGet(name, keyCache.getUnchecked(key)) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 227 | .whenComplete((r, e) -> timer.stop(e)) |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 228 | .thenApply(v -> v != null ? v.map(serializer::decode) : null); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | @Override |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 232 | public CompletableFuture<Versioned<V>> computeIfAbsent(K key, |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 233 | Function<? super K, ? extends V> mappingFunction) { |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 234 | checkNotNull(key, ERROR_NULL_KEY); |
| 235 | checkNotNull(mappingFunction, "Mapping function cannot be null"); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 236 | final MeteringAgent.Context timer = monitor.startTimer(COMPUTE_IF_ABSENT); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 237 | return updateAndGet(key, Match.ifNull(), Match.any(), mappingFunction.apply(key)) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 238 | .whenComplete((r, e) -> timer.stop(e)) |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 239 | .thenApply(v -> v.newValue()); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public CompletableFuture<Versioned<V>> computeIfPresent(K key, |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 244 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 245 | return computeIf(key, Objects::nonNull, remappingFunction); |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public CompletableFuture<Versioned<V>> compute(K key, |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 250 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 251 | return computeIf(key, v -> true, remappingFunction); |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public CompletableFuture<Versioned<V>> computeIf(K key, |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 256 | Predicate<? super V> condition, |
| 257 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 258 | checkNotNull(key, ERROR_NULL_KEY); |
| 259 | checkNotNull(condition, "predicate function cannot be null"); |
| 260 | checkNotNull(remappingFunction, "Remapping function cannot be null"); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 261 | final MeteringAgent.Context timer = monitor.startTimer(COMPUTE_IF); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 262 | 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 Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 276 | if (computedValue.get() == null && r1 == null) { |
| 277 | return CompletableFuture.completedFuture(null); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 278 | } |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 279 | 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 Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 282 | .whenComplete((r, e) -> timer.stop(e)) |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 283 | .thenApply(v -> { |
| 284 | if (v.updated()) { |
| 285 | return v.newValue(); |
| 286 | } else { |
| 287 | throw new ConsistentMapException.ConcurrentModification(); |
| 288 | } |
| 289 | }); |
| 290 | }); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | @Override |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 294 | public CompletableFuture<Versioned<V>> put(K key, V value) { |
| 295 | checkNotNull(key, ERROR_NULL_KEY); |
| 296 | checkNotNull(value, ERROR_NULL_VALUE); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 297 | final MeteringAgent.Context timer = monitor.startTimer(PUT); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 298 | return updateAndGet(key, Match.any(), Match.any(), value).thenApply(v -> v.oldValue()) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 299 | .whenComplete((r, e) -> timer.stop(e)); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 300 | } |
| 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 Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 306 | final MeteringAgent.Context timer = monitor.startTimer(PUT_AND_GET); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 307 | return updateAndGet(key, Match.any(), Match.any(), value).thenApply(v -> v.newValue()) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 308 | .whenComplete((r, e) -> timer.stop(e)); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public CompletableFuture<Versioned<V>> remove(K key) { |
| 313 | checkNotNull(key, ERROR_NULL_KEY); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 314 | final MeteringAgent.Context timer = monitor.startTimer(REMOVE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 315 | return updateAndGet(key, Match.any(), Match.any(), null).thenApply(v -> v.oldValue()) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 316 | .whenComplete((r, e) -> timer.stop(e)); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | @Override |
| 320 | public CompletableFuture<Void> clear() { |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 321 | checkIfUnmodifiable(); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 322 | final MeteringAgent.Context timer = monitor.startTimer(CLEAR); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 323 | return database.mapClear(name).thenApply(this::unwrapResult) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 324 | .whenComplete((r, e) -> timer.stop(e)); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | @Override |
| 328 | public CompletableFuture<Set<K>> keySet() { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 329 | final MeteringAgent.Context timer = monitor.startTimer(KEY_SET); |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 330 | return database.mapKeySet(name) |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 331 | .thenApply(s -> s |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 332 | .stream() |
| 333 | .map(this::dK) |
| 334 | .collect(Collectors.toSet())) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 335 | .whenComplete((r, e) -> timer.stop(e)); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public CompletableFuture<Collection<Versioned<V>>> values() { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 340 | final MeteringAgent.Context timer = monitor.startTimer(VALUES); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 341 | return database.mapValues(name) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 342 | .whenComplete((r, e) -> timer.stop(e)) |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 343 | .thenApply(c -> c |
| 344 | .stream() |
| 345 | .map(v -> v.<V>map(serializer::decode)) |
| 346 | .collect(Collectors.toList())); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | @Override |
| 350 | public CompletableFuture<Set<Entry<K, Versioned<V>>>> entrySet() { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 351 | final MeteringAgent.Context timer = monitor.startTimer(ENTRY_SET); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 352 | return database.mapEntrySet(name) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 353 | .whenComplete((r, e) -> timer.stop(e)) |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 354 | .thenApply(s -> s |
| 355 | .stream() |
| 356 | .map(this::mapRawEntry) |
| 357 | .collect(Collectors.toSet())); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 358 | } |
| 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 Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 364 | final MeteringAgent.Context timer = monitor.startTimer(PUT_IF_ABSENT); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 365 | return updateAndGet(key, Match.ifNull(), Match.any(), value) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 366 | .whenComplete((r, e) -> timer.stop(e)) |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 367 | .thenApply(v -> v.oldValue()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 368 | } |
| 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 Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 374 | final MeteringAgent.Context timer = monitor.startTimer(REMOVE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 375 | return updateAndGet(key, Match.ifValue(value), Match.any(), null) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 376 | .whenComplete((r, e) -> timer.stop(e)) |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 377 | .thenApply(v -> v.updated()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | @Override |
| 381 | public CompletableFuture<Boolean> remove(K key, long version) { |
| 382 | checkNotNull(key, ERROR_NULL_KEY); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 383 | final MeteringAgent.Context timer = monitor.startTimer(REMOVE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 384 | return updateAndGet(key, Match.any(), Match.ifValue(version), null) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 385 | .whenComplete((r, e) -> timer.stop(e)) |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 386 | .thenApply(v -> v.updated()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | @Override |
| 390 | public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) { |
| 391 | checkNotNull(key, ERROR_NULL_KEY); |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 392 | checkNotNull(oldValue, ERROR_NULL_VALUE); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 393 | checkNotNull(newValue, ERROR_NULL_VALUE); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 394 | final MeteringAgent.Context timer = monitor.startTimer(REPLACE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 395 | return updateAndGet(key, Match.ifValue(oldValue), Match.any(), newValue) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 396 | .whenComplete((r, e) -> timer.stop(e)) |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 397 | .thenApply(v -> v.updated()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | @Override |
| 401 | public CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue) { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 402 | final MeteringAgent.Context timer = monitor.startTimer(REPLACE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 403 | return updateAndGet(key, Match.any(), Match.ifValue(oldVersion), newValue) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 404 | .whenComplete((r, e) -> timer.stop(e)) |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 405 | .thenApply(v -> v.updated()); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Madan Jampani | 14a38da | 2015-11-16 11:29:00 -0800 | [diff] [blame] | 408 | /** |
| 409 | * Pre-update hook for performing required checks/actions before going forward with an update operation. |
| 410 | * @param key map key. |
| 411 | */ |
| 412 | protected void beforeUpdate(K key) { |
| 413 | checkIfUnmodifiable(); |
| 414 | } |
| 415 | |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 416 | private Map.Entry<K, Versioned<V>> mapRawEntry(Map.Entry<String, Versioned<byte[]>> e) { |
| 417 | return Maps.immutableEntry(dK(e.getKey()), e.getValue().<V>map(serializer::decode)); |
| 418 | } |
| 419 | |
| 420 | private CompletableFuture<UpdateResult<K, V>> updateAndGet(K key, |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 421 | Match<V> oldValueMatch, |
| 422 | Match<Long> oldVersionMatch, |
| 423 | V value) { |
Madan Jampani | 14a38da | 2015-11-16 11:29:00 -0800 | [diff] [blame] | 424 | beforeUpdate(key); |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 425 | return database.mapUpdate(name, |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 426 | keyCache.getUnchecked(key), |
| 427 | oldValueMatch.map(serializer::encode), |
| 428 | oldVersionMatch, |
| 429 | value == null ? null : serializer.encode(value)) |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 430 | .thenApply(this::unwrapResult) |
Madan Jampani | 8566863 | 2015-08-10 10:46:40 -0700 | [diff] [blame] | 431 | .thenApply(r -> r.<K, V>map(this::dK, serializer::decode)); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 432 | } |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 433 | |
| 434 | private <T> T unwrapResult(Result<T> result) { |
| 435 | if (result.status() == Result.Status.LOCKED) { |
| 436 | throw new ConsistentMapException.ConcurrentModification(); |
| 437 | } else if (result.success()) { |
| 438 | return result.value(); |
| 439 | } else { |
| 440 | throw new IllegalStateException("Must not be here"); |
| 441 | } |
| 442 | } |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 443 | |
| 444 | private void checkIfUnmodifiable() { |
| 445 | if (readOnly) { |
| 446 | throw new UnsupportedOperationException(); |
| 447 | } |
| 448 | } |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 449 | |
| 450 | @Override |
| 451 | public void addListener(MapEventListener<K, V> listener) { |
| 452 | listeners.add(listener); |
| 453 | } |
| 454 | |
| 455 | @Override |
| 456 | public void removeListener(MapEventListener<K, V> listener) { |
| 457 | listeners.remove(listener); |
| 458 | } |
| 459 | |
| 460 | protected void notifyListeners(MapEvent<K, V> event) { |
Madan Jampani | 8566863 | 2015-08-10 10:46:40 -0700 | [diff] [blame] | 461 | if (event == null) { |
| 462 | return; |
| 463 | } |
| 464 | listeners.forEach(listener -> { |
| 465 | try { |
| 466 | listener.event(event); |
| 467 | } catch (Exception e) { |
| 468 | log.warn("Failure notifying listener about {}", event, e); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 469 | } |
Madan Jampani | 8566863 | 2015-08-10 10:46:40 -0700 | [diff] [blame] | 470 | }); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 471 | } |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 472 | |
Madan Jampani | bab51a4 | 2015-08-10 13:53:35 -0700 | [diff] [blame] | 473 | } |