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