Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 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 | |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 19 | import java.util.Map; |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 20 | |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 21 | import javax.annotation.concurrent.NotThreadSafe; |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 22 | |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 23 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 24 | * A hierarchical <a href="https://en.wikipedia.org/wiki/Document_Object_Model">document tree</a> data structure. |
| 25 | * |
Aaron Kruglikov | 7c99811 | 2016-09-07 17:45:07 -0700 | [diff] [blame] | 26 | * @param <V> document tree value type |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 27 | */ |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 28 | @NotThreadSafe |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 29 | public interface DocumentTree<V> extends DistributedPrimitive { |
| 30 | |
| 31 | @Override |
| 32 | default Type primitiveType() { |
| 33 | return Type.DOCUMENT_TREE; |
| 34 | } |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 37 | * Returns the {@link DocumentPath path} to root of the tree. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 38 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 39 | * @return path to root of the tree |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 40 | */ |
| 41 | DocumentPath root(); |
| 42 | |
| 43 | /** |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 44 | * Returns the child values for this node. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 45 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 46 | * @param path path to the node |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 47 | * @return mapping from a child name to its value |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 48 | * @throws NoSuchDocumentPathException if the path does not point to a valid node |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 49 | */ |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 50 | Map<String, Versioned<V>> getChildren(DocumentPath path); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 53 | * Returns a document tree node. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 54 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 55 | * @param path path to node |
| 56 | * @return node value or {@code null} if path does not point to a valid node |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 57 | */ |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 58 | Versioned<V> get(DocumentPath path); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 61 | * Creates or updates a document tree node. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 62 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 63 | * @param path path for the node to create or update |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 64 | * @param value the non-null value to be associated with the key |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 65 | * @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 Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 67 | */ |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 68 | Versioned<V> set(DocumentPath path, V value); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 71 | * Creates a document tree node if one does not exist already. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 72 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 73 | * @param path path for the node to create |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 74 | * @param value the non-null value to be associated with the key |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 75 | * @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 Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 77 | */ |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 78 | boolean create(DocumentPath path, V value); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 79 | |
| 80 | /** |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 81 | * 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 Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 92 | * 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 Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 97 | * @return returns {@code true} if the update was made and the tree was modified, {@code false} otherwise |
Madan Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 98 | * @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 Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 108 | * @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 Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 110 | * @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 Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 115 | * Removes the node with the specified path. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 116 | * |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 117 | * @param path path for the node to remove |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 118 | * @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 Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 120 | */ |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 121 | Versioned<V> removeNode(DocumentPath path); |
Madan Jampani | 64ff13b | 2016-09-08 11:18:50 -0700 | [diff] [blame] | 122 | |
| 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 Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 147 | } |