Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -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 | ad5b8c7 | 2016-09-12 15:05:01 -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 | |
Yuta HIGUCHI | 6b2ec28 | 2017-11-03 21:08:38 -0700 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkArgument; |
| 20 | |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 21 | import java.util.Iterator; |
| 22 | import java.util.Map; |
| 23 | import java.util.Objects; |
Yuta HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame^] | 24 | import java.util.Optional; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 25 | import java.util.concurrent.atomic.AtomicLong; |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 26 | |
| 27 | import org.onosproject.store.service.DocumentPath; |
| 28 | import org.onosproject.store.service.DocumentTree; |
| 29 | import org.onosproject.store.service.DocumentTreeListener; |
| 30 | import org.onosproject.store.service.DocumentTreeNode; |
| 31 | import org.onosproject.store.service.IllegalDocumentModificationException; |
| 32 | import org.onosproject.store.service.NoSuchDocumentPathException; |
Jordan Halterman | d0d8035 | 2017-08-10 15:08:27 -0700 | [diff] [blame] | 33 | import org.onosproject.store.service.Ordering; |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 34 | import org.onosproject.store.service.Versioned; |
| 35 | |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 36 | import com.google.common.base.Supplier; |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 37 | import com.google.common.collect.Maps; |
| 38 | |
| 39 | /** |
| 40 | * Simple implementation of a {@link DocumentTree}. |
| 41 | * |
| 42 | * @param <V> tree node value type |
| 43 | */ |
| 44 | public class DefaultDocumentTree<V> implements DocumentTree<V> { |
| 45 | |
| 46 | private static final DocumentPath ROOT_PATH = DocumentPath.from("root"); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 47 | final DefaultDocumentTreeNode<V> root; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 48 | private final Supplier<Long> versionSupplier; |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 49 | |
| 50 | public DefaultDocumentTree() { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 51 | AtomicLong versionCounter = new AtomicLong(0); |
| 52 | versionSupplier = versionCounter::incrementAndGet; |
Yuta HIGUCHI | 6b2ec28 | 2017-11-03 21:08:38 -0700 | [diff] [blame] | 53 | root = new DefaultDocumentTreeNode<>(ROOT_PATH, null, versionSupplier.get(), Ordering.NATURAL, null); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Jordan Halterman | d0d8035 | 2017-08-10 15:08:27 -0700 | [diff] [blame] | 56 | public DefaultDocumentTree(Supplier<Long> versionSupplier, Ordering ordering) { |
Yuta HIGUCHI | 6b2ec28 | 2017-11-03 21:08:38 -0700 | [diff] [blame] | 57 | root = new DefaultDocumentTreeNode<>(ROOT_PATH, null, versionSupplier.get(), ordering, null); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 58 | this.versionSupplier = versionSupplier; |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 61 | DefaultDocumentTree(Supplier<Long> versionSupplier, DefaultDocumentTreeNode<V> root) { |
| 62 | this.root = root; |
| 63 | this.versionSupplier = versionSupplier; |
| 64 | } |
| 65 | |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 66 | @Override |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 67 | public String name() { |
| 68 | return null; |
| 69 | } |
| 70 | |
| 71 | @Override |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 72 | public DocumentPath root() { |
| 73 | return ROOT_PATH; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public Map<String, Versioned<V>> getChildren(DocumentPath path) { |
| 78 | DocumentTreeNode<V> node = getNode(path); |
| 79 | if (node != null) { |
Jordan Halterman | d0d8035 | 2017-08-10 15:08:27 -0700 | [diff] [blame] | 80 | Map<String, Versioned<V>> childrenValues = Maps.newLinkedHashMap(); |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 81 | node.children().forEachRemaining(n -> childrenValues.put(simpleName(n.path()), n.value())); |
| 82 | return childrenValues; |
| 83 | } |
| 84 | throw new NoSuchDocumentPathException(); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public Versioned<V> get(DocumentPath path) { |
| 89 | DocumentTreeNode<V> currentNode = getNode(path); |
| 90 | return currentNode != null ? currentNode.value() : null; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public Versioned<V> set(DocumentPath path, V value) { |
| 95 | checkRootModification(path); |
| 96 | DefaultDocumentTreeNode<V> node = getNode(path); |
| 97 | if (node != null) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 98 | return node.update(value, versionSupplier.get()); |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 99 | } else { |
| 100 | create(path, value); |
| 101 | return null; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public boolean create(DocumentPath path, V value) { |
| 107 | checkRootModification(path); |
| 108 | DocumentTreeNode<V> node = getNode(path); |
| 109 | if (node != null) { |
| 110 | return false; |
| 111 | } |
| 112 | DocumentPath parentPath = path.parent(); |
| 113 | DefaultDocumentTreeNode<V> parentNode = getNode(parentPath); |
| 114 | if (parentNode == null) { |
| 115 | throw new IllegalDocumentModificationException(); |
| 116 | } |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 117 | parentNode.addChild(simpleName(path), value, versionSupplier.get()); |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | |
| 121 | @Override |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 122 | public boolean createRecursive(DocumentPath path, V value) { |
| 123 | checkRootModification(path); |
| 124 | DocumentTreeNode<V> node = getNode(path); |
| 125 | if (node != null) { |
| 126 | return false; |
| 127 | } |
| 128 | DocumentPath parentPath = path.parent(); |
| 129 | if (getNode(parentPath) == null) { |
| 130 | createRecursive(parentPath, null); |
| 131 | } |
| 132 | DefaultDocumentTreeNode<V> parentNode = getNode(parentPath); |
| 133 | if (parentNode == null) { |
| 134 | throw new IllegalDocumentModificationException(); |
| 135 | } |
| 136 | parentNode.addChild(simpleName(path), value, versionSupplier.get()); |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | @Override |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 141 | public boolean replace(DocumentPath path, V newValue, long version) { |
| 142 | checkRootModification(path); |
| 143 | DocumentTreeNode<V> node = getNode(path); |
| 144 | if (node != null && node.value() != null && node.value().version() == version) { |
Jordan Halterman | 9052b20 | 2017-08-01 09:41:32 -0700 | [diff] [blame] | 145 | set(path, newValue); |
| 146 | return true; |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 147 | } |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | @Override |
Yuta HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame^] | 152 | public boolean replace(DocumentPath path, V newValue, V expectedValue) { |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 153 | checkRootModification(path); |
Yuta HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame^] | 154 | if (Objects.equals(newValue, expectedValue)) { |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 155 | return false; |
| 156 | } |
| 157 | DocumentTreeNode<V> node = getNode(path); |
Yuta HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame^] | 158 | V prevValue = Optional.ofNullable(node) |
| 159 | .map(DocumentTreeNode::value) |
| 160 | .map(Versioned::valueOrNull) |
| 161 | .orElse(null); |
| 162 | if (Objects.equals(prevValue, expectedValue)) { |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 163 | set(path, newValue); |
| 164 | return true; |
| 165 | } |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public Versioned<V> removeNode(DocumentPath path) { |
| 171 | checkRootModification(path); |
| 172 | DefaultDocumentTreeNode<V> nodeToRemove = getNode(path); |
| 173 | if (nodeToRemove == null) { |
| 174 | throw new NoSuchDocumentPathException(); |
| 175 | } |
| 176 | if (nodeToRemove.hasChildren()) { |
| 177 | throw new IllegalDocumentModificationException(); |
| 178 | } |
| 179 | DefaultDocumentTreeNode<V> parent = (DefaultDocumentTreeNode<V>) nodeToRemove.parent(); |
| 180 | parent.removeChild(simpleName(path)); |
| 181 | return nodeToRemove.value(); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void addListener(DocumentPath path, DocumentTreeListener<V> listener) { |
| 186 | // TODO Auto-generated method stub |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void removeListener(DocumentTreeListener<V> listener) { |
| 191 | // TODO Auto-generated method stub |
| 192 | } |
| 193 | |
| 194 | private DefaultDocumentTreeNode<V> getNode(DocumentPath path) { |
| 195 | Iterator<String> pathElements = path.pathElements().iterator(); |
| 196 | DefaultDocumentTreeNode<V> currentNode = root; |
Yuta HIGUCHI | 6b2ec28 | 2017-11-03 21:08:38 -0700 | [diff] [blame] | 197 | checkArgument("root".equals(pathElements.next()), "Path should start with root: %s", path); |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 198 | while (pathElements.hasNext() && currentNode != null) { |
| 199 | currentNode = (DefaultDocumentTreeNode<V>) currentNode.child(pathElements.next()); |
| 200 | } |
| 201 | return currentNode; |
| 202 | } |
| 203 | |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 204 | private String simpleName(DocumentPath path) { |
| 205 | return path.pathElements().get(path.pathElements().size() - 1); |
| 206 | } |
| 207 | |
| 208 | private void checkRootModification(DocumentPath path) { |
| 209 | if (ROOT_PATH.equals(path)) { |
| 210 | throw new IllegalDocumentModificationException(); |
| 211 | } |
| 212 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 213 | } |