Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 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.primitives.resources.impl; |
| 18 | |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 19 | import java.util.Collection; |
| 20 | import java.util.ConcurrentModificationException; |
| 21 | import java.util.List; |
| 22 | import java.util.Map; |
| 23 | import java.util.NavigableMap; |
| 24 | import java.util.NavigableSet; |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 25 | import java.util.Set; |
| 26 | import java.util.concurrent.CompletableFuture; |
| 27 | import java.util.concurrent.Executor; |
| 28 | import java.util.concurrent.atomic.AtomicReference; |
| 29 | import java.util.function.BiFunction; |
| 30 | import java.util.function.Predicate; |
| 31 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 32 | import com.google.common.collect.Maps; |
| 33 | import io.atomix.protocols.raft.proxy.RaftProxy; |
| 34 | import org.onlab.util.KryoNamespace; |
| 35 | import org.onlab.util.Match; |
| 36 | import org.onosproject.store.primitives.MapUpdate; |
| 37 | import org.onosproject.store.primitives.TransactionId; |
| 38 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.FloorEntry; |
| 39 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.HigherEntry; |
| 40 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.LowerEntry; |
| 41 | import org.onosproject.store.serializers.KryoNamespaces; |
| 42 | import org.onosproject.store.service.AsyncConsistentTreeMap; |
| 43 | import org.onosproject.store.service.MapEvent; |
| 44 | import org.onosproject.store.service.MapEventListener; |
| 45 | import org.onosproject.store.service.Serializer; |
| 46 | import org.onosproject.store.service.TransactionLog; |
| 47 | import org.onosproject.store.service.Version; |
| 48 | import org.onosproject.store.service.Versioned; |
| 49 | |
| 50 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapEvents.CHANGE; |
| 51 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.ADD_LISTENER; |
| 52 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.CEILING_ENTRY; |
| 53 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.CEILING_KEY; |
| 54 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.CLEAR; |
| 55 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.CONTAINS_KEY; |
| 56 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.CONTAINS_VALUE; |
| 57 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.CeilingEntry; |
| 58 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.CeilingKey; |
| 59 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.ContainsKey; |
| 60 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.ContainsValue; |
| 61 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.ENTRY_SET; |
| 62 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.FIRST_ENTRY; |
| 63 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.FIRST_KEY; |
| 64 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.FLOOR_ENTRY; |
| 65 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.FLOOR_KEY; |
| 66 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.FloorKey; |
| 67 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.GET; |
| 68 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.GET_OR_DEFAULT; |
| 69 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.Get; |
| 70 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.GetOrDefault; |
| 71 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.HIGHER_ENTRY; |
| 72 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.HIGHER_KEY; |
| 73 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.HigherKey; |
| 74 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.IS_EMPTY; |
| 75 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.KEY_SET; |
| 76 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.LAST_ENTRY; |
| 77 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.LAST_KEY; |
| 78 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.LOWER_ENTRY; |
| 79 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.LOWER_KEY; |
| 80 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.LowerKey; |
| 81 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.POLL_FIRST_ENTRY; |
| 82 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.POLL_LAST_ENTRY; |
| 83 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.REMOVE_LISTENER; |
| 84 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.SIZE; |
| 85 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.UPDATE_AND_GET; |
| 86 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.UpdateAndGet; |
| 87 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.VALUES; |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * Implementation of {@link AsyncConsistentTreeMap}. |
| 91 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 92 | public class AtomixConsistentTreeMap extends AbstractRaftPrimitive implements AsyncConsistentTreeMap<byte[]> { |
| 93 | private static final Serializer SERIALIZER = Serializer.using(KryoNamespace.newBuilder() |
| 94 | .register(KryoNamespaces.BASIC) |
| 95 | .register(AtomixConsistentTreeMapOperations.NAMESPACE) |
| 96 | .register(AtomixConsistentTreeMapEvents.NAMESPACE) |
| 97 | .build()); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 98 | |
| 99 | private final Map<MapEventListener<String, byte[]>, Executor> |
| 100 | mapEventListeners = Maps.newConcurrentMap(); |
| 101 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 102 | public AtomixConsistentTreeMap(RaftProxy proxy) { |
| 103 | super(proxy); |
| 104 | proxy.addEventListener(CHANGE, SERIALIZER::decode, this::handleEvent); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | private void handleEvent(List<MapEvent<String, byte[]>> events) { |
| 108 | events.forEach(event -> mapEventListeners. |
| 109 | forEach((listener, executor) -> |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 110 | executor.execute(() -> |
| 111 | listener.event(event)))); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public CompletableFuture<Boolean> isEmpty() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 116 | return proxy.invoke(IS_EMPTY, SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public CompletableFuture<Integer> size() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 121 | return proxy.invoke(SIZE, SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public CompletableFuture<Boolean> containsKey(String key) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 126 | return proxy.invoke(CONTAINS_KEY, SERIALIZER::encode, new ContainsKey(key), SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public CompletableFuture<Boolean> containsValue(byte[] value) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 131 | return proxy.invoke(CONTAINS_VALUE, SERIALIZER::encode, new ContainsValue(value), SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public CompletableFuture<Versioned<byte[]>> get(String key) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 136 | return proxy.invoke(GET, SERIALIZER::encode, new Get(key), SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Override |
Jordan Halterman | f627244 | 2017-04-20 02:18:08 -0700 | [diff] [blame] | 140 | public CompletableFuture<Versioned<byte[]>> getOrDefault(String key, byte[] defaultValue) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 141 | return proxy.invoke( |
| 142 | GET_OR_DEFAULT, |
| 143 | SERIALIZER::encode, |
| 144 | new GetOrDefault(key, defaultValue), |
| 145 | SERIALIZER::decode); |
Jordan Halterman | f627244 | 2017-04-20 02:18:08 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 149 | public CompletableFuture<Set<String>> keySet() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 150 | return proxy.invoke(KEY_SET, SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public CompletableFuture<Collection<Versioned<byte[]>>> values() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 155 | return proxy.invoke(VALUES, SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public CompletableFuture<Set<Map.Entry<String, Versioned<byte[]>>>> entrySet() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 160 | return proxy.invoke(ENTRY_SET, SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Override |
| 164 | @SuppressWarnings("unchecked") |
| 165 | public CompletableFuture<Versioned<byte[]>> put(String key, byte[] value) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 166 | return proxy.<UpdateAndGet, MapEntryUpdateResult<String, byte[]>>invoke( |
| 167 | UPDATE_AND_GET, |
| 168 | SERIALIZER::encode, |
| 169 | new UpdateAndGet(key, value, Match.ANY, Match.ANY), |
| 170 | SERIALIZER::decode) |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 171 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 172 | .thenApply(v -> v.oldValue()); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | @SuppressWarnings("unchecked") |
| 177 | public CompletableFuture<Versioned<byte[]>> putAndGet(String key, byte[] value) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 178 | return proxy.<UpdateAndGet, MapEntryUpdateResult<String, byte[]>>invoke( |
| 179 | UPDATE_AND_GET, |
| 180 | SERIALIZER::encode, |
| 181 | new UpdateAndGet(key, value, Match.ANY, Match.ANY), |
| 182 | SERIALIZER::decode) |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 183 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 184 | .thenApply(v -> v.newValue()); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | @SuppressWarnings("unchecked") |
| 189 | public CompletableFuture<Versioned<byte[]>> putIfAbsent(String key, byte[] value) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 190 | return proxy.<UpdateAndGet, MapEntryUpdateResult<String, byte[]>>invoke( |
| 191 | UPDATE_AND_GET, |
| 192 | SERIALIZER::encode, |
| 193 | new UpdateAndGet(key, value, Match.NULL, Match.ANY), |
| 194 | SERIALIZER::decode) |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 195 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 196 | .thenApply(v -> v.oldValue()); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | @SuppressWarnings("unchecked") |
| 201 | public CompletableFuture<Versioned<byte[]>> remove(String key) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 202 | return proxy.<UpdateAndGet, MapEntryUpdateResult<String, byte[]>>invoke( |
| 203 | UPDATE_AND_GET, |
| 204 | SERIALIZER::encode, |
| 205 | new UpdateAndGet(key, null, Match.ANY, Match.ANY), |
| 206 | SERIALIZER::decode) |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 207 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 208 | .thenApply(v -> v.oldValue()); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | @SuppressWarnings("unchecked") |
| 213 | public CompletableFuture<Boolean> remove(String key, byte[] value) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 214 | return proxy.<UpdateAndGet, MapEntryUpdateResult<String, byte[]>>invoke( |
| 215 | UPDATE_AND_GET, |
| 216 | SERIALIZER::encode, |
| 217 | new UpdateAndGet(key, null, Match.ifValue(value), Match.ANY), |
| 218 | SERIALIZER::decode) |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 219 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 220 | .thenApply(v -> v.updated()); |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | @SuppressWarnings("unchecked") |
| 225 | public CompletableFuture<Boolean> remove(String key, long version) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 226 | return proxy.<UpdateAndGet, MapEntryUpdateResult<String, byte[]>>invoke( |
| 227 | UPDATE_AND_GET, |
| 228 | SERIALIZER::encode, |
| 229 | new UpdateAndGet(key, null, Match.ANY, Match.ifValue(version)), |
| 230 | SERIALIZER::decode) |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 231 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 232 | .thenApply(v -> v.updated()); |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | @SuppressWarnings("unchecked") |
| 237 | public CompletableFuture<Versioned<byte[]>> replace(String key, byte[] value) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 238 | return proxy.<UpdateAndGet, MapEntryUpdateResult<String, byte[]>>invoke( |
| 239 | UPDATE_AND_GET, |
| 240 | SERIALIZER::encode, |
| 241 | new UpdateAndGet(key, value, Match.NOT_NULL, Match.ANY), |
| 242 | SERIALIZER::decode) |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 243 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 244 | .thenApply(v -> v.oldValue()); |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | @SuppressWarnings("unchecked") |
| 249 | public CompletableFuture<Boolean> replace(String key, byte[] oldValue, byte[] newValue) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 250 | return proxy.<UpdateAndGet, MapEntryUpdateResult<String, byte[]>>invoke( |
| 251 | UPDATE_AND_GET, |
| 252 | SERIALIZER::encode, |
| 253 | new UpdateAndGet(key, newValue, Match.ifValue(oldValue), Match.ANY), |
| 254 | SERIALIZER::decode) |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 255 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 256 | .thenApply(v -> v.updated()); |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | @SuppressWarnings("unchecked") |
| 261 | public CompletableFuture<Boolean> replace(String key, long oldVersion, byte[] newValue) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 262 | return proxy.<UpdateAndGet, MapEntryUpdateResult<String, byte[]>>invoke( |
| 263 | UPDATE_AND_GET, |
| 264 | SERIALIZER::encode, |
| 265 | new UpdateAndGet(key, newValue, Match.ANY, Match.ifValue(oldVersion)), |
| 266 | SERIALIZER::decode) |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 267 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 268 | .thenApply(v -> v.updated()); |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public CompletableFuture<Void> clear() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 273 | return proxy.<MapEntryUpdateResult.Status>invoke(CLEAR, SERIALIZER::decode) |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 274 | .whenComplete((r, e) -> throwIfLocked(r)) |
| 275 | .thenApply(v -> null); |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | @SuppressWarnings("unchecked") |
| 280 | public CompletableFuture<Versioned<byte[]>> computeIf(String key, |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 281 | Predicate<? super byte[]> condition, |
| 282 | BiFunction<? super String, |
| 283 | ? super byte[], |
| 284 | ? extends byte[]> remappingFunction) { |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 285 | return get(key).thenCompose(r1 -> { |
| 286 | byte[] existingValue = r1 == null ? null : r1.value(); |
| 287 | |
| 288 | if (!condition.test(existingValue)) { |
| 289 | return CompletableFuture.completedFuture(r1); |
| 290 | } |
| 291 | |
| 292 | AtomicReference<byte[]> computedValue = new AtomicReference<byte[]>(); |
| 293 | try { |
| 294 | computedValue.set(remappingFunction.apply(key, existingValue)); |
| 295 | } catch (Exception e) { |
| 296 | CompletableFuture<Versioned<byte[]>> future = new CompletableFuture<>(); |
| 297 | future.completeExceptionally(e); |
| 298 | return future; |
| 299 | } |
| 300 | if (computedValue.get() == null && r1 == null) { |
| 301 | return CompletableFuture.completedFuture(null); |
| 302 | } |
| 303 | Match<byte[]> valueMatch = r1 == null ? Match.NULL : Match.ANY; |
| 304 | Match<Long> versionMatch = r1 == null ? Match.ANY : Match.ifValue(r1.version()); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 305 | return proxy.<UpdateAndGet, MapEntryUpdateResult<String, byte[]>>invoke( |
| 306 | UPDATE_AND_GET, |
| 307 | SERIALIZER::encode, |
| 308 | new UpdateAndGet(key, computedValue.get(), valueMatch, versionMatch), |
| 309 | SERIALIZER::decode) |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 310 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 311 | .thenApply(v -> v.newValue()); |
| 312 | }); |
| 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public CompletableFuture<Void> addListener( |
| 317 | MapEventListener<String, byte[]> listener, Executor executor) { |
| 318 | if (mapEventListeners.isEmpty()) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 319 | return proxy.invoke(ADD_LISTENER).thenRun(() -> |
| 320 | mapEventListeners.put(listener, |
| 321 | executor)); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 322 | } else { |
| 323 | mapEventListeners.put(listener, executor); |
| 324 | return CompletableFuture.completedFuture(null); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public synchronized CompletableFuture<Void> removeListener(MapEventListener<String, byte[]> listener) { |
| 330 | if (mapEventListeners.remove(listener) != null && |
| 331 | mapEventListeners.isEmpty()) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 332 | return proxy.invoke(REMOVE_LISTENER) |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 333 | .thenApply(v -> null); |
| 334 | } |
| 335 | return CompletableFuture.completedFuture(null); |
| 336 | } |
| 337 | |
| 338 | |
| 339 | private void throwIfLocked(MapEntryUpdateResult.Status status) { |
| 340 | if (status == MapEntryUpdateResult.Status.WRITE_LOCK) { |
| 341 | throw new ConcurrentModificationException("Cannot update TreeMap: another update is in progress."); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public CompletableFuture<String> firstKey() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 347 | return proxy.invoke(FIRST_KEY, SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public CompletableFuture<String> lastKey() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 352 | return proxy.invoke(LAST_KEY, SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public CompletableFuture<Map.Entry<String, Versioned<byte[]>>> ceilingEntry(String key) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 357 | return proxy.invoke(CEILING_ENTRY, SERIALIZER::encode, new CeilingEntry(key), SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public CompletableFuture<Map.Entry<String, Versioned<byte[]>>> floorEntry(String key) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 362 | return proxy.invoke(FLOOR_ENTRY, SERIALIZER::encode, new FloorEntry(key), SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public CompletableFuture<Map.Entry<String, Versioned<byte[]>>> higherEntry( |
| 367 | String key) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 368 | return proxy.invoke(HIGHER_ENTRY, SERIALIZER::encode, new HigherEntry(key), SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | @Override |
| 372 | public CompletableFuture<Map.Entry<String, Versioned<byte[]>>> lowerEntry( |
| 373 | String key) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 374 | return proxy.invoke(LOWER_ENTRY, SERIALIZER::encode, new LowerEntry(key), SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | @Override |
| 378 | public CompletableFuture<Map.Entry<String, Versioned<byte[]>>> firstEntry() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 379 | return proxy.invoke(FIRST_ENTRY, SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | @Override |
| 383 | public CompletableFuture<Map.Entry<String, Versioned<byte[]>>> lastEntry() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 384 | return proxy.invoke(LAST_ENTRY, SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | @Override |
| 388 | public CompletableFuture<Map.Entry<String, Versioned<byte[]>>> pollFirstEntry() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 389 | return proxy.invoke(POLL_FIRST_ENTRY, SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public CompletableFuture<Map.Entry<String, Versioned<byte[]>>> pollLastEntry() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 394 | return proxy.invoke(POLL_LAST_ENTRY, SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | @Override |
| 398 | public CompletableFuture<String> lowerKey(String key) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 399 | return proxy.invoke(LOWER_KEY, SERIALIZER::encode, new LowerKey(key), SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | @Override |
| 403 | public CompletableFuture<String> floorKey(String key) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 404 | return proxy.invoke(FLOOR_KEY, SERIALIZER::encode, new FloorKey(key), SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | @Override |
| 408 | public CompletableFuture<String> ceilingKey(String key) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 409 | return proxy.invoke(CEILING_KEY, SERIALIZER::encode, new CeilingKey(key), SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public CompletableFuture<String> higherKey(String key) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 414 | return proxy.invoke(HIGHER_KEY, SERIALIZER::encode, new HigherKey(key), SERIALIZER::decode); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public CompletableFuture<NavigableSet<String>> navigableKeySet() { |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 419 | throw new UnsupportedOperationException("This operation is not yet supported."); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | @Override |
| 423 | public CompletableFuture<NavigableMap<String, byte[]>> subMap( |
| 424 | String upperKey, String lowerKey, boolean inclusiveUpper, |
| 425 | boolean inclusiveLower) { |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 426 | throw new UnsupportedOperationException("This operation is not yet supported."); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | @Override |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 430 | public CompletableFuture<Version> begin(TransactionId transactionId) { |
| 431 | throw new UnsupportedOperationException("This operation is not yet supported."); |
| 432 | } |
| 433 | |
| 434 | @Override |
| 435 | public CompletableFuture<Boolean> prepare(TransactionLog<MapUpdate<String, byte[]>> transactionLog) { |
| 436 | throw new UnsupportedOperationException("This operation is not yet supported."); |
| 437 | } |
| 438 | |
| 439 | @Override |
| 440 | public CompletableFuture<Boolean> prepareAndCommit(TransactionLog<MapUpdate<String, byte[]>> transactionLog) { |
| 441 | throw new UnsupportedOperationException("This operation is not yet supported."); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | @Override |
| 445 | public CompletableFuture<Void> commit(TransactionId transactionId) { |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 446 | throw new UnsupportedOperationException("This operation is not yet supported."); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | @Override |
| 450 | public CompletableFuture<Void> rollback(TransactionId transactionId) { |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 451 | throw new UnsupportedOperationException("This operation is not yet supported."); |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 452 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 453 | } |