IntervalShardingAlgorithm implements StandardShardingAlgorithm<Comparable<?>> { @Override public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Comparable<?>> shardingValue) { return availableTargetNames.stream() .filter(each -> each.endsWith(parseDateTime(shardingValue.getValue().toString()).format(tableSuffixPattern))) .findFirst().orElseThrow(() -> new ShardingAlgorithmException(String.format("failed to shard value %s, and availableTables %s", shardingValue, availableTargetNames))); }  @Override void init(); @Override String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Comparable<?>> shardingValue); @Override Collection<String> doSharding(final Collection<String> availableTargetNames, final RangeShardingValue<Comparable<?>> shardingValue); @Override String getType();  }
@Test public void assertPreciseDoShardingByQuarter() { List<RouteValue> shardingValues = Collections.singletonList(new ListRouteValue<>("create_time", "t_order", Arrays.asList("2020-01-01 00:00:01", "2020-01-01 00:00:02", "2020-04-15 10:59:08"))); Collection<String> actual = shardingStrategyByQuarter.doSharding(availableTablesForQuarterStrategy, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actual.size(), is(2)); assertTrue(actual.contains("t_order_202001")); assertTrue(actual.contains("t_order_202002")); }
@Test public void assertRangeDoShardingByQuarter() { Range<String> rangeValue = Range.closed("2019-10-15 10:59:08", "2020-04-08 10:59:08"); List<RouteValue> shardingValues = Collections.singletonList(new RangeRouteValue<>("create_time", "t_order", rangeValue)); Collection<String> actual = shardingStrategyByQuarter.doSharding(availableTablesForQuarterStrategy, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actual.size(), is(3)); }
@Test public void assertPreciseDoShardingByMonth() { List<RouteValue> shardingValues = Collections.singletonList(new ListRouteValue<>("create_time", "t_order", Arrays.asList("2020-01-01 00:00:01", "2020-01-01 00:00:02", "2020-04-15 10:59:08"))); Collection<String> actual = shardingStrategyByMonth.doSharding(availableTablesForMonthStrategy, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actual.size(), is(2)); assertTrue(actual.contains("t_order_202001")); assertTrue(actual.contains("t_order_202004")); }
@Test public void assertRangeDoShardingByMonth() { Range<String> rangeValue = Range.closed("2019-10-15 10:59:08", "2020-04-08 10:59:08"); List<RouteValue> shardingValues = Collections.singletonList(new RangeRouteValue<>("create_time", "t_order", rangeValue)); Collection<String> actual = shardingStrategyByMonth.doSharding(availableTablesForMonthStrategy, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actual.size(), is(7)); }
@Test public void assertLowerHalfRangeDoSharding() { Range<String> rangeValue = Range.atLeast("2018-10-15 10:59:08"); List<RouteValue> shardingValues = Collections.singletonList(new RangeRouteValue<>("create_time", "t_order", rangeValue)); Collection<String> actual = shardingStrategyByQuarter.doSharding(availableTablesForQuarterStrategy, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actual.size(), is(9)); }
@Test public void assertUpperHalfRangeDoSharding() { Range<String> rangeValue = Range.atMost("2019-09-01 00:00:00"); List<RouteValue> shardingValues = Collections.singletonList(new RangeRouteValue<>("create_time", "t_order", rangeValue)); Collection<String> actual = shardingStrategyByQuarter.doSharding(availableTablesForQuarterStrategy, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actual.size(), is(15)); }