Merge "BUG-648: Improve performance by reusing old data nodes"
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / tree / StoreMetadataNode.java
index ea83047a3101996e891a033f49f0fe34eee13990..455777b7fb9a1b315c036c6a5d9d6482473e0fa7 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.controller.md.sal.dom.store.impl.tree;
 
 import static com.google.common.base.Preconditions.checkState;
 
+import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.Map;
 
@@ -46,10 +47,24 @@ public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>,
         this.children = Preconditions.checkNotNull(children);
     }
 
+    public static StoreMetadataNode createEmpty(final NormalizedNode<?, ?> data) {
+        return new StoreMetadataNode(data, UnsignedLong.ZERO, UnsignedLong.ZERO,
+                Collections.<PathArgument, StoreMetadataNode>emptyMap());
+    }
+
+    public StoreMetadataNode(final NormalizedNode<?, ?> data, final UnsignedLong nodeVersion,
+            final UnsignedLong subtreeVersion) {
+        this(data, nodeVersion, subtreeVersion, Collections.<PathArgument, StoreMetadataNode>emptyMap());
+    }
+
     public static Builder builder() {
         return new Builder();
     }
 
+    public static Builder builder(StoreMetadataNode node) {
+        return new Builder(node);
+    }
+
     public UnsignedLong getNodeVersion() {
         return this.nodeVersion;
     }
@@ -96,7 +111,7 @@ public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>,
         return Optional.absent();
     }
 
-    public static final StoreMetadataNode createRecursivelly(final NormalizedNode<?, ?> node,
+    public static final StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node,
             final UnsignedLong nodeVersion, final UnsignedLong subtreeVersion) {
         Builder builder = builder() //
                 .setNodeVersion(nodeVersion) //
@@ -107,7 +122,7 @@ public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>,
             @SuppressWarnings("unchecked")
             NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> nodeContainer = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) node;
             for (NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
-                builder.add(createRecursivelly(subNode, nodeVersion, subtreeVersion));
+                builder.add(createRecursively(subNode, nodeVersion, subtreeVersion));
             }
         }
         return builder.build();
@@ -118,11 +133,16 @@ public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>,
         private UnsignedLong nodeVersion;
         private UnsignedLong subtreeVersion;
         private NormalizedNode<?, ?> data;
-        private Map<PathArgument, StoreMetadataNode> children = new LinkedHashMap<>();
+        private Map<PathArgument, StoreMetadataNode> children;
         private boolean dirty = false;
 
-        private Builder() {}
+        private Builder() {
+            children = new LinkedHashMap<>();
+        }
 
+        public Builder(StoreMetadataNode node) {
+            children = new LinkedHashMap<>(node.children);
+        }
 
         public UnsignedLong getVersion() {
             return nodeVersion;
@@ -153,6 +173,15 @@ public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>,
             return this;
         }
 
+        public Builder remove(final PathArgument id) {
+            if (dirty) {
+                children = new LinkedHashMap<>(children);
+                dirty = false;
+            }
+            children.remove(id);
+            return this;
+        }
+
         public StoreMetadataNode build() {
             checkState(data != null, "Data node should not be null.");
             checkState(subtreeVersion.compareTo(nodeVersion) >= 0,
@@ -162,8 +191,7 @@ public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>,
         }
     }
 
-    public static StoreMetadataNode createRecursivelly(final NormalizedNode<?, ?> node, final UnsignedLong version) {
-        return createRecursivelly(node, version, version);
+    public static StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node, final UnsignedLong version) {
+        return createRecursively(node, version, version);
     }
-
 }