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 | |
Madan Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 17 | package org.onosproject.store.primitives.impl; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 18 | |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | import static org.onosproject.store.primitives.impl.StateMachineUpdate.Target.MAP_UPDATE; |
| 21 | import static org.onosproject.store.primitives.impl.StateMachineUpdate.Target.TX_COMMIT; |
| 22 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 23 | |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 24 | import java.util.Collection; |
HIGUCHI Yuta | 14e865d | 2015-11-25 20:42:32 -0800 | [diff] [blame] | 25 | import java.util.Collections; |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 26 | import java.util.Map; |
| 27 | import java.util.Map.Entry; |
| 28 | import java.util.Objects; |
| 29 | import java.util.Set; |
| 30 | import java.util.concurrent.CompletableFuture; |
| 31 | import java.util.concurrent.CopyOnWriteArraySet; |
| 32 | import java.util.concurrent.atomic.AtomicReference; |
| 33 | import java.util.function.BiFunction; |
| 34 | import java.util.function.Function; |
| 35 | import java.util.function.Predicate; |
| 36 | import java.util.stream.Collectors; |
| 37 | |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 38 | import org.onlab.util.HexString; |
| 39 | import org.onlab.util.Match; |
| 40 | import org.onlab.util.SharedExecutors; |
| 41 | import org.onlab.util.Tools; |
| 42 | import org.onosproject.core.ApplicationId; |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 43 | import org.onosproject.store.primitives.TransactionId; |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 44 | import org.onosproject.store.service.AsyncConsistentMap; |
| 45 | import org.onosproject.store.service.ConsistentMapException; |
| 46 | import org.onosproject.store.service.ConsistentMapException.ConcurrentModification; |
| 47 | import org.onosproject.store.service.MapEvent; |
| 48 | import org.onosproject.store.service.MapEventListener; |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 49 | import org.onosproject.store.service.MapTransaction; |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 50 | import org.onosproject.store.service.Serializer; |
| 51 | import org.onosproject.store.service.Versioned; |
Aaron Kruglikov | 1110b2c | 2016-02-02 16:24:37 -0800 | [diff] [blame] | 52 | import org.onosproject.utils.MeteringAgent; |
Madan Jampani | fa24218 | 2016-01-22 13:42:54 -0800 | [diff] [blame] | 53 | import org.slf4j.Logger; |
| 54 | |
| 55 | import com.google.common.cache.CacheBuilder; |
| 56 | import com.google.common.cache.CacheLoader; |
| 57 | import com.google.common.cache.LoadingCache; |
| 58 | import com.google.common.collect.Maps; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * AsyncConsistentMap implementation that is backed by a Raft consensus |
| 62 | * based database. |
| 63 | * |
| 64 | * @param <K> type of key. |
| 65 | * @param <V> type of value. |
| 66 | */ |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 67 | public class DefaultAsyncConsistentMap<K, V> implements AsyncConsistentMap<K, V> { |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 68 | |
| 69 | private final String name; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 70 | private final ApplicationId applicationId; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 71 | private final Database database; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 72 | private final Serializer serializer; |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 73 | private final boolean readOnly; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 74 | private final boolean purgeOnUninstall; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 75 | |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 76 | private static final String PRIMITIVE_NAME = "consistentMap"; |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 77 | private static final String SIZE = "size"; |
| 78 | private static final String IS_EMPTY = "isEmpty"; |
| 79 | private static final String CONTAINS_KEY = "containsKey"; |
| 80 | private static final String CONTAINS_VALUE = "containsValue"; |
| 81 | private static final String GET = "get"; |
| 82 | private static final String COMPUTE_IF = "computeIf"; |
| 83 | private static final String PUT = "put"; |
| 84 | private static final String PUT_AND_GET = "putAndGet"; |
| 85 | private static final String PUT_IF_ABSENT = "putIfAbsent"; |
| 86 | private static final String REMOVE = "remove"; |
| 87 | private static final String CLEAR = "clear"; |
| 88 | private static final String KEY_SET = "keySet"; |
| 89 | private static final String VALUES = "values"; |
| 90 | private static final String ENTRY_SET = "entrySet"; |
| 91 | private static final String REPLACE = "replace"; |
| 92 | private static final String COMPUTE_IF_ABSENT = "computeIfAbsent"; |
| 93 | |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 94 | private final Set<MapEventListener<K, V>> listeners = new CopyOnWriteArraySet<>(); |
| 95 | |
| 96 | private final Logger log = getLogger(getClass()); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 97 | private final MeteringAgent monitor; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 98 | |
| 99 | private static final String ERROR_NULL_KEY = "Key cannot be null"; |
| 100 | private static final String ERROR_NULL_VALUE = "Null values are not allowed"; |
| 101 | |
HIGUCHI Yuta | 14e865d | 2015-11-25 20:42:32 -0800 | [diff] [blame] | 102 | // String representation of serialized byte[] -> original key Object |
| 103 | private final LoadingCache<String, K> keyCache = CacheBuilder.newBuilder() |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 104 | .softValues() |
HIGUCHI Yuta | 14e865d | 2015-11-25 20:42:32 -0800 | [diff] [blame] | 105 | .build(new CacheLoader<String, K>() { |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 106 | |
| 107 | @Override |
HIGUCHI Yuta | 14e865d | 2015-11-25 20:42:32 -0800 | [diff] [blame] | 108 | public K load(String key) { |
| 109 | return serializer.decode(HexString.fromHexString(key)); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 110 | } |
| 111 | }); |
| 112 | |
HIGUCHI Yuta | 14e865d | 2015-11-25 20:42:32 -0800 | [diff] [blame] | 113 | protected String sK(K key) { |
| 114 | String s = HexString.toHexString(serializer.encode(key)); |
| 115 | keyCache.put(s, key); |
| 116 | return s; |
| 117 | } |
| 118 | |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 119 | protected K dK(String key) { |
HIGUCHI Yuta | 14e865d | 2015-11-25 20:42:32 -0800 | [diff] [blame] | 120 | return keyCache.getUnchecked(key); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | public DefaultAsyncConsistentMap(String name, |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 124 | ApplicationId applicationId, |
| 125 | Database database, |
| 126 | Serializer serializer, |
| 127 | boolean readOnly, |
| 128 | boolean purgeOnUninstall, |
| 129 | boolean meteringEnabled) { |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 130 | this.name = checkNotNull(name, "map name cannot be null"); |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 131 | this.applicationId = applicationId; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 132 | this.database = checkNotNull(database, "database cannot be null"); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 133 | this.serializer = checkNotNull(serializer, "serializer cannot be null"); |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 134 | this.readOnly = readOnly; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 135 | this.purgeOnUninstall = purgeOnUninstall; |
Madan Jampani | 648451f | 2015-07-21 22:09:05 -0700 | [diff] [blame] | 136 | this.database.registerConsumer(update -> { |
| 137 | SharedExecutors.getSingleThreadExecutor().execute(() -> { |
Madan Jampani | 9eb55d1 | 2015-08-14 07:47:56 -0700 | [diff] [blame] | 138 | if (listeners.isEmpty()) { |
| 139 | return; |
| 140 | } |
| 141 | try { |
| 142 | if (update.target() == MAP_UPDATE) { |
| 143 | Result<UpdateResult<String, byte[]>> result = update.output(); |
| 144 | if (result.success() && result.value().mapName().equals(name)) { |
| 145 | MapEvent<K, V> mapEvent = result.value() |
| 146 | .<K, V>map(this::dK, |
| 147 | v -> serializer.decode(Tools.copyOf(v))) |
| 148 | .toMapEvent(); |
| 149 | notifyListeners(mapEvent); |
| 150 | } |
| 151 | } else if (update.target() == TX_COMMIT) { |
| 152 | CommitResponse response = update.output(); |
| 153 | if (response.success()) { |
| 154 | response.updates().forEach(u -> { |
| 155 | if (u.mapName().equals(name)) { |
| 156 | MapEvent<K, V> mapEvent = |
| 157 | u.<K, V>map(this::dK, |
| 158 | v -> serializer.decode(Tools.copyOf(v))) |
| 159 | .toMapEvent(); |
| 160 | notifyListeners(mapEvent); |
| 161 | } |
| 162 | }); |
| 163 | } |
Madan Jampani | 648451f | 2015-07-21 22:09:05 -0700 | [diff] [blame] | 164 | } |
Madan Jampani | 9eb55d1 | 2015-08-14 07:47:56 -0700 | [diff] [blame] | 165 | } catch (Exception e) { |
| 166 | log.warn("Error notifying listeners", e); |
Madan Jampani | 648451f | 2015-07-21 22:09:05 -0700 | [diff] [blame] | 167 | } |
| 168 | }); |
| 169 | }); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 170 | this.monitor = new MeteringAgent(PRIMITIVE_NAME, name, meteringEnabled); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Returns this map name. |
| 175 | * @return map name |
| 176 | */ |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 177 | @Override |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 178 | public String name() { |
| 179 | return name; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Returns the serializer for map entries. |
| 184 | * @return map entry serializer |
| 185 | */ |
| 186 | public Serializer serializer() { |
| 187 | return serializer; |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 190 | /** |
| 191 | * Returns the applicationId owning this map. |
| 192 | * @return application Id |
| 193 | */ |
Madan Jampani | a090a11 | 2016-01-18 16:38:17 -0800 | [diff] [blame] | 194 | @Override |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 195 | public ApplicationId applicationId() { |
| 196 | return applicationId; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Returns whether the map entries should be purged when the application |
| 201 | * owning it is uninstalled. |
| 202 | * @return true is map needs to cleared on app uninstall; false otherwise |
| 203 | */ |
| 204 | public boolean purgeOnUninstall() { |
| 205 | return purgeOnUninstall; |
| 206 | } |
| 207 | |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 208 | @Override |
| 209 | public CompletableFuture<Integer> size() { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 210 | final MeteringAgent.Context timer = monitor.startTimer(SIZE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 211 | return database.mapSize(name) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 212 | .whenComplete((r, e) -> timer.stop(e)); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public CompletableFuture<Boolean> isEmpty() { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 217 | final MeteringAgent.Context timer = monitor.startTimer(IS_EMPTY); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 218 | return database.mapIsEmpty(name) |
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<Boolean> containsKey(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(CONTAINS_KEY); |
HIGUCHI Yuta | 14e865d | 2015-11-25 20:42:32 -0800 | [diff] [blame] | 226 | return database.mapContainsKey(name, sK(key)) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 227 | .whenComplete((r, e) -> timer.stop(e)); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public CompletableFuture<Boolean> containsValue(V value) { |
| 232 | checkNotNull(value, ERROR_NULL_VALUE); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 233 | final MeteringAgent.Context timer = monitor.startTimer(CONTAINS_VALUE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 234 | return database.mapContainsValue(name, serializer.encode(value)) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 235 | .whenComplete((r, e) -> timer.stop(e)); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public CompletableFuture<Versioned<V>> get(K key) { |
| 240 | checkNotNull(key, ERROR_NULL_KEY); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 241 | final MeteringAgent.Context timer = monitor.startTimer(GET); |
HIGUCHI Yuta | 14e865d | 2015-11-25 20:42:32 -0800 | [diff] [blame] | 242 | return database.mapGet(name, sK(key)) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 243 | .whenComplete((r, e) -> timer.stop(e)) |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 244 | .thenApply(v -> v != null ? v.map(serializer::decode) : null); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | @Override |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 248 | public CompletableFuture<Versioned<V>> computeIfAbsent(K key, |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 249 | Function<? super K, ? extends V> mappingFunction) { |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 250 | checkNotNull(key, ERROR_NULL_KEY); |
| 251 | checkNotNull(mappingFunction, "Mapping function cannot be null"); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 252 | final MeteringAgent.Context timer = monitor.startTimer(COMPUTE_IF_ABSENT); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 253 | return updateAndGet(key, Match.ifNull(), Match.any(), mappingFunction.apply(key)) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 254 | .whenComplete((r, e) -> timer.stop(e)) |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 255 | .thenApply(v -> v.newValue()); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public CompletableFuture<Versioned<V>> computeIfPresent(K key, |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 260 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 261 | return computeIf(key, Objects::nonNull, remappingFunction); |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public CompletableFuture<Versioned<V>> compute(K key, |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 266 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 267 | return computeIf(key, v -> true, remappingFunction); |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public CompletableFuture<Versioned<V>> computeIf(K key, |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 272 | Predicate<? super V> condition, |
| 273 | BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 274 | checkNotNull(key, ERROR_NULL_KEY); |
| 275 | checkNotNull(condition, "predicate function cannot be null"); |
| 276 | checkNotNull(remappingFunction, "Remapping function cannot be null"); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 277 | final MeteringAgent.Context timer = monitor.startTimer(COMPUTE_IF); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 278 | return get(key).thenCompose(r1 -> { |
| 279 | V existingValue = r1 == null ? null : r1.value(); |
| 280 | // if the condition evaluates to false, return existing value. |
| 281 | if (!condition.test(existingValue)) { |
| 282 | return CompletableFuture.completedFuture(r1); |
| 283 | } |
| 284 | |
| 285 | AtomicReference<V> computedValue = new AtomicReference<>(); |
| 286 | // if remappingFunction throws an exception, return the exception. |
| 287 | try { |
| 288 | computedValue.set(remappingFunction.apply(key, existingValue)); |
| 289 | } catch (Exception e) { |
| 290 | return Tools.exceptionalFuture(e); |
| 291 | } |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 292 | if (computedValue.get() == null && r1 == null) { |
| 293 | return CompletableFuture.completedFuture(null); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 294 | } |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 295 | Match<V> valueMatcher = r1 == null ? Match.ifNull() : Match.any(); |
| 296 | Match<Long> versionMatcher = r1 == null ? Match.any() : Match.ifValue(r1.version()); |
| 297 | return updateAndGet(key, valueMatcher, versionMatcher, computedValue.get()) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 298 | .whenComplete((r, e) -> timer.stop(e)) |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 299 | .thenApply(v -> { |
| 300 | if (v.updated()) { |
| 301 | return v.newValue(); |
| 302 | } else { |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 303 | throw new ConcurrentModification("Concurrent update to " + name + " detected"); |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 304 | } |
| 305 | }); |
| 306 | }); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | @Override |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 310 | public CompletableFuture<Versioned<V>> put(K key, V value) { |
| 311 | checkNotNull(key, ERROR_NULL_KEY); |
| 312 | checkNotNull(value, ERROR_NULL_VALUE); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 313 | final MeteringAgent.Context timer = monitor.startTimer(PUT); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 314 | return updateAndGet(key, Match.any(), Match.any(), value).thenApply(v -> v.oldValue()) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 315 | .whenComplete((r, e) -> timer.stop(e)); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public CompletableFuture<Versioned<V>> putAndGet(K key, V value) { |
| 320 | checkNotNull(key, ERROR_NULL_KEY); |
| 321 | checkNotNull(value, ERROR_NULL_VALUE); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 322 | final MeteringAgent.Context timer = monitor.startTimer(PUT_AND_GET); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 323 | return updateAndGet(key, Match.any(), Match.any(), value).thenApply(v -> v.newValue()) |
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<Versioned<V>> remove(K key) { |
| 329 | checkNotNull(key, ERROR_NULL_KEY); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 330 | final MeteringAgent.Context timer = monitor.startTimer(REMOVE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 331 | return updateAndGet(key, Match.any(), Match.any(), null).thenApply(v -> v.oldValue()) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 332 | .whenComplete((r, e) -> timer.stop(e)); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | @Override |
| 336 | public CompletableFuture<Void> clear() { |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 337 | checkIfUnmodifiable(); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 338 | final MeteringAgent.Context timer = monitor.startTimer(CLEAR); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 339 | return database.mapClear(name).thenApply(this::unwrapResult) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 340 | .whenComplete((r, e) -> timer.stop(e)); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public CompletableFuture<Set<K>> keySet() { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 345 | final MeteringAgent.Context timer = monitor.startTimer(KEY_SET); |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 346 | return database.mapKeySet(name) |
HIGUCHI Yuta | 14e865d | 2015-11-25 20:42:32 -0800 | [diff] [blame] | 347 | .thenApply(s -> newMappingKeySet(s)) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 348 | .whenComplete((r, e) -> timer.stop(e)); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | @Override |
| 352 | public CompletableFuture<Collection<Versioned<V>>> values() { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 353 | final MeteringAgent.Context timer = monitor.startTimer(VALUES); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 354 | return database.mapValues(name) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 355 | .whenComplete((r, e) -> timer.stop(e)) |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 356 | .thenApply(c -> c |
| 357 | .stream() |
| 358 | .map(v -> v.<V>map(serializer::decode)) |
| 359 | .collect(Collectors.toList())); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | @Override |
| 363 | public CompletableFuture<Set<Entry<K, Versioned<V>>>> entrySet() { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 364 | final MeteringAgent.Context timer = monitor.startTimer(ENTRY_SET); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 365 | return database.mapEntrySet(name) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 366 | .whenComplete((r, e) -> timer.stop(e)) |
HIGUCHI Yuta | 14e865d | 2015-11-25 20:42:32 -0800 | [diff] [blame] | 367 | .thenApply(s -> newMappingEntrySet(s)); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | @Override |
| 371 | public CompletableFuture<Versioned<V>> putIfAbsent(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(PUT_IF_ABSENT); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 375 | return updateAndGet(key, Match.ifNull(), Match.any(), value) |
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.oldValue()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | @Override |
| 381 | public CompletableFuture<Boolean> remove(K key, V value) { |
| 382 | checkNotNull(key, ERROR_NULL_KEY); |
| 383 | checkNotNull(value, ERROR_NULL_VALUE); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 384 | final MeteringAgent.Context timer = monitor.startTimer(REMOVE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 385 | return updateAndGet(key, Match.ifValue(value), Match.any(), null) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 386 | .whenComplete((r, e) -> timer.stop(e)) |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 387 | .thenApply(v -> v.updated()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | @Override |
| 391 | public CompletableFuture<Boolean> remove(K key, long version) { |
| 392 | checkNotNull(key, ERROR_NULL_KEY); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 393 | final MeteringAgent.Context timer = monitor.startTimer(REMOVE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 394 | return updateAndGet(key, Match.any(), Match.ifValue(version), null) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 395 | .whenComplete((r, e) -> timer.stop(e)) |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 396 | .thenApply(v -> v.updated()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | @Override |
Jihwan Kim | 9887ad9 | 2015-12-12 00:23:57 +0900 | [diff] [blame] | 400 | public CompletableFuture<Versioned<V>> replace(K key, V value) { |
| 401 | checkNotNull(key, ERROR_NULL_KEY); |
| 402 | checkNotNull(value, ERROR_NULL_VALUE); |
| 403 | final MeteringAgent.Context timer = monitor.startTimer(REPLACE); |
| 404 | return updateAndGet(key, Match.ifNotNull(), Match.any(), value) |
| 405 | .whenComplete((r, e) -> timer.stop(e)) |
| 406 | .thenApply(v -> v.oldValue()); |
| 407 | } |
| 408 | |
| 409 | @Override |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 410 | public CompletableFuture<Boolean> replace(K key, V oldValue, V newValue) { |
| 411 | checkNotNull(key, ERROR_NULL_KEY); |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 412 | checkNotNull(oldValue, ERROR_NULL_VALUE); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 413 | checkNotNull(newValue, ERROR_NULL_VALUE); |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 414 | final MeteringAgent.Context timer = monitor.startTimer(REPLACE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 415 | return updateAndGet(key, Match.ifValue(oldValue), Match.any(), newValue) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 416 | .whenComplete((r, e) -> timer.stop(e)) |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 417 | .thenApply(v -> v.updated()); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | @Override |
| 421 | public CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue) { |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 422 | final MeteringAgent.Context timer = monitor.startTimer(REPLACE); |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 423 | return updateAndGet(key, Match.any(), Match.ifValue(oldVersion), newValue) |
Flavio Castro | 6e04461 | 2015-08-13 14:13:58 -0700 | [diff] [blame] | 424 | .whenComplete((r, e) -> timer.stop(e)) |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 425 | .thenApply(v -> v.updated()); |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Madan Jampani | 14a38da | 2015-11-16 11:29:00 -0800 | [diff] [blame] | 428 | /** |
| 429 | * Pre-update hook for performing required checks/actions before going forward with an update operation. |
| 430 | * @param key map key. |
| 431 | */ |
| 432 | protected void beforeUpdate(K key) { |
| 433 | checkIfUnmodifiable(); |
| 434 | } |
| 435 | |
HIGUCHI Yuta | 14e865d | 2015-11-25 20:42:32 -0800 | [diff] [blame] | 436 | private Set<K> newMappingKeySet(Set<String> s) { |
| 437 | return new MappingSet<>(s, Collections::unmodifiableSet, |
| 438 | this::sK, this::dK); |
| 439 | } |
| 440 | |
| 441 | private Set<Entry<K, Versioned<V>>> newMappingEntrySet(Set<Entry<String, Versioned<byte[]>>> s) { |
| 442 | return new MappingSet<>(s, Collections::unmodifiableSet, |
| 443 | this::reverseMapRawEntry, this::mapRawEntry); |
| 444 | } |
| 445 | |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 446 | private Map.Entry<K, Versioned<V>> mapRawEntry(Map.Entry<String, Versioned<byte[]>> e) { |
| 447 | return Maps.immutableEntry(dK(e.getKey()), e.getValue().<V>map(serializer::decode)); |
| 448 | } |
| 449 | |
HIGUCHI Yuta | 14e865d | 2015-11-25 20:42:32 -0800 | [diff] [blame] | 450 | private Map.Entry<String, Versioned<byte[]>> reverseMapRawEntry(Map.Entry<K, Versioned<V>> e) { |
| 451 | return Maps.immutableEntry(sK(e.getKey()), e.getValue().map(serializer::encode)); |
| 452 | } |
| 453 | |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 454 | private CompletableFuture<UpdateResult<K, V>> updateAndGet(K key, |
Flavio Castro | 41b1f3a | 2015-07-31 13:51:32 -0700 | [diff] [blame] | 455 | Match<V> oldValueMatch, |
| 456 | Match<Long> oldVersionMatch, |
| 457 | V value) { |
Madan Jampani | 14a38da | 2015-11-16 11:29:00 -0800 | [diff] [blame] | 458 | beforeUpdate(key); |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 459 | return database.mapUpdate(name, |
HIGUCHI Yuta | 14e865d | 2015-11-25 20:42:32 -0800 | [diff] [blame] | 460 | sK(key), |
Flavio Castro | 5e1f129 | 2015-07-23 17:14:09 -0700 | [diff] [blame] | 461 | oldValueMatch.map(serializer::encode), |
| 462 | oldVersionMatch, |
| 463 | value == null ? null : serializer.encode(value)) |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 464 | .thenApply(this::unwrapResult) |
Madan Jampani | 8566863 | 2015-08-10 10:46:40 -0700 | [diff] [blame] | 465 | .thenApply(r -> r.<K, V>map(this::dK, serializer::decode)); |
Madan Jampani | 7c52100 | 2015-03-23 12:23:01 -0700 | [diff] [blame] | 466 | } |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 467 | |
| 468 | private <T> T unwrapResult(Result<T> result) { |
| 469 | if (result.status() == Result.Status.LOCKED) { |
| 470 | throw new ConsistentMapException.ConcurrentModification(); |
| 471 | } else if (result.success()) { |
| 472 | return result.value(); |
| 473 | } else { |
| 474 | throw new IllegalStateException("Must not be here"); |
| 475 | } |
| 476 | } |
Madan Jampani | 02b7fb8 | 2015-05-01 13:01:20 -0700 | [diff] [blame] | 477 | |
| 478 | private void checkIfUnmodifiable() { |
| 479 | if (readOnly) { |
| 480 | throw new UnsupportedOperationException(); |
| 481 | } |
| 482 | } |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 483 | |
| 484 | @Override |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 485 | public CompletableFuture<Void> addListener(MapEventListener<K, V> listener) { |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 486 | listeners.add(listener); |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 487 | return CompletableFuture.completedFuture(null); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | @Override |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 491 | public CompletableFuture<Void> removeListener(MapEventListener<K, V> listener) { |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 492 | listeners.remove(listener); |
Madan Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 493 | return CompletableFuture.completedFuture(null); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 496 | @Override |
| 497 | public CompletableFuture<Boolean> prepare(MapTransaction<K, V> transaction) { |
| 498 | return Tools.exceptionalFuture(new UnsupportedOperationException()); |
| 499 | } |
| 500 | |
| 501 | @Override |
| 502 | public CompletableFuture<Void> commit(TransactionId transactionId) { |
| 503 | return Tools.exceptionalFuture(new UnsupportedOperationException()); |
| 504 | } |
| 505 | |
| 506 | @Override |
| 507 | public CompletableFuture<Void> rollback(TransactionId transactionId) { |
| 508 | return Tools.exceptionalFuture(new UnsupportedOperationException()); |
| 509 | } |
| 510 | |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 511 | protected void notifyListeners(MapEvent<K, V> event) { |
Madan Jampani | 8566863 | 2015-08-10 10:46:40 -0700 | [diff] [blame] | 512 | if (event == null) { |
| 513 | return; |
| 514 | } |
| 515 | listeners.forEach(listener -> { |
| 516 | try { |
| 517 | listener.event(event); |
| 518 | } catch (Exception e) { |
| 519 | log.warn("Failure notifying listener about {}", event, e); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 520 | } |
Madan Jampani | 8566863 | 2015-08-10 10:46:40 -0700 | [diff] [blame] | 521 | }); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 522 | } |
Madan Jampani | bab51a4 | 2015-08-10 13:53:35 -0700 | [diff] [blame] | 523 | } |