You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/04/14 04:16:57 UTC

[shardingsphere] branch master updated: Refactor IntervalShardingAlgorithm (#16808)

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

panjuan 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 ce35ceabe16 Refactor IntervalShardingAlgorithm (#16808)
ce35ceabe16 is described below

commit ce35ceabe16dca50ec136327c5a06617a9fad0aa
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Thu Apr 14 12:16:52 2022 +0800

    Refactor IntervalShardingAlgorithm (#16808)
---
 .../datetime/IntervalShardingAlgorithm.java        | 26 ++++++++++------------
 .../datetime/IntervalShardingAlgorithmTest.java    |  2 +-
 2 files changed, 13 insertions(+), 15 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 45c721bc245..5dc8db44b07 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
@@ -145,28 +145,26 @@ public final class IntervalShardingAlgorithm implements StandardShardingAlgorith
     }
     
     private boolean hasIntersection(final Range<LocalDateTime> calculateRange, final Range<Comparable<?>> range) {
-        LocalDateTime lower = range.hasLowerBound() ? getLocalDateTime(range.lowerEndpoint()) : dateTimeLower;
-        LocalDateTime upper = range.hasUpperBound() ? getLocalDateTime(range.upperEndpoint()) : dateTimeUpper;
+        LocalDateTime lower = range.hasLowerBound() ? parseLocalDateTime(range.lowerEndpoint()) : dateTimeLower;
+        LocalDateTime upper = range.hasUpperBound() ? parseLocalDateTime(range.upperEndpoint()) : 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 getLocalDateTime(final Comparable<?> endpoint) {
-        String timeString;
-        if (endpoint instanceof LocalDateTime) {
-            timeString = ((LocalDateTime) endpoint).format(dateTimeFormatter);
-        } else if (endpoint instanceof Date) {
-            timeString = new SimpleDateFormat(getDateTimePattern()).format(endpoint);
-        } else {
-            timeString = endpoint.toString();
-        }
-        return parseDateTime(timeString);
+    private LocalDateTime parseLocalDateTime(final Comparable<?> endpoint) {
+        return LocalDateTime.parse(getDateTimeText(endpoint).substring(0, dateTimePatternLength), dateTimeFormatter);
     }
     
-    private LocalDateTime parseDateTime(final String value) {
-        return LocalDateTime.parse(value.substring(0, dateTimePatternLength), dateTimeFormatter);
+    private String getDateTimeText(final Comparable<?> endpoint) {
+        if (endpoint instanceof LocalDateTime) {
+            return ((LocalDateTime) endpoint).format(dateTimeFormatter);
+        }
+        if (endpoint instanceof Date) {
+            return new SimpleDateFormat(getDateTimePattern()).format(endpoint);
+        }
+        return endpoint.toString();
     }
     
     private Collection<String> getMatchedTables(final LocalDateTime dateTime, final Collection<String> availableTargetNames) {
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 a841c12c976..196c757d5c3 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
@@ -111,7 +111,7 @@ public final class IntervalShardingAlgorithmTest {
             }
         }
     }
-
+    
     private void initShardStrategyByDayWithMillisecond() {
         shardingAlgorithmByDayWithMillisecond = new IntervalShardingAlgorithm();
         shardingAlgorithmByDayWithMillisecond.getProps().setProperty("datetime-pattern", "yyyy-MM-dd HH:mm:ss.SSS");