Search in sources :

Example 6 with DatabaseSet

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

the class DalShardingHelper method shuffleByTable.

/**
 * Shuffle by table shard id.
 * @param logicDbName
 * @param pojos
 * @return
 * @throws SQLException
 */
public static Map<String, Map<Integer, Map<String, ?>>> shuffleByTable(String logicDbName, String tableName, String tableShardId, Map<Integer, Map<String, ?>> pojos) 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 (Integer index : pojos.keySet()) {
        Map<String, ?> fields = pojos.get(index);
        String shardId = tableShardId == null ? strategy.locateTableShard(config, logicDbName, tableName, tmpHints.setFields(fields)) : tableShardId;
        Map<Integer, Map<String, ?>> pojosInShard = shuffled.get(shardId);
        if (pojosInShard == null) {
            pojosInShard = new LinkedHashMap<>();
            shuffled.put(shardId, pojosInShard);
        }
        pojosInShard.put(index, fields);
    }
    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) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 7 with DatabaseSet

use of com.ctrip.platform.dal.dao.configure.DatabaseSet 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;
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DalConfigure(com.ctrip.platform.dal.dao.configure.DalConfigure) DatabaseSet(com.ctrip.platform.dal.dao.configure.DatabaseSet) DalShardingStrategy(com.ctrip.platform.dal.dao.strategy.DalShardingStrategy) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

DatabaseSet (com.ctrip.platform.dal.dao.configure.DatabaseSet)7 DalShardingStrategy (com.ctrip.platform.dal.dao.strategy.DalShardingStrategy)5 DalConfigure (com.ctrip.platform.dal.dao.configure.DalConfigure)4 DalHints (com.ctrip.platform.dal.dao.DalHints)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)2 DalEventEnum (com.ctrip.platform.dal.dao.DalEventEnum)1 DalException (com.ctrip.platform.dal.exceptions.DalException)1 Connection (java.sql.Connection)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1