Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | |
| 24 | /** |
| 25 | * A hierarchical <a href="https://en.wikipedia.org/wiki/Document_Object_Model">document tree</a> data structure. |
| 26 | * |
| 27 | * @param <V> document tree value type |
| 28 | */ |
| 29 | @NotThreadSafe |
| 30 | public interface AsyncDocumentTree<V> extends DistributedPrimitive { |
| 31 | |
| 32 | /** |
| 33 | * Returns the {@link DocumentPath path} to root of the tree. |
| 34 | * |
| 35 | * @return path to root of the tree |
| 36 | */ |
| 37 | DocumentPath root(); |
| 38 | |
| 39 | /** |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame^] | 40 | * 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] | 41 | * |
| 42 | * @param path path to the node |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame^] | 43 | * @return future for mapping from child name to child value |
| 44 | * @throws {@code NoSuchDocumentPathException} if the path does not point to a valid node |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 45 | */ |
| 46 | CompletableFuture<Map<String, Versioned<V>>> getChildren(DocumentPath path); |
| 47 | |
| 48 | /** |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame^] | 49 | * Returns the value of the tree node at specified path. |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 50 | * |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame^] | 51 | * @param path path to the node |
| 52 | * @return future that will be either be completed with node value or {@code null} if path |
| 53 | * does not point to a valid node |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 54 | */ |
| 55 | CompletableFuture<Versioned<V>> get(DocumentPath path); |
| 56 | |
| 57 | /** |
| 58 | * Creates or updates a document tree node. |
| 59 | * |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame^] | 60 | * @param path path to the node |
| 61 | * @param value the non-null value to be associated with the node |
| 62 | * @return future that will either be completed with the previous node value or {@code null} if there was no |
| 63 | * node previously at that path. Future will be completed with a {@code IllegalDocumentModificationException} |
| 64 | * 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] | 65 | */ |
| 66 | CompletableFuture<Versioned<V>> set(DocumentPath path, V value); |
| 67 | |
| 68 | /** |
| 69 | * Creates a document tree node if one does not exist already. |
| 70 | * |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame^] | 71 | * @param path path to the node |
| 72 | * @param value the non-null value to be associated with the node |
| 73 | * @return future that is completed with {@code true} if the new node was successfully |
| 74 | * created. Future will be completed with {@code false} if a node already exists at the specified path. |
| 75 | * Future will be completed exceptionally with a {@code IllegalDocumentModificationException} if the parent |
| 76 | * node (for the node to create) does not exist |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 77 | */ |
| 78 | CompletableFuture<Boolean> create(DocumentPath path, V value); |
| 79 | |
| 80 | /** |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame^] | 81 | * Creates a document tree node recursively by creating all missing intermediate nodes in the path. |
| 82 | * |
| 83 | * @param path path to the node |
| 84 | * @param value the non-null value to be associated with the key |
| 85 | * @return future that is completed with {@code true} if the new node was successfully |
| 86 | * created. Future will be completed with {@code false} if a node already exists at the specified path |
| 87 | */ |
| 88 | CompletableFuture<Boolean> createRecursive(DocumentPath path, V value); |
| 89 | |
| 90 | /** |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 91 | * Conditionally updates a tree node if the current version matches a specified version. |
| 92 | * |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame^] | 93 | * @param path path to the node |
| 94 | * @param newValue the non-null value to be associated with the node |
| 95 | * @param version current version of the node for update to occur |
| 96 | * @return future that is either completed with {@code true} if the update was made and the tree was |
| 97 | * modified or {@code false} if update did not happen |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 98 | */ |
| 99 | CompletableFuture<Boolean> replace(DocumentPath path, V newValue, long version); |
| 100 | |
| 101 | /** |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame^] | 102 | * 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] | 103 | * |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame^] | 104 | * @param path path to the node |
| 105 | * @param newValue the non-null value to be associated with the node |
| 106 | * @param currentValue current value of the node for update to occur |
| 107 | * @return future that is either completed with {@code true} if the update was made and the tree was |
| 108 | * modified or {@code false} if update did not happen |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 109 | */ |
| 110 | CompletableFuture<Boolean> replace(DocumentPath path, V newValue, V currentValue); |
| 111 | |
| 112 | /** |
| 113 | * Removes the node with the specified path. |
| 114 | * |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame^] | 115 | * @param path path to the node |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 116 | * @return future for the previous value. Future will be completed with a |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame^] | 117 | * {@code IllegalDocumentModificationException} if the node to be removed is either the root |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 118 | * 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^] | 119 | * {@code NoSuchDocumentPathException} if the node to be removed does not exist |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 120 | */ |
| 121 | CompletableFuture<Versioned<V>> removeNode(DocumentPath path); |
| 122 | |
| 123 | /** |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame^] | 124 | * 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] | 125 | * is modified. |
| 126 | * |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame^] | 127 | * @param path path to the node |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 128 | * @param listener listener to be notified |
Madan Jampani | 3188803 | 2016-09-14 14:37:58 -0700 | [diff] [blame] | 129 | * @return a future that is completed when the operation completes |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 130 | */ |
| 131 | CompletableFuture<Void> addListener(DocumentPath path, DocumentTreeListener<V> listener); |
| 132 | |
| 133 | /** |
| 134 | * Unregisters a previously added listener. |
| 135 | * |
| 136 | * @param listener listener to unregister |
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> removeListener(DocumentTreeListener<V> listener); |
| 140 | |
| 141 | /** |
| 142 | * Registers a listener to be notified when the tree is modified. |
| 143 | * |
| 144 | * @param listener listener to be notified |
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 | default CompletableFuture<Void> addListener(DocumentTreeListener<V> listener) { |
| 148 | return addListener(root(), listener); |
| 149 | } |
| 150 | } |