From: Robert Varga Date: Sat, 29 Jul 2017 12:05:36 +0000 (+0200) Subject: Optimize use of YangInstanceIdentifier.getPathArguments() X-Git-Tag: release/nitrogen~19 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=8f333ba6544e8f85dcf10dd6a4cf235ff6e0ddef Optimize use of YangInstanceIdentifier.getPathArguments() This method returns a list, hence we can lookup the first item without iterating and also can use Lists.transform(). Change-Id: Ie26bfcc225c74154d65ef963e3444ac5ec10bafb Signed-off-by: Robert Varga --- diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategyFactory.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategyFactory.java index 724499477e..e63ab97445 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategyFactory.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategyFactory.java @@ -20,7 +20,7 @@ public class ShardStrategyFactory { private final Configuration configuration; private final LogicalDatastoreType logicalStoreType; - public ShardStrategyFactory(final Configuration configuration, LogicalDatastoreType logicalStoreType) { + public ShardStrategyFactory(final Configuration configuration, final LogicalDatastoreType logicalStoreType) { Preconditions.checkState(configuration != null, "configuration should not be missing"); this.configuration = configuration; this.logicalStoreType = Preconditions.checkNotNull(logicalStoreType); @@ -45,8 +45,8 @@ public class ShardStrategyFactory { return shardStrategy; } - public static ShardStrategy newShardStrategyInstance(String moduleName, String strategyName, - Configuration configuration) { + public static ShardStrategy newShardStrategyInstance(final String moduleName, final String strategyName, + final Configuration configuration) { if (ModuleShardStrategy.NAME.equals(strategyName)) { return new ModuleShardStrategy(moduleName, configuration); } @@ -59,7 +59,7 @@ public class ShardStrategyFactory { return UNKNOWN_MODULE_NAME; } - String namespace = path.getPathArguments().iterator().next().getNodeType().getNamespace().toASCIIString(); + String namespace = path.getPathArguments().get(0).getNodeType().getNamespace().toASCIIString(); String moduleName = configuration.getModuleNameFromNameSpace(namespace); return moduleName != null ? moduleName : UNKNOWN_MODULE_NAME; } diff --git a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/util/YangSchemaUtils.java b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/util/YangSchemaUtils.java index 27630fbce8..f8f612d469 100644 --- a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/util/YangSchemaUtils.java +++ b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/util/YangSchemaUtils.java @@ -10,13 +10,10 @@ package org.opendaylight.controller.sal.dom.broker.util; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; -import com.google.common.base.Function; -import com.google.common.collect.FluentIterable; - +import com.google.common.collect.Lists; import java.util.Iterator; import java.util.List; import java.util.Set; - import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; @@ -38,28 +35,20 @@ import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; import org.opendaylight.yangtools.yang.model.api.UsesNode; public final class YangSchemaUtils { - - private static final Function QNAME_FROM_PATH_ARGUMENT = input -> { - if (input == null) { - return null; - } - return input.getNodeType(); - }; - private YangSchemaUtils() { throw new UnsupportedOperationException("Utility class."); } - public static DataSchemaNode getSchemaNode(final SchemaContext schema,final YangInstanceIdentifier path) { - checkArgument(schema != null,"YANG Schema must not be null."); - checkArgument(path != null,"Path must not be null."); - return getSchemaNode(schema, FluentIterable.from(path.getPathArguments()).transform(QNAME_FROM_PATH_ARGUMENT)); + public static DataSchemaNode getSchemaNode(final SchemaContext schema, final YangInstanceIdentifier path) { + checkArgument(schema != null, "YANG Schema must not be null."); + checkArgument(path != null, "Path must not be null."); + return getSchemaNode(schema, Lists.transform(path.getPathArguments(), PathArgument::getNodeType)); } - public static DataSchemaNode getSchemaNode(final SchemaContext schema,final Iterable path) { - checkArgument(schema != null,"YANG Schema must not be null."); - checkArgument(path != null,"Path must not be null."); - if(!path.iterator().hasNext()){ + public static DataSchemaNode getSchemaNode(final SchemaContext schema, final Iterable path) { + checkArgument(schema != null, "YANG Schema must not be null."); + checkArgument(path != null, "Path must not be null."); + if (!path.iterator().hasNext()) { return toRootDataNode(schema); } @@ -69,10 +58,10 @@ public final class YangSchemaUtils { Iterator iterator = path.iterator(); while (iterator.hasNext()) { - checkArgument(previous!= null, "Supplied path does not resolve into valid schema node."); + checkArgument(previous != null, "Supplied path does not resolve into valid schema node."); QName arg = iterator.next(); DataSchemaNode currentNode = previous.getDataChildByName(arg); - if (currentNode == null && previous instanceof DataNodeContainer) { + if (currentNode == null) { currentNode = searchInChoices(previous, arg); } if (currentNode instanceof DataNodeContainer) { @@ -81,9 +70,10 @@ public final class YangSchemaUtils { checkArgument(!iterator.hasNext(), "Path nests inside leaf node, which is not allowed."); return currentNode; } - checkState(currentNode != null, "Current node should not be null for %s",path); + checkState(currentNode != null, "Current node should not be null for %s", path); } - checkState(previous instanceof DataSchemaNode, "Schema node for %s should be instance of DataSchemaNode. Found %s",path,previous); + checkState(previous instanceof DataSchemaNode, + "Schema node for %s should be instance of DataSchemaNode. Found %s", path, previous); return (DataSchemaNode) previous; } diff --git a/opendaylight/md-sal/sal-dom-spi/src/main/java/org/opendaylight/controller/sal/core/spi/data/AbstractDOMStoreTreeChangePublisher.java b/opendaylight/md-sal/sal-dom-spi/src/main/java/org/opendaylight/controller/sal/core/spi/data/AbstractDOMStoreTreeChangePublisher.java index d191fc397c..bf07daeaaa 100644 --- a/opendaylight/md-sal/sal-dom-spi/src/main/java/org/opendaylight/controller/sal/core/spi/data/AbstractDOMStoreTreeChangePublisher.java +++ b/opendaylight/md-sal/sal-dom-spi/src/main/java/org/opendaylight/controller/sal/core/spi/data/AbstractDOMStoreTreeChangePublisher.java @@ -7,7 +7,6 @@ */ package org.opendaylight.controller.sal.core.spi.data; -import com.google.common.collect.ImmutableList; import java.util.Collection; import java.util.List; import javax.annotation.Nonnull; @@ -63,8 +62,7 @@ public abstract class AbstractDOMStoreTreeChangePublisher extends AbstractRegist } try (final RegistrationTreeSnapshot> snapshot = takeSnapshot()) { - final List toLookup = ImmutableList.copyOf(candidate.getRootPath().getPathArguments()); - lookupAndNotify(toLookup, 0, snapshot.getRootNode(), candidate); + lookupAndNotify(candidate.getRootPath().getPathArguments(), 0, snapshot.getRootNode(), candidate); } }