Search in sources :

Example 1 with DalConfigure

use of com.ctrip.platform.dal.dao.configure.DalConfigure in project dal by ctripcorp.

the class ShardColModShardStrategyTest method testLocateDbShardByShardValue.

@Test
public void testLocateDbShardByShardValue() throws Exception {
    DalConfigure configure = DalConfigureFactory.load();
    ShardColModShardStrategy strategy = new ShardColModShardStrategy();
    Map<String, String> settings = new HashMap<String, String>();
    settings.put(ShardColModShardStrategy.COLUMNS, "id,id1");
    settings.put(ShardColModShardStrategy.MOD, "2");
    settings.put(ShardColModShardStrategy.TABLE_COLUMNS, "index,index1");
    settings.put(ShardColModShardStrategy.TABLE_MOD, "4");
    strategy.initialize(settings);
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardValue(0)));
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardValue(1)));
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardValue(2)));
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardValue(3)));
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardValue(100000000000L)));
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardValue(100000000001L)));
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardValue(100000000002L)));
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardValue(100000000003L)));
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalConfigure(com.ctrip.platform.dal.dao.configure.DalConfigure) HashMap(java.util.HashMap) ShardColModShardStrategy(com.ctrip.platform.dal.dao.strategy.ShardColModShardStrategy) Test(org.junit.Test)

Example 2 with DalConfigure

use of com.ctrip.platform.dal.dao.configure.DalConfigure in project dal by ctripcorp.

the class ShardColModShardStrategyTest method testLocateDbShardByParameters.

@Test
public void testLocateDbShardByParameters() throws Exception {
    DalConfigure configure = DalConfigureFactory.load();
    ShardColModShardStrategy strategy = new ShardColModShardStrategy();
    Map<String, String> settings = new HashMap<String, String>();
    settings.put(ShardColModShardStrategy.COLUMNS, "id,id1");
    settings.put(ShardColModShardStrategy.MOD, "2");
    settings.put(ShardColModShardStrategy.TABLE_COLUMNS, "index,index1");
    settings.put(ShardColModShardStrategy.TABLE_MOD, "4");
    strategy.initialize(settings);
    StatementParameters parameters = null;
    parameters = new StatementParameters();
    parameters.set(1, "id", Types.INTEGER, 0);
    parameters.set(1, "abc", Types.INTEGER, 1);
    parameters.set(1, "def", Types.INTEGER, 1);
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "id", Types.INTEGER, 1);
    parameters.set(1, "abc", Types.INTEGER, 1);
    parameters.set(1, "def", Types.INTEGER, 1);
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "id", Types.INTEGER, 2);
    parameters.set(2, "abc", Types.INTEGER, 1);
    parameters.set(3, "def", Types.INTEGER, 1);
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "id", Types.INTEGER, 3);
    parameters.set(2, "abc", Types.INTEGER, 1);
    parameters.set(3, "def", Types.INTEGER, 1);
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "id", Types.BIGINT, 100000000003L);
    parameters.set(2, "abc", Types.INTEGER, 1);
    parameters.set(3, "def", Types.INTEGER, 1);
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setParameters(parameters)));
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalConfigure(com.ctrip.platform.dal.dao.configure.DalConfigure) HashMap(java.util.HashMap) ShardColModShardStrategy(com.ctrip.platform.dal.dao.strategy.ShardColModShardStrategy) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) Test(org.junit.Test)

Example 3 with DalConfigure

use of com.ctrip.platform.dal.dao.configure.DalConfigure in project dal by ctripcorp.

the class ShardColModShardStrategyTest method testLocateTableShardByTableShardValue.

@Test
public void testLocateTableShardByTableShardValue() throws Exception {
    DalConfigure configure = DalConfigureFactory.load();
    ShardColModShardStrategy strategy = new ShardColModShardStrategy();
    Map<String, String> settings = new HashMap<String, String>();
    settings.put(ShardColModShardStrategy.COLUMNS, "id,id1");
    settings.put(ShardColModShardStrategy.MOD, "2");
    settings.put(ShardColModShardStrategy.TABLE_COLUMNS, "index,index1");
    settings.put(ShardColModShardStrategy.TABLE_MOD, "4");
    settings.put(ShardColModShardStrategy.SEPARATOR, "_");
    strategy.initialize(settings);
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue("0")));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue("1")));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue("2")));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue("3")));
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue("4")));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue("5")));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue("6")));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue("7")));
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(0)));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(1)));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(2)));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(3)));
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(4)));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(5)));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(6)));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(7)));
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(100000000004L)));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(100000000005L)));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(100000000006L)));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, new DalHints().setTableShardValue(100000000007L)));
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalConfigure(com.ctrip.platform.dal.dao.configure.DalConfigure) HashMap(java.util.HashMap) ShardColModShardStrategy(com.ctrip.platform.dal.dao.strategy.ShardColModShardStrategy) Test(org.junit.Test)

