strongduanmu commented on a change in pull request #14132: URL: https://github.com/apache/shardingsphere/pull/14132#discussion_r773566394
########## File path: shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/cosid/CosIdIntervalShardingAlgorithm.java ########## @@ -0,0 +1,195 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.sharding.algorithm.sharding.cosid; + +import com.google.common.base.Strings; +import com.google.common.collect.BoundType; +import com.google.common.collect.Range; + +import lombok.Getter; +import lombok.Setter; +import me.ahoo.cosid.sharding.IntervalStep; +import me.ahoo.cosid.util.LocalDateTimeConvert; +import me.ahoo.cosid.sharding.IntervalTimeline; +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.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; +import java.util.Collection; +import java.util.Date; +import java.util.Properties; + +/** + * Interval-based time range sharding algorithm. + */ +public final class CosIdIntervalShardingAlgorithm implements StandardShardingAlgorithm<Comparable<?>> { + + public static final String TYPE = CosIdAlgorithm.TYPE_PREFIX + "INTERVAL"; + + public static final String DATE_TIME_PATTERN_KEY = "datetime-pattern"; + + public static final String DEFAULT_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; + + public static final DateTimeFormatter DEFAULT_DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_PATTERN); + + public static final String DATE_TIME_LOWER_KEY = "datetime-lower"; + + public static final String DATE_TIME_UPPER_KEY = "datetime-upper"; + + public static final String SHARDING_SUFFIX_FORMAT_KEY = "sharding-suffix-pattern"; + + public static final String INTERVAL_UNIT_KEY = "datetime-interval-unit"; + + public static final String INTERVAL_AMOUNT_KEY = "datetime-interval-amount"; + + public static final String TIMESTAMP_SECOND_TYPE = "SECOND"; + + public static final String ZONE_ID_KEY = "zone-id"; + + public static final String TIMESTAMP_TYPE_KEY = "ts-type"; + + @Getter + @Setter + private Properties props = new Properties(); + + private volatile AlgorithmConfig algorithmConfig; + + @Override + public void init() { + String logicNamePrefix = PropertiesUtil.getRequiredValue(getProps(), CosIdAlgorithm.LOGIC_NAME_PREFIX_KEY); + LocalDateTime effectiveLower = LocalDateTime.parse(PropertiesUtil.getRequiredValue(getProps(), DATE_TIME_LOWER_KEY), DEFAULT_DATE_TIME_FORMATTER); + LocalDateTime effectiveUpper = LocalDateTime.parse(PropertiesUtil.getRequiredValue(getProps(), DATE_TIME_UPPER_KEY), DEFAULT_DATE_TIME_FORMATTER); + DateTimeFormatter suffixFormatter = DateTimeFormatter.ofPattern(PropertiesUtil.getRequiredValue(getProps(), SHARDING_SUFFIX_FORMAT_KEY)); + ChronoUnit stepUnit = ChronoUnit.valueOf(PropertiesUtil.getRequiredValue(getProps(), INTERVAL_UNIT_KEY)); + int stepAmount = Integer.parseInt(getProps().getProperty(INTERVAL_AMOUNT_KEY, "1")); + IntervalTimeline intervalTimeline = new IntervalTimeline(logicNamePrefix, Range.closed(effectiveLower, effectiveUpper), IntervalStep.of(stepUnit, stepAmount), suffixFormatter); + boolean isSecondTs = getProps().containsKey(TIMESTAMP_TYPE_KEY) + && TIMESTAMP_SECOND_TYPE.equalsIgnoreCase(getProps().getProperty(TIMESTAMP_TYPE_KEY)); + final String dateTimePattern = getProps().getProperty(DATE_TIME_PATTERN_KEY, DEFAULT_DATE_TIME_PATTERN); Review comment: @Ahoo-Wang Please remove this final modifier. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
