Search in sources :

Example 16 with DalConfigure

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

the class DalShardingHelper method locateTableShardId.

/**
 * Locate table shard id by hints.
 * @param logicDbName
 * @param hints
 * @return
 * @throws SQLException
 */
public static String locateTableShardId(String logicDbName, String tableName, DalHints hints, StatementParameters parameters, Map<String, ?> fields) throws SQLException {
    DalConfigure config = DalClientFactory.getDalConfigure();
    DalShardingStrategy strategy = config.getDatabaseSet(logicDbName).getStrategy();
    // First check if we can locate the table shard id with the original hints
    String shard = strategy.locateTableShard(config, logicDbName, tableName, hints);
    if (shard != null)
        return shard;
    shard = strategy.locateTableShard(config, logicDbName, tableName, new DalHints().setParameters(parameters).setFields(fields));
    if (shard != null)
        return shard;
    throw new SQLException("Can not locate table shard for " + logicDbName);
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalShardingStrategy(com.ctrip.platform.dal.dao.strategy.DalShardingStrategy) DalConfigure(com.ctrip.platform.dal.dao.configure.DalConfigure) SQLException(java.sql.SQLException)

Example 17 with DalConfigure

use of com.ctrip.platform.dal.dao.configure.DalConfigure 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 18 with DalConfigure

use of com.ctrip.platform.dal.dao.configure.DalConfigure 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 19 with DalConfigure

use of com.ctrip.platform.dal.dao.configure.DalConfigure 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 20 with DalConfigure

use of com.ctrip.platform.dal.dao.configure.DalConfigure 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)

Aggregations

DalConfigure (com.ctrip.platform.dal.dao.configure.DalConfigure)20 DalHints (com.ctrip.platform.dal.dao.DalHints)15 HashMap (java.util.HashMap)14 ShardColModShardStrategy (com.ctrip.platform.dal.dao.strategy.ShardColModShardStrategy)11 Test (org.junit.Test)11 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