BUG-2350: improve performance of data tree merges
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / OperationWithModification.java
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() {