Example 4 with DalConfigure

use of com.ctrip.platform.dal.dao.configure.DalConfigure in project dal by ctripcorp.

the class ShardColModShardStrategyTest method testLocateTableShardByTableShard.

@Test
public void testLocateTableShardByTableShard() throws Exception {
    DalConfigure configure = DalConfigureFactory.load();
    ShardColModShardStrategy strategy = new ShardColModShardStrategy();
    Map<String, String> settings = new HashMap<String, String>();
    settings.put(ShardColModShardStrategy.COLUMNS, "id,id1");
    settings.put(ShardColModShardStrategy.MOD, "2");
    settings.put(ShardColModShardStrategy.TABLE_COLUMNS, "index,index1");
    settings.put(ShardColModShardStrategy.TABLE_MOD, "4");
    settings.put(ShardColModShardStrategy.SEPARATOR, "_");
    strategy.initialize(settings);
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, new DalHints().inTableShard("0")));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, new DalHints().inTableShard("1")));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, new DalHints().inTableShard("2")));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, new DalHints().inTableShard("3")));
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalConfigure(com.ctrip.platform.dal.dao.configure.DalConfigure) HashMap(java.util.HashMap) ShardColModShardStrategy(com.ctrip.platform.dal.dao.strategy.ShardColModShardStrategy) Test(org.junit.Test)

Example 5 with DalConfigure

use of com.ctrip.platform.dal.dao.configure.DalConfigure in project dal by ctripcorp.

the class ShardColModShardStrategyTest method testLocateTableShardByFields.

@Test
public void testLocateTableShardByFields() throws Exception {
    DalConfigure configure = DalConfigureFactory.load();
    ShardColModShardStrategy strategy = new ShardColModShardStrategy();
    Map<String, String> settings = new HashMap<String, String>();
    settings.put(ShardColModShardStrategy.COLUMNS, "id,id1");
    settings.put(ShardColModShardStrategy.MOD, "2");
    settings.put(ShardColModShardStrategy.TABLE_COLUMNS, "index,index1");
    settings.put(ShardColModShardStrategy.TABLE_MOD, "4");
    settings.put(ShardColModShardStrategy.SEPARATOR, "_");
    strategy.initialize(settings);
    Map<String, Object> fields = null;
    fields = new HashMap<String, Object>();
    fields.put("index", 0);
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("index1", 1);
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("index", 2);
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("index1", 3);
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("index", 4);
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("index1", 5);
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("index", 6);
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("index1", 7);
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("index1", 100000000007L);
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, new DalHints().setFields(fields)));
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalConfigure(com.ctrip.platform.dal.dao.configure.DalConfigure) HashMap(java.util.HashMap) ShardColModShardStrategy(com.ctrip.platform.dal.dao.strategy.ShardColModShardStrategy) Test(org.junit.Test)

Aggregations

DalConfigure (com.ctrip.platform.dal.dao.configure.DalConfigure)19 DalHints (com.ctrip.platform.dal.dao.DalHints)14 HashMap (java.util.HashMap)13 ShardColModShardStrategy (com.ctrip.platform.dal.dao.strategy.ShardColModShardStrategy)10 Test (org.junit.Test)10 DalShardingStrategy (com.ctrip.platform.dal.dao.strategy.DalShardingStrategy)5 DatabaseSet (com.ctrip.platform.dal.dao.configure.DatabaseSet)4 LinkedHashMap (java.util.LinkedHashMap)3 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)2 Map (java.util.Map)2 DalDirectClient (com.ctrip.platform.dal.dao.client.DalDirectClient)1 DalConfigLoader (com.ctrip.platform.dal.dao.configure.DalConfigLoader)1 SQLException (java.sql.SQLException)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1