blob: 71758a0aecf7b6661372984d7997aad32ff01de3 [file] [log] [blame]
Jordan Haltermancb1e02c2017-08-25 16:20:43 -07001/*
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 */
16package org.onosproject.store.primitives.impl;
17
18import java.util.Collection;
19import java.util.Map;
20import java.util.Objects;
21import java.util.TreeMap;
22import java.util.concurrent.CompletableFuture;
23import java.util.stream.Collectors;
24
25import com.google.common.collect.Maps;
26import org.onlab.util.Tools;
27import org.onosproject.cluster.PartitionId;
28import org.onosproject.store.primitives.NodeUpdate;
29import org.onosproject.store.primitives.TransactionId;
30import org.onosproject.store.service.AsyncDocumentTree;
31import org.onosproject.store.service.DocumentPath;
32import org.onosproject.store.service.DocumentTreeListener;
33import org.onosproject.store.service.NoSuchDocumentPathException;
34import org.onosproject.store.service.TransactionLog;
35import org.onosproject.store.service.Version;
36import org.onosproject.store.service.Versioned;
37
38import static com.google.common.base.Preconditions.checkNotNull;
39
40/**
41 * Partitioned asynchronous document tree.
42 */
43public 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) {
Yuta HIGUCHIff9af3e2017-09-12 13:21:13 -0700110 if (path.parent() == null) {
111 // create value on root
112 return partition(path).createRecursive(path, value);
113 }
Jordan Haltermancb1e02c2017-08-25 16:20:43 -0700114 // TODO: This operation is not atomic
Yuta HIGUCHIff9af3e2017-09-12 13:21:13 -0700115 return partition(path.parent()).get(path.parent()).thenCompose(parentValue -> {
Jordan Haltermancb1e02c2017-08-25 16:20:43 -0700116 if (parentValue == null) {
Yuta HIGUCHI30161e72017-09-11 16:38:07 -0700117 return Tools.exceptionalFuture(new NoSuchDocumentPathException(String.valueOf(path.parent())));
Jordan Haltermancb1e02c2017-08-25 16:20:43 -0700118 } else {
Yuta HIGUCHIff9af3e2017-09-12 13:21:13 -0700119 // not atomic: parent did exist at some point, so moving forward
Jordan Haltermancb1e02c2017-08-25 16:20:43 -0700120 return partition(path).createRecursive(path, value);
121 }
122 });
123 }
124
125 @Override
126 public CompletableFuture<Boolean> createRecursive(DocumentPath path, V value) {
127 return partition(path).createRecursive(path, value);
128 }
129
130 @Override
131 public CompletableFuture<Boolean> replace(DocumentPath path, V newValue, long version) {
132 return partition(path).replace(path, newValue, version);
133 }
134
135 @Override
136 public CompletableFuture<Boolean> replace(DocumentPath path, V newValue, V currentValue) {
137 return partition(path).replace(path, newValue, currentValue);
138 }
139
140 @Override
141 public CompletableFuture<Versioned<V>> removeNode(DocumentPath path) {
142 return partition(path).removeNode(path);
143 }
144
145 @Override
146 public CompletableFuture<Void> addListener(DocumentPath path, DocumentTreeListener<V> listener) {
147 return CompletableFuture.allOf(partitions().stream()
148 .map(map -> map.addListener(path, listener))
149 .toArray(CompletableFuture[]::new));
150 }
151
152 @Override
153 public CompletableFuture<Void> removeListener(DocumentTreeListener<V> listener) {
154 return CompletableFuture.allOf(partitions().stream()
155 .map(map -> map.removeListener(listener))
156 .toArray(CompletableFuture[]::new));
157 }
158
159 @Override
160 public CompletableFuture<Version> begin(TransactionId transactionId) {
161 throw new UnsupportedOperationException();
162 }
163
164 @Override
165 public CompletableFuture<Boolean> prepare(TransactionLog<NodeUpdate<V>> transactionLog) {
166 throw new UnsupportedOperationException();
167 }
168
169 @Override
170 public CompletableFuture<Boolean> prepareAndCommit(TransactionLog<NodeUpdate<V>> transactionLog) {
171 throw new UnsupportedOperationException();
172 }
173
174 @Override
175 public CompletableFuture<Void> commit(TransactionId transactionId) {
176 throw new UnsupportedOperationException();
177 }
178
179 @Override
180 public CompletableFuture<Void> rollback(TransactionId transactionId) {
181 throw new UnsupportedOperationException();
182 }
183}