Search in sources :

Example 6 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)));
    // 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 7 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, 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 8 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
 */
private static boolean locateTableShardId(String logicDbName, String tableName, DalHints hints) 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 tableShardId = strategy.locateTableShard(config, logicDbName, tableName, hints);
    if (tableShardId == null)
        return false;
    hints.inTableShard(tableShardId);
    return true;
}
Also used : DalShardingStrategy(com.ctrip.platform.dal.dao.strategy.DalShardingStrategy) DalConfigure(com.ctrip.platform.dal.dao.configure.DalConfigure)

Example 9 with DalConfigure

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

the class DalShardingHelper method shuffle.

/**
 * Shuffle by given values like id list for DB shard
 * @param logicDbName
 * @param parameters
 * @return
 * @throws SQLException
 */
public static Map<String, List<?>> shuffle(String logicDbName, List<?> parameters) throws SQLException {
    Map<String, List<?>> shuffled = new HashMap<>();
    DalConfigure config = DalClientFactory.getDalConfigure();
    DatabaseSet dbSet = config.getDatabaseSet(logicDbName);
    DalShardingStrategy strategy = dbSet.getStrategy();
    DalHints tmpHints = new DalHints();
    for (int i = 0; i < parameters.size(); i++) {
        Object value = parameters.get(i);
        String tmpShardId = strategy.locateDbShard(config, logicDbName, tmpHints.setShardValue(value));
        // If this can not be located
        if (tmpShardId == null)
            throw new NullPointerException("Can not locate shard id for " + value);
        dbSet.validate(tmpShardId);
        List pojosInShard = shuffled.get(tmpShardId);
        if (pojosInShard == null) {
            pojosInShard = new LinkedList();
            shuffled.put(tmpShardId, pojosInShard);
        }
        pojosInShard.add(value);
    }
    detectDistributedTransaction(shuffled.keySet());
    return shuffled;
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DatabaseSet(com.ctrip.platform.dal.dao.configure.DatabaseSet) DalShardingStrategy(com.ctrip.platform.dal.dao.strategy.DalShardingStrategy) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DalConfigure(com.ctrip.platform.dal.dao.configure.DalConfigure) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList)

Example 10 with DalConfigure

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

the class DalShardingHelper method locateShardId.

/**
 * Try to locate DB shard id by hints. If can not be located, return false.
 * @param logicDbName
 * @param hints
 * @return true if shard id can be located
 * @throws SQLException
 */
private static boolean locateShardId(String logicDbName, DalHints hints) throws SQLException {
    DalConfigure config = DalClientFactory.getDalConfigure();
    DatabaseSet dbSet = config.getDatabaseSet(logicDbName);
    String shardId = dbSet.getStrategy().locateDbShard(config, logicDbName, hints);
    if (shardId == null)
        return false;
    // Fail fast asap
    dbSet.validate(shardId);
    hints.inShard(shardId);
    return true;
}
Also used : DatabaseSet(com.ctrip.platform.dal.dao.configure.DatabaseSet) DalConfigure(com.ctrip.platform.dal.dao.configure.DalConfigure)

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