Merge "BUG-2350: improve performance of data tree merges"
authorTony Tkacik <ttkacik@cisco.com>
Wed, 7 Jan 2015 09:14:03 +0000 (09:14 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 7 Jan 2015 09:14:03 +0000 (09:14 +0000)
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ModifiedNode.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/OperationWithModification.java

index ecda366de034c91513cbe452c8841e121c055179..bc6b46d46f6c05f186b5d9511cf9561bb532bec8 100644 (file)
@@ -270,6 +270,19 @@ final class ModifiedNode implements StoreTreeNode<ModifiedNode>, Identifiable<Pa
                 + modificationType + ", childModification=" + children + "]";
     }
 
+    /**
+     * Create a node which will reflect the state of this node, except it will behave as newly-written
+     * value. This is useful only for merge validation.
+     *
+     * @param value Value associated with the node
+     * @return An isolated node. This node should never reach a datatree.
+     */
+    ModifiedNode asNewlyWritten(final NormalizedNode<?, ?> value) {
+        final ModifiedNode ret = new ModifiedNode(getIdentifier(), Optional.<TreeNode>absent(), false);
+        ret.write(value);
+        return ret;
+    }
+
     public static ModifiedNode createUnmodified(final TreeNode metadataTree, final boolean isOrdered) {
         return new ModifiedNode(metadataTree.getIdentifier(), Optional.of(metadataTree), isOrdered);
     }
index bcf2101ff07d49572d3cbdb8a92cc3e849dbfc45..3293a05b066edea7cc77a9e51796794063fd4c4c 100644 (file)
@@ -31,13 +31,13 @@ final class OperationWithModification {
         applyOperation.verifyStructure(modification);
     }
 
-    private void mergeImpl(final NormalizedNode<?,?> data) {
+    private void recursiveMerge(final NormalizedNode<?,?> data) {
         if (data instanceof NormalizedNodeContainer<?,?,?>) {
             @SuppressWarnings({ "rawtypes", "unchecked" })
             NormalizedNodeContainer<?,?,NormalizedNode<PathArgument, ?>> dataContainer = (NormalizedNodeContainer) data;
             for (NormalizedNode<PathArgument, ?> child : dataContainer.getValue()) {
                 PathArgument childId = child.getIdentifier();
-                forChild(childId).mergeImpl(child);
+                forChild(childId).recursiveMerge(child);
             }
         }
 
@@ -45,8 +45,16 @@ final class OperationWithModification {
     }
 
     void merge(final NormalizedNode<?, ?> data) {
-        mergeImpl(data);
-        applyOperation.verifyStructure(modification);
+        /*
+         * A merge operation will end up overwriting parts of the tree, retaining others.
+         * We want to make sure we do not validate the complete resulting structure, but
+         * rather just what was written. In order to do that, we first pretend the data
+         * was written, run verification and then perform the merge -- with the explicit
+         * assumption that adding the newly-validated data with the previously-validated
+         * data will not result in invalid data.
+         */
+        applyOperation.verifyStructure(modification.asNewlyWritten(data));
+        recursiveMerge(data);
     }
 
     void delete() {