strongduanmu commented on a change in pull request #14132:
URL: https://github.com/apache/shardingsphere/pull/14132#discussion_r771315951



##########
File path: 
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/cosid/PropertiesUtil.java
##########
@@ -0,0 +1,31 @@
+/*
+ * 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.Preconditions;
+
+import java.util.Properties;
+
+/**
+ * Properties tool class.
+ */
+final class PropertiesUtil {

Review comment:
       @Ahoo-Wang Please add public for PropertiesUtil class and 
getRequiredValue method.

##########
File path: 
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/keygen/CosIdKeyGenerateAlgorithm.java
##########
@@ -0,0 +1,86 @@
+/*
+ * 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.keygen;
+
+import me.ahoo.cosid.CosId;
+import me.ahoo.cosid.provider.IdGeneratorProvider;
+import me.ahoo.cosid.provider.LazyIdGenerator;
+import 
org.apache.shardingsphere.sharding.algorithm.sharding.cosid.CosIdAlgorithm;
+import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
+
+import java.util.Properties;
+
+/**
+ * CosId key generate algorithm.
+ */
+public final class CosIdKeyGenerateAlgorithm implements KeyGenerateAlgorithm {
+
+    public static final String TYPE = CosId.COSID.toUpperCase();
+
+    private Properties props = new Properties();
+
+    private volatile LazyIdGenerator cosIdProvider;
+
+    /**
+     * Get properties.
+     *
+     * @return properties
+     */
+    @Override
+    public Properties getProps() {
+        return props;

Review comment:
       @Ahoo-Wang Maybe use @getter @setter  annotation is better.

##########
File path: 
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/cosid/CosIdModShardingAlgorithm.java
##########
@@ -0,0 +1,103 @@
+/*
+ * 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 me.ahoo.cosid.sharding.ModCycle;
+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.util.Collection;
+import java.util.Properties;
+
+/**
+ * Modular sharding algorithm.
+ */
+public class CosIdModShardingAlgorithm<T extends Number & Comparable<T>> 
implements StandardShardingAlgorithm<T> {

Review comment:
       @Ahoo-Wang Please add final for CosIdModShardingAlgorithm.

##########
File path: 
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/cosid/CosIdIntervalShardingAlgorithm.java
##########
@@ -0,0 +1,229 @@
+/*
+ * 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 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";
+
+    /**
+     * type of timestamp.
+     */
+    public static final String TIMESTAMP_TYPE_KEY = "ts-type";
+
+    private Properties props = new Properties();
+
+    private volatile AlgorithmConfig algorithmConfig;
+
+    @Override
+    public Properties getProps() {
+        return props;
+    }
+
+    @Override
+    public void setProps(final Properties props) {
+        this.props = props;
+    }
+
+    /**
+     * Initialize algorithm.
+     */
+    @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);
+        DateTimeFormatter dateTimeFormatter = 
DateTimeFormatter.ofPattern(dateTimePattern);
+        ZoneId zoneId = ZoneId.systemDefault();
+        if (getProps().containsKey(ZONE_ID_KEY)) {
+            zoneId = ZoneId.of(PropertiesUtil.getRequiredValue(getProps(), 
ZONE_ID_KEY));
+        }
+        algorithmConfig = new AlgorithmConfig(isSecondTs, dateTimeFormatter, 
zoneId, intervalTimeline);
+    }
+
+    /**
+     * Sharding.
+     *
+     * @param availableTargetNames available data sources or table names
+     * @param shardingValue        sharding value
+     * @return sharding result for data source or table name
+     */
+    @Override
+    public String doSharding(final Collection<String> availableTargetNames, 
final PreciseShardingValue<Comparable<?>> shardingValue) {
+        LocalDateTime shardingTime = 
convertShardingValue(shardingValue.getValue());
+        return algorithmConfig.getIntervalTimeline().sharding(shardingTime);
+    }
+
+    /**
+     * Sharding.
+     *
+     * @param availableTargetNames available data sources or table names
+     * @param shardingValue        sharding value
+     * @return sharding results for data sources or table names
+     */
+    @Override
+    public Collection<String> doSharding(final Collection<String> 
availableTargetNames, final RangeShardingValue<Comparable<?>> shardingValue) {
+        Range<LocalDateTime> shardingRangeTime = 
convertRangeShardingValue(shardingValue.getValueRange());
+        return 
algorithmConfig.getIntervalTimeline().sharding(shardingRangeTime);
+    }
+
+    private LocalDateTime convertShardingValue(final Comparable<?> 
shardingValue) {
+        if (shardingValue instanceof LocalDateTime) {
+            return (LocalDateTime) shardingValue;
+        }
+
+        if (shardingValue instanceof Date) {
+            return LocalDateTimeConvert.fromDate((Date) shardingValue, 
algorithmConfig.getZoneId());
+        }
+
+        if (shardingValue instanceof Long) {
+            if (algorithmConfig.isSecondTs()) {
+                return LocalDateTimeConvert.fromTimestampSecond((Long) 
shardingValue, algorithmConfig.getZoneId());
+            }
+            return LocalDateTimeConvert.fromTimestamp((Long) shardingValue, 
algorithmConfig.getZoneId());
+        }
+
+        if (shardingValue instanceof String) {
+            return LocalDateTimeConvert.fromString((String) shardingValue, 
algorithmConfig.getDateTimeFormatter());
+        }
+        throw new IllegalArgumentException(Strings.lenientFormat("The current 
shard type:[%s] is not supported!", shardingValue.getClass()));
+    }
+
+    private Range<LocalDateTime> convertRangeShardingValue(final 
Range<Comparable<?>> shardingValue) {
+        if (Range.all().equals(shardingValue)) {
+            return Range.all();
+        }
+        Comparable<?> endpointValue = shardingValue.hasLowerBound() ? 
shardingValue.lowerEndpoint() : shardingValue.upperEndpoint();
+        if (endpointValue instanceof LocalDateTime) {
+            @SuppressWarnings("unchecked")
+            Range<LocalDateTime> targetRange = (Range<LocalDateTime>) (Object) 
shardingValue;
+            return targetRange;
+        }
+
+        if (shardingValue.hasLowerBound() && shardingValue.hasUpperBound()) {
+            LocalDateTime lower = 
convertShardingValue(shardingValue.lowerEndpoint());
+            LocalDateTime upper = 
convertShardingValue(shardingValue.upperEndpoint());
+            return Range.range(lower, shardingValue.lowerBoundType(), upper, 
shardingValue.upperBoundType());
+        }
+
+        if (shardingValue.hasLowerBound()) {
+            LocalDateTime lower = 
convertShardingValue(shardingValue.lowerEndpoint());
+            if (BoundType.OPEN.equals(shardingValue.lowerBoundType())) {
+                return Range.greaterThan(lower);
+            }
+            return Range.atLeast(lower);
+        }
+
+        LocalDateTime upper = 
convertShardingValue(shardingValue.upperEndpoint());
+        if (BoundType.OPEN.equals(shardingValue.upperBoundType())) {
+            return Range.lessThan(upper);
+        }
+        return Range.atMost(upper);
+    }
+
+    /**
+     * Get type.
+     *
+     * @return type
+     */
+    @Override
+    public String getType() {
+        return TYPE;
+    }
+
+    static class AlgorithmConfig {

Review comment:
       @Ahoo-Wang add private for AlgorithmConfig?




-- 
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]


Reply via email to