Use a singleton instance for DefaultShardStrategy 90/14490/1
authorRobert Varga <rovarga@cisco.com>
Mon, 26 Jan 2015 13:45:30 +0000 (14:45 +0100)
committerRobert Varga <rovarga@cisco.com>
Mon, 26 Jan 2015 14:38:18 +0000 (15:38 +0100)
Instead of instantiating the DefaultShardStrategy everywhere it is used,
share a singleton instance.

Change-Id: I24ebd4b1d713a09b4fc4161e9c8c69a088af5ac6
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ConfigurationImpl.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/DefaultShardStrategy.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/shardstrategy/ShardStrategyFactory.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/shardstrategy/DefaultShardStrategyTest.java

index df5a576..0541e3a 100644 (file)
@@ -286,7 +286,7 @@ public class ConfigurationImpl implements Configuration {
             if(ModuleShardStrategy.NAME.equals(shardStrategy)){
                 this.shardStrategy = new ModuleShardStrategy(name, ConfigurationImpl.this);
             } else {
-                this.shardStrategy = new DefaultShardStrategy();
+                this.shardStrategy = DefaultShardStrategy.getInstance();
             }
         }
 
index 55c682b..6a3de42 100644 (file)
@@ -16,13 +16,21 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
  *   The default shard stores data for all modules for which a specific set of shards has not been configured
  * </p>
  */
-public class DefaultShardStrategy implements ShardStrategy{
+public final class DefaultShardStrategy implements ShardStrategy {
+    public static final String NAME = "default";
+    public static final String DEFAULT_SHARD = "default";
+    private static final DefaultShardStrategy INSTANCE = new DefaultShardStrategy();
 
-  public static final String NAME = "default";
-  public static final String DEFAULT_SHARD = "default";
+    private DefaultShardStrategy() {
+        // Hidden to force a singleton instnace
+    }
 
-  @Override
-  public String findShard(YangInstanceIdentifier path) {
-    return DEFAULT_SHARD;
-  }
+    public static DefaultShardStrategy getInstance() {
+        return INSTANCE;
+    }
+
+    @Override
+    public String findShard(YangInstanceIdentifier path) {
+        return DEFAULT_SHARD;
+    }
 }
index 62fb654..fd786c9 100644 (file)
@@ -36,7 +36,7 @@ public class ShardStrategyFactory {
         String moduleName = getModuleName(path);
         ShardStrategy shardStrategy = moduleNameToStrategyMap.get(moduleName);
         if (shardStrategy == null) {
-            return new DefaultShardStrategy();
+            return DefaultShardStrategy.getInstance();
         }
 
         return shardStrategy;
index c065782..5842d56 100644 (file)
@@ -5,10 +5,9 @@ import org.junit.Test;
 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
 
 public class DefaultShardStrategyTest {
-
-  @Test
-  public void testFindShard() throws Exception {
-    String shard = new DefaultShardStrategy().findShard(TestModel.TEST_PATH);
-    Assert.assertEquals(DefaultShardStrategy.DEFAULT_SHARD, shard);
-  }
+    @Test
+    public void testFindShard() throws Exception {
+        String shard = DefaultShardStrategy.getInstance().findShard(TestModel.TEST_PATH);
+        Assert.assertEquals(DefaultShardStrategy.DEFAULT_SHARD, shard);
+    }
 }
\ No newline at end of file