Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.store.primitives.resources.impl; |
| 18 | |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 19 | import java.util.HashMap; |
| 20 | import java.util.List; |
| 21 | import java.util.Map; |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 22 | import java.util.Optional; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 23 | import java.util.concurrent.CompletableFuture; |
| 24 | import java.util.concurrent.Executor; |
| 25 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 26 | import com.google.common.util.concurrent.MoreExecutors; |
| 27 | import io.atomix.protocols.raft.proxy.RaftProxy; |
| 28 | import org.onlab.util.KryoNamespace; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 29 | import org.onlab.util.Match; |
| 30 | import org.onlab.util.Tools; |
Sithara Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 31 | import org.onosproject.store.primitives.NodeUpdate; |
| 32 | import org.onosproject.store.primitives.TransactionId; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 33 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.Get; |
| 34 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.GetChildren; |
| 35 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.Listen; |
| 36 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.Unlisten; |
| 37 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.Update; |
Sithara Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 38 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.TransactionBegin; |
| 39 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.TransactionPrepare; |
| 40 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.TransactionPrepareAndCommit; |
| 41 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.TransactionCommit; |
| 42 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.TransactionRollback; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 43 | import org.onosproject.store.serializers.KryoNamespaces; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 44 | import org.onosproject.store.service.AsyncDocumentTree; |
| 45 | import org.onosproject.store.service.DocumentPath; |
| 46 | import org.onosproject.store.service.DocumentTreeEvent; |
| 47 | import org.onosproject.store.service.DocumentTreeListener; |
| 48 | import org.onosproject.store.service.IllegalDocumentModificationException; |
| 49 | import org.onosproject.store.service.NoSuchDocumentPathException; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 50 | import org.onosproject.store.service.Serializer; |
Sithara Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 51 | import org.onosproject.store.service.TransactionLog; |
| 52 | import org.onosproject.store.service.Version; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 53 | import org.onosproject.store.service.Versioned; |
| 54 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 55 | import static com.google.common.base.Preconditions.checkNotNull; |
| 56 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeEvents.CHANGE; |
Sithara Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 57 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.BEGIN; |
| 58 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.PREPARE; |
| 59 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.PREPARE_AND_COMMIT; |
| 60 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.COMMIT; |
| 61 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.ROLLBACK; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 62 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.CLEAR; |
| 63 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.GET; |
| 64 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.GET_CHILDREN; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 65 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.UPDATE; |
Sithara Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 66 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.ADD_LISTENER; |
| 67 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.REMOVE_LISTENER; |
Jordan Halterman | e853d03 | 2017-08-01 15:10:28 -0700 | [diff] [blame] | 68 | import static org.onosproject.store.primitives.resources.impl.DocumentTreeResult.Status.ILLEGAL_MODIFICATION; |
| 69 | import static org.onosproject.store.primitives.resources.impl.DocumentTreeResult.Status.INVALID_PATH; |
| 70 | import static org.onosproject.store.primitives.resources.impl.DocumentTreeResult.Status.OK; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Distributed resource providing the {@link AsyncDocumentTree} primitive. |
| 74 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 75 | public class AtomixDocumentTree extends AbstractRaftPrimitive implements AsyncDocumentTree<byte[]> { |
| 76 | private static final Serializer SERIALIZER = Serializer.using(KryoNamespace.newBuilder() |
| 77 | .register(KryoNamespaces.BASIC) |
| 78 | .register(AtomixDocumentTreeOperations.NAMESPACE) |
| 79 | .register(AtomixDocumentTreeEvents.NAMESPACE) |
| 80 | .build()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 81 | |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 82 | private final Map<DocumentTreeListener<byte[]>, InternalListener> eventListeners = new HashMap<>(); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 83 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 84 | public AtomixDocumentTree(RaftProxy proxy) { |
| 85 | super(proxy); |
| 86 | proxy.addStateChangeListener(state -> { |
| 87 | if (state == RaftProxy.State.CONNECTED && isListening()) { |
| 88 | proxy.invoke(ADD_LISTENER, SERIALIZER::encode, new Listen()); |
| 89 | } |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 90 | }); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 91 | proxy.addEventListener(CHANGE, SERIALIZER::decode, this::processTreeUpdates); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public Type primitiveType() { |
| 96 | return Type.DOCUMENT_TREE; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public CompletableFuture<Void> destroy() { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 101 | return proxy.invoke(CLEAR); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public DocumentPath root() { |
Jordan Halterman | cb1e02c | 2017-08-25 16:20:43 -0700 | [diff] [blame^] | 106 | return DocumentPath.ROOT; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public CompletableFuture<Map<String, Versioned<byte[]>>> getChildren(DocumentPath path) { |
Jordan Halterman | e853d03 | 2017-08-01 15:10:28 -0700 | [diff] [blame] | 111 | return proxy.<GetChildren, DocumentTreeResult<Map<String, Versioned<byte[]>>>>invoke( |
| 112 | GET_CHILDREN, |
| 113 | SERIALIZER::encode, |
| 114 | new GetChildren(checkNotNull(path)), |
| 115 | SERIALIZER::decode) |
| 116 | .thenCompose(result -> { |
| 117 | if (result.status() == INVALID_PATH) { |
| 118 | return Tools.exceptionalFuture(new NoSuchDocumentPathException()); |
| 119 | } else if (result.status() == ILLEGAL_MODIFICATION) { |
| 120 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
| 121 | } else { |
| 122 | return CompletableFuture.completedFuture(result); |
| 123 | } |
| 124 | }).thenApply(result -> result.result()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public CompletableFuture<Versioned<byte[]>> get(DocumentPath path) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 129 | return proxy.invoke(GET, SERIALIZER::encode, new Get(checkNotNull(path)), SERIALIZER::decode); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public CompletableFuture<Versioned<byte[]>> set(DocumentPath path, byte[] value) { |
Jordan Halterman | e853d03 | 2017-08-01 15:10:28 -0700 | [diff] [blame] | 134 | return proxy.<Update, DocumentTreeResult<Versioned<byte[]>>>invoke(UPDATE, |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 135 | SERIALIZER::encode, |
| 136 | new Update(checkNotNull(path), Optional.ofNullable(value), Match.any(), Match.any()), |
| 137 | SERIALIZER::decode) |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 138 | .thenCompose(result -> { |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 139 | if (result.status() == INVALID_PATH) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 140 | return Tools.exceptionalFuture(new NoSuchDocumentPathException()); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 141 | } else if (result.status() == ILLEGAL_MODIFICATION) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 142 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
| 143 | } else { |
| 144 | return CompletableFuture.completedFuture(result); |
| 145 | } |
Jordan Halterman | e853d03 | 2017-08-01 15:10:28 -0700 | [diff] [blame] | 146 | }).thenApply(result -> result.result()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public CompletableFuture<Boolean> create(DocumentPath path, byte[] value) { |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 151 | return createInternal(path, value) |
| 152 | .thenCompose(status -> { |
| 153 | if (status == ILLEGAL_MODIFICATION) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 154 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 155 | } |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 156 | return CompletableFuture.completedFuture(true); |
| 157 | }); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public CompletableFuture<Boolean> createRecursive(DocumentPath path, byte[] value) { |
| 162 | return createInternal(path, value) |
| 163 | .thenCompose(status -> { |
| 164 | if (status == ILLEGAL_MODIFICATION) { |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 165 | return createRecursive(path.parent(), null) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 166 | .thenCompose(r -> createInternal(path, value).thenApply(v -> true)); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 167 | } |
| 168 | return CompletableFuture.completedFuture(status == OK); |
| 169 | }); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public CompletableFuture<Boolean> replace(DocumentPath path, byte[] newValue, long version) { |
Jordan Halterman | e853d03 | 2017-08-01 15:10:28 -0700 | [diff] [blame] | 174 | return proxy.<Update, DocumentTreeResult<byte[]>>invoke(UPDATE, |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 175 | SERIALIZER::encode, |
| 176 | new Update(checkNotNull(path), |
| 177 | Optional.ofNullable(newValue), |
| 178 | Match.any(), |
| 179 | Match.ifValue(version)), SERIALIZER::decode) |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 180 | .thenApply(result -> result.updated()); |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public CompletableFuture<Boolean> replace(DocumentPath path, byte[] newValue, byte[] currentValue) { |
Jordan Halterman | e853d03 | 2017-08-01 15:10:28 -0700 | [diff] [blame] | 185 | return proxy.<Update, DocumentTreeResult<byte[]>>invoke(UPDATE, |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 186 | SERIALIZER::encode, |
| 187 | new Update(checkNotNull(path), |
| 188 | Optional.ofNullable(newValue), |
| 189 | Match.ifValue(currentValue), |
| 190 | Match.any()), |
| 191 | SERIALIZER::decode) |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 192 | .thenCompose(result -> { |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 193 | if (result.status() == INVALID_PATH) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 194 | return Tools.exceptionalFuture(new NoSuchDocumentPathException()); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 195 | } else if (result.status() == ILLEGAL_MODIFICATION) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 196 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
| 197 | } else { |
| 198 | return CompletableFuture.completedFuture(result); |
| 199 | } |
| 200 | }).thenApply(result -> result.updated()); |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public CompletableFuture<Versioned<byte[]>> removeNode(DocumentPath path) { |
| 205 | if (path.equals(DocumentPath.from("root"))) { |
| 206 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
| 207 | } |
Jordan Halterman | e853d03 | 2017-08-01 15:10:28 -0700 | [diff] [blame] | 208 | return proxy.<Update, DocumentTreeResult<Versioned<byte[]>>>invoke(UPDATE, |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 209 | SERIALIZER::encode, |
| 210 | new Update(checkNotNull(path), null, Match.any(), Match.ifNotNull()), |
| 211 | SERIALIZER::decode) |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 212 | .thenCompose(result -> { |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 213 | if (result.status() == INVALID_PATH) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 214 | return Tools.exceptionalFuture(new NoSuchDocumentPathException()); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 215 | } else if (result.status() == ILLEGAL_MODIFICATION) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 216 | return Tools.exceptionalFuture(new IllegalDocumentModificationException()); |
| 217 | } else { |
| 218 | return CompletableFuture.completedFuture(result); |
| 219 | } |
Jordan Halterman | e853d03 | 2017-08-01 15:10:28 -0700 | [diff] [blame] | 220 | }).thenApply(result -> result.result()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public CompletableFuture<Void> addListener(DocumentPath path, DocumentTreeListener<byte[]> listener) { |
| 225 | checkNotNull(path); |
| 226 | checkNotNull(listener); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 227 | InternalListener internalListener = new InternalListener(path, listener, MoreExecutors.directExecutor()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 228 | // TODO: Support API that takes an executor |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 229 | if (!eventListeners.containsKey(listener)) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 230 | return proxy.invoke(ADD_LISTENER, SERIALIZER::encode, new Listen(path)) |
| 231 | .thenRun(() -> eventListeners.put(listener, internalListener)); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 232 | } |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 233 | return CompletableFuture.completedFuture(null); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public CompletableFuture<Void> removeListener(DocumentTreeListener<byte[]> listener) { |
| 238 | checkNotNull(listener); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 239 | InternalListener internalListener = eventListeners.remove(listener); |
| 240 | if (internalListener != null && eventListeners.isEmpty()) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 241 | return proxy.invoke(REMOVE_LISTENER, SERIALIZER::encode, new Unlisten(internalListener.path)) |
| 242 | .thenApply(v -> null); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 243 | } |
| 244 | return CompletableFuture.completedFuture(null); |
| 245 | } |
| 246 | |
Jordan Halterman | e853d03 | 2017-08-01 15:10:28 -0700 | [diff] [blame] | 247 | private CompletableFuture<DocumentTreeResult.Status> createInternal(DocumentPath path, byte[] value) { |
| 248 | return proxy.<Update, DocumentTreeResult<byte[]>>invoke(UPDATE, |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 249 | SERIALIZER::encode, |
| 250 | new Update(checkNotNull(path), Optional.ofNullable(value), Match.any(), Match.ifNull()), |
| 251 | SERIALIZER::decode) |
| 252 | .thenApply(result -> result.status()); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 255 | private boolean isListening() { |
| 256 | return !eventListeners.isEmpty(); |
| 257 | } |
| 258 | |
| 259 | private void processTreeUpdates(List<DocumentTreeEvent<byte[]>> events) { |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 260 | events.forEach(event -> eventListeners.values().forEach(listener -> listener.event(event))); |
| 261 | } |
| 262 | |
Sithara Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 263 | @Override |
| 264 | public CompletableFuture<Version> begin(TransactionId transactionId) { |
| 265 | return proxy.<TransactionBegin, Long>invoke( |
| 266 | BEGIN, |
| 267 | SERIALIZER::encode, |
| 268 | new TransactionBegin(transactionId), |
| 269 | SERIALIZER::decode) |
| 270 | .thenApply(Version::new); |
| 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public CompletableFuture<Boolean> prepare(TransactionLog<NodeUpdate<byte[]>> transactionLog) { |
| 275 | return proxy.<TransactionPrepare, PrepareResult>invoke( |
| 276 | PREPARE, |
| 277 | SERIALIZER::encode, |
| 278 | new TransactionPrepare(transactionLog), |
| 279 | SERIALIZER::decode) |
| 280 | .thenApply(v -> v == PrepareResult.OK); |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public CompletableFuture<Boolean> prepareAndCommit(TransactionLog<NodeUpdate<byte[]>> transactionLog) { |
| 285 | return proxy.<TransactionPrepareAndCommit, PrepareResult>invoke( |
| 286 | PREPARE_AND_COMMIT, |
| 287 | SERIALIZER::encode, |
| 288 | new TransactionPrepareAndCommit(transactionLog), |
| 289 | SERIALIZER::decode) |
| 290 | .thenApply(v -> v == PrepareResult.OK); |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public CompletableFuture<Void> commit(TransactionId transactionId) { |
| 295 | return proxy.<TransactionCommit, CommitResult>invoke( |
| 296 | COMMIT, |
| 297 | SERIALIZER::encode, |
| 298 | new TransactionCommit(transactionId), |
| 299 | SERIALIZER::decode) |
| 300 | .thenApply(v -> null); |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public CompletableFuture<Void> rollback(TransactionId transactionId) { |
| 305 | return proxy.invoke( |
| 306 | ROLLBACK, |
| 307 | SERIALIZER::encode, |
| 308 | new TransactionRollback(transactionId), |
| 309 | SERIALIZER::decode) |
| 310 | .thenApply(v -> null); |
| 311 | } |
| 312 | |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 313 | private class InternalListener implements DocumentTreeListener<byte[]> { |
| 314 | |
| 315 | private final DocumentPath path; |
| 316 | private final DocumentTreeListener<byte[]> listener; |
| 317 | private final Executor executor; |
| 318 | |
| 319 | public InternalListener(DocumentPath path, DocumentTreeListener<byte[]> listener, Executor executor) { |
| 320 | this.path = path; |
| 321 | this.listener = listener; |
| 322 | this.executor = executor; |
| 323 | } |
| 324 | |
| 325 | @Override |
| 326 | public void event(DocumentTreeEvent<byte[]> event) { |
| 327 | if (event.path().isDescendentOf(path)) { |
| 328 | executor.execute(() -> listener.event(event)); |
| 329 | } |
| 330 | } |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 331 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 332 | } |