blob: 976772eeb9baf56a6f34522af8418b84879a4a11 [file] [log] [blame]
Madan Jampani79924fa2016-09-13 13:57:03 -07001/*
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
17package org.onosproject.store.service;
18
19import java.util.Map;
20import java.util.concurrent.CompletableFuture;
21
22import 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
30public 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 Jampani86983282016-09-15 14:55:48 -070040 * Returns the children of node at specified path in the form of a mapping from child name to child value.
Madan Jampani79924fa2016-09-13 13:57:03 -070041 *
42 * @param path path to the node
Madan Jampani86983282016-09-15 14:55:48 -070043 * @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 Jampani79924fa2016-09-13 13:57:03 -070045 */
46 CompletableFuture<Map<String, Versioned<V>>> getChildren(DocumentPath path);
47
48 /**
Madan Jampani86983282016-09-15 14:55:48 -070049 * Returns the value of the tree node at specified path.
Madan Jampani79924fa2016-09-13 13:57:03 -070050 *
Madan Jampani86983282016-09-15 14:55:48 -070051 * @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 Jampani79924fa2016-09-13 13:57:03 -070054 */
55 CompletableFuture<Versioned<V>> get(DocumentPath path);
56
57 /**
58 * Creates or updates a document tree node.
59 *
Madan Jampani86983282016-09-15 14:55:48 -070060 * @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 Jampani79924fa2016-09-13 13:57:03 -070065 */
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 Jampani86983282016-09-15 14:55:48 -070071 * @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 Jampani79924fa2016-09-13 13:57:03 -070077 */
78 CompletableFuture<Boolean> create(DocumentPath path, V value);
79
80 /**
Madan Jampani86983282016-09-15 14:55:48 -070081 * 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 Jampani79924fa2016-09-13 13:57:03 -070091 * Conditionally updates a tree node if the current version matches a specified version.
92 *
Madan Jampani86983282016-09-15 14:55:48 -070093 * @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 Jampani79924fa2016-09-13 13:57:03 -070098 */
99 CompletableFuture<Boolean> replace(DocumentPath path, V newValue, long version);
100
101 /**
Madan Jampani86983282016-09-15 14:55:48 -0700102 * Conditionally updates a tree node if the current node value matches a specified version.
Madan Jampani79924fa2016-09-13 13:57:03 -0700103 *
Madan Jampani86983282016-09-15 14:55:48 -0700104 * @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 Jampani79924fa2016-09-13 13:57:03 -0700109 */
110 CompletableFuture<Boolean> replace(DocumentPath path, V newValue, V currentValue);
111
112 /**
113 * Removes the node with the specified path.
114 *
Madan Jampani86983282016-09-15 14:55:48 -0700115 * @param path path to the node
Madan Jampani79924fa2016-09-13 13:57:03 -0700116 * @return future for the previous value. Future will be completed with a
Madan Jampani86983282016-09-15 14:55:48 -0700117 * {@code IllegalDocumentModificationException} if the node to be removed is either the root
Madan Jampani79924fa2016-09-13 13:57:03 -0700118 * node or has one or more children. Future will be completed with a
Madan Jampani86983282016-09-15 14:55:48 -0700119 * {@code NoSuchDocumentPathException} if the node to be removed does not exist
Madan Jampani79924fa2016-09-13 13:57:03 -0700120 */
121 CompletableFuture<Versioned<V>> removeNode(DocumentPath path);
122
123 /**
Madan Jampani86983282016-09-15 14:55:48 -0700124 * Registers a listener to be notified when the subtree rooted at the specified path
Madan Jampani79924fa2016-09-13 13:57:03 -0700125 * is modified.
126 *
Madan Jampani86983282016-09-15 14:55:48 -0700127 * @param path path to the node
Madan Jampani79924fa2016-09-13 13:57:03 -0700128 * @param listener listener to be notified
Madan Jampani31888032016-09-14 14:37:58 -0700129 * @return a future that is completed when the operation completes
Madan Jampani79924fa2016-09-13 13:57:03 -0700130 */
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 Jampani31888032016-09-14 14:37:58 -0700137 * @return a future that is completed when the operation completes
Madan Jampani79924fa2016-09-13 13:57:03 -0700138 */
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 Jampani31888032016-09-14 14:37:58 -0700145 * @return a future that is completed when the operation completes
Madan Jampani79924fa2016-09-13 13:57:03 -0700146 */
147 default CompletableFuture<Void> addListener(DocumentTreeListener<V> listener) {
148 return addListener(root(), listener);
149 }
150}