Search in sources :

Example 1 with ShardingDBModel

use of org.sagacity.sqltoy.model.ShardingDBModel in project sagacity-sqltoy by chenrenfei.

the class HashShardingStrategy method getShardingDB.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.sagacity.sqltoy.plugin.ShardingStrategy#getShardingModel(org.sagacity.
	 * sqltoy.SqlToyContext, java.lang.String, java.lang.String, java.util.HashMap)
	 */
@Override
public ShardingDBModel getShardingDB(SqlToyContext sqlToyContext, Class entityClass, String tableOrSql, String strategyVar, IgnoreCaseLinkedMap<String, Object> paramsMap) {
    ShardingDBModel shardingModel = new ShardingDBModel();
    if (mode < 1 || paramsMap == null || paramsMap.isEmpty())
        return shardingModel;
    // 单值hash取模
    Object shardingValue = paramsMap.values().iterator().next();
    int hashCode = shardingValue.hashCode();
    shardingModel.setDataSourceName(dataSourceMap.get(Integer.toString(hashCode % mode)));
    return shardingModel;
}
Also used : ShardingDBModel(org.sagacity.sqltoy.model.ShardingDBModel)

Example 2 with ShardingDBModel

use of org.sagacity.sqltoy.model.ShardingDBModel in project sagacity-sqltoy by chenrenfei.

the class ShardingUtils method getSharding.

/**
 * @todo 单个对象sharding策略处理,适用于load、save、update、delete单对象操作
 * @param sqlToyContext
 * @param entity
 * @param wrapIdValue
 * @param dataSource
 * @return
 * @throws Exception
 */
public static ShardingModel getSharding(SqlToyContext sqlToyContext, Serializable entity, boolean wrapIdValue, DataSource dataSource) throws Exception {
    ShardingModel shardingModel = new ShardingModel();
    shardingModel.setDataSource(dataSource);
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
    // 主键值需要提前按照主键策略赋予(sequence 和assign模式的不会实际执行赋值)
    if (wrapIdValue)
        assignPK(sqlToyContext, entityMeta, entity);
    ShardingConfig shardingConfig = entityMeta.getShardingConfig();
    if (shardingConfig == null)
        return shardingModel;
    ShardingStrategy shardingStrategy;
    ShardingStrategyConfig strategyConfig;
    // 分库策略处理
    if (shardingConfig.getShardingDBStrategy() != null) {
        strategyConfig = shardingConfig.getShardingDBStrategy();
        shardingStrategy = sqlToyContext.getShardingStrategy(strategyConfig.getName());
        if (shardingStrategy == null)
            throw new Exception("POJO 对象:" + entity.getClass().getName() + " Sharding DB Strategy:" + strategyConfig.getName() + " 未定义,请检查!");
        IgnoreCaseLinkedMap<String, Object> valueMap = hashParams(strategyConfig.getAliasNames(), BeanUtil.reflectBeanToAry(entity, strategyConfig.getFields(), null, null));
        ShardingDBModel dbModel = shardingStrategy.getShardingDB(sqlToyContext, entity.getClass(), entityMeta.getSchemaTable(), strategyConfig.getDecisionType(), valueMap);
        shardingModel.setDataSourceName(dbModel.getDataSourceName());
        if (dbModel.getDataSource() == null)
            shardingModel.setDataSource(sqlToyContext.getDataSource(dbModel.getDataSourceName()));
        else
            shardingModel.setDataSource(dbModel.getDataSource());
    }
    // 分表策略
    if (shardingConfig.getShardingTableStrategy() != null) {
        strategyConfig = shardingConfig.getShardingTableStrategy();
        shardingStrategy = sqlToyContext.getShardingStrategy(strategyConfig.getName());
        if (shardingStrategy == null)
            throw new Exception("POJO 对象:" + entity.getClass().getName() + " Sharding Table Strategy:" + strategyConfig.getName() + " 未定义,请检查!");
        IgnoreCaseLinkedMap<String, Object> valueMap = hashParams(strategyConfig.getAliasNames(), BeanUtil.reflectBeanToAry(entity, strategyConfig.getFields(), null, null));
        String tableName = shardingStrategy.getShardingTable(sqlToyContext, entity.getClass(), entityMeta.getSchemaTable(), strategyConfig.getDecisionType(), valueMap);
        if (StringUtil.isNotBlank(tableName))
            shardingModel.setTableName(tableName);
    }
    return shardingModel;
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) ShardingStrategy(org.sagacity.sqltoy.plugin.ShardingStrategy) ShardingConfig(org.sagacity.sqltoy.config.model.ShardingConfig) ShardingStrategyConfig(org.sagacity.sqltoy.config.model.ShardingStrategyConfig) ShardingDBModel(org.sagacity.sqltoy.model.ShardingDBModel) QueryShardingModel(org.sagacity.sqltoy.config.model.QueryShardingModel) ShardingModel(org.sagacity.sqltoy.model.ShardingModel)

