diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/CustomDateTimeShardingAlgorithm.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/CustomDateTimeShardingAlgorithm.java index b936f705d5c102691d51e01fc211a78f954fe384..b75f625394b6d2e26942058e23dc37a44b6cea1b 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/CustomDateTimeShardingAlgorithm.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/CustomDateTimeShardingAlgorithm.java @@ -84,12 +84,12 @@ public final class CustomDateTimeShardingAlgorithm implements StandardShardingAl @Getter @Setter private Properties properties = new Properties(); - + @Override public String getType() { return "CUSTOM_DATE_TIME"; } - + @Override public void init() { Preconditions.checkNotNull(properties.getProperty(DATE_TIME_FORMAT)); diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/DatetimeShardingAlgorithm.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/DatetimeShardingAlgorithm.java index 97157e8bad9c96eacebfdfbdad20960ee12a2e3d..5098021a4706155b10aaa94c26e97361f56cfb57 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/DatetimeShardingAlgorithm.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/DatetimeShardingAlgorithm.java @@ -48,7 +48,7 @@ public final class DatetimeShardingAlgorithm implements StandardShardingAlgorith private static final String PARTITION_SECONDS = "partition.seconds"; private static final String DATETIME_LOWER = "datetime.lower"; - + private static final String DATETIME_UPPER = "datetime.upper"; private static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; @@ -58,10 +58,10 @@ public final class DatetimeShardingAlgorithm implements StandardShardingAlgorith @Getter @Setter private Properties properties = new Properties(); - + @Getter private int autoTablesAmount; - + @Override public void init() { Preconditions.checkNotNull(properties.get(PARTITION_SECONDS), "Sharding partition volume cannot be null."); @@ -98,21 +98,21 @@ public final class DatetimeShardingAlgorithm implements StandardShardingAlgorith } return result; } - + private int doSharding(final long shardingValue) { DecimalFormat decimalFormat = new DecimalFormat("0.00"); String position = decimalFormat.format((float) shardingValue / getPartitionValue()); return Math.min(Math.max(0, (int) Math.ceil(Float.parseFloat(position))), autoTablesAmount - 1); } - + private int getFirstPartition(final Range> valueRange) { return valueRange.hasLowerBound() ? doSharding(parseDate(valueRange.lowerEndpoint())) : 0; } - + private int getLastPartition(final Range> valueRange) { return valueRange.hasUpperBound() ? doSharding(parseDate(valueRange.upperEndpoint())) : autoTablesAmount - 1; } - + private boolean checkDatetimePattern(final String datetime) { try { DATE_FORMAT.parse(datetime); diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/HashShardingAlgorithm.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/HashShardingAlgorithm.java index e8a87c44d922809042ddafdef967a6dc416f4560..07d97bf7ccbe3827cab27261b544cdfe694f1ae8 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/HashShardingAlgorithm.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/HashShardingAlgorithm.java @@ -42,7 +42,7 @@ public final class HashShardingAlgorithm implements StandardShardingAlgorithm closure; private Properties properties = new Properties(); @@ -72,7 +72,7 @@ public final class InlineShardingAlgorithm implements StandardShardingAlgorithm< public void setProperties(final Properties properties) { this.properties = properties; } - + @Override public void init() { Preconditions.checkNotNull(properties.get(ALGORITHM_EXPRESSION), "Inline sharding algorithm expression cannot be null."); diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/range/AbstractRangeShardingAlgorithm.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/range/AbstractRangeShardingAlgorithm.java index d125f39e388c2eee10c8cfbfa83ce945987a5be4..5c04ca15cb728f2283bb473a13534032921eb0b1 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/range/AbstractRangeShardingAlgorithm.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/range/AbstractRangeShardingAlgorithm.java @@ -35,26 +35,26 @@ import java.util.Properties; * Abstract range sharding algorithm. */ public abstract class AbstractRangeShardingAlgorithm implements StandardShardingAlgorithm, ShardingAutoTableAlgorithm { - + private Map> partitionRangeMap; - + @Getter @Setter private Properties properties = new Properties(); - + @Override public final void init() { partitionRangeMap = createPartitionRangeMap(properties); } - + abstract Map> createPartitionRangeMap(Properties properties); - + @Override public final String doSharding(final Collection availableTargetNames, final PreciseShardingValue shardingValue) { return availableTargetNames.stream().filter(each -> each.endsWith(getPartition(partitionRangeMap, shardingValue.getValue()) + "")) .findFirst().orElseThrow(UnsupportedOperationException::new); } - + @Override public final Collection doSharding(final Collection availableTargetNames, final RangeShardingValue shardingValue) { Collection result = new LinkedHashSet<>(availableTargetNames.size()); @@ -69,15 +69,15 @@ public abstract class AbstractRangeShardingAlgorithm implements StandardSharding } return result; } - + private int getFirstPartition(final Range valueRange) { return valueRange.hasLowerBound() ? getPartition(partitionRangeMap, valueRange.lowerEndpoint()) : 0; } - + private int getLastPartition(final Range valueRange) { return valueRange.hasUpperBound() ? getPartition(partitionRangeMap, valueRange.upperEndpoint()) : partitionRangeMap.size() - 1; } - + private Integer getPartition(final Map> partitionRangeMap, final Long value) { for (Entry> entry : partitionRangeMap.entrySet()) { if (entry.getValue().contains(value)) { @@ -86,7 +86,7 @@ public abstract class AbstractRangeShardingAlgorithm implements StandardSharding } throw new UnsupportedOperationException(); } - + @Override public final int getAutoTablesAmount() { return partitionRangeMap.size(); diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/range/CustomRangeShardingAlgorithm.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/range/CustomRangeShardingAlgorithm.java index 18af984568a00a1e2e8af50a9b87f3555725efa1..b9f1a8898099ea0562568626694424acf9b18712 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/range/CustomRangeShardingAlgorithm.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/range/CustomRangeShardingAlgorithm.java @@ -51,7 +51,7 @@ public final class CustomRangeShardingAlgorithm extends AbstractRangeShardingAlg public String getType() { return "CUSTOM_RANGE"; } - + @Override public Map> createPartitionRangeMap(final Properties properties) { Preconditions.checkNotNull(properties.get(PARTITION_RANGES), "Custom range sharding algorithm partition ranges cannot be null."); diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/range/StandardRangeShardingAlgorithm.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/range/StandardRangeShardingAlgorithm.java index 89cab338cfbe932a6fc29463e447dc93c091a09d..a4b3ffbd73d5a5e207b835a955bdcbea1692f7ed 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/range/StandardRangeShardingAlgorithm.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/range/StandardRangeShardingAlgorithm.java @@ -52,7 +52,7 @@ public final class StandardRangeShardingAlgorithm extends AbstractRangeShardingA public String getType() { return "STANDARD_RANGE"; } - + @Override public Map> createPartitionRangeMap(final Properties properties) { Preconditions.checkNotNull(properties.get(RANGE_LOWER), "Standard range sharding algorithm partition lower cannot be null."); diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/CustomDateTimeShardingAlgorithmTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/CustomDateTimeShardingAlgorithmTest.java index e504dd2bc588acb4af1279f41239030355817e2e..e4799ba453957c316b8b749b9e71bbca108e6cb5 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/CustomDateTimeShardingAlgorithmTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/CustomDateTimeShardingAlgorithmTest.java @@ -58,7 +58,7 @@ public class CustomDateTimeShardingAlgorithmTest { initShardStrategyByMonth(); initShardStrategyByQuarter(); } - + @Test public void assertPreciseDoShardingByQuarter() { List shardingValues = Lists.newArrayList(new ListRouteValue<>("create_time", "t_order", @@ -68,7 +68,7 @@ public class CustomDateTimeShardingAlgorithmTest { assertTrue(actual.contains("t_order_202001")); assertTrue(actual.contains("t_order_202002")); } - + @Test public void assertRangeDoShardingByQuarter() { Range rangeValue = Range.closed("2019-10-15 10:59:08", "2020-04-08 10:59:08"); @@ -76,7 +76,7 @@ public class CustomDateTimeShardingAlgorithmTest { Collection actual = shardingStrategyByQuarter.doSharding(availableTablesForQuarterStrategy, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actual.size(), is(3)); } - + @Test public void assertPreciseDoShardingByMonth() { List shardingValues = Lists.newArrayList(new ListRouteValue<>("create_time", "t_order", @@ -86,7 +86,7 @@ public class CustomDateTimeShardingAlgorithmTest { assertTrue(actual.contains("t_order_202001")); assertTrue(actual.contains("t_order_202004")); } - + @Test public void assertRangeDoShardingByMonth() { Range rangeValue = Range.closed("2019-10-15 10:59:08", "2020-04-08 10:59:08"); @@ -94,7 +94,7 @@ public class CustomDateTimeShardingAlgorithmTest { Collection actual = shardingStrategyByMonth.doSharding(availableTablesForMonthStrategy, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actual.size(), is(7)); } - + @Test public void assertLowerHalfRangeDoSharding() { Range rangeValue = Range.atLeast("2018-10-15 10:59:08"); @@ -102,7 +102,7 @@ public class CustomDateTimeShardingAlgorithmTest { Collection actual = shardingStrategyByQuarter.doSharding(availableTablesForQuarterStrategy, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actual.size(), is(9)); } - + @Test public void assertUpperHalfRangeDoSharding() { Range rangeValue = Range.atMost("2019-09-01 00:00:00"); @@ -110,7 +110,7 @@ public class CustomDateTimeShardingAlgorithmTest { Collection actual = shardingStrategyByQuarter.doSharding(availableTablesForQuarterStrategy, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actual.size(), is(15)); } - + @Test public void assertFormat() { String inputFormat = "yyyy-MM-dd HH:mm:ss.SSS"; @@ -123,7 +123,7 @@ public class CustomDateTimeShardingAlgorithmTest { assertEquals("202004", tableNameShardedByQuarter); assertEquals("202010", tableNameShardedByMonth); } - + private void initShardStrategyByQuarter() { CustomDateTimeShardingAlgorithm shardingAlgorithm = new CustomDateTimeShardingAlgorithm(); shardingAlgorithm.getProperties().setProperty("datetime.format", "yyyy-MM-dd HH:mm:ss"); @@ -141,7 +141,7 @@ public class CustomDateTimeShardingAlgorithmTest { } } } - + private void initShardStrategyByMonth() { CustomDateTimeShardingAlgorithm shardingAlgorithm = new CustomDateTimeShardingAlgorithm(); shardingAlgorithm.getProperties().setProperty("datetime.format", "yyyy-MM-dd HH:mm:ss"); diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/DatetimeShardingAlgorithmTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/DatetimeShardingAlgorithmTest.java index 73925f3f40aaa02990583ca6d393b88421240005..b5b1161efee9e36c5c2f08b7db7620d7e1258f96 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/DatetimeShardingAlgorithmTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/DatetimeShardingAlgorithmTest.java @@ -60,7 +60,7 @@ public final class DatetimeShardingAlgorithmTest { assertThat(actual.size(), is(1)); assertTrue(actual.contains("t_order_1")); } - + @Test public void assertPreciseDoShardingWithFirstPartition() { List availableTargetNames = Lists.newArrayList("t_order_0", "t_order_1", "t_order_2", "t_order_3"); @@ -71,7 +71,7 @@ public final class DatetimeShardingAlgorithmTest { assertTrue(actual.contains("t_order_0")); assertTrue(actual.contains("t_order_1")); } - + @Test public void assertPreciseDoShardingBeyondTheLastOne() { List availableTargetNames = Lists.newArrayList("t_order_0", "t_order_1", "t_order_2", "t_order_3", "t_order_4", "t_order_5"); @@ -102,7 +102,7 @@ public final class DatetimeShardingAlgorithmTest { assertTrue(actual.contains("t_order_2")); assertTrue(actual.contains("t_order_3")); } - + @Test public void assertRangeDoShardingWithoutLowerBound() { List availableTargetNames = Lists.newArrayList("t_order_0", "t_order_1", "t_order_2", "t_order_3"); @@ -115,7 +115,7 @@ public final class DatetimeShardingAlgorithmTest { assertTrue(actual.contains("t_order_2")); assertTrue(actual.contains("t_order_3")); } - + @Test public void assertRangeDoShardingWithoutUpperBound() { List availableTargetNames = Lists.newArrayList("t_order_0", "t_order_1", "t_order_2", "t_order_3", "t_order_4", "t_order_5"); @@ -127,7 +127,7 @@ public final class DatetimeShardingAlgorithmTest { assertTrue(actual.contains("t_order_4")); assertTrue(actual.contains("t_order_5")); } - + @Test public void assertGetAutoTablesAmount() { DatetimeShardingAlgorithm shardingAlgorithm = new DatetimeShardingAlgorithm(); diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/inline/InlineShardingAlgorithmTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/inline/InlineShardingAlgorithmTest.java index cf81761cde06af6bbe1232ddcd27c0cfd0d4bd6c..2b5b7dec50b1f8ff778be4416b440e4a0951d73e 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/inline/InlineShardingAlgorithmTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/inline/InlineShardingAlgorithmTest.java @@ -38,7 +38,7 @@ public final class InlineShardingAlgorithmTest { private StandardShardingStrategy shardingStrategy; private StandardShardingStrategy shardingStrategyWithSimplified; - + @Before public void setUp() { InlineShardingAlgorithm shardingAlgorithm = new InlineShardingAlgorithm(); @@ -52,7 +52,7 @@ public final class InlineShardingAlgorithmTest { StandardShardingStrategyConfiguration shardingStrategyConfigWithSimplified = new StandardShardingStrategyConfiguration("order_id", shardingAlgorithmWithSimplified); shardingStrategyWithSimplified = new StandardShardingStrategy(shardingStrategyConfigWithSimplified); } - + @Test public void assertDoSharding() { List availableTargetNames = Lists.newArrayList("t_order_0", "t_order_1", "t_order_2", "t_order_3"); @@ -62,7 +62,7 @@ public final class InlineShardingAlgorithmTest { Collection actualWithSimplified = shardingStrategyWithSimplified.doSharding(availableTargetNames, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actualWithSimplified.size(), is(4)); } - + @Test public void assertDoShardingWithNonExistNodes() { List availableTargetNames = Lists.newArrayList("t_order_0", "t_order_1"); @@ -72,7 +72,7 @@ public final class InlineShardingAlgorithmTest { Collection actualWithSimplified = shardingStrategyWithSimplified.doSharding(availableTargetNames, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actualWithSimplified.size(), is(2)); } - + @Test public void assertGetShardingColumns() { assertThat(shardingStrategy.getShardingColumns().size(), is(1)); diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/range/StandardRangeShardingAlgorithmTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/range/StandardRangeShardingAlgorithmTest.java index a62d682b8a8735a38eceb516fe7084e07fdec97f..a914b5665cddd1f30ac3291e49b6bf493c9e64fb 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/range/StandardRangeShardingAlgorithmTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/strategy/algorithm/sharding/range/StandardRangeShardingAlgorithmTest.java @@ -62,7 +62,7 @@ public final class StandardRangeShardingAlgorithmTest { assertTrue(actual.contains("t_order_2")); assertTrue(actual.contains("t_order_5")); } - + @Test public void assertRangeDoShardingWithoutLowerBound() { List availableTargetNames = Lists.newArrayList("t_order_0", "t_order_1", "t_order_2", "t_order_3", "t_order_4", "t_order_5"); @@ -72,7 +72,7 @@ public final class StandardRangeShardingAlgorithmTest { assertTrue(actual.contains("t_order_0")); assertTrue(actual.contains("t_order_1")); } - + @Test public void assertRangeDoShardingWithoutUpperBound() { List availableTargetNames = Lists.newArrayList("t_order_0", "t_order_1", "t_order_2", "t_order_3", "t_order_4", "t_order_5");