@Override public Integer[] calculateRange(String beginValue, String endValue) { Integer begin = 0, end = 0; if (isUseDefaultNode(beginValue) || isUseDefaultNode(endValue)) { begin = 0; end = longRanges.length - 1; } else { begin = calculate(beginValue); end = calculate(endValue); } if (begin == null || end == null) { return new Integer[0]; } if (end >= begin) { int len = end - begin + 1; Integer[] re = new Integer[len]; for (int i = 0; i < len; i++) { re[i] = begin + i; } return re; } else { return new Integer[0]; } }
@Override public void init() { initialize(); initHashCode(); }
@Test public void test2() { AutoPartitionByLong autoPartition = new AutoPartitionByLong(); autoPartition.setMapFile("autopartition-long.txt"); autoPartition.setDefaultNode(0); autoPartition.init(); String idVal = "6000001"; Assert.assertEquals(true, 0 == autoPartition.calculate(idVal)); }
@Test public void test1() { AutoPartitionByLong autoPartition = new AutoPartitionByLong(); autoPartition.setMapFile("autopartition-long.txt"); autoPartition.init(); String idVal = "0"; Assert.assertEquals(true, 0 == autoPartition.calculate(idVal)); idVal = "2000000"; Assert.assertEquals(true, 0 == autoPartition.calculate(idVal)); idVal = "2000001"; Assert.assertEquals(true, 1 == autoPartition.calculate(idVal)); idVal = "4000000"; Assert.assertEquals(true, 1 == autoPartition.calculate(idVal)); idVal = "4000001"; Assert.assertEquals(true, 2 == autoPartition.calculate(idVal)); idVal = "6000000"; Assert.assertEquals(true, 2 == autoPartition.calculate(idVal)); idVal = "6000001"; Assert.assertEquals(true, null == autoPartition.calculate(idVal)); Map<String, String> map = autoPartition.getAllProperties(); Assert.assertEquals(true, map.get("mapFile").equals("{\"0-200M\":\"0\"," + "\"200M1-400M\":\"1\","+ "\"400M1-600M\":\"2\"}") ); }
@Test public void test3() { AutoPartitionByLong autoPartition = new AutoPartitionByLong(); autoPartition.setMapFile("autopartition-long.txt"); autoPartition.setDefaultNode(0); autoPartition.init(); Integer[] res = autoPartition.calculateRange("-1", "9999999999"); Assert.assertEquals(3, res.length); res = autoPartition.calculateRange("-1", "10000000"); Assert.assertEquals(3, res.length); res = autoPartition.calculateRange("-1", "100"); Assert.assertEquals(3, res.length); res = autoPartition.calculateRange("0", "100"); Assert.assertEquals(1, res.length); res = autoPartition.calculateRange("0", "100000"); Assert.assertEquals(1, res.length); res = autoPartition.calculateRange("2000009", "3999999"); Assert.assertEquals(1, res.length); res = autoPartition.calculateRange("2000009", "5999999"); Assert.assertEquals(2, res.length); res = autoPartition.calculateRange("2000009", "59999999"); Assert.assertEquals(3, res.length); }
private AbstractPartitionAlgorithm createFunction(String name, String clazz) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException { String lowerClass = clazz.toLowerCase(); switch (lowerClass) { case "hash": return new PartitionByLong(); case "stringhash": return new PartitionByString(); case "enum": return new PartitionByFileMap(); case "jumpstringhash": return new PartitionByJumpConsistentHash(); case "numberrange": return new AutoPartitionByLong(); case "patternrange": return new PartitionByPattern(); case "date": return new PartitionByDate(); default: Class<?> clz = Class.forName(clazz); //all function must be extend from AbstractPartitionAlgorithm if (!AbstractPartitionAlgorithm.class.isAssignableFrom(clz)) { throw new IllegalArgumentException("rule function must implements " + AbstractPartitionAlgorithm.class.getName() + ", name=" + name); } return (AbstractPartitionAlgorithm) clz.newInstance(); } }