Search in sources :

Example 1 with DateStrategyEnum

use of com.chao.cloud.common.extra.mybatis.common.DateStrategyEnum in project chao-cloud by chaojunzi.

the class DateShardingAlgorithm method doSharding.

@Override
public Collection<String> doSharding(Collection<String> tableNames, ComplexKeysShardingValue<Date> complexKeysShardingValue) {
    // 获取配置文件
    ShardingProperties prop = SpringUtil.getBean(ShardingProperties.class);
    String table = complexKeysShardingValue.getLogicTableName();
    // 日期分表策略
    DateStrategyEnum dateStrategy = prop.getDateStrategy();
    // 
    List<String> tableList = null;
    // 根据表获取相关字段
    String columnName = prop.getDateTableColumnMap().get(table);
    Assert.notBlank(columnName, "table:{}, 未设置DateShardingAlgorithm分表", table);
    // 日期精确解析(= in)
    Map<String, Collection<Date>> shardingMap = complexKeysShardingValue.getColumnNameAndShardingValuesMap();
    if (shardingMap.containsKey(columnName)) {
        // 获取时间范围
        Collection<Date> dateList = shardingMap.get(columnName);
        // 生成表节点
        this.createTableNodes(prop, table, tableNames, dateStrategy, dateList);
        // 
        tableList = dateStrategy.findTables(tableNames, dateList);
    }
    // 范围日期解析
    Map<String, Range<Date>> rangeMap = complexKeysShardingValue.getColumnNameAndRangeValuesMap();
    if (rangeMap.containsKey(columnName)) {
        Range<Date> range = rangeMap.get(columnName);
        Date start = null, end = null;
        if (range.hasLowerBound()) {
            start = range.lowerEndpoint();
        }
        if (range.hasUpperBound()) {
            end = range.upperEndpoint();
        }
        tableList = dateStrategy.findTables(tableNames, start, end);
    }
    if (CollUtil.isEmpty(tableList)) {
        String tableName = CollUtil.getFirst(tableNames);
        // 空表
        tableList = CollUtil.toList(ShardingConstant.getTableNameOfNumberSuffix(tableName));
    }
    // 打印
    log.info("【日期】【{}】Table={}.{}", CollUtil.join(tableList, StrUtil.COMMA), table, columnName);
    return tableList;
}
Also used : ShardingProperties(com.chao.cloud.common.extra.sharding.annotation.ShardingProperties) DateStrategyEnum(com.chao.cloud.common.extra.mybatis.common.DateStrategyEnum) Collection(java.util.Collection) Range(com.google.common.collect.Range) Date(java.util.Date)

Aggregations

DateStrategyEnum (com.chao.cloud.common.extra.mybatis.common.DateStrategyEnum)1 ShardingProperties (com.chao.cloud.common.extra.sharding.annotation.ShardingProperties)1 Range (com.google.common.collect.Range)1 Collection (java.util.Collection)1 Date (java.util.Date)1