SpelShardRouteRule implements ShardRouteRule { @Override public String toString() { return new DDRToStringBuilder() .append("scRouteRule", scRouteRule) .append("tbRouteRule", tbRouteRule) .toString(); } private  SpelShardRouteRule();  SpelShardRouteRule(String scRouteRule, String tbRouteRule);  SpelShardRouteRule(String scRouteRule, String tbRouteRule, Integer rangeSizeLimit); void setScRouteRule(String scRouteRule); String getScRouteRule(); void setTbRouteRule(String tbRouteRule); String getTbRouteRule(); Integer getRangeSizeLimit(); void setRangeSizeLimit(Integer rangeSizeLimit); @Override String parseScName(String scName, Object sdValue); @Override String parseTbName(String tbName, Object sdValue); @Override Map<ShardRouteInfo, List<RangeShardValue>> groupSdValuesByRouteInfo(String scName, String tbName,
                                                                               RangeShardValue rangeShardValue); @Override String toString();  }
@Test public void test03() { List<String> expectedResult = new ArrayList(); expectedResult.add("db_index_01.user_0001"); expectedResult.add("db_index_02.user_0002"); expectedResult.add("db_index_03.user_0003"); expectedResult.add("db_index_00.user_0004"); expectedResult.add("db_index_01.user_0005"); expectedResult.add("db_index_02.user_0006"); expectedResult.add("db_index_03.user_0007"); expectedResult.add("db_index_00.user_0008"); expectedResult.add("db_index_01.user_0009"); expectedResult.add("db_index_02.user_0010"); expectedResult.add("db_index_03.user_0011"); expectedResult.add("db_index_00.user_0012"); expectedResult.add("db_index_01.user_0013"); expectedResult.add("db_index_02.user_0014"); expectedResult.add("db_index_03.user_0015"); expectedResult.add("db_index_00.user_0000"); List<SimpleShardRouteRuleBinding> bindings = new ArrayList<SimpleShardRouteRuleBinding>(); SpelShardRouteRule strRule = new SpelShardRouteRule("{scName}_{format('%02d', sdValue.hashCode() % 4)}", "{tbName}_{format('%04d', sdValue.hashCode() % 16)}"); SimpleShardRouteRuleBinding user = new SimpleShardRouteRuleBinding(); user.setScName("db_index"); user.setTbName("user"); user.setSdKey("name"); user.setSdValues("[1..16]"); user.setRule(strRule); bindings.add(user); SimpleShardRouter shardRouter = new SimpleShardRouter(bindings); List<ShardRouteInfo> routeInfos = shardRouter.getRouteInfos("db_index", "user"); int count = 0; for (ShardRouteInfo si : routeInfos) { Assert.equals(si.toString(), expectedResult.get(count)); count++; } }
@Test public void test02() { List<String> expectedResult = new ArrayList(); expectedResult.add("db_00.user_0000"); expectedResult.add("db_01.user_0001"); expectedResult.add("db_02.user_0002"); expectedResult.add("db_03.user_0003"); expectedResult.add("db_00.user_0004"); expectedResult.add("db_01.user_0005"); expectedResult.add("db_02.user_0006"); expectedResult.add("db_03.user_0007"); expectedResult.add("db_00.user_0008"); expectedResult.add("db_01.user_0009"); expectedResult.add("db_02.user_0010"); expectedResult.add("db_03.user_0011"); expectedResult.add("db_00.user_0012"); expectedResult.add("db_01.user_0013"); expectedResult.add("db_02.user_0014"); expectedResult.add("db_03.user_0015"); List<SimpleShardRouteRuleBinding> bindings = new ArrayList<SimpleShardRouteRuleBinding>(); SpelShardRouteRule numRule = new SpelShardRouteRule("{scName}_{format('%02d', sdValue % 4)}", "{tbName}_{format('%04d', sdValue % 16)}"); SimpleShardRouteRuleBinding user = new SimpleShardRouteRuleBinding(); user.setScName("db"); user.setTbName("user"); user.setSdKey("id"); user.setSdValues("[0..15]"); user.setRule(numRule); bindings.add(user); SimpleShardRouter shardRouter = new SimpleShardRouter(bindings); List<ShardRouteInfo> routeInfos = shardRouter.getRouteInfos("db", "user"); int count = 0; for (ShardRouteInfo si : routeInfos) { Assert.equals(si.toString(), expectedResult.get(count)); count++; } }