Jordan Halterman | cb1e02c | 2017-08-25 16:20:43 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.impl; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | import java.util.Map; |
| 20 | import java.util.Objects; |
| 21 | import java.util.TreeMap; |
| 22 | import java.util.concurrent.CompletableFuture; |
| 23 | import java.util.stream.Collectors; |
| 24 | |
| 25 | import com.google.common.collect.Maps; |
| 26 | import org.onlab.util.Tools; |
| 27 | import org.onosproject.cluster.PartitionId; |
| 28 | import org.onosproject.store.primitives.NodeUpdate; |
| 29 | import org.onosproject.store.primitives.TransactionId; |
| 30 | import org.onosproject.store.service.AsyncDocumentTree; |
| 31 | import org.onosproject.store.service.DocumentPath; |
| 32 | import org.onosproject.store.service.DocumentTreeListener; |
| 33 | import org.onosproject.store.service.NoSuchDocumentPathException; |
| 34 | import org.onosproject.store.service.TransactionLog; |
| 35 | import org.onosproject.store.service.Version; |
| 36 | import org.onosproject.store.service.Versioned; |
| 37 | |
| 38 | import static com.google.common.base.Preconditions.checkNotNull; |
| 39 | |
| 40 | /** |
| 41 | * Partitioned asynchronous document tree. |
| 42 | */ |
| 43 | public class PartitionedAsyncDocumentTree<V> implements AsyncDocumentTree<V> { |
| 44 | |
| 45 | private final String name; |
| 46 | private final TreeMap<PartitionId, AsyncDocumentTree<V>> partitions = Maps.newTreeMap(); |
| 47 | private final Hasher<DocumentPath> pathHasher; |
| 48 | |
| 49 | public PartitionedAsyncDocumentTree( |
| 50 | String name, |
| 51 | Map<PartitionId, AsyncDocumentTree<V>> partitions, |
| 52 | Hasher<DocumentPath> pathHasher) { |
| 53 | this.name = name; |
| 54 | this.partitions.putAll(checkNotNull(partitions)); |
| 55 | this.pathHasher = checkNotNull(pathHasher); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public String name() { |
| 60 | return name; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public DocumentPath root() { |
| 65 | return DocumentPath.ROOT; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns the document tree (partition) to which the specified path maps. |
| 70 | * |
| 71 | * @param path path |
| 72 | * @return AsyncConsistentMap to which path maps |
| 73 | */ |
| 74 | private AsyncDocumentTree<V> partition(DocumentPath path) { |
| 75 | return partitions.get(pathHasher.hash(path)); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns all the constituent trees. |
| 80 | * |
| 81 | * @return collection of partitions. |
| 82 | */ |
| 83 | private Collection<AsyncDocumentTree<V>> partitions() { |
| 84 | return partitions.values(); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public CompletableFuture<Map<String, Versioned<V>>> getChildren(DocumentPath path) { |
| 89 | return Tools.allOf(partitions().stream() |
| 90 | .map(partition -> partition.getChildren(path).exceptionally(r -> null)) |
| 91 | .collect(Collectors.toList())).thenApply(allChildren -> { |
| 92 | Map<String, Versioned<V>> children = Maps.newLinkedHashMap(); |
| 93 | allChildren.stream().filter(Objects::nonNull).forEach(children::putAll); |
| 94 | return children; |
| 95 | }); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public CompletableFuture<Versioned<V>> get(DocumentPath path) { |
| 100 | return partition(path).get(path); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public CompletableFuture<Versioned<V>> set(DocumentPath path, V value) { |
| 105 | return partition(path).set(path, value); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public CompletableFuture<Boolean> create(DocumentPath path, V value) { |
| 110 | // TODO: This operation is not atomic |
| 111 | return partition(path.parent()).get(path).thenCompose(parentValue -> { |
| 112 | if (parentValue == null) { |
| 113 | return Tools.exceptionalFuture(new NoSuchDocumentPathException(path.parent().toString())); |
| 114 | } else { |
| 115 | return partition(path).createRecursive(path, value); |
| 116 | } |
| 117 | }); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public CompletableFuture<Boolean> createRecursive(DocumentPath path, V value) { |
| 122 | return partition(path).createRecursive(path, value); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public CompletableFuture<Boolean> replace(DocumentPath path, V newValue, long version) { |
| 127 | return partition(path).replace(path, newValue, version); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public CompletableFuture<Boolean> replace(DocumentPath path, V newValue, V currentValue) { |
| 132 | return partition(path).replace(path, newValue, currentValue); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public CompletableFuture<Versioned<V>> removeNode(DocumentPath path) { |
| 137 | return partition(path).removeNode(path); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public CompletableFuture<Void> addListener(DocumentPath path, DocumentTreeListener<V> listener) { |
| 142 | return CompletableFuture.allOf(partitions().stream() |
| 143 | .map(map -> map.addListener(path, listener)) |
| 144 | .toArray(CompletableFuture[]::new)); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public CompletableFuture<Void> removeListener(DocumentTreeListener<V> listener) { |
| 149 | return CompletableFuture.allOf(partitions().stream() |
| 150 | .map(map -> map.removeListener(listener)) |
| 151 | .toArray(CompletableFuture[]::new)); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public CompletableFuture<Version> begin(TransactionId transactionId) { |
| 156 | throw new UnsupportedOperationException(); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public CompletableFuture<Boolean> prepare(TransactionLog<NodeUpdate<V>> transactionLog) { |
| 161 | throw new UnsupportedOperationException(); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public CompletableFuture<Boolean> prepareAndCommit(TransactionLog<NodeUpdate<V>> transactionLog) { |
| 166 | throw new UnsupportedOperationException(); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public CompletableFuture<Void> commit(TransactionId transactionId) { |
| 171 | throw new UnsupportedOperationException(); |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public CompletableFuture<Void> rollback(TransactionId transactionId) { |
| 176 | throw new UnsupportedOperationException(); |
| 177 | } |
| 178 | } |