Search in sources :

Example 6 with ShardColModShardStrategy

use of com.ctrip.platform.dal.dao.strategy.ShardColModShardStrategy in project dal by ctripcorp.

the class ShardColModShardStrategyTest method testLocateTableShardByTableShardForBackwardCompatible.

@Test
public void testLocateTableShardByTableShardForBackwardCompatible() 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 7 with ShardColModShardStrategy

use of com.ctrip.platform.dal.dao.strategy.ShardColModShardStrategy 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, tableName, new DalHints().setTableShardValue("0")));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setTableShardValue("1")));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setTableShardValue("2")));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setTableShardValue("3")));
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setTableShardValue("4")));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setTableShardValue("5")));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setTableShardValue("6")));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setTableShardValue("7")));
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setTableShardValue(0)));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setTableShardValue(1)));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setTableShardValue(2)));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setTableShardValue(3)));
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setTableShardValue(4)));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setTableShardValue(5)));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setTableShardValue(6)));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setTableShardValue(7)));
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setTableShardValue(100000000004L)));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setTableShardValue(100000000005L)));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setTableShardValue(100000000006L)));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, 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 8 with ShardColModShardStrategy

use of com.ctrip.platform.dal.dao.strategy.ShardColModShardStrategy 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)));
    // Test case insensitive
    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 9 with ShardColModShardStrategy

use of com.ctrip.platform.dal.dao.strategy.ShardColModShardStrategy 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, tableName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("index1", 1);
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("index", 2);
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("index1", 3);
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("index", 4);
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("index1", 5);
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("index", 6);
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("index1", 7);
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("index1", 100000000007L);
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setFields(fields)));
    // Test case insensitive
    fields = new HashMap<String, Object>();
    fields.put("Index", 0);
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("iNdex1", 1);
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("inDex", 2);
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("indEx1", 3);
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("indeX", 4);
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("INdex1", 5);
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("iNDex", 6);
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("inDEx1", 7);
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setFields(fields)));
    fields = new HashMap<String, Object>();
    fields.put("indEX1", 100000000007L);
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, 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)

Example 10 with ShardColModShardStrategy

use of com.ctrip.platform.dal.dao.strategy.ShardColModShardStrategy in project dal by ctripcorp.

the class ShardColModShardStrategyTest method testLocateDbShardByShard.

@Test
public void testLocateDbShardByShard() 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().inShard("0")));
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().inShard("1")));
}
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

ShardColModShardStrategy (com.ctrip.platform.dal.dao.strategy.ShardColModShardStrategy)16 HashMap (java.util.HashMap)16 Test (org.junit.Test)16 DalHints (com.ctrip.platform.dal.dao.DalHints)11 DalConfigure (com.ctrip.platform.dal.dao.configure.DalConfigure)11 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)2