This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new bdba15f  Fix IntervalShardingAlgorithm. (#12625)
bdba15f is described below

commit bdba15f7454820c1ffe1ca39183b020b98f91ba8
Author: tuichenchuxin <[email protected]>
AuthorDate: Thu Sep 23 16:42:38 2021 +0800

    Fix IntervalShardingAlgorithm. (#12625)
    
    * Fix IntervalShardingAlgorithm.
    
    * Fix IntervalShardingAlgorithm.
    
    * Fix IntervalShardingAlgorithm.
    
    * Fix IntervalShardingAlgorithm.
---
 .../datetime/IntervalShardingAlgorithm.java        | 35 +++++++++++++---------
 .../datetime/IntervalShardingAlgorithmTest.java    | 13 ++++----
 2 files changed, 29 insertions(+), 19 deletions(-)

diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithm.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithm.java
index 4e799c8..85b5bdc 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithm.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithm.java
@@ -18,6 +18,8 @@
 package org.apache.shardingsphere.sharding.algorithm.sharding.datetime;
 
 import com.google.common.base.Preconditions;
+import com.google.common.collect.BoundType;
+import com.google.common.collect.Range;
 import lombok.Getter;
 import lombok.Setter;
 import 
org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
@@ -120,30 +122,35 @@ public final class IntervalShardingAlgorithm implements 
StandardShardingAlgorith
     
     @Override
     public String doSharding(final Collection<String> availableTargetNames, 
final PreciseShardingValue<Comparable<?>> shardingValue) {
-        String tableNameSuffix = 
parseDateTime(shardingValue.getValue().toString()).format(tableSuffixPattern);
-        return availableTargetNames.stream()
-                .filter(each -> each.endsWith(tableNameSuffix))
-                .findFirst().orElse(null);
+        return doSharding(availableTargetNames, 
Range.singleton(shardingValue.getValue())).stream().findFirst().orElse(null);
     }
     
     @Override
     public Collection<String> doSharding(final Collection<String> 
availableTargetNames, final RangeShardingValue<Comparable<?>> shardingValue) {
-        boolean hasStartTime = shardingValue.getValueRange().hasLowerBound();
-        boolean hasEndTime = shardingValue.getValueRange().hasUpperBound();
-        if (!hasStartTime && !hasEndTime) {
-            return availableTargetNames;
-        }
-        LocalDateTime startTime = hasStartTime ? 
parseDateTime(shardingValue.getValueRange().lowerEndpoint().toString()) : 
dateTimeLower;
-        LocalDateTime endTime = hasEndTime ? 
parseDateTime(shardingValue.getValueRange().upperEndpoint().toString()) : 
dateTimeUpper;
-        LocalDateTime calculateTime = startTime;
+        return doSharding(availableTargetNames, shardingValue.getValueRange());
+    }
+    
+    private Collection<String> doSharding(final Collection<String> 
availableTargetNames, final Range<Comparable<?>> range) {
         Set<String> result = new HashSet<>();
-        while (!calculateTime.isAfter(endTime) || 
calculateTime.format(tableSuffixPattern).equals(endTime.format(tableSuffixPattern)))
 {
-            result.addAll(getMatchedTables(calculateTime, 
availableTargetNames));
+        LocalDateTime calculateTime = dateTimeLower;
+        while (!calculateTime.isAfter(dateTimeUpper)) {
+            if (hasIntersection(Range.closedOpen(calculateTime, 
calculateTime.plus(stepAmount, stepUnit)), range)) {
+                result.addAll(getMatchedTables(calculateTime, 
availableTargetNames));
+            }
             calculateTime = calculateTime.plus(stepAmount, stepUnit);
         }
         return result;
     }
     
+    private boolean hasIntersection(final Range<LocalDateTime> calculateRange, 
final Range<Comparable<?>> range) {
+        LocalDateTime lower = range.hasLowerBound() ? 
parseDateTime(range.lowerEndpoint().toString()) : dateTimeLower;
+        LocalDateTime upper = range.hasUpperBound() ? 
parseDateTime(range.upperEndpoint().toString()) : dateTimeUpper;
+        BoundType lowerBoundType = range.hasLowerBound() ? 
range.lowerBoundType() : BoundType.CLOSED;
+        BoundType upperBoundType = range.hasUpperBound() ? 
range.upperBoundType() : BoundType.CLOSED;
+        Range<LocalDateTime> dateTimeRange = Range.range(lower, 
lowerBoundType, upper, upperBoundType);
+        return calculateRange.isConnected(dateTimeRange) && 
!calculateRange.intersection(dateTimeRange).isEmpty();
+    }
+    
     private LocalDateTime parseDateTime(final String value) {
         return LocalDateTime.parse(value.substring(0, dateTimePatternLength), 
dateTimeFormatter);
     }
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithmTest.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithmTest.java
index d61b683..79f4233 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithmTest.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithmTest.java
@@ -91,10 +91,11 @@ public final class IntervalShardingAlgorithmTest {
         shardingAlgorithmByDay.getProps().setProperty("datetime-lower", 
"2021-06-01 00:00:00");
         shardingAlgorithmByDay.getProps().setProperty("datetime-upper", 
"2021-07-31 00:00:00");
         
shardingAlgorithmByDay.getProps().setProperty("sharding-suffix-pattern", 
"yyyyMMdd");
-        
shardingAlgorithmByDay.getProps().setProperty("datetime-interval-amount", "1");
+        int stepAmount = 2;
+        
shardingAlgorithmByDay.getProps().setProperty("datetime-interval-amount", 
Integer.toString(stepAmount));
         shardingAlgorithmByDay.init();
         for (int j = 6; j <= 7; j++) {
-            for (int i = 1; j == 6 ? i <= 30 : i <= 31; i++) {
+            for (int i = 1; j == 6 ? i <= 30 : i <= 31; i = i + stepAmount) {
                 
availableTablesForDayDataSources.add(String.format("t_order_%04d%02d%02d", 
2021, j, i));
             }
         }
@@ -103,6 +104,7 @@ public final class IntervalShardingAlgorithmTest {
     @Test
     public void assertPreciseDoShardingByQuarter() {
         
assertThat(shardingAlgorithmByQuarter.doSharding(availableTablesForQuarterDataSources,
 new PreciseShardingValue<>("t_order", "create_time", "2020-01-01 00:00:01")), 
is("t_order_202001"));
+        
assertThat(shardingAlgorithmByQuarter.doSharding(availableTablesForQuarterDataSources,
 new PreciseShardingValue<>("t_order", "create_time", "2020-02-01 00:00:01")), 
is("t_order_202001"));
     }
     
     @Test
@@ -143,26 +145,27 @@ public final class IntervalShardingAlgorithmTest {
     public void assertLowerHalfRangeDoShardingByDay() {
         Collection<String> actual = shardingAlgorithmByDay.doSharding(
                 availableTablesForDayDataSources, new 
RangeShardingValue<>("t_order", "create_time", Range.atLeast("2021-01-01 
00:00:00")));
-        assertThat(actual.size(), is(61));
+        assertThat(actual.size(), is(31));
     }
     
     @Test
     public void assertUpperHalfRangeDoShardingByDay() {
         Collection<String> actual = shardingAlgorithmByDay.doSharding(
                 availableTablesForDayDataSources, new 
RangeShardingValue<>("t_order", "create_time", Range.atMost("2021-07-31 
01:00:00")));
-        assertThat(actual.size(), is(61));
+        assertThat(actual.size(), is(31));
     }
     
     @Test
     public void assertPreciseDoShardingByDay() {
         
assertThat(shardingAlgorithmByDay.doSharding(availableTablesForDayDataSources, 
new PreciseShardingValue<>("t_order", "create_time", "2021-07-01 00:00:01")), 
is("t_order_20210701"));
+        
assertThat(shardingAlgorithmByDay.doSharding(availableTablesForDayDataSources, 
new PreciseShardingValue<>("t_order", "create_time", "2021-07-02 00:00:01")), 
is("t_order_20210701"));
     }
     
     @Test
     public void assertRangeDoShardingByDay() {
         Collection<String> actual = shardingAlgorithmByDay.doSharding(
                 availableTablesForDayDataSources, new 
RangeShardingValue<>("t_order", "create_time", Range.closed("2021-06-15 
00:00:00", "2021-07-31 01:00:00")));
-        assertThat(actual.size(), is(47));
+        assertThat(actual.size(), is(24));
     }
     
     @Test

Reply via email to