Example 3 with ShardingDBModel

use of org.sagacity.sqltoy.model.ShardingDBModel in project sagacity-sqltoy by chenrenfei.

the class ShardingUtils method groupShardings.

/**
 * @todo 批量sharding策略处理
 * @param sqlToyContext
 * @param entities
 * @param entityMeta
 * @param dataSource
 * @return
 * @throws Exception
 */
public static Collection<ShardingGroupModel> groupShardings(SqlToyContext sqlToyContext, List<?> entities, EntityMeta entityMeta, DataSource dataSource) throws Exception {
    ShardingConfig shardingConfig = entityMeta.getShardingConfig();
    ShardingModel shardingModel = null;
    // 没有sharding配置,则作为单个分组返回
    if (shardingConfig == null) {
        Collection<ShardingGroupModel> result = new ArrayList<ShardingGroupModel>();
        ShardingGroupModel model = new ShardingGroupModel();
        shardingModel = new ShardingModel();
        shardingModel.setDataSource(dataSource);
        model.setShardingModel(shardingModel);
        model.setEntities(entities);
        result.add(model);
        return result;
    }
    Class entityClass = entities.get(0).getClass();
    String entityTable = entityMeta.getSchemaTable();
    // 分库
    boolean hasDB = false;
    ShardingStrategy dbStrategy = null;
    List<Object[]> shardingDBValues = null;
    ShardingStrategyConfig dbConfig = shardingConfig.getShardingDBStrategy();
    if (dbConfig != null) {
        hasDB = true;
        dbStrategy = sqlToyContext.getShardingStrategy(dbConfig.getName());
        if (dbStrategy == null)
            throw new Exception("POJO 对象:" + entityClass.getName() + " Sharding DB Strategy:" + dbConfig.getName() + " 未定义,请检查!");
        shardingDBValues = BeanUtil.reflectBeansToInnerAry(entities, dbConfig.getFields(), null, null, false, 0);
    }
    // 分表
    boolean hasTable = false;
    ShardingStrategy tableStrategy = null;
    ShardingStrategyConfig tableConfig = shardingConfig.getShardingTableStrategy();
    List<Object[]> shardingTableValues = null;
    if (tableConfig != null) {
        hasTable = true;
        tableStrategy = sqlToyContext.getShardingStrategy(tableConfig.getName());
        if (tableStrategy == null)
            throw new Exception("POJO 对象:" + entityClass.getName() + " Sharding Table Strategy:" + tableConfig.getName() + " 未定义,请检查!");
        shardingTableValues = BeanUtil.reflectBeansToInnerAry(entities, tableConfig.getFields(), null, null, false, 0);
    }
    Map<String, ShardingGroupModel> shardingGroupMaps = new HashMap<String, ShardingGroupModel>();
    IgnoreCaseLinkedMap<String, Object> valueMap;
    ShardingDBModel shardingDBModel = null;
    // 数据分组key(dataSourceName+tableName)
    String dataGroupKey;
    String tableName = null;
    String dataSourceName = null;
    for (int i = 0; i < entities.size(); i++) {
        // 分库
        if (hasDB) {
            valueMap = hashParams(dbConfig.getAliasNames(), shardingDBValues.get(i));
            shardingDBModel = dbStrategy.getShardingDB(sqlToyContext, entityClass, entityTable, dbConfig.getDecisionType(), valueMap);
            dataSourceName = shardingDBModel.getDataSourceName();
        }
        // 分表
        if (hasTable) {
            valueMap = hashParams(tableConfig.getAliasNames(), shardingTableValues.get(i));
            tableName = tableStrategy.getShardingTable(sqlToyContext, entityClass, entityTable, tableConfig.getDecisionType(), valueMap);
        }
        dataGroupKey = dataSourceName + tableName;
        // 归并到相同分组
        if (shardingGroupMaps.containsKey(dataGroupKey)) {
            shardingGroupMaps.get(dataGroupKey).getEntities().add(entities.get(i));
        } else {
            // 不同分组
            ShardingGroupModel groupModel = new ShardingGroupModel();
            groupModel.setKey(dataGroupKey);
            // 创建数据分组集合
            List items = new ArrayList();
            items.add(entities.get(i));
            groupModel.setEntities(items);
            shardingModel = new ShardingModel();
            // 分库,设置分组对应的数据库
            if (hasDB) {
                shardingModel.setDataSourceName(dataSourceName);
                if (shardingDBModel.getDataSource() == null)
                    shardingModel.setDataSource(sqlToyContext.getDataSource(shardingDBModel.getDataSourceName()));
                else
                    shardingModel.setDataSource(shardingDBModel.getDataSource());
            } else
                shardingModel.setDataSource(dataSource);
            // 分表,设置表名
            if (hasTable && StringUtil.isNotBlank(tableName))
                shardingModel.setTableName(tableName);
            groupModel.setShardingModel(shardingModel);
            shardingGroupMaps.put(dataGroupKey, groupModel);
        }
    }
    return shardingGroupMaps.values();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ShardingDBModel(org.sagacity.sqltoy.model.ShardingDBModel) ShardingGroupModel(org.sagacity.sqltoy.model.ShardingGroupModel) ShardingStrategy(org.sagacity.sqltoy.plugin.ShardingStrategy) ShardingConfig(org.sagacity.sqltoy.config.model.ShardingConfig) ShardingStrategyConfig(org.sagacity.sqltoy.config.model.ShardingStrategyConfig) ArrayList(java.util.ArrayList) List(java.util.List) QueryShardingModel(org.sagacity.sqltoy.config.model.QueryShardingModel) ShardingModel(org.sagacity.sqltoy.model.ShardingModel)

Example 4 with ShardingDBModel

use of org.sagacity.sqltoy.model.ShardingDBModel in project sagacity-sqltoy by chenrenfei.

the class ShardingUtils method getShardingDataSource.

/**
 * @todo 根据数据获取sharding对应的DataSource
 * @param sqlToyContext
 * @param sqlToyConfig
 * @param valueMap
 * @return
 */
private static DataSource getShardingDataSource(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, IgnoreCaseLinkedMap<String, Object> valueMap) {
    if (sqlToyConfig.getDataSourceShardingStragety() == null)
        return null;
    ShardingStrategy shardingStrategy = sqlToyContext.getShardingStrategy(sqlToyConfig.getDataSourceShardingStragety());
    if (shardingStrategy == null)
        return null;
    IgnoreCaseLinkedMap<String, Object> realDataMap = null;
    if (sqlToyConfig.getDataSourceShardingParams() != null) {
        realDataMap = new IgnoreCaseLinkedMap<String, Object>();
        for (int i = 0, n = sqlToyConfig.getDataSourceShardingParams().length; i < n; i++) {
            realDataMap.put(sqlToyConfig.getDataSourceShardingParamsAlias()[i], valueMap.get(sqlToyConfig.getDataSourceShardingParams()[i]));
        }
    } else
        realDataMap = valueMap;
    ShardingDBModel shardingDBModel = shardingStrategy.getShardingDB(sqlToyContext, null, sqlToyConfig.getId(), sqlToyConfig.getDataSourceShardingStrategyValue(), realDataMap);
    if (shardingDBModel.getDataSource() != null)
        return shardingDBModel.getDataSource();
    else
        return sqlToyContext.getDataSource(shardingDBModel.getDataSourceName());
}
Also used : ShardingStrategy(org.sagacity.sqltoy.plugin.ShardingStrategy) ShardingDBModel(org.sagacity.sqltoy.model.ShardingDBModel)

Example 5 with ShardingDBModel

use of org.sagacity.sqltoy.model.ShardingDBModel in project sagacity-sqltoy by chenrenfei.

the class DefaultShardingStrategy method getDataSource.

/**
 * 根据权重配置分配数据库
 *
 * @param dataSourceMap
 * @return
 */
private ShardingDBModel getDataSource() {
    int index = 0;
    String chooseDataSource = null;
    // 根据权重进行随机取具体哪个dataSource
    if (dataSourceWeightConfig.length > 1) {
        index = CommonUtils.getProbabilityIndex(weights);
    }
    chooseDataSource = dataSourceWeightConfig[index][0].toString();
    if (logger.isDebugEnabled())
        logger.debug("本次sharding选择中的数据库为:{}", chooseDataSource);
    else
        System.out.println("本次sharding选择中的数据库为:{" + chooseDataSource + "}");
    ShardingDBModel shardingModel = new ShardingDBModel();
    shardingModel.setDataSourceName(chooseDataSource);
    shardingModel.setDataSource((DataSource) applicationContext.getBean(chooseDataSource));
    return shardingModel;
}
Also used : ShardingDBModel(org.sagacity.sqltoy.model.ShardingDBModel)

Aggregations

ShardingDBModel (org.sagacity.sqltoy.model.ShardingDBModel)5 ShardingStrategy (org.sagacity.sqltoy.plugin.ShardingStrategy)3 QueryShardingModel (org.sagacity.sqltoy.config.model.QueryShardingModel)2 ShardingConfig (org.sagacity.sqltoy.config.model.ShardingConfig)2 ShardingStrategyConfig (org.sagacity.sqltoy.config.model.ShardingStrategyConfig)2 ShardingModel (org.sagacity.sqltoy.model.ShardingModel)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)1 ShardingGroupModel (org.sagacity.sqltoy.model.ShardingGroupModel)1