DivideShardRouteRule 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"); } Map<ShardRouteInfo, List<RangeShardValue>> map = new LinkedHashMap<>(); if (scSdValueDividend == null && tbSdValueDividend == null) { ShardRouteInfo routeInfo = new ShardRouteInfo(scName, tbName); List<RangeShardValue> list = new ArrayList(1); list.add(new RangeShardValue(begin, end)); map.put(routeInfo, list); return map; } else if (scSdValueDividend != null && tbSdValueDividend != null) { int scBegin = (int) (begin / scSdValueDividend); int scEnd = (int) (end / scSdValueDividend); int tbBegin = (int) (begin / tbSdValueDividend); int tbEnd = (int) (end / tbSdValueDividend); for (int i = scBegin; i <= scEnd; i++) { String scName0 = scName; if (i != 0 || !useOrignalNameIfZero) { scName0 = scName + '_' + i; } for (int j = tbBegin; j <= tbEnd; j++) { String tbName0 = tbName; if (j != 0 || !useOrignalNameIfZero) { tbName0 = tbName + '_' + j; } ShardRouteInfo routeInfo = new ShardRouteInfo(scName0, tbName0); List<RangeShardValue> list = map.get(routeInfo); if (list == null) { list = new ArrayList<>(); map.put(routeInfo, list); } RangeShardValue rangeShardValue1 = new RangeShardValue(j * tbSdValueDividend, (j + 1) * tbSdValueDividend - 1); adjustBorder(rangeShardValue1, rangeShardValue); list.add(rangeShardValue1); } } return map; } else if (scSdValueDividend == null && tbSdValueDividend != null) { int tbBegin = (int) (begin / tbSdValueDividend); int tbEnd = (int) (end / tbSdValueDividend); for (int j = tbBegin; j <= tbEnd; j++) { String tbName0 = tbName; if (j != 0 || !useOrignalNameIfZero) { tbName0 = tbName + '_' + j; } ShardRouteInfo routeInfo = new ShardRouteInfo(scName, tbName0); List<RangeShardValue> list = map.get(routeInfo); if (list == null) { list = new ArrayList<>(); map.put(routeInfo, list); } RangeShardValue rangeShardValue1 = new RangeShardValue(j * tbSdValueDividend, (j + 1) * tbSdValueDividend - 1); adjustBorder(rangeShardValue1, rangeShardValue); list.add(rangeShardValue1); } return map; } else { int scBegin = (int) (begin / scSdValueDividend); int scEnd = (int) (end / scSdValueDividend); for (int j = scBegin; j <= scEnd; j++) { String scName0 = scName; if (j != 0 || !useOrignalNameIfZero) { scName0 = scName + '_' + j; } ShardRouteInfo routeInfo = new ShardRouteInfo(scName0, tbName); List<RangeShardValue> list = map.get(routeInfo); if (list == null) { list = new ArrayList<>(); map.put(routeInfo, list); } RangeShardValue rangeShardValue1 = new RangeShardValue(j * scSdValueDividend, (j + 1) * scSdValueDividend - 1); adjustBorder(rangeShardValue1, rangeShardValue); list.add(rangeShardValue1); } return map; } } private  DivideShardRouteRule();  DivideShardRouteRule(Long scSdValueDividend, Long tbSdValueDividend);  DivideShardRouteRule(Long scSdValueDividend, Long tbSdValueDividend, boolean useOrignalNameIfZero); Long getScSdValueDividend(); Long getTbSdValueDividend(); boolean isUseOrignalNameIfZero(); @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);  }
@Test public void test03() { DivideShardRouteRule rule = new DivideShardRouteRule(8L, 4L); Map<ShardRouteInfo, List<RangeShardValue>> map = rule.groupSdValuesByRouteInfo("member", "user", new RangeShardValue(2l, 7l)); Map<ShardRouteInfo, List<RangeShardValue>> expectedResult = new LinkedHashMap<>(); expectedResult.put(new ShardRouteInfo("member_0", "user_0"), Arrays.asList(new RangeShardValue(2l, 3l))); expectedResult.put(new ShardRouteInfo("member_0", "user_1"), Arrays.asList(new RangeShardValue(4l, 7l))); Assert.equals(map, expectedResult); rule = new DivideShardRouteRule(null, 4L); map = rule.groupSdValuesByRouteInfo("member", "user", new RangeShardValue(2l, 7l)); expectedResult = new LinkedHashMap<>(); expectedResult.put(new ShardRouteInfo("member", "user_0"), Arrays.asList(new RangeShardValue(2l, 3l))); expectedResult.put(new ShardRouteInfo("member", "user_1"), Arrays.asList(new RangeShardValue(4l, 7l))); Assert.equals(map, expectedResult); rule = new DivideShardRouteRule(8l, null); map = rule.groupSdValuesByRouteInfo("member", "user", new RangeShardValue(2l, 7l)); expectedResult = new LinkedHashMap<>(); expectedResult.put(new ShardRouteInfo("member_0", "user"), Arrays.asList(new RangeShardValue(2l, 7l))); Assert.equals(map, expectedResult); rule = new DivideShardRouteRule(null, null); map = rule.groupSdValuesByRouteInfo("member", "user", new RangeShardValue(2l, 7l)); expectedResult = new LinkedHashMap<>(); expectedResult.put(new ShardRouteInfo("member", "user"), Arrays.asList(new RangeShardValue(2l, 7l))); Assert.equals(map, expectedResult); }
@Test public void test04() { DivideShardRouteRule rule = new DivideShardRouteRule(8L, 4L, true); Map<ShardRouteInfo, List<RangeShardValue>> map = rule.groupSdValuesByRouteInfo("member", "user", new RangeShardValue(2l, 7l)); Map<ShardRouteInfo, List<RangeShardValue>> expectedResult = new LinkedHashMap<>(); expectedResult.put(new ShardRouteInfo("member", "user"), Arrays.asList(new RangeShardValue(2l, 3l))); expectedResult.put(new ShardRouteInfo("member", "user_1"), Arrays.asList(new RangeShardValue(4l, 7l))); Assert.equals(map, expectedResult); rule = new DivideShardRouteRule(null, 4L, true); map = rule.groupSdValuesByRouteInfo("member", "user", new RangeShardValue(2l, 7l)); expectedResult = new LinkedHashMap<>(); expectedResult.put(new ShardRouteInfo("member", "user"), Arrays.asList(new RangeShardValue(2l, 3l))); expectedResult.put(new ShardRouteInfo("member", "user_1"), Arrays.asList(new RangeShardValue(4l, 7l))); Assert.equals(map, expectedResult); rule = new DivideShardRouteRule(8l, null, true); map = rule.groupSdValuesByRouteInfo("member", "user", new RangeShardValue(2l, 7l)); expectedResult = new LinkedHashMap<>(); expectedResult.put(new ShardRouteInfo("member", "user"), Arrays.asList(new RangeShardValue(2l, 7l))); Assert.equals(map, expectedResult); rule = new DivideShardRouteRule(null, null, true); map = rule.groupSdValuesByRouteInfo("member", "user", new RangeShardValue(2l, 7l)); expectedResult = new LinkedHashMap<>(); expectedResult.put(new ShardRouteInfo("member", "user"), Arrays.asList(new RangeShardValue(2l, 7l))); Assert.equals(map, expectedResult); }