tiandihai commented on issue #29225:
URL: 
https://github.com/apache/shardingsphere/issues/29225#issuecomment-1828925413

   `import lombok.extern.slf4j.Slf4j;
   import org.apache.commons.lang3.BooleanUtils;
   import 
org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
   import 
org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
   import 
org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
   
   import java.text.SimpleDateFormat;
   import java.util.*;
   
   /**
    * @Description:
    * @author wanglei10
    * @date 2023-08-10 13:55
    */
   @Slf4j
   public class TableShardingAlgorithm implements 
StandardShardingAlgorithm<String> {
   
       public static final InheritableThreadLocal<Boolean> NO_SHARDING_HOLDER = 
new InheritableThreadLocal<>();
   
       /**
        * 分片算法的属性配置
        */
       private final Properties properties = new Properties();
   
       /**
        * 根据精确分片值进行分片操作,返回分片后的目标表名
        *
        * @param availableTargetNames           所有的目标表名集合
        * @param preciseShardingValue 精确分片值对象,包含了逻辑表名、分片键和分片值
        * @return 分片后的目标表名
        */
       @Override
       public String doSharding(Collection<String> availableTargetNames, 
PreciseShardingValue<String> preciseShardingValue) {
           return routeTables(availableTargetNames, preciseShardingValue);
       }
   
       private String routeTables(Collection<String> availableTargetNames, 
PreciseShardingValue<String> preciseShardingValue) {
           if (preciseShardingValue == null) {
               throw new RuntimeException("shardingValue不能为空");
           }
   
           SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
           String date = simpleDateFormat.format(new Date());
           String actualTableName;
           if (BooleanUtils.isTrue(NO_SHARDING_HOLDER.get())) {
               actualTableName = preciseShardingValue.getLogicTableName();
           }else {
               actualTableName = 
preciseShardingValue.getLogicTableName().concat("_").concat(date);
           }
   
           //把真实表名放进来
           availableTargetNames.add(actualTableName);
           return actualTableName;
       }
   
       /**
        * 根据范围分片值进行分片操作,返回分片后的目标表名集合
        *
        * @param availableTargetNames         所有的目标表名集合
        * @param rangeShardingValue 范围分片值对象,包含了逻辑表名、分片键和分片范围
        * @return 分片后的目标表名集合
        */
       @Override
       public Collection<String> doSharding(Collection<String> 
availableTargetNames, RangeShardingValue<String> rangeShardingValue) {
           return null;
       }
   
       /**
        * 获取分片算法的类型
        *
        * @return 分片算法的类型
        */
       @Override
       public String getType() {
           return "TB_SHARDING_BY_DATE";
       }
   
       /**
        * 获取分片算法的属性配置
        *
        * @return 分片算法的属性配置
        */
       public Properties getProps() {
           return properties;
       }
   
   
       /**
        * 初始化分片算法和属性配置
        *
        * @param properties 分片算法的属性配置
        */
       @Override
       public void init(Properties properties) {
           log.info("初始化按日期分表算法 -> properties:{}", properties.toString());
       }
   }`


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to