Optimize augment conflict checking 41/69241/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 8 Mar 2018 01:12:43 +0000 (02:12 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 8 Mar 2018 09:55:59 +0000 (10:55 +0100)
When we are validating whether a node can be introduced, we end up
needlessly iterating when the source is not a DataDefinitionStatement
and also end up running comparison when a substatement is not a
DataDefinitionStatement,

Refactor the code to take these invariants into account.

Change-Id: Ie55c7dfebacfad1189de1929c23809fd284958b4
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 6e4ed947d08856771dbbdf428ba84c44aed45126)

yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/AugmentStatementImpl.java

index 819ca5630be90ad9f8d737e20b6df9edb91c3c99..b1c25f0967dab11ab042f1f4fd5256dac3c2ae46 100644 (file)
@@ -8,11 +8,10 @@
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
 import com.google.common.base.Verify;
-import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.List;
 import java.util.Objects;
 import java.util.Set;
 import java.util.regex.Pattern;
@@ -267,21 +266,22 @@ public class AugmentStatementImpl extends AbstractDeclaredStatement<SchemaNodeId
                 checkForMandatoryNodes(sourceCtx);
             }
 
-            final List<Mutable<?, ?, ?>> targetSubStatements = ImmutableList.<Mutable<?, ?, ?>>builder()
-                    .addAll(targetCtx.mutableDeclaredSubstatements()).addAll(targetCtx.mutableEffectiveSubstatements())
-                    .build();
-
-            for (final Mutable<?, ?, ?> subStatement : targetSubStatements) {
-                final boolean sourceIsDataNode = DataDefinitionStatement.class.isAssignableFrom(sourceCtx
-                        .getPublicDefinition().getDeclaredRepresentationClass());
-                final boolean targetIsDataNode = DataDefinitionStatement.class.isAssignableFrom(subStatement
-                        .getPublicDefinition().getDeclaredRepresentationClass());
-                final boolean qNamesEqual = sourceIsDataNode && targetIsDataNode
-                        && Objects.equals(sourceCtx.getStatementArgument(), subStatement.getStatementArgument());
-
-                InferenceException.throwIf(qNamesEqual, sourceCtx.getStatementSourceReference(),
-                        "An augment cannot add node named '%s' because this name is already used in target",
-                        sourceCtx.rawStatementArgument());
+            // Data definition statements must not collide on their namespace
+            if (DataDefinitionStatement.class.isAssignableFrom(
+                sourceCtx.getPublicDefinition().getDeclaredRepresentationClass())) {
+                final Iterable<Mutable<?, ?, ?>> targetSubStatements = Iterables.concat(
+                    targetCtx.mutableDeclaredSubstatements(), targetCtx.mutableEffectiveSubstatements());
+                for (final StmtContext<?, ?, ?> subStatement : targetSubStatements) {
+                    if (DataDefinitionStatement.class.isAssignableFrom(
+                        subStatement.getPublicDefinition().getDeclaredRepresentationClass())) {
+
+                        InferenceException.throwIf(
+                            Objects.equals(sourceCtx.getStatementArgument(), subStatement.getStatementArgument()),
+                            sourceCtx.getStatementSourceReference(),
+                            "An augment cannot add node named '%s' because this name is already used in target",
+                            sourceCtx.rawStatementArgument());
+                    }
+                }
             }
         }