Search in sources :

Example 11 with DalConfigure

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

the class DalClientFactory method internalInitClientFactory.

private static void internalInitClientFactory(String path) throws Exception {
    if (configureRef.get() != null) {
        logger.warn("Dal Java Client Factory is already initialized.");
        return;
    }
    synchronized (DalClientFactory.class) {
        if (configureRef.get() != null) {
            return;
        }
        DalConfigure config = null;
        if (path == null) {
            DalConfigLoader loader = ServiceLoaderHelper.getInstance(DalConfigLoader.class);
            if (loader == null)
                config = DalConfigureFactory.load();
            else
                config = loader.load();
            logger.info("Successfully initialized Dal Java Client Factory");
        } else {
            config = DalConfigureFactory.load(path);
            logger.info("Successfully initialized Dal Java Client Factory with " + path);
        }
        LogEntry.init();
        DalRequestExecutor.init(config.getFacory().getProperty(DalRequestExecutor.MAX_POOL_SIZE), config.getFacory().getProperty(DalRequestExecutor.KEEP_ALIVE_TIME));
        DalStatusManager.initialize(config);
        configureRef.set(config);
    }
}
Also used : DalConfigure(com.ctrip.platform.dal.dao.configure.DalConfigure) DalConfigLoader(com.ctrip.platform.dal.dao.configure.DalConfigLoader)

Example 12 with DalConfigure

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

the class DalClientFactory method getClient.

public static DalClient getClient(String logicDbName) {
    if (logicDbName == null)
        throw new NullPointerException("Database Set name can not be null");
    DalConfigure config = getDalConfigure();
    // Verify if it is existed
    config.getDatabaseSet(logicDbName);
    return new DalDirectClient(config, logicDbName);
}
Also used : DalConfigure(com.ctrip.platform.dal.dao.configure.DalConfigure) DalDirectClient(com.ctrip.platform.dal.dao.client.DalDirectClient)

Example 13 with DalConfigure

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

the class ShardColModShardStrategyTest method testLocateDbShardByShard.

@Test
public void testLocateDbShardByShard() 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().inShard("0")));
    assertEquals("1", strategy.locateDbShard(configure, logicDbName, new DalHints().inShard("1")));
}
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 14 with DalConfigure

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

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

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