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 |
Yuta HIGUCHI | d2db7ad | 2016-09-16 21:40:13 -0700 | [diff] [blame] | 44 | * @throws 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 |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 52 | * @return future that will be either be completed with node's {@link Versioned versioned} value |
| 53 | * or {@code null} if path 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 |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 61 | * @param value value to be associated with the node ({@code null} is a valid value) |
| 62 | * @return future that will either be completed with the previous {@link Versioned versioned} |
| 63 | * value or {@code null} if there was no node previously at that path. |
| 64 | * Future will be completed with a {@code IllegalDocumentModificationException} |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 65 | * 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] | 66 | */ |
| 67 | CompletableFuture<Versioned<V>> set(DocumentPath path, V value); |
| 68 | |
| 69 | /** |
| 70 | * Creates a document tree node if one does not exist already. |
| 71 | * |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 72 | * @param path path to the node |
| 73 | * @param value the non-null value to be associated with the node |
| 74 | * @return future that is completed with {@code true} if the new node was successfully |
| 75 | * created. Future will be completed with {@code false} if a node already exists at the specified path. |
| 76 | * Future will be completed exceptionally with a {@code IllegalDocumentModificationException} if the parent |
| 77 | * node (for the node to create) does not exist |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 78 | */ |
| 79 | CompletableFuture<Boolean> create(DocumentPath path, V value); |
| 80 | |
| 81 | /** |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 82 | * Creates a document tree node recursively by creating all missing intermediate nodes in the path. |
| 83 | * |
| 84 | * @param path path to the node |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 85 | * @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] | 86 | * @return future that is completed with {@code true} if the new node was successfully |
| 87 | * created. Future will be completed with {@code false} if a node already exists at the specified path |
| 88 | */ |
| 89 | CompletableFuture<Boolean> createRecursive(DocumentPath path, V value); |
| 90 | |
| 91 | /** |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 92 | * Conditionally updates a tree node if the current version matches a specified version. |
| 93 | * |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 94 | * @param path path to the node |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 95 | * @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] | 96 | * @param version current version of the node for update to occur |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 97 | * @return future that is either completed with {@code true} if the update was made |
| 98 | * or {@code false} if update did not happen |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 99 | */ |
| 100 | CompletableFuture<Boolean> replace(DocumentPath path, V newValue, long version); |
| 101 | |
| 102 | /** |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 103 | * 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] | 104 | * |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 105 | * @param path path to the node |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 106 | * @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] | 107 | * @param currentValue current value of the node for update to occur |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 108 | * @return future that is either completed with {@code true} if the update was made |
| 109 | * or {@code false} if update did not happen |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 110 | */ |
| 111 | CompletableFuture<Boolean> replace(DocumentPath path, V newValue, V currentValue); |
| 112 | |
| 113 | /** |
| 114 | * Removes the node with the specified path. |
| 115 | * |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 116 | * @param path path to the node |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 117 | * @return future for the previous value. Future will be completed with a |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 118 | * {@code IllegalDocumentModificationException} if the node to be removed is either the root |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 119 | * 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] | 120 | * {@code NoSuchDocumentPathException} if the node to be removed does not exist |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 121 | */ |
| 122 | CompletableFuture<Versioned<V>> removeNode(DocumentPath path); |
| 123 | |
| 124 | /** |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 125 | * 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] | 126 | * is modified. |
| 127 | * |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 128 | * @param path path to the node |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 129 | * @param listener listener to be notified |
Madan Jampani | 3188803 | 2016-09-14 14:37:58 -0700 | [diff] [blame] | 130 | * @return a future that is completed when the operation completes |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 131 | */ |
| 132 | CompletableFuture<Void> addListener(DocumentPath path, DocumentTreeListener<V> listener); |
| 133 | |
| 134 | /** |
| 135 | * Unregisters a previously added listener. |
| 136 | * |
| 137 | * @param listener listener to unregister |
Madan Jampani | 3188803 | 2016-09-14 14:37:58 -0700 | [diff] [blame] | 138 | * @return a future that is completed when the operation completes |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 139 | */ |
| 140 | CompletableFuture<Void> removeListener(DocumentTreeListener<V> listener); |
| 141 | |
| 142 | /** |
| 143 | * Registers a listener to be notified when the tree is modified. |
| 144 | * |
| 145 | * @param listener listener to be notified |
Madan Jampani | 3188803 | 2016-09-14 14:37:58 -0700 | [diff] [blame] | 146 | * @return a future that is completed when the operation completes |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 147 | */ |
| 148 | default CompletableFuture<Void> addListener(DocumentTreeListener<V> listener) { |
| 149 | return addListener(root(), listener); |
| 150 | } |
| 151 | } |