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