From: Robert Varga Date: Mon, 26 Jan 2015 13:07:32 +0000 (+0100) Subject: Optimize getShardNamesFromModuleName() X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=ee1a4976ce56697338e160e237c2e468d37f7ce2 Optimize getShardNamesFromModuleName() Instead of creating the list each and everytime, take advantage of the fact the inputs are constants -- and pre-instantiate a map. Change-Id: If754942561a186fa1ca7806fe86834a3530a97ca Signed-off-by: Robert Varga --- diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ConfigurationImpl.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ConfigurationImpl.java index 56b2d094c7..674c7d5298 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ConfigurationImpl.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ConfigurationImpl.java @@ -12,7 +12,9 @@ import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList.Builder; +import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ListMultimap; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; import com.typesafe.config.ConfigObject; @@ -47,8 +49,9 @@ public class ConfigurationImpl implements Configuration { // key = shardName, value = list of replicaNames (replicaNames are the same as memberNames) private final Map> shardReplicaNames = new HashMap<>(); - private final Map namespaceToModuleName; + private final ListMultimap moduleNameToShardName; private final Map moduleNameToStrategy; + private final Map namespaceToModuleName; public ConfigurationImpl(final String moduleShardsConfigPath, @@ -82,6 +85,7 @@ public class ConfigurationImpl implements Configuration { this.moduleShards = readModuleShards(moduleShardsConfig); this.modules = readModules(modulesConfig); + this.moduleNameToShardName = createModuleNameToShardName(moduleShards); this.moduleNameToStrategy = createModuleNameToStrategy(modules); this.namespaceToModuleName = createNamespaceToModuleName(modules); } @@ -102,6 +106,18 @@ public class ConfigurationImpl implements Configuration { return b.build(); } + private static ListMultimap createModuleNameToShardName(Iterable moduleShards) { + final com.google.common.collect.ImmutableListMultimap.Builder b = ImmutableListMultimap.builder(); + + for (ModuleShard m : moduleShards) { + for (Shard s : m.getShards()) { + b.put(m.getModuleName(), s.getName()); + } + } + + return b.build(); + } + @Override public List getMemberShardNames(final String memberName){ Preconditions.checkNotNull(memberName, "memberName should not be null"); @@ -138,22 +154,10 @@ public class ConfigurationImpl implements Configuration { return moduleNameToStrategy; } - @Override public List getShardNamesFromModuleName(final String moduleName) { - + @Override + public List getShardNamesFromModuleName(final String moduleName) { Preconditions.checkNotNull(moduleName, "moduleName should not be null"); - - // FIXME: can be constant view of moduleShards - for(ModuleShard m : moduleShards){ - if(m.getModuleName().equals(moduleName)){ - List l = new ArrayList<>(); - for(Shard s : m.getShards()){ - l.add(s.getName()); - } - return l; - } - } - - return Collections.emptyList(); + return moduleNameToShardName.get(moduleName); } @Override public List getMembersFromShardName(final String shardName) {