Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [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 | package org.onosproject.store.primitives.resources.impl; |
| 17 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 18 | import java.util.Collection; |
| 19 | import java.util.ConcurrentModificationException; |
Madan Jampani | fc98177 | 2016-02-16 09:46:42 -0800 | [diff] [blame] | 20 | import java.util.List; |
Madan Jampani | 0463cf9 | 2016-05-04 14:46:08 -0700 | [diff] [blame] | 21 | import java.util.Map; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 22 | import java.util.Map.Entry; |
| 23 | import java.util.Set; |
| 24 | import java.util.concurrent.CompletableFuture; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 25 | import java.util.concurrent.ConcurrentHashMap; |
Madan Jampani | 0463cf9 | 2016-05-04 14:46:08 -0700 | [diff] [blame] | 26 | import java.util.concurrent.Executor; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 27 | import java.util.concurrent.atomic.AtomicReference; |
| 28 | import java.util.function.BiFunction; |
| 29 | import java.util.function.Predicate; |
| 30 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 31 | import io.atomix.protocols.raft.proxy.RaftProxy; |
| 32 | import org.onlab.util.KryoNamespace; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 33 | import org.onlab.util.Match; |
Madan Jampani | 80d2e97 | 2016-07-12 17:11:52 -0700 | [diff] [blame] | 34 | import org.onlab.util.Tools; |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 35 | import org.onosproject.store.primitives.MapUpdate; |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 36 | import org.onosproject.store.primitives.TransactionId; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 37 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.ContainsKey; |
| 38 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.ContainsValue; |
| 39 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.Get; |
| 40 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.GetOrDefault; |
| 41 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.TransactionBegin; |
| 42 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.TransactionCommit; |
| 43 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.TransactionPrepare; |
| 44 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.TransactionPrepareAndCommit; |
| 45 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.TransactionRollback; |
| 46 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.UpdateAndGet; |
| 47 | import org.onosproject.store.serializers.KryoNamespaces; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 48 | import org.onosproject.store.service.AsyncConsistentMap; |
Madan Jampani | 80d2e97 | 2016-07-12 17:11:52 -0700 | [diff] [blame] | 49 | import org.onosproject.store.service.ConsistentMapException; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 50 | import org.onosproject.store.service.MapEvent; |
| 51 | import org.onosproject.store.service.MapEventListener; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 52 | import org.onosproject.store.service.Serializer; |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 53 | import org.onosproject.store.service.TransactionLog; |
| 54 | import org.onosproject.store.service.Version; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 55 | import org.onosproject.store.service.Versioned; |
Madan Jampani | 8e18c0e | 2016-06-20 14:08:16 -0700 | [diff] [blame] | 56 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 57 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapEvents.CHANGE; |
| 58 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.ADD_LISTENER; |
| 59 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.BEGIN; |
| 60 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.CLEAR; |
| 61 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.COMMIT; |
| 62 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.CONTAINS_KEY; |
| 63 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.CONTAINS_VALUE; |
| 64 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.ENTRY_SET; |
| 65 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.GET; |
| 66 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.GET_OR_DEFAULT; |
| 67 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.IS_EMPTY; |
| 68 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.KEY_SET; |
| 69 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.PREPARE; |
| 70 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.PREPARE_AND_COMMIT; |
| 71 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.REMOVE_LISTENER; |
| 72 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.ROLLBACK; |
| 73 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.SIZE; |
| 74 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.UPDATE_AND_GET; |
| 75 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.VALUES; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Distributed resource providing the {@link AsyncConsistentMap} primitive. |
| 79 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 80 | public class AtomixConsistentMap extends AbstractRaftPrimitive implements AsyncConsistentMap<String, byte[]> { |
| 81 | private static final Serializer SERIALIZER = Serializer.using(KryoNamespace.newBuilder() |
| 82 | .register(KryoNamespaces.BASIC) |
| 83 | .register(AtomixConsistentMapOperations.NAMESPACE) |
| 84 | .register(AtomixConsistentMapEvents.NAMESPACE) |
| 85 | .build()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 86 | |
Madan Jampani | 8e18c0e | 2016-06-20 14:08:16 -0700 | [diff] [blame] | 87 | private final Map<MapEventListener<String, byte[]>, Executor> mapEventListeners = new ConcurrentHashMap<>(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 88 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 89 | public AtomixConsistentMap(RaftProxy proxy) { |
| 90 | super(proxy); |
| 91 | proxy.addEventListener(CHANGE, SERIALIZER::decode, this::handleEvent); |
| 92 | proxy.addStateChangeListener(state -> { |
| 93 | if (state == RaftProxy.State.CONNECTED && isListening()) { |
| 94 | proxy.invoke(ADD_LISTENER); |
| 95 | } |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 96 | }); |
| 97 | } |
| 98 | |
Madan Jampani | fc98177 | 2016-02-16 09:46:42 -0800 | [diff] [blame] | 99 | private void handleEvent(List<MapEvent<String, byte[]>> events) { |
Madan Jampani | 0463cf9 | 2016-05-04 14:46:08 -0700 | [diff] [blame] | 100 | events.forEach(event -> |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 101 | mapEventListeners.forEach((listener, executor) -> executor.execute(() -> listener.event(event)))); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public CompletableFuture<Boolean> isEmpty() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 106 | return proxy.invoke(IS_EMPTY, SERIALIZER::decode); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public CompletableFuture<Integer> size() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 111 | return proxy.invoke(SIZE, SERIALIZER::decode); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public CompletableFuture<Boolean> containsKey(String key) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 116 | return proxy.invoke(CONTAINS_KEY, SERIALIZER::encode, new ContainsKey(key), SERIALIZER::decode); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public CompletableFuture<Boolean> containsValue(byte[] value) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 121 | return proxy.invoke(CONTAINS_VALUE, SERIALIZER::encode, new ContainsValue(value), SERIALIZER::decode); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public CompletableFuture<Versioned<byte[]>> get(String key) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 126 | return proxy.invoke(GET, SERIALIZER::encode, new Get(key), SERIALIZER::decode); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
Jordan Halterman | f627244 | 2017-04-20 02:18:08 -0700 | [diff] [blame] | 130 | public CompletableFuture<Versioned<byte[]>> getOrDefault(String key, byte[] defaultValue) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 131 | return proxy.invoke( |
| 132 | GET_OR_DEFAULT, |
| 133 | SERIALIZER::encode, |
| 134 | new GetOrDefault(key, defaultValue), |
| 135 | SERIALIZER::decode); |
Jordan Halterman | f627244 | 2017-04-20 02:18:08 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 139 | public CompletableFuture<Set<String>> keySet() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 140 | return proxy.invoke(KEY_SET, SERIALIZER::decode); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public CompletableFuture<Collection<Versioned<byte[]>>> values() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 145 | return proxy.invoke(VALUES, SERIALIZER::decode); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public CompletableFuture<Set<Entry<String, Versioned<byte[]>>>> entrySet() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 150 | return proxy.invoke(ENTRY_SET, SERIALIZER::decode); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | @SuppressWarnings("unchecked") |
| 155 | public CompletableFuture<Versioned<byte[]>> put(String key, byte[] value) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 156 | return proxy.<UpdateAndGet, MapEntryUpdateResult<String, byte[]>>invoke( |
| 157 | UPDATE_AND_GET, |
| 158 | SERIALIZER::encode, |
| 159 | new UpdateAndGet(key, value, Match.ANY, Match.ANY), |
| 160 | SERIALIZER::decode) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 161 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 162 | .thenApply(v -> v.oldValue()); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | @SuppressWarnings("unchecked") |
| 167 | public CompletableFuture<Versioned<byte[]>> putAndGet(String key, byte[] value) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 168 | return proxy.<UpdateAndGet, MapEntryUpdateResult<String, byte[]>>invoke( |
| 169 | UPDATE_AND_GET, |
| 170 | SERIALIZER::encode, |
| 171 | new UpdateAndGet(key, value, Match.ANY, Match.ANY), |
| 172 | SERIALIZER::decode) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 173 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 174 | .thenApply(v -> v.newValue()); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | @SuppressWarnings("unchecked") |
| 179 | public CompletableFuture<Versioned<byte[]>> putIfAbsent(String key, byte[] value) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 180 | return proxy.<UpdateAndGet, MapEntryUpdateResult<String, byte[]>>invoke( |
| 181 | UPDATE_AND_GET, |
| 182 | SERIALIZER::encode, |
| 183 | new UpdateAndGet(key, value, Match.NULL, Match.ANY), |
| 184 | SERIALIZER::decode) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 185 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 186 | .thenApply(v -> v.oldValue()); |
| 187 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 188 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 189 | @Override |
| 190 | @SuppressWarnings("unchecked") |
| 191 | public CompletableFuture<Versioned<byte[]>> remove(String key) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 192 | return proxy.<UpdateAndGet, MapEntryUpdateResult<String, byte[]>>invoke( |
| 193 | UPDATE_AND_GET, |
| 194 | SERIALIZER::encode, |
| 195 | new UpdateAndGet(key, null, Match.ANY, Match.ANY), |
| 196 | SERIALIZER::decode) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 197 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 198 | .thenApply(v -> v.oldValue()); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | @SuppressWarnings("unchecked") |
| 203 | public CompletableFuture<Boolean> remove(String key, byte[] value) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 204 | return proxy.<UpdateAndGet, MapEntryUpdateResult<String, byte[]>>invoke( |
| 205 | UPDATE_AND_GET, |
| 206 | SERIALIZER::encode, |
| 207 | new UpdateAndGet(key, null, Match.ifValue(value), Match.ANY), |
| 208 | SERIALIZER::decode) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 209 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 210 | .thenApply(v -> v.updated()); |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | @SuppressWarnings("unchecked") |
| 215 | public CompletableFuture<Boolean> remove(String key, long version) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 216 | return proxy.<UpdateAndGet, MapEntryUpdateResult<String, byte[]>>invoke( |
| 217 | UPDATE_AND_GET, |
| 218 | SERIALIZER::encode, |
| 219 | new UpdateAndGet(key, null, Match.ANY, Match.ifValue(version)), |
| 220 | SERIALIZER::decode) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 221 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 222 | .thenApply(v -> v.updated()); |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | @SuppressWarnings("unchecked") |
| 227 | public CompletableFuture<Versioned<byte[]>> replace(String key, byte[] value) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 228 | return proxy.<UpdateAndGet, MapEntryUpdateResult<String, byte[]>>invoke( |
| 229 | UPDATE_AND_GET, |
| 230 | SERIALIZER::encode, |
| 231 | new UpdateAndGet(key, value, Match.NOT_NULL, Match.ANY), |
| 232 | SERIALIZER::decode) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 233 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 234 | .thenApply(v -> v.oldValue()); |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | @SuppressWarnings("unchecked") |
| 239 | public CompletableFuture<Boolean> replace(String key, byte[] oldValue, byte[] newValue) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 240 | return proxy.<UpdateAndGet, MapEntryUpdateResult<String, byte[]>>invoke( |
| 241 | UPDATE_AND_GET, |
| 242 | SERIALIZER::encode, |
| 243 | new UpdateAndGet(key, newValue, Match.ifValue(oldValue), Match.ANY), |
| 244 | SERIALIZER::decode) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 245 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 246 | .thenApply(v -> v.updated()); |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | @SuppressWarnings("unchecked") |
| 251 | public CompletableFuture<Boolean> replace(String key, long oldVersion, byte[] newValue) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 252 | return proxy.<UpdateAndGet, MapEntryUpdateResult<String, byte[]>>invoke( |
| 253 | UPDATE_AND_GET, |
| 254 | SERIALIZER::encode, |
| 255 | new UpdateAndGet(key, newValue, Match.ANY, Match.ifValue(oldVersion)), |
| 256 | SERIALIZER::decode) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 257 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 258 | .thenApply(v -> v.updated()); |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public CompletableFuture<Void> clear() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 263 | return proxy.<MapEntryUpdateResult.Status>invoke(CLEAR, SERIALIZER::decode) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 264 | .whenComplete((r, e) -> throwIfLocked(r)) |
| 265 | .thenApply(v -> null); |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | @SuppressWarnings("unchecked") |
| 270 | public CompletableFuture<Versioned<byte[]>> computeIf(String key, |
| 271 | Predicate<? super byte[]> condition, |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 272 | BiFunction<? super String, ? super byte[], ? extends byte[]> remappingFunction) { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 273 | return get(key).thenCompose(r1 -> { |
| 274 | byte[] existingValue = r1 == null ? null : r1.value(); |
| 275 | // if the condition evaluates to false, return existing value. |
| 276 | if (!condition.test(existingValue)) { |
| 277 | return CompletableFuture.completedFuture(r1); |
| 278 | } |
| 279 | |
| 280 | AtomicReference<byte[]> computedValue = new AtomicReference<>(); |
| 281 | // if remappingFunction throws an exception, return the exception. |
| 282 | try { |
| 283 | computedValue.set(remappingFunction.apply(key, existingValue)); |
| 284 | } catch (Exception e) { |
| 285 | CompletableFuture<Versioned<byte[]>> future = new CompletableFuture<>(); |
| 286 | future.completeExceptionally(e); |
| 287 | return future; |
| 288 | } |
| 289 | if (computedValue.get() == null && r1 == null) { |
| 290 | return CompletableFuture.completedFuture(null); |
| 291 | } |
| 292 | Match<byte[]> valueMatch = r1 == null ? Match.NULL : Match.ANY; |
| 293 | Match<Long> versionMatch = r1 == null ? Match.ANY : Match.ifValue(r1.version()); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 294 | return proxy.<UpdateAndGet, MapEntryUpdateResult<String, byte[]>>invoke( |
| 295 | UPDATE_AND_GET, |
| 296 | SERIALIZER::encode, |
| 297 | new UpdateAndGet(key, |
| 298 | computedValue.get(), |
| 299 | valueMatch, |
| 300 | versionMatch), |
| 301 | SERIALIZER::decode) |
| 302 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 303 | .thenCompose(r -> { |
| 304 | if (r.status() == MapEntryUpdateResult.Status.PRECONDITION_FAILED || |
| 305 | r.status() == MapEntryUpdateResult.Status.WRITE_LOCK) { |
| 306 | return Tools.exceptionalFuture(new ConsistentMapException.ConcurrentModification()); |
| 307 | } |
| 308 | return CompletableFuture.completedFuture(r); |
| 309 | }) |
| 310 | .thenApply(v -> v.newValue()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 311 | }); |
| 312 | } |
| 313 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 314 | @Override |
Madan Jampani | 0463cf9 | 2016-05-04 14:46:08 -0700 | [diff] [blame] | 315 | public synchronized CompletableFuture<Void> addListener(MapEventListener<String, byte[]> listener, |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 316 | Executor executor) { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 317 | if (mapEventListeners.isEmpty()) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 318 | return proxy.invoke(ADD_LISTENER).thenRun(() -> mapEventListeners.put(listener, executor)); |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 319 | } else { |
Madan Jampani | 0463cf9 | 2016-05-04 14:46:08 -0700 | [diff] [blame] | 320 | mapEventListeners.put(listener, executor); |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 321 | return CompletableFuture.completedFuture(null); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 322 | } |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | @Override |
| 326 | public synchronized CompletableFuture<Void> removeListener(MapEventListener<String, byte[]> listener) { |
Madan Jampani | 0463cf9 | 2016-05-04 14:46:08 -0700 | [diff] [blame] | 327 | if (mapEventListeners.remove(listener) != null && mapEventListeners.isEmpty()) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 328 | return proxy.invoke(REMOVE_LISTENER).thenApply(v -> null); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 329 | } |
| 330 | return CompletableFuture.completedFuture(null); |
| 331 | } |
| 332 | |
| 333 | private void throwIfLocked(MapEntryUpdateResult.Status status) { |
| 334 | if (status == MapEntryUpdateResult.Status.WRITE_LOCK) { |
| 335 | throw new ConcurrentModificationException("Cannot update map: Another transaction in progress"); |
| 336 | } |
| 337 | } |
| 338 | |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 339 | @Override |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 340 | public CompletableFuture<Version> begin(TransactionId transactionId) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 341 | return proxy.<TransactionBegin, Long>invoke( |
| 342 | BEGIN, |
| 343 | SERIALIZER::encode, |
| 344 | new TransactionBegin(transactionId), |
| 345 | SERIALIZER::decode) |
| 346 | .thenApply(Version::new); |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | @Override |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 350 | public CompletableFuture<Boolean> prepare(TransactionLog<MapUpdate<String, byte[]>> transactionLog) { |
| 351 | return proxy.<TransactionPrepare, PrepareResult>invoke( |
| 352 | PREPARE, |
| 353 | SERIALIZER::encode, |
| 354 | new TransactionPrepare(transactionLog), |
| 355 | SERIALIZER::decode) |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 356 | .thenApply(v -> v == PrepareResult.OK); |
| 357 | } |
| 358 | |
| 359 | @Override |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 360 | public CompletableFuture<Boolean> prepareAndCommit(TransactionLog<MapUpdate<String, byte[]>> transactionLog) { |
| 361 | return proxy.<TransactionPrepareAndCommit, PrepareResult>invoke( |
| 362 | PREPARE_AND_COMMIT, |
| 363 | SERIALIZER::encode, |
| 364 | new TransactionPrepareAndCommit(transactionLog), |
| 365 | SERIALIZER::decode) |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 366 | .thenApply(v -> v == PrepareResult.OK); |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | @Override |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 370 | public CompletableFuture<Void> commit(TransactionId transactionId) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 371 | return proxy.<TransactionCommit, CommitResult>invoke( |
| 372 | COMMIT, |
| 373 | SERIALIZER::encode, |
| 374 | new TransactionCommit(transactionId), |
| 375 | SERIALIZER::decode) |
| 376 | .thenApply(v -> null); |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | @Override |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 380 | public CompletableFuture<Void> rollback(TransactionId transactionId) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 381 | return proxy.invoke( |
| 382 | ROLLBACK, |
| 383 | SERIALIZER::encode, |
| 384 | new TransactionRollback(transactionId), |
| 385 | SERIALIZER::decode) |
| 386 | .thenApply(v -> null); |
Madan Jampani | 1d3b617 | 2016-04-28 13:22:57 -0700 | [diff] [blame] | 387 | } |
Madan Jampani | fb78638 | 2016-06-13 10:25:35 -0700 | [diff] [blame] | 388 | |
| 389 | private boolean isListening() { |
| 390 | return !mapEventListeners.isEmpty(); |
| 391 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 392 | } |