Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -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 | |
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 |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 29 | public interface DocumentTree<V> { |
| 30 | |
| 31 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 32 | * Returns the {@link DocumentPath path} to root of the tree. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 33 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 34 | * @return path to root of the tree |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 35 | */ |
| 36 | DocumentPath root(); |
| 37 | |
| 38 | /** |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 39 | * Returns the child values for this node. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 40 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 41 | * @param path path to the node |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 42 | * @return mapping from a child name to its value |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 43 | * @throws NoSuchDocumentPathException if the path does not point to a valid node |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 44 | */ |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 45 | Map<String, Versioned<V>> getChildren(DocumentPath path); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 48 | * Returns a document tree node. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 49 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 50 | * @param path path to node |
| 51 | * @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] | 52 | */ |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 53 | Versioned<V> get(DocumentPath path); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 56 | * Creates or updates a document tree node. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 57 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 58 | * @param path path for the node to create or update |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 59 | * @param value the non-null value to be associated with the key |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 60 | * @return the previous mapping or {@code null} if there was no previous mapping |
| 61 | * @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] | 62 | */ |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 63 | Versioned<V> set(DocumentPath path, V value); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 64 | |
| 65 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 66 | * Creates a document tree node if one does not exist already. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 67 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 68 | * @param path path for the node to create |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 69 | * @param value the non-null value to be associated with the key |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 70 | * @return returns {@code true} if the mapping could be added successfully, {@code false} otherwise |
| 71 | * @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] | 72 | */ |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 73 | boolean create(DocumentPath path, V value); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 76 | * Creates a document tree node by first creating any missing intermediate nodes in the path. |
| 77 | * |
| 78 | * @param path path for the node to create |
| 79 | * @param value the non-null value to be associated with the key |
| 80 | * @return returns {@code true} if the mapping could be added successfully, {@code false} if |
| 81 | * a node already exists at that path |
| 82 | * @throws IllegalDocumentModificationException if {@code path} points to root |
| 83 | */ |
| 84 | boolean createRecursive(DocumentPath path, V value); |
| 85 | |
| 86 | /** |
Madan Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 87 | * Conditionally updates a tree node if the current version matches a specified version. |
| 88 | * |
| 89 | * @param path path for the node to create |
| 90 | * @param newValue the non-null value to be associated with the key |
| 91 | * @param version current version of the value for update to occur |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 92 | * @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] | 93 | * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist |
| 94 | */ |
| 95 | boolean replace(DocumentPath path, V newValue, long version); |
| 96 | |
| 97 | /** |
| 98 | * Conditionally updates a tree node if the current value matches a specified value. |
| 99 | * |
| 100 | * @param path path for the node to create |
| 101 | * @param newValue the non-null value to be associated with the key |
| 102 | * @param currentValue current value for update to occur |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 103 | * @return returns {@code true} if the update was made and the tree was modified, {@code false} otherwise. |
| 104 | * This method returns {@code false} if the newValue and currentValue are same. |
Madan Jampani | 1184bc7 | 2016-09-08 08:37:05 -0700 | [diff] [blame] | 105 | * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist |
| 106 | */ |
| 107 | boolean replace(DocumentPath path, V newValue, V currentValue); |
| 108 | |
| 109 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 110 | * Removes the node with the specified path. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 111 | * |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 112 | * @param path path for the node to remove |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 113 | * @return the previous value of the node or {@code null} if it did not exist |
| 114 | * @throws IllegalDocumentModificationException if the remove to be removed |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 115 | */ |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 116 | Versioned<V> removeNode(DocumentPath path); |
Madan Jampani | 64ff13b | 2016-09-08 11:18:50 -0700 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * Registers a listener to be notified when a subtree rooted at the specified path |
| 120 | * is modified. |
| 121 | * |
| 122 | * @param path path to root of subtree to monitor for updates |
| 123 | * @param listener listener to be notified |
| 124 | */ |
| 125 | void addListener(DocumentPath path, DocumentTreeListener<V> listener); |
| 126 | |
| 127 | /** |
| 128 | * Unregisters a previously added listener. |
| 129 | * |
| 130 | * @param listener listener to unregister |
| 131 | */ |
| 132 | void removeListener(DocumentTreeListener<V> listener); |
| 133 | |
| 134 | /** |
| 135 | * Registers a listener to be notified when the tree is modified. |
| 136 | * |
| 137 | * @param listener listener to be notified |
| 138 | */ |
| 139 | default void addListener(DocumentTreeListener<V> listener) { |
| 140 | addListener(root(), listener); |
| 141 | } |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 142 | } |