Search in sources :

Example 11 with ShardColModShardStrategy

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

the class ShardColModShardStrategyTest method testLocateDbShardByFields.

@Test
public void testLocateDbShardByFields() 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);
    Map<String, Object> fields = new HashMap<String, Object>();
    fields.put("id", 0);
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setFields(fields)));
    fields.put("id", 1);
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setFields(fields)));
    fields.put("id", 2);
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setFields(fields)));
    fields.put("id", 3);
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setFields(fields)));
    // Test long
    fields.put("id", 100000000000L);
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setFields(fields)));
    fields.put("id", 100000000001L);
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setFields(fields)));
    fields.put("id", 100000000002L);
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setFields(fields)));
    fields.put("id", 100000000003L);
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setFields(fields)));
    // Test case insensitive
    fields.clear();
    fields.put("iD", 0);
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setFields(fields)));
    fields.clear();
    fields.put("Id", 1);
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setFields(fields)));
    fields.clear();
    fields.put("ID", 2);
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setFields(fields)));
    // Test long
    fields.clear();
    fields.put("iD", 100000000000L);
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setFields(fields)));
    fields.clear();
    fields.put("Id", 100000000001L);
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setFields(fields)));
    fields.clear();
    fields.put("ID", 100000000002L);
    assertEquals("0", strategy.locateDbShard(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)

Example 12 with ShardColModShardStrategy

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

the class ShardColModShardStrategyTest method testLocateDbShardByShardCol.

@Test
public void testLocateDbShardByShardCol() 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().setShardColValue("id", 0)));
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardColValue("id", 1)));
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardColValue("id", 2)));
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardColValue("id", 3)));
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardColValue("id", 100000000000L)));
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardColValue("id", 100000000001L)));
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardColValue("id", 100000000002L)));
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardColValue("id", 100000000003L)));
    // Test case insensitive
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardColValue("Id", 0)));
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardColValue("iD", 1)));
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardColValue("ID", 2)));
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardColValue("iD", 3)));
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardColValue("iD", 100000000000L)));
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardColValue("Id", 100000000001L)));
    assertEquals("0", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardColValue("ID", 100000000002L)));
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().setShardColValue("iD", 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 13 with ShardColModShardStrategy

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

the class ShardColModShardStrategyTest method testLocateTableShardByParameters.

@Test
public void testLocateTableShardByParameters() 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);
    StatementParameters parameters = null;
    parameters = new StatementParameters();
    parameters.set(1, "index", Types.INTEGER, 0);
    parameters.set(1, "abc", Types.INTEGER, 1);
    parameters.set(1, "def", Types.INTEGER, 1);
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setParameters(parameters)));
    parameters.set(1, "index", Types.INTEGER, 100000000004L);
    parameters.set(1, "abc", Types.INTEGER, 1);
    parameters.set(1, "def", Types.INTEGER, 1);
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "index", Types.INTEGER, 1);
    parameters.set(1, "abc", Types.INTEGER, 1);
    parameters.set(1, "def", Types.INTEGER, 1);
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "index", Types.INTEGER, 100000000001L);
    parameters.set(1, "abc", Types.INTEGER, 1);
    parameters.set(1, "def", Types.INTEGER, 1);
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "index", Types.INTEGER, 2);
    parameters.set(2, "abc", Types.INTEGER, 1);
    parameters.set(3, "def", Types.INTEGER, 1);
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "index", Types.INTEGER, 3);
    parameters.set(2, "abc", Types.INTEGER, 1);
    parameters.set(3, "def", Types.INTEGER, 1);
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setParameters(parameters)));
    // Test case insensitive
    parameters = new StatementParameters();
    parameters.set(1, "indeX", Types.INTEGER, 0);
    parameters.set(1, "abc", Types.INTEGER, 1);
    parameters.set(1, "def", Types.INTEGER, 1);
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setParameters(parameters)));
    parameters.set(1, "indEx", Types.INTEGER, 100000000004L);
    parameters.set(1, "abc", Types.INTEGER, 1);
    parameters.set(1, "def", Types.INTEGER, 1);
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "inDex", Types.INTEGER, 1);
    parameters.set(1, "abc", Types.INTEGER, 1);
    parameters.set(1, "def", Types.INTEGER, 1);
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "iNdex", Types.INTEGER, 100000000001L);
    parameters.set(1, "abc", Types.INTEGER, 1);
    parameters.set(1, "def", Types.INTEGER, 1);
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "Index", Types.INTEGER, 2);
    parameters.set(2, "abc", Types.INTEGER, 1);
    parameters.set(3, "def", Types.INTEGER, 1);
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "INDEX", Types.INTEGER, 3);
    parameters.set(2, "abc", Types.INTEGER, 1);
    parameters.set(3, "def", Types.INTEGER, 1);
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setParameters(parameters)));
    // Test another key
    parameters = new StatementParameters();
    parameters.set(1, "index1", Types.INTEGER, 0);
    parameters.set(1, "abc", Types.INTEGER, 1);
    parameters.set(1, "def", Types.INTEGER, 1);
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "index1", Types.INTEGER, 1);
    parameters.set(1, "abc", Types.INTEGER, 1);
    parameters.set(1, "def", Types.INTEGER, 1);
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "index1", Types.INTEGER, 2);
    parameters.set(2, "abc", Types.INTEGER, 1);
    parameters.set(3, "def", Types.INTEGER, 1);
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "index1", Types.INTEGER, 3);
    parameters.set(2, "abc", Types.INTEGER, 1);
    parameters.set(3, "def", Types.INTEGER, 1);
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "index", Types.INTEGER, 0);
    parameters.set(1, "index1", Types.INTEGER, 1);
    parameters.set(1, "def", Types.INTEGER, 1);
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "index", Types.INTEGER, 1);
    parameters.set(1, "index1", Types.INTEGER, 1);
    parameters.set(1, "def", Types.INTEGER, 1);
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "index", Types.INTEGER, 2);
    parameters.set(2, "index1", Types.INTEGER, 1);
    parameters.set(3, "def", Types.INTEGER, 1);
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setParameters(parameters)));
    parameters = new StatementParameters();
    parameters.set(1, "index", Types.INTEGER, 3);
    parameters.set(2, "index1", Types.INTEGER, 1);
    parameters.set(3, "def", Types.INTEGER, 1);
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, 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 14 with ShardColModShardStrategy

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

