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)));
}
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)));
}
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;
}
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;
}
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;
}
Aggregations