strongduanmu commented on a change in pull request #12625:
URL: https://github.com/apache/shardingsphere/pull/12625#discussion_r714528396
##########
File path:
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithm.java
##########
@@ -120,30 +122,37 @@ private ChronoUnit getStepUnit(final String stepUnit) {
@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) {
Review comment:
@tuichenchuxin Can we replace this logic with guava range intersection?
--
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]