Committed by
Gerrit Code Review
DocumentTreeNode: add support for a version
Change-Id: Ib07503beda17b904ce6b6f52d7bf9d3622085a24
Showing
2 changed files
with
59 additions
and
24 deletions
... | @@ -16,24 +16,25 @@ | ... | @@ -16,24 +16,25 @@ |
16 | 16 | ||
17 | package org.onosproject.store.primitives; | 17 | package org.onosproject.store.primitives; |
18 | 18 | ||
19 | -import com.google.common.base.MoreObjects; | 19 | +import static com.google.common.base.Preconditions.checkNotNull; |
20 | -import com.google.common.collect.Sets; | ||
21 | -import org.onosproject.store.service.DocumentPath; | ||
22 | 20 | ||
23 | import java.util.Comparator; | 21 | import java.util.Comparator; |
24 | import java.util.Iterator; | 22 | import java.util.Iterator; |
25 | import java.util.Objects; | 23 | import java.util.Objects; |
26 | import java.util.TreeSet; | 24 | import java.util.TreeSet; |
27 | 25 | ||
28 | -import static com.google.common.base.Preconditions.checkNotNull; | 26 | +import org.onosproject.store.service.DocumentPath; |
27 | + | ||
28 | +import com.google.common.base.MoreObjects; | ||
29 | +import com.google.common.collect.Sets; | ||
29 | 30 | ||
30 | /** | 31 | /** |
31 | - * A tree node made for {@code DocumentTree} | 32 | + * A {@code DocumentTree} node. |
32 | - * implementations that keeps records of its parent and children. | ||
33 | */ | 33 | */ |
34 | public class DocumentTreeNode<V> { | 34 | public class DocumentTreeNode<V> { |
35 | private final DocumentPath key; | 35 | private final DocumentPath key; |
36 | private V value; | 36 | private V value; |
37 | + private long version; | ||
37 | private final TreeSet<DocumentTreeNode<V>> children = | 38 | private final TreeSet<DocumentTreeNode<V>> children = |
38 | Sets.newTreeSet(new Comparator<DocumentTreeNode<V>>() { | 39 | Sets.newTreeSet(new Comparator<DocumentTreeNode<V>>() { |
39 | @Override | 40 | @Override |
... | @@ -42,17 +43,20 @@ public class DocumentTreeNode<V> { | ... | @@ -42,17 +43,20 @@ public class DocumentTreeNode<V> { |
42 | return o1.getKey().compareTo(o2.getKey()); | 43 | return o1.getKey().compareTo(o2.getKey()); |
43 | } | 44 | } |
44 | }); | 45 | }); |
45 | - private DocumentTreeNode parent; | 46 | + private final DocumentTreeNode<V> parent; |
46 | 47 | ||
47 | - public DocumentTreeNode(DocumentPath key, V value, | 48 | + public DocumentTreeNode(DocumentPath key, |
48 | - DocumentTreeNode parent) { | 49 | + V value, |
50 | + long version, | ||
51 | + DocumentTreeNode<V> parent) { | ||
49 | this.key = checkNotNull(key); | 52 | this.key = checkNotNull(key); |
50 | this.value = checkNotNull(value); | 53 | this.value = checkNotNull(value); |
54 | + this.version = version; | ||
51 | this.parent = parent; | 55 | this.parent = parent; |
52 | } | 56 | } |
53 | 57 | ||
54 | /** | 58 | /** |
55 | - * Returns this objects key. | 59 | + * Returns this node's key. |
56 | * | 60 | * |
57 | * @return the key | 61 | * @return the key |
58 | */ | 62 | */ |
... | @@ -61,7 +65,7 @@ public class DocumentTreeNode<V> { | ... | @@ -61,7 +65,7 @@ public class DocumentTreeNode<V> { |
61 | } | 65 | } |
62 | 66 | ||
63 | /** | 67 | /** |
64 | - * Returns this objects value. | 68 | + * Returns this node's value. |
65 | * | 69 | * |
66 | * @return the value | 70 | * @return the value |
67 | */ | 71 | */ |
... | @@ -70,18 +74,29 @@ public class DocumentTreeNode<V> { | ... | @@ -70,18 +74,29 @@ public class DocumentTreeNode<V> { |
70 | } | 74 | } |
71 | 75 | ||
72 | /** | 76 | /** |
73 | - * Sets this objects value. | 77 | + * Returns this node's version. |
78 | + * | ||
79 | + * @return the version | ||
80 | + */ | ||
81 | + public long getVersion() { | ||
82 | + return version; | ||
83 | + } | ||
84 | + | ||
85 | + /** | ||
86 | + * Updates this node. | ||
74 | * | 87 | * |
75 | - * @param value the value to be set | 88 | + * @param newValue new value to be set |
89 | + * @param newVersion new version to be set | ||
76 | */ | 90 | */ |
77 | - public void setValue(V value) { | 91 | + public void update(V newValue, long newVersion) { |
78 | - this.value = value; | 92 | + this.value = newValue; |
93 | + this.version = newVersion; | ||
79 | } | 94 | } |
80 | 95 | ||
81 | /** | 96 | /** |
82 | * Returns a collection of the children of this node. | 97 | * Returns a collection of the children of this node. |
83 | * | 98 | * |
84 | - * @return a sorted iterator for the children of this node. | 99 | + * @return iterator for the children of this node. |
85 | */ | 100 | */ |
86 | public Iterator<DocumentTreeNode<V>> getChildren() { | 101 | public Iterator<DocumentTreeNode<V>> getChildren() { |
87 | return children.iterator(); | 102 | return children.iterator(); |
... | @@ -91,19 +106,17 @@ public class DocumentTreeNode<V> { | ... | @@ -91,19 +106,17 @@ public class DocumentTreeNode<V> { |
91 | * Adds a child to this node. | 106 | * Adds a child to this node. |
92 | * | 107 | * |
93 | * @param child the child node to be added | 108 | * @param child the child node to be added |
94 | - * @return true if the child set was modified as a result of this call, | 109 | + * @return {@code true} if the child set was modified as a result of this call, {@code false} otherwise |
95 | - * false otherwise | ||
96 | */ | 110 | */ |
97 | public boolean addChild(DocumentTreeNode<V> child) { | 111 | public boolean addChild(DocumentTreeNode<V> child) { |
98 | return children.add(child); | 112 | return children.add(child); |
99 | } | 113 | } |
100 | 114 | ||
101 | /** | 115 | /** |
102 | - * Removes a child from the children of this node. | 116 | + * Removes a child node. |
103 | * | 117 | * |
104 | * @param child the child node to be removed | 118 | * @param child the child node to be removed |
105 | - * @return true if the child set was modified as a result of this call, | 119 | + * @return {@code true} if the child set was modified as a result of this call, {@code false} otherwise |
106 | - * false otherwise | ||
107 | */ | 120 | */ |
108 | public boolean removeChild(String child) { | 121 | public boolean removeChild(String child) { |
109 | return children.remove(child); | 122 | return children.remove(child); |
... | @@ -126,10 +139,10 @@ public class DocumentTreeNode<V> { | ... | @@ -126,10 +139,10 @@ public class DocumentTreeNode<V> { |
126 | @Override | 139 | @Override |
127 | public boolean equals(Object obj) { | 140 | public boolean equals(Object obj) { |
128 | if (obj instanceof DocumentTreeNode) { | 141 | if (obj instanceof DocumentTreeNode) { |
129 | - DocumentTreeNode that = (DocumentTreeNode) obj; | 142 | + DocumentTreeNode<V> that = (DocumentTreeNode<V>) obj; |
130 | if (this.parent.equals(that.parent)) { | 143 | if (this.parent.equals(that.parent)) { |
131 | if (this.children.size() == that.children.size()) { | 144 | if (this.children.size() == that.children.size()) { |
132 | - for (DocumentTreeNode child : this.children) { | 145 | + for (DocumentTreeNode<V> child : this.children) { |
133 | if (!that.children.contains(child)) { | 146 | if (!that.children.contains(child)) { |
134 | return false; | 147 | return false; |
135 | } | 148 | } |
... | @@ -148,7 +161,7 @@ public class DocumentTreeNode<V> { | ... | @@ -148,7 +161,7 @@ public class DocumentTreeNode<V> { |
148 | .add("parent", this.parent) | 161 | .add("parent", this.parent) |
149 | .add("key", this.key) | 162 | .add("key", this.key) |
150 | .add("value", this.value); | 163 | .add("value", this.value); |
151 | - for (DocumentTreeNode child : children) { | 164 | + for (DocumentTreeNode<V> child : children) { |
152 | helper = helper.add("child", child.key); | 165 | helper = helper.add("child", child.key); |
153 | } | 166 | } |
154 | return helper.toString(); | 167 | return helper.toString(); | ... | ... |
... | @@ -72,6 +72,28 @@ public interface DocumentTree<V> { | ... | @@ -72,6 +72,28 @@ public interface DocumentTree<V> { |
72 | boolean createNode(DocumentPath path, V value); | 72 | boolean createNode(DocumentPath path, V value); |
73 | 73 | ||
74 | /** | 74 | /** |
75 | + * Conditionally updates a tree node if the current version matches a specified version. | ||
76 | + * | ||
77 | + * @param path path for the node to create | ||
78 | + * @param newValue the non-null value to be associated with the key | ||
79 | + * @param version current version of the value for update to occur | ||
80 | + * @return returns {@code true} if the update was made, {@code false} otherwise | ||
81 | + * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist | ||
82 | + */ | ||
83 | + boolean replace(DocumentPath path, V newValue, long version); | ||
84 | + | ||
85 | + /** | ||
86 | + * Conditionally updates a tree node if the current value matches a specified value. | ||
87 | + * | ||
88 | + * @param path path for the node to create | ||
89 | + * @param newValue the non-null value to be associated with the key | ||
90 | + * @param currentValue current value for update to occur | ||
91 | + * @return returns {@code true} if the update was made, {@code false} otherwise | ||
92 | + * @throws NoSuchDocumentPathException if the parent node (for the node to create) does not exist | ||
93 | + */ | ||
94 | + boolean replace(DocumentPath path, V newValue, V currentValue); | ||
95 | + | ||
96 | + /** | ||
75 | * Removes the node with the specified path. | 97 | * Removes the node with the specified path. |
76 | * | 98 | * |
77 | * is not a leaf node i.e has one or more children | 99 | * is not a leaf node i.e has one or more children | ... | ... |
-
Please register or login to post a comment