Instead of looking up in the global array which is constant,
pre-construct a constant lookup map.
Change-Id: I85d54332b0a3b717d33da9758eaafbce5bd5595d
Signed-off-by: Robert Varga <rovarga@cisco.com>
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
+import com.google.common.collect.ImmutableMap;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigObject;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigObject;
// key = shardName, value = list of replicaNames (replicaNames are the same as memberNames)
private final Map<String, List<String>> shardReplicaNames = new HashMap<>();
// key = shardName, value = list of replicaNames (replicaNames are the same as memberNames)
private final Map<String, List<String>> shardReplicaNames = new HashMap<>();
+ private final Map<String, String> namespaceToModuleName;
public ConfigurationImpl(final String moduleShardsConfigPath,
public ConfigurationImpl(final String moduleShardsConfigPath,
this.moduleShards = readModuleShards(moduleShardsConfig);
this.modules = readModules(modulesConfig);
this.moduleShards = readModuleShards(moduleShardsConfig);
this.modules = readModules(modulesConfig);
+
+ namespaceToModuleName = createNamespaceToModuleName(modules);
+ }
+
+ private static Map<String, String> createNamespaceToModuleName(Iterable<Module> modules) {
+ final com.google.common.collect.ImmutableMap.Builder<String, String> b = ImmutableMap.builder();
+ for (Module m : modules) {
+ b.put(m.getNameSpace(), m.getName());
+ }
+ return b.build();
}
@Override public List<String> getMemberShardNames(final String memberName){
}
@Override public List<String> getMemberShardNames(final String memberName){
- @Override public Optional<String> getModuleNameFromNameSpace(final String nameSpace) {
-
+ @Override
+ public Optional<String> getModuleNameFromNameSpace(final String nameSpace) {
Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
-
- for(Module m : modules){
- if(m.getNameSpace().equals(nameSpace)){
- return Optional.of(m.getName());
- }
- }
- return Optional.absent();
+ return Optional.fromNullable(namespaceToModuleName.get(nameSpace));
}
@Override public Map<String, ShardStrategy> getModuleNameToShardStrategyMap() {
}
@Override public Map<String, ShardStrategy> getModuleNameToShardStrategyMap() {
+ // FIXME: can be constant view of modules
Map<String, ShardStrategy> map = new HashMap<>();
for(Module m : modules){
map.put(m.getName(), m.getShardStrategy());
Map<String, ShardStrategy> map = new HashMap<>();
for(Module m : modules){
map.put(m.getName(), m.getShardStrategy());
Preconditions.checkNotNull(moduleName, "moduleName should not be null");
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<String> l = new ArrayList<>();
for(ModuleShard m : moduleShards){
if(m.getModuleName().equals(moduleName)){
List<String> l = new ArrayList<>();