public Integer[] calculateRange(String beginValue, String endValue) { if (!isNumeric(beginValue) || !isNumeric(endValue)) { return calcType3(); } long bv = Long.parseLong(beginValue); long ev = Long.parseLong(endValue); long hbv = bv % patternValue; long hev = ev % patternValue; if (findNode(hbv) == null || findNode(hev) == null) { return calcType3(); } if (ev >= bv) { if (ev - bv >= patternValue) { return calcType3(); } if (hbv < hev) { return calcType1(hbv, hev); } else { return calcType2(hbv, hev); } } else { return new Integer[0]; } }
@Override public void init() { initialize(); initHashCode(); }
@Test public void test1() { PartitionByPattern autoPartition = new PartitionByPattern(); autoPartition.setPatternValue(256); autoPartition.setDefaultNode(2); autoPartition.setMapFile("partition-pattern.txt"); autoPartition.init(); String idVal = "0"; Assert.assertEquals(true, 7 == autoPartition.calculate(idVal)); idVal = "45a"; Assert.assertEquals(true, 2 == autoPartition.calculate(idVal)); Integer[] err1 = autoPartition.calculateRange("45a", "0"); Assert.assertEquals(true, 8 == err1.length); Integer[] err2 = autoPartition.calculateRange("45", "0"); Assert.assertEquals(true, 0 == err2.length); Integer[] normal = autoPartition.calculateRange("0", "45"); Assert.assertEquals(true, 3 == normal.length); Integer[] type1 = autoPartition.calculateRange("1", "45"); Assert.assertEquals(true, 2 == type1.length); Integer[] type2 = autoPartition.calculateRange("200", "260"); Assert.assertEquals(true, 3 == type2.length); Integer[] type3 = autoPartition.calculateRange("200", "456"); Assert.assertEquals(true, 8 == type3.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(); } }
private Integer[] calcType1(long begin, long end) { HashSet<Integer> ids = new HashSet<>(); calcAux(ids, begin, end); return ids.toArray(new Integer[ids.size()]); }