strongduanmu commented on a change in pull request #15067:
URL: https://github.com/apache/shardingsphere/pull/15067#discussion_r792328133
##########
File path:
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/keygen/CosIdKeyGenerateAlgorithm.java
##########
@@ -20,57 +20,47 @@
import me.ahoo.cosid.CosId;
import me.ahoo.cosid.provider.IdGeneratorProvider;
import me.ahoo.cosid.provider.LazyIdGenerator;
-import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereInstanceRequiredAlgorithm;
-import org.apache.shardingsphere.infra.instance.InstanceContext;
import
org.apache.shardingsphere.sharding.algorithm.sharding.cosid.CosIdAlgorithm;
import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
-import java.util.Optional;
import java.util.Properties;
/**
* CosId key generate algorithm.
*/
-public final class CosIdKeyGenerateAlgorithm implements KeyGenerateAlgorithm,
ShardingSphereInstanceRequiredAlgorithm {
-
+public final class CosIdKeyGenerateAlgorithm implements KeyGenerateAlgorithm {
+
public static final String TYPE = CosId.COSID.toUpperCase();
-
Review comment:
@Ahoo-Wang Please keep indents consistent with the previous one.
##########
File path:
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/cosid/AbstractIntervalShardingAlgorithm.java
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.collect.BoundType;
+import com.google.common.collect.Range;
+import lombok.Getter;
+import lombok.Setter;
+import me.ahoo.cosid.sharding.IntervalStep;
+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.Properties;
+
+/**
+ * Interval-based time range sharding algorithm.
+ */
+public abstract class AbstractIntervalShardingAlgorithm<T extends
Comparable<?>> implements StandardShardingAlgorithm<T> {
+
+ 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 ZONE_ID_KEY = "zone-id";
+
+ private volatile IntervalTimeline intervalTimeline;
+
+ private ZoneId zoneId = ZoneId.systemDefault();
+
+ @Getter
+ @Setter
+ private Properties props = new Properties();
+
+ /**
+ * get zone id.
+ *
+ * @return zone id
+ */
+ protected ZoneId getZoneId() {
+ return zoneId;
+ }
+
+ @Override
+ public void init() {
+ if (getProps().containsKey(ZONE_ID_KEY)) {
+ zoneId = ZoneId.of(getRequiredValue(ZONE_ID_KEY));
+ }
+ String logicNamePrefix =
getRequiredValue(CosIdAlgorithm.LOGIC_NAME_PREFIX_KEY);
+ LocalDateTime effectiveLower =
LocalDateTime.parse(getRequiredValue(DATE_TIME_LOWER_KEY),
DEFAULT_DATE_TIME_FORMATTER);
+ LocalDateTime effectiveUpper =
LocalDateTime.parse(getRequiredValue(DATE_TIME_UPPER_KEY),
DEFAULT_DATE_TIME_FORMATTER);
+ DateTimeFormatter suffixFormatter =
DateTimeFormatter.ofPattern(getRequiredValue(SHARDING_SUFFIX_FORMAT_KEY));
+ ChronoUnit stepUnit =
ChronoUnit.valueOf(getRequiredValue(INTERVAL_UNIT_KEY));
+ int stepAmount =
Integer.parseInt(getProps().getProperty(INTERVAL_AMOUNT_KEY, "1"));
+ intervalTimeline = new IntervalTimeline(logicNamePrefix,
Range.closed(effectiveLower, effectiveUpper), IntervalStep.of(stepUnit,
stepAmount), suffixFormatter);
+ }
+
+ private String getRequiredValue(final String key) {
+ return PropertiesUtil.getRequiredValue(getProps(), key);
+ }
+
+ @Override
+ public String doSharding(final Collection<String> availableTargetNames,
final PreciseShardingValue<T> shardingValue) {
+ LocalDateTime shardingTime =
convertShardingValue(shardingValue.getValue());
+ return this.intervalTimeline.sharding(shardingTime);
+ }
+
+ @Override
+ public Collection<String> doSharding(final Collection<String>
availableTargetNames, final RangeShardingValue<T> shardingValue) {
+ Range<LocalDateTime> shardingRangeTime =
convertRangeShardingValue(shardingValue.getValueRange());
+ return this.intervalTimeline.sharding(shardingRangeTime);
+ }
+
+ /**
+ * convert sharding value to {@link LocalDateTime}.
+ *
+ * @param shardingValue sharding value
+ * @return The {@link LocalDateTime} represented by the sharding value
+ */
+ protected abstract LocalDateTime convertShardingValue(T shardingValue);
+
+ private Range<LocalDateTime> convertRangeShardingValue(final Range<T>
shardingValue) {
+ if (Range.all().equals(shardingValue)) {
+ return Range.all();
+ }
+ Object endpointValue = shardingValue.hasLowerBound() ?
shardingValue.lowerEndpoint() : shardingValue.upperEndpoint();
+ if (endpointValue instanceof LocalDateTime) {
+ @SuppressWarnings("unchecked")
+ Range<LocalDateTime> targetRange = (Range<LocalDateTime>)
shardingValue;
+ return targetRange;
+ }
+
Review comment:
Please remove useless blank line.
##########
File path: pom.xml
##########
@@ -57,7 +57,7 @@
<guava.version>30.0-jre</guava.version>
<gson.version>2.8.6</gson.version>
<slf4j.version>1.7.7</slf4j.version>
- <cosid.version>1.7.6</cosid.version>
+ <cosid.version>1.8.4</cosid.version>
Review comment:
@Ahoo-Wang Please update `cosid-core` version in LICENSE file.
##########
File path:
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/keygen/CosIdKeyGenerateAlgorithm.java
##########
@@ -20,57 +20,47 @@
import me.ahoo.cosid.CosId;
import me.ahoo.cosid.provider.IdGeneratorProvider;
import me.ahoo.cosid.provider.LazyIdGenerator;
-import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereInstanceRequiredAlgorithm;
-import org.apache.shardingsphere.infra.instance.InstanceContext;
import
org.apache.shardingsphere.sharding.algorithm.sharding.cosid.CosIdAlgorithm;
import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
-import java.util.Optional;
import java.util.Properties;
/**
* CosId key generate algorithm.
*/
-public final class CosIdKeyGenerateAlgorithm implements KeyGenerateAlgorithm,
ShardingSphereInstanceRequiredAlgorithm {
Review comment:
@Ahoo-Wang Why remove ShardingSphereInstanceRequiredAlgorithm interface?
##########
File path:
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithm.java
##########
@@ -0,0 +1,90 @@
+/*
+ * 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 lombok.Getter;
+import lombok.Setter;
+import me.ahoo.cosid.converter.Radix62IdConverter;
+import me.ahoo.cosid.snowflake.ClockSyncSnowflakeId;
+import me.ahoo.cosid.snowflake.MillisecondSnowflakeId;
+import me.ahoo.cosid.snowflake.SnowflakeId;
+import me.ahoo.cosid.snowflake.StringSnowflakeId;
+import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereInstanceRequiredAlgorithm;
+import org.apache.shardingsphere.infra.instance.InstanceContext;
+import
org.apache.shardingsphere.sharding.algorithm.sharding.cosid.CosIdAlgorithm;
+import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
+
+import java.util.Properties;
+
+/**
+ * CosId-Snowflake key generate algorithm.
+ */
+public final class CosIdSnowflakeKeyGenerateAlgorithm implements
KeyGenerateAlgorithm, ShardingSphereInstanceRequiredAlgorithm {
+
+ public static final String TYPE = CosIdAlgorithm.TYPE_PREFIX + "SNOWFLAKE";
+
+ public static final long DEFAULT_EPOCH =
SnowflakeKeyGenerateAlgorithm.EPOCH;
Review comment:
Can we change these public field to private?
##########
File path:
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/cosid/AbstractIntervalShardingAlgorithm.java
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.collect.BoundType;
+import com.google.common.collect.Range;
+import lombok.Getter;
+import lombok.Setter;
+import me.ahoo.cosid.sharding.IntervalStep;
+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.Properties;
+
+/**
+ * Interval-based time range sharding algorithm.
+ */
+public abstract class AbstractIntervalShardingAlgorithm<T extends
Comparable<?>> implements StandardShardingAlgorithm<T> {
+
+ 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 ZONE_ID_KEY = "zone-id";
+
+ private volatile IntervalTimeline intervalTimeline;
+
+ private ZoneId zoneId = ZoneId.systemDefault();
+
+ @Getter
+ @Setter
+ private Properties props = new Properties();
+
+ /**
+ * get zone id.
+ *
+ * @return zone id
+ */
+ protected ZoneId getZoneId() {
+ return zoneId;
+ }
+
+ @Override
+ public void init() {
+ if (getProps().containsKey(ZONE_ID_KEY)) {
+ zoneId = ZoneId.of(getRequiredValue(ZONE_ID_KEY));
+ }
+ String logicNamePrefix =
getRequiredValue(CosIdAlgorithm.LOGIC_NAME_PREFIX_KEY);
+ LocalDateTime effectiveLower =
LocalDateTime.parse(getRequiredValue(DATE_TIME_LOWER_KEY),
DEFAULT_DATE_TIME_FORMATTER);
+ LocalDateTime effectiveUpper =
LocalDateTime.parse(getRequiredValue(DATE_TIME_UPPER_KEY),
DEFAULT_DATE_TIME_FORMATTER);
+ DateTimeFormatter suffixFormatter =
DateTimeFormatter.ofPattern(getRequiredValue(SHARDING_SUFFIX_FORMAT_KEY));
+ ChronoUnit stepUnit =
ChronoUnit.valueOf(getRequiredValue(INTERVAL_UNIT_KEY));
+ int stepAmount =
Integer.parseInt(getProps().getProperty(INTERVAL_AMOUNT_KEY, "1"));
+ intervalTimeline = new IntervalTimeline(logicNamePrefix,
Range.closed(effectiveLower, effectiveUpper), IntervalStep.of(stepUnit,
stepAmount), suffixFormatter);
+ }
+
+ private String getRequiredValue(final String key) {
+ return PropertiesUtil.getRequiredValue(getProps(), key);
+ }
+
+ @Override
+ public String doSharding(final Collection<String> availableTargetNames,
final PreciseShardingValue<T> shardingValue) {
+ LocalDateTime shardingTime =
convertShardingValue(shardingValue.getValue());
+ return this.intervalTimeline.sharding(shardingTime);
+ }
+
+ @Override
+ public Collection<String> doSharding(final Collection<String>
availableTargetNames, final RangeShardingValue<T> shardingValue) {
+ Range<LocalDateTime> shardingRangeTime =
convertRangeShardingValue(shardingValue.getValueRange());
+ return this.intervalTimeline.sharding(shardingRangeTime);
+ }
+
+ /**
+ * convert sharding value to {@link LocalDateTime}.
+ *
+ * @param shardingValue sharding value
+ * @return The {@link LocalDateTime} represented by the sharding value
+ */
+ protected abstract LocalDateTime convertShardingValue(T shardingValue);
+
+ private Range<LocalDateTime> convertRangeShardingValue(final Range<T>
shardingValue) {
+ if (Range.all().equals(shardingValue)) {
+ return Range.all();
+ }
+ Object endpointValue = shardingValue.hasLowerBound() ?
shardingValue.lowerEndpoint() : shardingValue.upperEndpoint();
+ if (endpointValue instanceof LocalDateTime) {
+ @SuppressWarnings("unchecked")
Review comment:
@Ahoo-Wang Move this annotation to method?
##########
File path:
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithm.java
##########
@@ -0,0 +1,90 @@
+/*
+ * 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 lombok.Getter;
+import lombok.Setter;
+import me.ahoo.cosid.converter.Radix62IdConverter;
+import me.ahoo.cosid.snowflake.ClockSyncSnowflakeId;
+import me.ahoo.cosid.snowflake.MillisecondSnowflakeId;
+import me.ahoo.cosid.snowflake.SnowflakeId;
+import me.ahoo.cosid.snowflake.StringSnowflakeId;
+import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereInstanceRequiredAlgorithm;
+import org.apache.shardingsphere.infra.instance.InstanceContext;
+import
org.apache.shardingsphere.sharding.algorithm.sharding.cosid.CosIdAlgorithm;
+import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
+
+import java.util.Properties;
+
+/**
+ * CosId-Snowflake key generate algorithm.
+ */
+public final class CosIdSnowflakeKeyGenerateAlgorithm implements
KeyGenerateAlgorithm, ShardingSphereInstanceRequiredAlgorithm {
+
+ public static final String TYPE = CosIdAlgorithm.TYPE_PREFIX + "SNOWFLAKE";
+
+ public static final long DEFAULT_EPOCH =
SnowflakeKeyGenerateAlgorithm.EPOCH;
+
+ public static final String AS_STRING_KEY = "as-string";
+
+ public static final String EPOCH_KEY = "epoch";
+
+ private volatile InstanceContext instanceContext;
+
+ private volatile SnowflakeId snowflakeId;
+
+ private volatile boolean asString;
+
+ @Getter
+ @Setter
+ private Properties props = new Properties();
+
+ @Override
+ public Comparable<?> generateKey() {
+ if (asString) {
+ return snowflakeId.generateAsString();
+ }
+ return snowflakeId.generate();
+ }
+
+ @Override
+ public void init() {
+ String asStringStr = getProps().getProperty(AS_STRING_KEY,
Boolean.FALSE.toString());
+ asString = Boolean.parseBoolean(asStringStr);
+ long workerId = 0;
+ if (instanceContext != null) {
Review comment:
@Ahoo-Wang Please put null on the left condition.
##########
File path:
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithm.java
##########
@@ -0,0 +1,90 @@
+/*
+ * 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 lombok.Getter;
+import lombok.Setter;
+import me.ahoo.cosid.converter.Radix62IdConverter;
+import me.ahoo.cosid.snowflake.ClockSyncSnowflakeId;
+import me.ahoo.cosid.snowflake.MillisecondSnowflakeId;
+import me.ahoo.cosid.snowflake.SnowflakeId;
+import me.ahoo.cosid.snowflake.StringSnowflakeId;
+import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereInstanceRequiredAlgorithm;
+import org.apache.shardingsphere.infra.instance.InstanceContext;
+import
org.apache.shardingsphere.sharding.algorithm.sharding.cosid.CosIdAlgorithm;
+import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
+
+import java.util.Properties;
+
+/**
+ * CosId-Snowflake key generate algorithm.
Review comment:
`CosId snowflake key generate algorithm.` may be better.
--
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]