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 io.atomix.copycat.client.CopycatClient; |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 19 | import io.atomix.resource.AbstractResource; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 20 | import io.atomix.resource.ResourceTypeInfo; |
| 21 | |
Madan Jampani | 8e18c0e | 2016-06-20 14:08:16 -0700 | [diff] [blame] | 22 | import java.util.concurrent.ConcurrentHashMap; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 23 | import java.util.Collection; |
| 24 | import java.util.ConcurrentModificationException; |
Madan Jampani | fc98177 | 2016-02-16 09:46:42 -0800 | [diff] [blame] | 25 | import java.util.List; |
Madan Jampani | 0463cf9 | 2016-05-04 14:46:08 -0700 | [diff] [blame] | 26 | import java.util.Map; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 27 | import java.util.Map.Entry; |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 28 | import java.util.Properties; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 29 | import java.util.Set; |
| 30 | import java.util.concurrent.CompletableFuture; |
Madan Jampani | 0463cf9 | 2016-05-04 14:46:08 -0700 | [diff] [blame] | 31 | import java.util.concurrent.Executor; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 32 | import java.util.concurrent.atomic.AtomicReference; |
| 33 | import java.util.function.BiFunction; |
Madan Jampani | 1d3b617 | 2016-04-28 13:22:57 -0700 | [diff] [blame] | 34 | import java.util.function.Consumer; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 35 | import java.util.function.Predicate; |
| 36 | |
| 37 | import org.onlab.util.Match; |
Madan Jampani | 80d2e97 | 2016-07-12 17:11:52 -0700 | [diff] [blame] | 38 | import org.onlab.util.Tools; |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 39 | import org.onosproject.store.primitives.MapUpdate; |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 40 | import org.onosproject.store.primitives.TransactionId; |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 41 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Clear; |
| 42 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.ContainsKey; |
| 43 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.ContainsValue; |
| 44 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.EntrySet; |
| 45 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Get; |
Jordan Halterman | f627244 | 2017-04-20 02:18:08 -0700 | [diff] [blame] | 46 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.GetOrDefault; |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 47 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.IsEmpty; |
| 48 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.KeySet; |
| 49 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Listen; |
| 50 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Size; |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 51 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.TransactionBegin; |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 52 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.TransactionCommit; |
| 53 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.TransactionPrepare; |
Madan Jampani | 542d9e2 | 2016-04-05 15:39:55 -0700 | [diff] [blame] | 54 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.TransactionPrepareAndCommit; |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 55 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.TransactionRollback; |
| 56 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Unlisten; |
| 57 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.UpdateAndGet; |
| 58 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Values; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 59 | import org.onosproject.store.service.AsyncConsistentMap; |
Madan Jampani | 80d2e97 | 2016-07-12 17:11:52 -0700 | [diff] [blame] | 60 | import org.onosproject.store.service.ConsistentMapException; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 61 | import org.onosproject.store.service.MapEvent; |
| 62 | import org.onosproject.store.service.MapEventListener; |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 63 | import org.onosproject.store.service.TransactionLog; |
| 64 | import org.onosproject.store.service.Version; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 65 | import org.onosproject.store.service.Versioned; |
Madan Jampani | 8e18c0e | 2016-06-20 14:08:16 -0700 | [diff] [blame] | 66 | |
Madan Jampani | 1d3b617 | 2016-04-28 13:22:57 -0700 | [diff] [blame] | 67 | import com.google.common.collect.ImmutableSet; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 68 | import com.google.common.collect.Sets; |
| 69 | |
| 70 | /** |
| 71 | * Distributed resource providing the {@link AsyncConsistentMap} primitive. |
| 72 | */ |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 73 | @ResourceTypeInfo(id = -151, factory = AtomixConsistentMapFactory.class) |
| 74 | public class AtomixConsistentMap extends AbstractResource<AtomixConsistentMap> |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 75 | implements AsyncConsistentMap<String, byte[]> { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 76 | |
Madan Jampani | 1d3b617 | 2016-04-28 13:22:57 -0700 | [diff] [blame] | 77 | private final Set<Consumer<Status>> statusChangeListeners = Sets.newCopyOnWriteArraySet(); |
Madan Jampani | 8e18c0e | 2016-06-20 14:08:16 -0700 | [diff] [blame] | 78 | private final Map<MapEventListener<String, byte[]>, Executor> mapEventListeners = new ConcurrentHashMap<>(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 79 | |
Madan Jampani | fc98177 | 2016-02-16 09:46:42 -0800 | [diff] [blame] | 80 | public static final String CHANGE_SUBJECT = "changeEvents"; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 81 | |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 82 | public AtomixConsistentMap(CopycatClient client, Properties properties) { |
| 83 | super(client, properties); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public String name() { |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public CompletableFuture<AtomixConsistentMap> open() { |
| 93 | return super.open().thenApply(result -> { |
Madan Jampani | fb78638 | 2016-06-13 10:25:35 -0700 | [diff] [blame] | 94 | client.onStateChange(state -> { |
| 95 | if (state == CopycatClient.State.CONNECTED && isListening()) { |
| 96 | client.submit(new Listen()); |
| 97 | } |
| 98 | }); |
Madan Jampani | 3a9911c | 2016-02-21 11:25:45 -0800 | [diff] [blame] | 99 | client.onEvent(CHANGE_SUBJECT, this::handleEvent); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 100 | return result; |
| 101 | }); |
| 102 | } |
| 103 | |
Madan Jampani | fc98177 | 2016-02-16 09:46:42 -0800 | [diff] [blame] | 104 | private void handleEvent(List<MapEvent<String, byte[]>> events) { |
Madan Jampani | 0463cf9 | 2016-05-04 14:46:08 -0700 | [diff] [blame] | 105 | events.forEach(event -> |
| 106 | mapEventListeners.forEach((listener, executor) -> executor.execute(() -> listener.event(event)))); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public CompletableFuture<Boolean> isEmpty() { |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 111 | return client.submit(new IsEmpty()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public CompletableFuture<Integer> size() { |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 116 | return client.submit(new Size()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public CompletableFuture<Boolean> containsKey(String key) { |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 121 | return client.submit(new ContainsKey(key)); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public CompletableFuture<Boolean> containsValue(byte[] value) { |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 126 | return client.submit(new ContainsValue(value)); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public CompletableFuture<Versioned<byte[]>> get(String key) { |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 131 | return client.submit(new Get(key)); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
Jordan Halterman | f627244 | 2017-04-20 02:18:08 -0700 | [diff] [blame] | 135 | public CompletableFuture<Versioned<byte[]>> getOrDefault(String key, byte[] defaultValue) { |
| 136 | return client.submit(new GetOrDefault(key, defaultValue)); |
| 137 | } |
| 138 | |
| 139 | @Override |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 140 | public CompletableFuture<Set<String>> keySet() { |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 141 | return client.submit(new KeySet()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public CompletableFuture<Collection<Versioned<byte[]>>> values() { |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 146 | return client.submit(new Values()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public CompletableFuture<Set<Entry<String, Versioned<byte[]>>>> entrySet() { |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 151 | return client.submit(new EntrySet()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
| 155 | @SuppressWarnings("unchecked") |
| 156 | public CompletableFuture<Versioned<byte[]>> put(String key, byte[] value) { |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 157 | return client.submit(new UpdateAndGet(key, value, Match.ANY, Match.ANY)) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 158 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 159 | .thenApply(v -> v.oldValue()); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | @SuppressWarnings("unchecked") |
| 164 | public CompletableFuture<Versioned<byte[]>> putAndGet(String key, byte[] value) { |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 165 | return client.submit(new UpdateAndGet(key, value, Match.ANY, Match.ANY)) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 166 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 167 | .thenApply(v -> v.newValue()); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | @SuppressWarnings("unchecked") |
| 172 | public CompletableFuture<Versioned<byte[]>> putIfAbsent(String key, byte[] value) { |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 173 | return client.submit(new UpdateAndGet(key, value, Match.NULL, Match.ANY)) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 174 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 175 | .thenApply(v -> v.oldValue()); |
| 176 | } |
| 177 | @Override |
| 178 | @SuppressWarnings("unchecked") |
| 179 | public CompletableFuture<Versioned<byte[]>> remove(String key) { |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 180 | return client.submit(new UpdateAndGet(key, null, Match.ANY, Match.ANY)) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 181 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 182 | .thenApply(v -> v.oldValue()); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | @SuppressWarnings("unchecked") |
| 187 | public CompletableFuture<Boolean> remove(String key, byte[] value) { |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 188 | return client.submit(new UpdateAndGet(key, null, Match.ifValue(value), Match.ANY)) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 189 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 190 | .thenApply(v -> v.updated()); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | @SuppressWarnings("unchecked") |
| 195 | public CompletableFuture<Boolean> remove(String key, long version) { |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 196 | return client.submit(new UpdateAndGet(key, null, Match.ANY, Match.ifValue(version))) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 197 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 198 | .thenApply(v -> v.updated()); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | @SuppressWarnings("unchecked") |
| 203 | public CompletableFuture<Versioned<byte[]>> replace(String key, byte[] value) { |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 204 | return client.submit(new UpdateAndGet(key, value, Match.NOT_NULL, Match.ANY)) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 205 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 206 | .thenApply(v -> v.oldValue()); |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | @SuppressWarnings("unchecked") |
| 211 | public CompletableFuture<Boolean> replace(String key, byte[] oldValue, byte[] newValue) { |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 212 | return client.submit(new UpdateAndGet(key, newValue, Match.ifValue(oldValue), Match.ANY)) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 213 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 214 | .thenApply(v -> v.updated()); |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | @SuppressWarnings("unchecked") |
| 219 | public CompletableFuture<Boolean> replace(String key, long oldVersion, byte[] newValue) { |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 220 | return client.submit(new UpdateAndGet(key, newValue, Match.ANY, Match.ifValue(oldVersion))) |
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 | public CompletableFuture<Void> clear() { |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 227 | return client.submit(new Clear()) |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 228 | .whenComplete((r, e) -> throwIfLocked(r)) |
| 229 | .thenApply(v -> null); |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | @SuppressWarnings("unchecked") |
| 234 | public CompletableFuture<Versioned<byte[]>> computeIf(String key, |
| 235 | Predicate<? super byte[]> condition, |
| 236 | BiFunction<? super String, ? super byte[], ? extends byte[]> remappingFunction) { |
| 237 | return get(key).thenCompose(r1 -> { |
| 238 | byte[] existingValue = r1 == null ? null : r1.value(); |
| 239 | // if the condition evaluates to false, return existing value. |
| 240 | if (!condition.test(existingValue)) { |
| 241 | return CompletableFuture.completedFuture(r1); |
| 242 | } |
| 243 | |
| 244 | AtomicReference<byte[]> computedValue = new AtomicReference<>(); |
| 245 | // if remappingFunction throws an exception, return the exception. |
| 246 | try { |
| 247 | computedValue.set(remappingFunction.apply(key, existingValue)); |
| 248 | } catch (Exception e) { |
| 249 | CompletableFuture<Versioned<byte[]>> future = new CompletableFuture<>(); |
| 250 | future.completeExceptionally(e); |
| 251 | return future; |
| 252 | } |
| 253 | if (computedValue.get() == null && r1 == null) { |
| 254 | return CompletableFuture.completedFuture(null); |
| 255 | } |
| 256 | Match<byte[]> valueMatch = r1 == null ? Match.NULL : Match.ANY; |
| 257 | Match<Long> versionMatch = r1 == null ? Match.ANY : Match.ifValue(r1.version()); |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 258 | return client.submit(new UpdateAndGet(key, |
Madan Jampani | 80d2e97 | 2016-07-12 17:11:52 -0700 | [diff] [blame] | 259 | computedValue.get(), |
| 260 | valueMatch, |
| 261 | versionMatch)) |
| 262 | .whenComplete((r, e) -> throwIfLocked(r.status())) |
| 263 | .thenCompose(r -> { |
| 264 | if (r.status() == MapEntryUpdateResult.Status.PRECONDITION_FAILED || |
| 265 | r.status() == MapEntryUpdateResult.Status.WRITE_LOCK) { |
| 266 | return Tools.exceptionalFuture(new ConsistentMapException.ConcurrentModification()); |
| 267 | } |
| 268 | return CompletableFuture.completedFuture(r); |
| 269 | }) |
| 270 | .thenApply(v -> v.newValue()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 271 | }); |
| 272 | } |
| 273 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 274 | @Override |
Madan Jampani | 0463cf9 | 2016-05-04 14:46:08 -0700 | [diff] [blame] | 275 | public synchronized CompletableFuture<Void> addListener(MapEventListener<String, byte[]> listener, |
| 276 | Executor executor) { |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 277 | if (mapEventListeners.isEmpty()) { |
Aaron Kruglikov | 3e29f66 | 2016-07-13 10:18:10 -0700 | [diff] [blame] | 278 | return client.submit(new Listen()).thenRun(() -> mapEventListeners.put(listener, executor)); |
Madan Jampani | 27a2a3d | 2016-03-03 08:46:24 -0800 | [diff] [blame] | 279 | } else { |
Madan Jampani | 0463cf9 | 2016-05-04 14:46:08 -0700 | [diff] [blame] | 280 | mapEventListeners.put(listener, executor); |
Madan Jampani | 40f022e | 2016-03-02 21:35:14 -0800 | [diff] [blame] | 281 | return CompletableFuture.completedFuture(null); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 282 | } |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public synchronized CompletableFuture<Void> removeListener(MapEventListener<String, byte[]> listener) { |
Madan Jampani | 0463cf9 | 2016-05-04 14:46:08 -0700 | [diff] [blame] | 287 | if (mapEventListeners.remove(listener) != null && mapEventListeners.isEmpty()) { |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 288 | return client.submit(new Unlisten()).thenApply(v -> null); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 289 | } |
| 290 | return CompletableFuture.completedFuture(null); |
| 291 | } |
| 292 | |
| 293 | private void throwIfLocked(MapEntryUpdateResult.Status status) { |
| 294 | if (status == MapEntryUpdateResult.Status.WRITE_LOCK) { |
| 295 | throw new ConcurrentModificationException("Cannot update map: Another transaction in progress"); |
| 296 | } |
| 297 | } |
| 298 | |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 299 | @Override |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 300 | public CompletableFuture<Version> begin(TransactionId transactionId) { |
Jordan Halterman | 5f97a30 | 2017-04-26 23:41:31 -0700 | [diff] [blame^] | 301 | return client.submit(new TransactionBegin(transactionId)).thenApply(Version::new); |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public CompletableFuture<Boolean> prepare( |
| 306 | TransactionLog<MapUpdate<String, byte[]>> transactionLog) { |
| 307 | return client.submit(new TransactionPrepare(transactionLog)) |
| 308 | .thenApply(v -> v == PrepareResult.OK); |
| 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public CompletableFuture<Boolean> prepareAndCommit( |
| 313 | TransactionLog<MapUpdate<String, byte[]>> transactionLog) { |
| 314 | return client.submit(new TransactionPrepareAndCommit(transactionLog)) |
| 315 | .thenApply(v -> v == PrepareResult.OK); |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | @Override |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 319 | public CompletableFuture<Void> commit(TransactionId transactionId) { |
Madan Jampani | 825a8b1 | 2016-06-06 19:42:01 -0700 | [diff] [blame] | 320 | return client.submit(new TransactionCommit(transactionId)).thenApply(v -> null); |
Madan Jampani | cadd70b | 2016-02-08 13:45:43 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | @Override |
Madan Jampani | 74da78b | 2016-02-09 21:18:36 -0800 | [diff] [blame] | 324 | public CompletableFuture<Void> rollback(TransactionId transactionId) { |
Jordan Halterman | 948d659 | 2017-04-20 17:18:24 -0700 | [diff] [blame] | 325 | return client.submit(new TransactionRollback(transactionId)).thenApply(v -> null); |
Madan Jampani | 542d9e2 | 2016-04-05 15:39:55 -0700 | [diff] [blame] | 326 | } |
Madan Jampani | 1d3b617 | 2016-04-28 13:22:57 -0700 | [diff] [blame] | 327 | |
| 328 | @Override |
| 329 | public void addStatusChangeListener(Consumer<Status> listener) { |
| 330 | statusChangeListeners.add(listener); |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public void removeStatusChangeListener(Consumer<Status> listener) { |
| 335 | statusChangeListeners.remove(listener); |
| 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public Collection<Consumer<Status>> statusChangeListeners() { |
| 340 | return ImmutableSet.copyOf(statusChangeListeners); |
| 341 | } |
Madan Jampani | fb78638 | 2016-06-13 10:25:35 -0700 | [diff] [blame] | 342 | |
| 343 | private boolean isListening() { |
| 344 | return !mapEventListeners.isEmpty(); |
| 345 | } |
Madan Jampani | 8e18c0e | 2016-06-20 14:08:16 -0700 | [diff] [blame] | 346 | } |