blob: 00bccda3f00352a9f6560f26a404d81b1bfdb67d [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
Yuta HIGUCHId2db7ad2016-09-16 21:40:13 -070044 * @throws 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
Madan Jampani4c8e3fe2016-09-16 16:20:28 -070052 * @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 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
Madan Jampani4c8e3fe2016-09-16 16:20:28 -070061 * @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 Jampani86983282016-09-15 14:55:48 -070065 * if the parent node (for the node to create/update) does not exist
Madan Jampani79924fa2016-09-13 13:57:03 -070066 */
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 Jampani86983282016-09-15 14:55:48 -070072 * @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 Jampani79924fa2016-09-13 13:57:03 -070078 */
79 CompletableFuture<Boolean> create(DocumentPath path, V value);
80
81 /**
Madan Jampani86983282016-09-15 14:55:48 -070082 * Creates a document tree node recursively by creating all missing intermediate nodes in the path.
83 *
84 * @param path path to the node
Madan Jampani4c8e3fe2016-09-16 16:20:28 -070085 * @param value value to be associated with the node ({@code null} is a valid value)
Madan Jampani86983282016-09-15 14:55:48 -070086 * @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 Jampani79924fa2016-09-13 13:57:03 -070092 * Conditionally updates a tree node if the current version matches a specified version.
93 *
Madan Jampani86983282016-09-15 14:55:48 -070094 * @param path path to the node
Madan Jampani4c8e3fe2016-09-16 16:20:28 -070095 * @param newValue value to associate with the node ({@code null} is a valid value)
Madan Jampani86983282016-09-15 14:55:48 -070096 * @param version current version of the node for update to occur
Madan Jampani4c8e3fe2016-09-16 16:20:28 -070097 * @return future that is either completed with {@code true} if the update was made
98 * or {@code false} if update did not happen
Madan Jampani79924fa2016-09-13 13:57:03 -070099 */
100 CompletableFuture<Boolean> replace(DocumentPath path, V newValue, long version);
101
102 /**
Madan Jampani86983282016-09-15 14:55:48 -0700103 * Conditionally updates a tree node if the current node value matches a specified version.
Madan Jampani79924fa2016-09-13 13:57:03 -0700104 *
Madan Jampani86983282016-09-15 14:55:48 -0700105 * @param path path to the node
Madan Jampani4c8e3fe2016-09-16 16:20:28 -0700106 * @param newValue value to associate with the node ({@code null} is a valid value)
Madan Jampani86983282016-09-15 14:55:48 -0700107 * @param currentValue current value of the node for update to occur
Madan Jampani4c8e3fe2016-09-16 16:20:28 -0700108 * @return future that is either completed with {@code true} if the update was made
109 * or {@code false} if update did not happen
Madan Jampani79924fa2016-09-13 13:57:03 -0700110 */
111 CompletableFuture<Boolean> replace(DocumentPath path, V newValue, V currentValue);
112
113 /**
114 * Removes the node with the specified path.
115 *
Madan Jampani86983282016-09-15 14:55:48 -0700116 * @param path path to the node
Madan Jampani79924fa2016-09-13 13:57:03 -0700117 * @return future for the previous value. Future will be completed with a
Madan Jampani86983282016-09-15 14:55:48 -0700118 * {@code IllegalDocumentModificationException} if the node to be removed is either the root
Madan Jampani79924fa2016-09-13 13:57:03 -0700119 * node or has one or more children. Future will be completed with a
Madan Jampani86983282016-09-15 14:55:48 -0700120 * {@code NoSuchDocumentPathException} if the node to be removed does not exist
Madan Jampani79924fa2016-09-13 13:57:03 -0700121 */
122 CompletableFuture<Versioned<V>> removeNode(DocumentPath path);
123
124 /**
Madan Jampani86983282016-09-15 14:55:48 -0700125 * Registers a listener to be notified when the subtree rooted at the specified path
Madan Jampani79924fa2016-09-13 13:57:03 -0700126 * is modified.
127 *
Madan Jampani86983282016-09-15 14:55:48 -0700128 * @param path path to the node
Madan Jampani79924fa2016-09-13 13:57:03 -0700129 * @param listener listener to be notified
Madan Jampani31888032016-09-14 14:37:58 -0700130 * @return a future that is completed when the operation completes
Madan Jampani79924fa2016-09-13 13:57:03 -0700131 */
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 Jampani31888032016-09-14 14:37:58 -0700138 * @return a future that is completed when the operation completes
Madan Jampani79924fa2016-09-13 13:57:03 -0700139 */
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 Jampani31888032016-09-14 14:37:58 -0700146 * @return a future that is completed when the operation completes
Madan Jampani79924fa2016-09-13 13:57:03 -0700147 */
148 default CompletableFuture<Void> addListener(DocumentTreeListener<V> listener) {
149 return addListener(root(), listener);
150 }
151}