AutoIntervalShardingAlgorithm implements StandardShardingAlgorithm<Comparable<?>>, ShardingAutoTableAlgorithm { @Override public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Comparable<?>> shardingValue) { for (String each : availableTargetNames) { if (each.endsWith(String.valueOf(doSharding(parseDate(shardingValue.getValue()))))) { return each; } } return null; }  @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 assertPreciseDoSharding() { List<String> availableTargetNames = Arrays.asList("t_order_0", "t_order_1", "t_order_2", "t_order_3"); 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"))); Collection<String> actual = shardingStrategy.doSharding(availableTargetNames, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actual.size(), is(1)); assertTrue(actual.contains("t_order_1")); }
@Test public void assertPreciseDoShardingWithFirstPartition() { List<String> availableTargetNames = Arrays.asList("t_order_0", "t_order_1", "t_order_2", "t_order_3"); List<RouteValue> shardingValues = Collections.singletonList(new ListRouteValue<>("create_time", "t_order", Arrays.asList("2019-12-01 00:00:01", "2020-01-01 00:00:02"))); Collection<String> actual = shardingStrategy.doSharding(availableTargetNames, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actual.size(), is(2)); assertTrue(actual.contains("t_order_0")); assertTrue(actual.contains("t_order_1")); }
@Test public void assertPreciseDoShardingBeyondTheLastOne() { List<String> availableTargetNames = Arrays.asList("t_order_0", "t_order_1", "t_order_2", "t_order_3", "t_order_4", "t_order_5"); List<RouteValue> shardingValues = Collections.singletonList(new ListRouteValue<>("create_time", "t_order", Collections.singletonList("2021-01-01 00:00:02"))); Collection<String> actual = shardingStrategy.doSharding(availableTargetNames, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actual.size(), is(1)); assertTrue(actual.contains("t_order_5")); }
@Test public void assertRangeDoShardingWithAllRange() { List<String> availableTargetNames = Arrays.asList("t_order_0", "t_order_1", "t_order_2", "t_order_3", "t_order_4"); Range<String> rangeValue = Range.closed("2019-01-01 00:00:00", "2020-01-01 00:00:15"); List<RouteValue> shardingValues = Collections.singletonList(new RangeRouteValue<>("create_time", "t_order", rangeValue)); Collection<String> actual = shardingStrategy.doSharding(availableTargetNames, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actual.size(), is(5)); }
@Test public void assertRangeDoShardingWithPartRange() { List<String> availableTargetNames = Arrays.asList("t_order_0", "t_order_1", "t_order_2", "t_order_3"); Range<String> rangeValue = Range.closed("2020-01-01 00:00:04", "2020-01-01 00:00:10"); List<RouteValue> shardingValues = Collections.singletonList(new RangeRouteValue<>("create_time", "t_order", rangeValue)); Collection<String> actual = shardingStrategy.doSharding(availableTargetNames, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actual.size(), is(3)); assertTrue(actual.contains("t_order_1")); assertTrue(actual.contains("t_order_2")); assertTrue(actual.contains("t_order_3")); }
@Test public void assertRangeDoShardingWithoutLowerBound() { List<String> availableTargetNames = Arrays.asList("t_order_0", "t_order_1", "t_order_2", "t_order_3"); Range<String> rangeValue = Range.lessThan("2020-01-01 00:00:11"); List<RouteValue> shardingValues = Collections.singletonList(new RangeRouteValue<>("create_time", "t_order", rangeValue)); Collection<String> actual = shardingStrategy.doSharding(availableTargetNames, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actual.size(), is(4)); assertTrue(actual.contains("t_order_0")); assertTrue(actual.contains("t_order_1")); assertTrue(actual.contains("t_order_2")); assertTrue(actual.contains("t_order_3")); }
@Test public void assertRangeDoShardingWithoutUpperBound() { List<String> availableTargetNames = Arrays.asList("t_order_0", "t_order_1", "t_order_2", "t_order_3", "t_order_4", "t_order_5"); Range<String> rangeValue = Range.greaterThan("2020-01-01 00:00:09"); List<RouteValue> shardingValues = Collections.singletonList(new RangeRouteValue<>("create_time", "t_order", rangeValue)); Collection<String> actual = shardingStrategy.doSharding(availableTargetNames, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actual.size(), is(3)); assertTrue(actual.contains("t_order_3")); assertTrue(actual.contains("t_order_4")); assertTrue(actual.contains("t_order_5")); }