blob: 1c816cf3aeebda61a89bc0b93e98fa4983f5de9c [file] [log] [blame]
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -07003 *
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
Madan Jampaniad5b8c72016-09-12 15:05:01 -070019import java.util.Map;
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070020
Madan Jampaniad5b8c72016-09-12 15:05:01 -070021import javax.annotation.concurrent.NotThreadSafe;
Madan Jampani5bdebd52016-09-07 16:18:12 -070022
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070023/**
Madan Jampani5bdebd52016-09-07 16:18:12 -070024 * A hierarchical <a href="https://en.wikipedia.org/wiki/Document_Object_Model">document tree</a> data structure.
25 *
Aaron Kruglikov7c998112016-09-07 17:45:07 -070026 * @param <V> document tree value type
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070027 */
Madan Jampaniad5b8c72016-09-12 15:05:01 -070028@NotThreadSafe
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070029public interface DocumentTree<V> extends DistributedPrimitive {
30
31 @Override
32 default Type primitiveType() {
33 return Type.DOCUMENT_TREE;
34 }
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070035
36 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -070037 * Returns the {@link DocumentPath path} to root of the tree.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070038 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070039 * @return path to root of the tree
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070040 */
41 DocumentPath root();
42
43 /**
Madan Jampaniad5b8c72016-09-12 15:05:01 -070044 * Returns the child values for this node.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070045 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070046 * @param path path to the node
Madan Jampaniad5b8c72016-09-12 15:05:01 -070047 * @return mapping from a child name to its value
Madan Jampani5bdebd52016-09-07 16:18:12 -070048 * @throws NoSuchDocumentPathException if the path does not point to a valid node
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070049 */
Madan Jampaniad5b8c72016-09-12 15:05:01 -070050 Map<String, Versioned<V>> getChildren(DocumentPath path);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070051
52 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -070053 * Returns a document tree node.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070054 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070055 * @param path path to node
56 * @return node value or {@code null} if path does not point to a valid node
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070057 */
Madan Jampaniad5b8c72016-09-12 15:05:01 -070058 Versioned<V> get(DocumentPath path);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070059
60 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -070061 * Creates or updates a document tree node.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070062 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070063 * @param path path for the node to create or update
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070064 * @param value the non-null value to be associated with the key
Madan Jampani5bdebd52016-09-07 16:18:12 -070065 * @return the previous mapping or {@code null} if there was no previous mapping
66 * @throws NoSuchDocumentPathException if the parent node (for the node to create/update) does not exist
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070067 */
Madan Jampaniad5b8c72016-09-12 15:05:01 -070068 Versioned<V> set(DocumentPath path, V value);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070069
70 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -070071 * Creates a document tree node if one does not exist already.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070072 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070073 * @param path path for the node to create
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070074 * @param value the non-null value to be associated with the key
Madan Jampani5bdebd52016-09-07 16:18:12 -070075 * @return returns {@code true} if the mapping could be added successfully, {@code false} otherwise
76 * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070077 */
Madan Jampaniad5b8c72016-09-12 15:05:01 -070078 boolean create(DocumentPath path, V value);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070079
80 /**
Madan Jampani86983282016-09-15 14:55:48 -070081 * Creates a document tree node by first creating any missing intermediate nodes in the path.
82 *
83 * @param path path for the node to create
84 * @param value the non-null value to be associated with the key
85 * @return returns {@code true} if the mapping could be added successfully, {@code false} if
86 * a node already exists at that path
87 * @throws IllegalDocumentModificationException if {@code path} points to root
88 */
89 boolean createRecursive(DocumentPath path, V value);
90
91 /**
Madan Jampani1184bc72016-09-08 08:37:05 -070092 * Conditionally updates a tree node if the current version matches a specified version.
93 *
94 * @param path path for the node to create
95 * @param newValue the non-null value to be associated with the key
96 * @param version current version of the value for update to occur
Madan Jampaniad5b8c72016-09-12 15:05:01 -070097 * @return returns {@code true} if the update was made and the tree was modified, {@code false} otherwise
Madan Jampani1184bc72016-09-08 08:37:05 -070098 * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist
99 */
100 boolean replace(DocumentPath path, V newValue, long version);
101
102 /**
103 * Conditionally updates a tree node if the current value matches a specified value.
104 *
105 * @param path path for the node to create
106 * @param newValue the non-null value to be associated with the key
107 * @param currentValue current value for update to occur
Madan Jampaniad5b8c72016-09-12 15:05:01 -0700108 * @return returns {@code true} if the update was made and the tree was modified, {@code false} otherwise.
109 * This method returns {@code false} if the newValue and currentValue are same.
Madan Jampani1184bc72016-09-08 08:37:05 -0700110 * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist
111 */
112 boolean replace(DocumentPath path, V newValue, V currentValue);
113
114 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -0700115 * Removes the node with the specified path.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700116 *
Madan Jampaniad5b8c72016-09-12 15:05:01 -0700117 * @param path path for the node to remove
Madan Jampani5bdebd52016-09-07 16:18:12 -0700118 * @return the previous value of the node or {@code null} if it did not exist
119 * @throws IllegalDocumentModificationException if the remove to be removed
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700120 */
Madan Jampaniad5b8c72016-09-12 15:05:01 -0700121 Versioned<V> removeNode(DocumentPath path);
Madan Jampani64ff13b2016-09-08 11:18:50 -0700122
123 /**
124 * Registers a listener to be notified when a subtree rooted at the specified path
125 * is modified.
126 *
127 * @param path path to root of subtree to monitor for updates
128 * @param listener listener to be notified
129 */
130 void addListener(DocumentPath path, DocumentTreeListener<V> listener);
131
132 /**
133 * Unregisters a previously added listener.
134 *
135 * @param listener listener to unregister
136 */
137 void removeListener(DocumentTreeListener<V> listener);
138
139 /**
140 * Registers a listener to be notified when the tree is modified.
141 *
142 * @param listener listener to be notified
143 */
144 default void addListener(DocumentTreeListener<V> listener) {
145 addListener(root(), listener);
146 }
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700147}