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