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.service; |
| 18 | |
| 19 | import java.util.Map; |
| 20 | import java.util.concurrent.CompletableFuture; |
| 21 | |
| 22 | import javax.annotation.concurrent.NotThreadSafe; |
| 23 | |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 24 | import org.onosproject.store.primitives.DefaultDocumentTree; |
| 25 | |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 26 | /** |
| 27 | * A hierarchical <a href="https://en.wikipedia.org/wiki/Document_Object_Model">document tree</a> data structure. |
| 28 | * |
| 29 | * @param <V> document tree value type |
| 30 | */ |
| 31 | @NotThreadSafe |
| 32 | public interface AsyncDocumentTree<V> extends DistributedPrimitive { |
| 33 | |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 34 | @Override |
| 35 | default Type primitiveType() { |
| 36 | return Type.DOCUMENT_TREE; |
| 37 | } |
| 38 | |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Returns the {@link DocumentPath path} to root of the tree. |
| 41 | * |
| 42 | * @return path to root of the tree |
| 43 | */ |
| 44 | DocumentPath root(); |
| 45 | |
| 46 | /** |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 47 | * Returns the children of node at specified path in the form of a mapping from child name to child value. |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 48 | * |
| 49 | * @param path path to the node |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 50 | * @return future for mapping from child name to child value |
Yuta HIGUCHI | d2db7ad | 2016-09-16 21:40:13 -0700 | [diff] [blame] | 51 | * @throws NoSuchDocumentPathException if the path does not point to a valid node |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 52 | */ |
| 53 | CompletableFuture<Map<String, Versioned<V>>> getChildren(DocumentPath path); |
| 54 | |
| 55 | /** |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 56 | * Returns the value of the tree node at specified path. |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 57 | * |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 58 | * @param path path to the node |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 59 | * @return future that will be either be completed with node's {@link Versioned versioned} value |
| 60 | * or {@code null} if path does not point to a valid node |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 61 | */ |
| 62 | CompletableFuture<Versioned<V>> get(DocumentPath path); |
| 63 | |
| 64 | /** |
| 65 | * Creates or updates a document tree node. |
| 66 | * |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 67 | * @param path path to the node |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 68 | * @param value value to be associated with the node ({@code null} is a valid value) |
| 69 | * @return future that will either be completed with the previous {@link Versioned versioned} |
| 70 | * value or {@code null} if there was no node previously at that path. |
| 71 | * Future will be completed with a {@code IllegalDocumentModificationException} |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 72 | * if the parent node (for the node to create/update) does not exist |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 73 | */ |
| 74 | CompletableFuture<Versioned<V>> set(DocumentPath path, V value); |
| 75 | |
| 76 | /** |
| 77 | * Creates a document tree node if one does not exist already. |
| 78 | * |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 79 | * @param path path to the node |
| 80 | * @param value the non-null value to be associated with the node |
| 81 | * @return future that is completed with {@code true} if the new node was successfully |
| 82 | * created. Future will be completed with {@code false} if a node already exists at the specified path. |
| 83 | * Future will be completed exceptionally with a {@code IllegalDocumentModificationException} if the parent |
| 84 | * node (for the node to create) does not exist |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 85 | */ |
| 86 | CompletableFuture<Boolean> create(DocumentPath path, V value); |
| 87 | |
| 88 | /** |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 89 | * Creates a document tree node recursively by creating all missing intermediate nodes in the path. |
| 90 | * |
| 91 | * @param path path to the node |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 92 | * @param value value to be associated with the node ({@code null} is a valid value) |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 93 | * @return future that is completed with {@code true} if the new node was successfully |
| 94 | * created. Future will be completed with {@code false} if a node already exists at the specified path |
| 95 | */ |
| 96 | CompletableFuture<Boolean> createRecursive(DocumentPath path, V value); |
| 97 | |
| 98 | /** |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 99 | * Conditionally updates a tree node if the current version matches a specified version. |
| 100 | * |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 101 | * @param path path to the node |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 102 | * @param newValue value to associate with the node ({@code null} is a valid value) |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 103 | * @param version current version of the node for update to occur |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 104 | * @return future that is either completed with {@code true} if the update was made |
| 105 | * or {@code false} if update did not happen |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 106 | */ |
| 107 | CompletableFuture<Boolean> replace(DocumentPath path, V newValue, long version); |
| 108 | |
| 109 | /** |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 110 | * Conditionally updates a tree node if the current node value matches a specified version. |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 111 | * |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 112 | * @param path path to the node |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 113 | * @param newValue value to associate with the node ({@code null} is a valid value) |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 114 | * @param currentValue current value of the node for update to occur |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 115 | * @return future that is either completed with {@code true} if the update was made |
| 116 | * or {@code false} if update did not happen |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 117 | */ |
| 118 | CompletableFuture<Boolean> replace(DocumentPath path, V newValue, V currentValue); |
| 119 | |
| 120 | /** |
| 121 | * Removes the node with the specified path. |
| 122 | * |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 123 | * @param path path to the node |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 124 | * @return future for the previous value. Future will be completed with a |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 125 | * {@code IllegalDocumentModificationException} if the node to be removed is either the root |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 126 | * node or has one or more children. Future will be completed with a |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 127 | * {@code NoSuchDocumentPathException} if the node to be removed does not exist |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 128 | */ |
| 129 | CompletableFuture<Versioned<V>> removeNode(DocumentPath path); |
| 130 | |
| 131 | /** |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 132 | * Registers a listener to be notified when the subtree rooted at the specified path |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 133 | * is modified. |
| 134 | * |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 135 | * @param path path to the node |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 136 | * @param listener listener to be notified |
Madan Jampani | 3188803 | 2016-09-14 14:37:58 -0700 | [diff] [blame] | 137 | * @return a future that is completed when the operation completes |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 138 | */ |
| 139 | CompletableFuture<Void> addListener(DocumentPath path, DocumentTreeListener<V> listener); |
| 140 | |
| 141 | /** |
| 142 | * Unregisters a previously added listener. |
| 143 | * |
| 144 | * @param listener listener to unregister |
Madan Jampani | 3188803 | 2016-09-14 14:37:58 -0700 | [diff] [blame] | 145 | * @return a future that is completed when the operation completes |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 146 | */ |
| 147 | CompletableFuture<Void> removeListener(DocumentTreeListener<V> listener); |
| 148 | |
| 149 | /** |
| 150 | * Registers a listener to be notified when the tree is modified. |
| 151 | * |
| 152 | * @param listener listener to be notified |
Madan Jampani | 3188803 | 2016-09-14 14:37:58 -0700 | [diff] [blame] | 153 | * @return a future that is completed when the operation completes |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 154 | */ |
| 155 | default CompletableFuture<Void> addListener(DocumentTreeListener<V> listener) { |
| 156 | return addListener(root(), listener); |
| 157 | } |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 158 | |
| 159 | /** |
| 160 | * Returns a new {@link DocumentTree} that is backed by this instance. |
| 161 | * |
| 162 | * @return new {@code DocumentTree} instance |
| 163 | */ |
| 164 | default DocumentTree<V> asDocumentTree() { |
| 165 | return asDocumentTree(DistributedPrimitive.DEFAULT_OPERATION_TIMEOUT_MILLIS); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Returns a new {@link DocumentTree} that is backed by this instance. |
| 170 | * |
| 171 | * @param timeoutMillis timeout duration for the returned DocumentTree operations |
| 172 | * @return new {@code DocumentTree} instance |
| 173 | */ |
| 174 | default DocumentTree<V> asDocumentTree(long timeoutMillis) { |
| 175 | return new DefaultDocumentTree<V>(this, timeoutMillis); |
| 176 | } |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 177 | } |