HashModShardingAlgorithm 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(hashShardingValue(shardingValue.getValue()) % shardingCount))) { 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 int getAutoTablesAmount(); @Override String getType();  }
@Test public void assertPreciseDoSharding() { List<String> availableTargetNames = Lists.newArrayList("t_order_0", "t_order_1", "t_order_2", "t_order_3"); List<RouteValue> shardingValues = Lists.newArrayList(new ListRouteValue<>("order_type", "t_order", Lists.newArrayList("a", "b", "c"))); 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 assertRangeDoSharding() { List<String> availableTargetNames = Lists.newArrayList("t_order_0", "t_order_1", "t_order_2", "t_order_3"); Range<String> rangeValue = Range.closed("a", "f"); List<RouteValue> shardingValues = Lists.newArrayList(new RangeRouteValue<>("order_type", "t_order", rangeValue)); Collection<String> actual = shardingStrategy.doSharding(availableTargetNames, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actual.size(), is(4)); }