the class ShardColModShardStrategyTest method testLocateTableShardByShardCol.

@Test
public void testLocateTableShardByShardCol() 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().setShardColValue("index", 0)));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("index1", 1)));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("index", 2)));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("index1", 3)));
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("index", 4)));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("index1", 5)));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("index", 6)));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("index1", 7)));
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("index", 100000000004L)));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("index1", 100000000005L)));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("index", 100000000006L)));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("index1", 100000000007L)));
    // Test case insensitive
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("Index", 0)));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("iNdex1", 1)));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("inDex", 2)));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("inDEx1", 3)));
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("indeX", 4)));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("INDEX1", 5)));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("INdex", 6)));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("inDEX1", 7)));
    assertEquals("0", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("InDeX", 100000000004L)));
    assertEquals("1", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("iNdEx1", 100000000005L)));
    assertEquals("2", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("InDEX", 100000000006L)));
    assertEquals("3", strategy.locateTableShard(configure, logicDbName, tableName, new DalHints().setShardColValue("iNDEX1", 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 15 with ShardColModShardStrategy

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

the class ShardColModShardStrategyTest method testIsShardingByDbFalse.

@Test
public void testIsShardingByDbFalse() {
    ShardColModShardStrategy strategy = new ShardColModShardStrategy();
    Map<String, String> settings = new HashMap<String, String>();
    // settings.put(ShardColModShardStrategy.COLUMNS, "id,id1");
    settings.put(ShardColModShardStrategy.MOD, "2");
    strategy.initialize(settings);
    assertFalse(strategy.isShardingByDb());
}
Also used : 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