menghaoranss commented on a change in pull request #14132: URL: https://github.com/apache/shardingsphere/pull/14132#discussion_r771280515
########## 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. Review comment: Unnecessary comments for override methods, please remove all comments ########## 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. Review comment: Unnecessary comment ########## 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. Review comment: same as above ########## File path: shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/cosid/CosIdModShardingAlgorithmTest.java ########## @@ -0,0 +1,185 @@ +/* + * 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.Range; +import me.ahoo.cosid.sharding.ExactCollection; +import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue; +import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.Collection; +import java.util.Properties; + +import static org.junit.Assert.assertEquals; + +public class CosIdModShardingAlgorithmTest { Review comment: class should be final ########## File path: shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/cosid/CosIdIntervalShardingAlgorithmTest.java ########## @@ -0,0 +1,416 @@ +/* + * 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.Range; +import me.ahoo.cosid.sharding.ExactCollection; +import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue; +import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.time.LocalDateTime; + +import java.time.ZoneOffset; +import java.util.Collection; +import java.util.Date; +import java.util.Properties; +import java.util.function.Function; + +import static org.junit.Assert.assertEquals; + +public class CosIdIntervalShardingAlgorithmTest { + + static final ZoneOffset ZONE_OFFSET_SHANGHAI; + + static final LocalDateTime LOWER_DATE_TIME; + + static final LocalDateTime UPPER_DATE_TIME; + + static final String LOGIC_NAME; + + static final String LOGIC_NAME_PREFIX; + + static final String COLUMN_NAME; + + static final String SUFFIX_FORMATTER_STRING; + + static final ExactCollection<String> ALL_NODES; + + static { + ZONE_OFFSET_SHANGHAI = ZoneOffset.of("+8"); + LOWER_DATE_TIME = LocalDateTime.of(2021, 1, 1, 0, 0); + UPPER_DATE_TIME = LOWER_DATE_TIME.plusYears(1); + LOGIC_NAME = "table"; + LOGIC_NAME_PREFIX = LOGIC_NAME + "_"; + COLUMN_NAME = "create_time"; + SUFFIX_FORMATTER_STRING = "yyyyMM"; + ALL_NODES = new ExactCollection<>("table_202101", "table_202102", "table_202103", "table_202104", + "table_202105", "table_202106", "table_202107", "table_202108", "table_202109", + "table_202110", "table_202111", "table_202112", "table_202201"); + } + + static CosIdIntervalShardingAlgorithm createShardingAlg() { + Properties properties = new Properties(); + properties.setProperty(CosIdAlgorithm.LOGIC_NAME_PREFIX_KEY, LOGIC_NAME_PREFIX); + properties.setProperty(CosIdIntervalShardingAlgorithm.DATE_TIME_LOWER_KEY, LOWER_DATE_TIME.format(CosIdIntervalShardingAlgorithm.DEFAULT_DATE_TIME_FORMATTER)); + properties.setProperty(CosIdIntervalShardingAlgorithm.DATE_TIME_UPPER_KEY, UPPER_DATE_TIME.format(CosIdIntervalShardingAlgorithm.DEFAULT_DATE_TIME_FORMATTER)); + properties.setProperty(CosIdIntervalShardingAlgorithm.SHARDING_SUFFIX_FORMAT_KEY, SUFFIX_FORMATTER_STRING); + properties.setProperty(CosIdIntervalShardingAlgorithm.INTERVAL_UNIT_KEY, "MONTHS"); + properties.setProperty(CosIdIntervalShardingAlgorithm.INTERVAL_AMOUNT_KEY, "1"); + CosIdIntervalShardingAlgorithm shardingAlgorithm = new CosIdIntervalShardingAlgorithm(); + shardingAlgorithm.setProps(properties); + shardingAlgorithm.init(); + return shardingAlgorithm; + } + + static Iterable<Object[]> preciseArgsProvider(final Function<LocalDateTime, ? extends Comparable<?>> datetimeConvert) { + return Arguments.ofArrayElement( + Arguments.of(datetimeConvert.apply(LOWER_DATE_TIME), "table_202101"), + Arguments.of(datetimeConvert.apply(LocalDateTime.of(2021, 2, 14, 22, 0)), "table_202102"), + Arguments.of(datetimeConvert.apply(LocalDateTime.of(2021, 10, 1, 0, 0)), "table_202110"), + Arguments.of(datetimeConvert.apply(UPPER_DATE_TIME), "table_202201") + ); + } + + static Iterable<Object[]> preciseArgsProviderAsLocalDateTime() { + return preciseArgsProvider(ldt -> ldt); + } + + static Iterable<Object[]> preciseArgsProviderAsString() { + return preciseArgsProvider(ldt -> ldt.format(CosIdIntervalShardingAlgorithm.DEFAULT_DATE_TIME_FORMATTER)); + } + + static Iterable<Object[]> preciseArgsProviderAsDate() { + return preciseArgsProvider(ldt -> new Date(ldt.toInstant(ZONE_OFFSET_SHANGHAI).toEpochMilli())); + } + + static Iterable<Object[]> preciseArgsProviderAsTimestamp() { + return preciseArgsProvider(ldt -> ldt.toInstant(ZONE_OFFSET_SHANGHAI).toEpochMilli()); + } + + static Iterable<Object[]> rangeArgsProvider(final Function<LocalDateTime, ? extends Comparable<?>> datetimeConvert) { + return Arguments.ofArrayElement( + Arguments.of(Range.all(), ALL_NODES), + Arguments.of(Range.closed(datetimeConvert.apply(LOWER_DATE_TIME), datetimeConvert.apply(UPPER_DATE_TIME)), ALL_NODES), + Arguments.of(Range.closed(datetimeConvert.apply(LocalDateTime.of(2021, 1, 1, 0, 0)), + datetimeConvert.apply(LocalDateTime.of(2021, 2, 1, 0, 0))), + new ExactCollection<>("table_202101", "table_202102")), + Arguments.of(Range.closed(datetimeConvert.apply(LOWER_DATE_TIME.minusMonths(1)), datetimeConvert.apply(UPPER_DATE_TIME.plusMonths(1))), ALL_NODES), + Arguments.of(Range.closed(datetimeConvert.apply(LocalDateTime.of(2021, 12, 1, 0, 0)), + datetimeConvert.apply(LocalDateTime.of(2022, 2, 1, 0, 0))), + new ExactCollection<>("table_202112", "table_202201")), + Arguments.of(Range.closedOpen(datetimeConvert.apply(LOWER_DATE_TIME), datetimeConvert.apply(UPPER_DATE_TIME)), + new ExactCollection<>("table_202101", "table_202102", "table_202103", "table_202104", "table_202105", "table_202106", + "table_202107", "table_202108", "table_202109", "table_202110", "table_202111", "table_202112")), + Arguments.of(Range.openClosed(datetimeConvert.apply(LOWER_DATE_TIME), datetimeConvert.apply(UPPER_DATE_TIME)), ALL_NODES), + + Arguments.of(Range.greaterThan(datetimeConvert.apply(LOWER_DATE_TIME)), ALL_NODES), + Arguments.of(Range.atLeast(datetimeConvert.apply(LOWER_DATE_TIME)), ALL_NODES), + Arguments.of(Range.greaterThan(datetimeConvert.apply(UPPER_DATE_TIME)), new ExactCollection<>("table_202201")), + Arguments.of(Range.atLeast(datetimeConvert.apply(UPPER_DATE_TIME)), new ExactCollection<>("table_202201")), + Arguments.of(Range.greaterThan(datetimeConvert.apply(LocalDateTime.of(2021, 12, 5, 0, 0))), + new ExactCollection<>("table_202112", "table_202201")), + Arguments.of(Range.atLeast(datetimeConvert.apply(LocalDateTime.of(2021, 12, 5, 0, 0))), + new ExactCollection<>("table_202112", "table_202201")), + + Arguments.of(Range.lessThan(datetimeConvert.apply(LOWER_DATE_TIME)), ExactCollection.empty()), + Arguments.of(Range.atMost(datetimeConvert.apply(LOWER_DATE_TIME)), new ExactCollection<>("table_202101")), + Arguments.of(Range.lessThan(datetimeConvert.apply(UPPER_DATE_TIME)), new ExactCollection<>("table_202101", + "table_202102", "table_202103", "table_202104", "table_202105", "table_202106", "table_202107", + "table_202108", "table_202109", "table_202110", "table_202111", "table_202112")), + Arguments.of(Range.atMost(datetimeConvert.apply(UPPER_DATE_TIME)), ALL_NODES), + Arguments.of(Range.lessThan(datetimeConvert.apply(LocalDateTime.of(2021, 5, 5, 0, 0))), + new ExactCollection<>("table_202101", "table_202102", "table_202103", "table_202104", "table_202105")), + Arguments.of(Range.atMost(datetimeConvert.apply(LocalDateTime.of(2021, 5, 5, 0, 0))), + new ExactCollection<>("table_202101", "table_202102", "table_202103", "table_202104", "table_202105")) + ); + } + + static Iterable<Object[]> rangeArgsProviderAsLocalDateTime() { + return rangeArgsProvider(ldt -> ldt); + } + + static Iterable<Object[]> rangeArgsProviderAsString() { + return rangeArgsProvider(ldt -> ldt.format(CosIdIntervalShardingAlgorithm.DEFAULT_DATE_TIME_FORMATTER)); + } + + static Iterable<Object[]> rangeArgsProviderAsDate() { + return rangeArgsProvider(ldt -> new Date(ldt.toInstant(ZONE_OFFSET_SHANGHAI).toEpochMilli())); + } + + static Iterable<Object[]> rangeArgsProviderAsTimestamp() { + return rangeArgsProvider(ldt -> ldt.toInstant(ZONE_OFFSET_SHANGHAI).toEpochMilli()); + } + + @RunWith(Parameterized.class) + public static class LocalDateTimePreciseValueDoShardingTest { Review comment: could these test methods be split into test classes? ########## 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"; Review comment: Please confirm whether all variables need `public` ########## File path: shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/keygen/CosIdKeyGenerateAlgorithmTest.java ########## @@ -0,0 +1,67 @@ +/* + * 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.CosIdException; +import me.ahoo.cosid.provider.DefaultIdGeneratorProvider; +import me.ahoo.cosid.segment.DefaultSegmentId; +import me.ahoo.cosid.segment.IdSegmentDistributor; +import org.apache.shardingsphere.sharding.algorithm.sharding.cosid.CosIdAlgorithm; +import org.junit.Test; + +import java.util.Properties; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +public class CosIdKeyGenerateAlgorithmTest { Review comment: class should be final. ########## 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 { Review comment: I want to know how it works in the cluster mode. ########## 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> { + + public static final String TYPE = CosIdAlgorithm.TYPE_PREFIX + "MOD"; + + public static final String MODULO_KEY = "mod"; + + private Properties props = new Properties(); + + private volatile ModCycle<T> modCycle; + + /** + * Get type. + * + * @return type + */ + @Override + public String getType() { + return TYPE; + } + + /** + * Get properties. + * + * @return properties + */ + @Override + public Properties getProps() { + return props; + } + + /** + * Set properties. + * + * @param props properties + */ + @Override + public void setProps(final Properties props) { + this.props = props; + } + + /** + * Sharding. + * + * @param availableTargetNames available data sources or table names + * @param shardingValue sharding value Review comment: same as above. ########## 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; + } + Review comment: please remove unnecessary empty line -- 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]
