use of com.chao.cloud.common.extra.sharding.annotation.ShardingProperties in project chao-cloud by chaojunzi.
the class DefaultDsShardingAlgorithm method doSharding.
@Override
public Collection<String> doSharding(Collection<String> collection, ComplexKeysShardingValue<String> complexKeysShardingValue) {
ShardingProperties prop = SpringUtil.getBean(ShardingProperties.class);
String dsShardingColumn = prop.getDsShardingColumn();
// 获取表名
String table = complexKeysShardingValue.getLogicTableName();
Set<String> dsSet = new HashSet<>();
// 获取分库字段及字段值
Map<String, Collection<String>> map = complexKeysShardingValue.getColumnNameAndShardingValuesMap();
Collection<String> vals = map.get(dsShardingColumn);
if (CollUtil.isNotEmpty(vals)) {
for (String v : vals) {
String ds = SpringUtil.getBean(ShardingExtraConfig.class).getDsByColumnValue(v);
if (StrUtil.isNotBlank(ds)) {
dsSet.add(ds);
}
}
}
if (CollUtil.isEmpty(dsSet)) {
// 默认数据源
dsSet.add(prop.getDefaultDsName());
}
log.info("【DS:default】【{}】{}.{}=[{}]", CollUtil.join(dsSet, StrUtil.COMMA), table, dsShardingColumn, CollUtil.join(vals, StrUtil.COMMA));
return dsSet;
}
use of com.chao.cloud.common.extra.sharding.annotation.ShardingProperties in project chao-cloud by chaojunzi.
the class DsHintShardingAlgorithm method doSharding.
@Override
public Collection<String> doSharding(Collection<String> dsList, HintShardingValue<String> shardingValue) {
// 获取配置信息
ShardingProperties prop = SpringUtil.getBean(ShardingProperties.class);
// 获取表名
String table = shardingValue.getLogicTableName();
// 获取传进来的值
Collection<String> vals = shardingValue.getValues();
// 返回数据源
Set<String> dsResult = new HashSet<>();
//
if (CollUtil.isNotEmpty(vals)) {
for (String v : vals) {
String ds = SpringUtil.getBean(ShardingExtraConfig.class).getDsByColumnValue(v);
if (StrUtil.isNotBlank(ds)) {
dsResult.add(ds);
}
}
}
if (CollUtil.isEmpty(dsResult)) {
// 默认数据源
dsResult.add(prop.getDefaultDsName());
}
log.info("【Hint】【{}】{}=[{}]", CollUtil.join(dsResult, StrUtil.COMMA), table, CollUtil.join(vals, StrUtil.COMMA));
return dsResult;
}
use of com.chao.cloud.common.extra.sharding.annotation.ShardingProperties in project chao-cloud by chaojunzi.
the class DsPartShardingAlgorithm method addDsSet.
private void addDsSet(Set<String> dsSet, String table, String column, String val) {
if (StrUtil.isBlank(val) || !NumberUtil.isNumber(val)) {
return;
}
long number = NumberUtil.parseLong(val);
ShardingProperties prop = SpringUtil.getBean(ShardingProperties.class);
long index = number % prop.getDsNum();
String dsName = StrUtil.format("{}{}", prop.getDsPrefix(), index);
log.info("【取余】【{}%{}={}】{}.{}.{}={}", number, prop.getDsNum(), index, dsName, table, column, val);
dsSet.add(dsName);
}
use of com.chao.cloud.common.extra.sharding.annotation.ShardingProperties in project chao-cloud by chaojunzi.
the class HintShardingColumnProxy method shardingCodeThrow.
private ShardingColumnModel shardingCodeThrow(String orgCode, boolean validateOrgCode) {
ShardingProperties prop = SpringUtil.getBean(ShardingProperties.class);
ShardingColumnModel m = convert.getShardingColumnModel(orgCode);
//
String shardingCode = m.getShardingCode();
// 匹配数据源
boolean matches = true;
if (StrUtil.isBlank(shardingCode)) {
matches = false;
} else if (prop.isEnable()) {
// 判断shardingCode 是否包含符合的数据库
String ds = SpringUtil.getBean(ShardingExtraConfig.class).getDsByColumnValue(shardingCode);
if (StrUtil.isBlank(ds)) {
matches = false;
}
}
// 数据源大于1执行校验
if (prop.getDsNum() > 1) {
// 匹配失败
if (!matches) {
throw new BusinessException("未匹配到数据源");
}
if (validateOrgCode && !m.isOrgExist()) {
throw new BusinessException("无效的code");
}
}
// 设置默认值
if (StrUtil.isBlank(shardingCode)) {
shardingCode = ShardingConstant.DEFAULT_VALUE;
m.setShardingCode(shardingCode);
}
return m;
}
use of com.chao.cloud.common.extra.sharding.annotation.ShardingProperties in project chao-cloud by chaojunzi.
the class HintShardingColumnProxy method hintShardingCodeProxy.
/**
* 慎用:此方法会给ThreadLocal 存放变量<br>
* 注:请不要嵌套使用
*
* @param <T>
* @param shardingCode
* @param function
* @return
*/
public static <T> T hintShardingCodeProxy(String shardingCode, Function<String, T> function) {
ShardingProperties prop = SpringUtil.getBean(ShardingProperties.class);
if (!prop.isEnable()) {
return function.apply(shardingCode);
}
// 设置shardingCode
if (StrUtil.isNotBlank(shardingCode)) {
HintManager instance = HintManager.getInstance();
instance.setDatabaseShardingValue(shardingCode);
}
try {
return function.apply(shardingCode);
} finally {
HintManager.clear();
}
}
Aggregations