InlineShardingAlgorithm implements StandardShardingAlgorithm<Comparable<?>> { @Override public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Comparable<?>> shardingValue) { Closure<?> closure = createClosure(); closure.setProperty(shardingValue.getColumnName(), shardingValue.getValue()); return closure.call().toString(); }  @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 assertDoSharding() { 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_id", "t_order", Lists.newArrayList(0, 1, 2, 3))); Collection<String> actual = shardingStrategy.doSharding(availableTargetNames, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actual.size(), is(4)); Collection<String> actualWithSimplified = shardingStrategyWithSimplified.doSharding(availableTargetNames, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actualWithSimplified.size(), is(4)); }
@Test public void assertDoShardingWithRangeRouteValue() { List<String> availableTargetNames = Lists.newArrayList("t_order_0", "t_order_1", "t_order_2", "t_order_3"); List<RouteValue> shardingValues = Lists.newArrayList(new RangeRouteValue<>("order_id", "t_order", mock(Range.class))); Collection<String> actual = shardingStrategy.doSharding(availableTargetNames, shardingValues, new ConfigurationProperties(new Properties())); assertTrue(actual.containsAll(availableTargetNames)); }
@Test public void assertDoShardingWithNonExistNodes() { List<String> availableTargetNames = Lists.newArrayList("t_order_0", "t_order_1"); List<RouteValue> shardingValues = Lists.newArrayList(new ListRouteValue<>("order_id", "t_order", Lists.newArrayList(0, 1, 2, 3))); Collection<String> actual = shardingStrategy.doSharding(availableTargetNames, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actual.size(), is(2)); Collection<String> actualWithSimplified = shardingStrategyWithSimplified.doSharding(availableTargetNames, shardingValues, new ConfigurationProperties(new Properties())); assertThat(actualWithSimplified.size(), is(2)); }