use of com.ctrip.platform.dal.dao.strategy.DalShardingStrategy in project dal by ctripcorp.
the class DalShardingHelper method shuffle.
/**
* Group pojos by shard id. Should be only used for DB that support sharding.
*
* @param logicDbName
* @param pojos
* @return Grouped pojos
* @throws SQLException In case locate shard id faild
*/
public static Map<String, Map<Integer, Map<String, ?>>> shuffle(String logicDbName, String shardId, List<Map<String, ?>> daoPojos) throws SQLException {
Map<String, Map<Integer, Map<String, ?>>> 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 < daoPojos.size(); i++) {
Map<String, ?> pojo = daoPojos.get(i);
String tmpShardId = shardId == null ? strategy.locateDbShard(config, logicDbName, tmpHints.setFields(pojo)) : shardId;
dbSet.validate(tmpShardId);
Map<Integer, Map<String, ?>> pojosInShard = shuffled.get(tmpShardId);
if (pojosInShard == null) {
pojosInShard = new LinkedHashMap<>();
shuffled.put(tmpShardId, pojosInShard);
}
pojosInShard.put(i, pojo);
}
detectDistributedTransaction(shuffled.keySet());
return shuffled;
}
use of com.ctrip.platform.dal.dao.strategy.DalShardingStrategy 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);
}
Aggregations