SpelShardRouteRule implements ShardRouteRule { @Override public Map<ShardRouteInfo, List<RangeShardValue>> groupSdValuesByRouteInfo(String scName, String tbName, RangeShardValue rangeShardValue) { Long begin = rangeShardValue.getBegin(); Long end = rangeShardValue.getEnd(); if (begin == null || end == null) { throw new IllegalArgumentException("rangeShardValue.begin and rangeShardValue.end can't be null"); } if (begin > end) { throw new IllegalArgumentException("rangeShardValue.begin can't be greater than rangeShardValue.end"); } if (rangeSizeLimit != null && end - begin + 1 > rangeSizeLimit) { throw new OutOfRangeSizeLimitException((end - begin) + " > " + rangeSizeLimit); } Map<ShardRouteInfo, List<RangeShardValue>> map = new LinkedHashMap<>(); if (scRouteRuleExpression == null && tbRouteRuleExpression == null) { List<RangeShardValue> list = new ArrayList(1); list.add(new RangeShardValue(begin, end)); ShardRouteInfo routeInfo = new ShardRouteInfo(scName, tbName); map.put(routeInfo, list); return map; } for (long l = begin; l <= end; l++) { String scName0 = parseScName(scName, l); String tbName0 = parseTbName(tbName, l); ShardRouteInfo routeInfo = new ShardRouteInfo(); routeInfo.setScName(scName0); routeInfo.setTbName(tbName0); List<RangeShardValue> rangeShardValues = map.get(routeInfo); if (rangeShardValues == null) { rangeShardValues = new ArrayList<>(); map.put(routeInfo, rangeShardValues); } rangeShardValues.add(new RangeShardValue(l, l)); } return map; } 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 test04() { SpelShardRouteRule strRule = new SpelShardRouteRule("{scName}_{format('%02d', sdValue.hashCode() % 4)}", "{tbName}_{format('%04d', sdValue.hashCode() % 16)}"); Map<ShardRouteInfo, List<RangeShardValue>> map = strRule.groupSdValuesByRouteInfo("db", "user", new RangeShardValue(0L, 31L)); Map<ShardRouteInfo, List<RangeShardValue>> r = new LinkedHashMap<>(); r.put(new ShardRouteInfo("db_00", "user_0000"), Arrays.asList(new RangeShardValue(0L, 0L), new RangeShardValue(16L, 16L))); r.put(new ShardRouteInfo("db_01", "user_0001"), Arrays.asList(new RangeShardValue(1L, 1L), new RangeShardValue(17L, 17L))); r.put(new ShardRouteInfo("db_02", "user_0002"), Arrays.asList(new RangeShardValue(2L, 2L), new RangeShardValue(18L, 18L))); r.put(new ShardRouteInfo("db_03", "user_0003"), Arrays.asList(new RangeShardValue(3L, 3L), new RangeShardValue(19L, 19L))); r.put(new ShardRouteInfo("db_00", "user_0004"), Arrays.asList(new RangeShardValue(4L, 4L), new RangeShardValue(20L, 20L))); r.put(new ShardRouteInfo("db_01", "user_0005"), Arrays.asList(new RangeShardValue(5L, 5L), new RangeShardValue(21L, 21L))); r.put(new ShardRouteInfo("db_02", "user_0006"), Arrays.asList(new RangeShardValue(6L, 6L), new RangeShardValue(22L, 22L))); r.put(new ShardRouteInfo("db_03", "user_0007"), Arrays.asList(new RangeShardValue(7L, 7L), new RangeShardValue(23L, 23L))); r.put(new ShardRouteInfo("db_00", "user_0008"), Arrays.asList(new RangeShardValue(8L, 8L), new RangeShardValue(24L, 24L))); r.put(new ShardRouteInfo("db_01", "user_0009"), Arrays.asList(new RangeShardValue(9L, 9L), new RangeShardValue(25L, 25L))); r.put(new ShardRouteInfo("db_02", "user_0010"), Arrays.asList(new RangeShardValue(10L, 10L), new RangeShardValue(26L, 26L))); r.put(new ShardRouteInfo("db_03", "user_0011"), Arrays.asList(new RangeShardValue(11L, 11L), new RangeShardValue(27L, 27L))); r.put(new ShardRouteInfo("db_00", "user_0012"), Arrays.asList(new RangeShardValue(12L, 12L), new RangeShardValue(28L, 28L))); r.put(new ShardRouteInfo("db_01", "user_0013"), Arrays.asList(new RangeShardValue(13L, 13L), new RangeShardValue(29L, 29L))); r.put(new ShardRouteInfo("db_02", "user_0014"), Arrays.asList(new RangeShardValue(14L, 14L), new RangeShardValue(30L, 30L))); r.put(new ShardRouteInfo("db_03", "user_0015"), Arrays.asList(new RangeShardValue(15L, 15L), new RangeShardValue(31L, 31L))); Assert.equals(map, r); }
@Test public void test05() { SpelShardRouteRule strRule = new SpelShardRouteRule(null, null); Map<ShardRouteInfo, List<RangeShardValue>> map = strRule.groupSdValuesByRouteInfo("db", "user", new RangeShardValue(0L, 131L)); Map<ShardRouteInfo, List<RangeShardValue>> r = new LinkedHashMap<>(); r.put(new ShardRouteInfo("db", "user"), Arrays.asList(new RangeShardValue(0L, 131L))); Assert.equals(map, r); }