Search in sources :

Example 1 with PlaceHolder

use of com.alibaba.cobar.parser.ast.expression.primary.PlaceHolder in project cobar by alibaba.

the class MySQLParser method createPlaceHolder.

protected PlaceHolder createPlaceHolder(String str, String strUp) {
    PlaceHolder ph = new PlaceHolder(str, strUp);
    ph.setCacheEvalRst(cacheEvalRst);
    return ph;
}
Also used : PlaceHolder(com.alibaba.cobar.parser.ast.expression.primary.PlaceHolder)

Example 2 with PlaceHolder

use of com.alibaba.cobar.parser.ast.expression.primary.PlaceHolder in project cobar by alibaba.

the class MySQLDataNode method setHeartbeat.

private void setHeartbeat(String heartbeat) {
    if (heartbeat == null) {
        heartbeatAST = null;
        placeHolderToStringer = null;
        return;
    }
    try {
        final Set<PlaceHolder> plist = new HashSet<PlaceHolder>(1, 1);
        SQLStatement ast = SQLParserDelegate.parse(heartbeat);
        ast.accept(new EmptySQLASTVisitor() {

            @Override
            public void visit(PlaceHolder node) {
                plist.add(node);
            }
        });
        if (plist.isEmpty()) {
            heartbeatAST = null;
            placeHolderToStringer = null;
            return;
        }
        Map<PlaceHolder, Object> phm = new HashMap<PlaceHolder, Object>(plist.size(), 1);
        for (PlaceHolder ph : plist) {
            final String content = ph.getName();
            final int low = Integer.parseInt(content.substring(content.indexOf('(') + 1, content.indexOf(',')).trim());
            final int high = Integer.parseInt(content.substring(content.indexOf(',') + 1, content.indexOf(')')).trim());
            phm.put(ph, new Object() {

                private Random rnd = new Random();

                @Override
                public String toString() {
                    return String.valueOf(rnd.nextInt(high - low + 1) + low);
                }
            });
        }
        heartbeatAST = ast;
        placeHolderToStringer = phm;
    } catch (SQLSyntaxErrorException e) {
        throw new ConfigException("heartbeat syntax err: " + heartbeat, e);
    }
}
Also used : EmptySQLASTVisitor(com.alibaba.cobar.parser.visitor.EmptySQLASTVisitor) PlaceHolder(com.alibaba.cobar.parser.ast.expression.primary.PlaceHolder) HashMap(java.util.HashMap) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) ConfigException(com.alibaba.cobar.config.util.ConfigException) SQLStatement(com.alibaba.cobar.parser.ast.stmt.SQLStatement) Random(java.util.Random) HashSet(java.util.HashSet)

Example 3 with PlaceHolder

use of com.alibaba.cobar.parser.ast.expression.primary.PlaceHolder in project cobar by alibaba.

the class PartitionByStringTest method testPartitionStartEqEnd.

/**
     * start == end , except 0:0,
     */
@SuppressWarnings("unchecked")
public void testPartitionStartEqEnd() {
    // 同号,不越界
    PartitionByString sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("1:1");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-5:-5");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    // 异号,不越界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("3:-7");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("5:-5");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    // 同号,边界值
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("0:0");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(641, (int) execute(sut, "skkdifisd-"));
    Assert.assertEquals(74, (int) execute(sut, "sdsdfsafaw"));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("10:10");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    // 异号,边界值
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("0:-10");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-1:9");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    // 同号,越界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-15:-15");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("15:15");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
// 异号,越界,不存在
}
Also used : PlaceHolder(com.alibaba.cobar.parser.ast.expression.primary.PlaceHolder) Expression(com.alibaba.cobar.parser.ast.expression.Expression) List(java.util.List)

Example 4 with PlaceHolder

use of com.alibaba.cobar.parser.ast.expression.primary.PlaceHolder in project cobar by alibaba.

the class PartitionByStringTest method testPartitionStartgtEnd.

@SuppressWarnings("unchecked")
public void testPartitionStartgtEnd() {
    String testKey = "abcdefghij";
    // 同号,不越界
    PartitionByString sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("1:6");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(36, (int) execute(sut, testKey));
    Assert.assertEquals(36, (int) execute(sut, "a" + testKey.substring(1, 6) + "abcd"));
    Assert.assertEquals(36, (int) execute(sut, "b" + testKey.substring(1, 6) + "sila"));
    Assert.assertTrue((36 != (int) execute(sut, "c" + testKey.substring(1, 5) + "sil2")));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-8:-5");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(36, (int) execute(sut, testKey));
    Assert.assertEquals(36, (int) execute(sut, "12" + testKey.substring(2, 5) + "12345"));
    Assert.assertEquals(36, (int) execute(sut, "45" + testKey.substring(2, 5) + "78923"));
    // 异号,不越界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-9:9");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(260, (int) execute(sut, "a" + testKey.substring(1, 9) + "8"));
    Assert.assertEquals(260, (int) execute(sut, "f" + testKey.substring(1, 9) + "*"));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("2:-1");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(934, (int) execute(sut, "ab" + testKey.substring(2, 9) + "8"));
    Assert.assertEquals(934, (int) execute(sut, "fj" + testKey.substring(2, 9) + "*"));
    // 同号,边界值, 双边界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("0:9");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(101, (int) execute(sut, testKey.substring(0, 9) + "#"));
    Assert.assertEquals(101, (int) execute(sut, testKey.substring(0, 9) + "*"));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-10:-1");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(101, (int) execute(sut, testKey.substring(0, 9) + "#"));
    Assert.assertEquals(101, (int) execute(sut, testKey.substring(0, 9) + "*"));
    // 同号,边界值, 单边界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("0:5");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(99, (int) execute(sut, testKey.substring(0, 5) + "#uiyt"));
    Assert.assertEquals(99, (int) execute(sut, testKey.substring(0, 5) + "*rfsj"));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("5:9");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(386, (int) execute(sut, "#uiyt" + testKey.substring(5, 9) + "a"));
    Assert.assertEquals(386, (int) execute(sut, "*rfsj" + testKey.substring(5, 9) + "%"));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-10:-7");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(36, (int) execute(sut, testKey.substring(0, 5) + "#uiyt45"));
    Assert.assertEquals(36, (int) execute(sut, testKey.substring(0, 5) + "*rfsjkm"));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-4:-1");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(936, (int) execute(sut, "#uiyt4" + testKey.substring(5, 9) + "a"));
    Assert.assertEquals(936, (int) execute(sut, "*rfsj$" + testKey.substring(5, 9) + "%"));
    // 异号,边界值,双边界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-10:9");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(101, (int) execute(sut, testKey.substring(0, 9) + "a"));
    Assert.assertEquals(101, (int) execute(sut, testKey.substring(0, 9) + "%"));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("0:-1");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(101, (int) execute(sut, testKey.substring(0, 9) + "a"));
    Assert.assertEquals(101, (int) execute(sut, testKey.substring(0, 9) + "%"));
    // 异号,边界值,单边界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-10:4");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(66, (int) execute(sut, testKey.substring(0, 4) + "asdebh"));
    Assert.assertEquals(66, (int) execute(sut, testKey.substring(0, 4) + "%^&*()"));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("0:-6");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(66, (int) execute(sut, testKey.substring(0, 4) + "asdebh"));
    Assert.assertEquals(66, (int) execute(sut, testKey.substring(0, 4) + "%^&*()"));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-5:9");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(386, (int) execute(sut, "#uiyt" + testKey.substring(5, 9) + "a"));
    Assert.assertEquals(386, (int) execute(sut, "*rfsj" + testKey.substring(5, 9) + "%"));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("5:-1");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(386, (int) execute(sut, "#uiyt" + testKey.substring(5, 9) + "a"));
    Assert.assertEquals(386, (int) execute(sut, "*rfsj" + testKey.substring(5, 9) + "%"));
    // 同号,双越界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("11:15");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-20:-15");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    // 同号,单越界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-15:-8");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(33, (int) execute(sut, testKey.substring(0, 2) + "dskfdijc"));
    Assert.assertEquals(33, (int) execute(sut, testKey.substring(0, 2) + "cuiejdjj"));
    Assert.assertEquals(129, (int) execute(sut, "$%cuiejdjj"));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("6:15");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(450, (int) execute(sut, "#uiyt#" + testKey.substring(6, 10)));
    Assert.assertEquals(450, (int) execute(sut, "*rfsj*" + testKey.substring(6, 10)));
    Assert.assertEquals(345, (int) execute(sut, "#uiyt#" + "dkug"));
    // 异号,双越界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-20:19");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(165, (int) execute(sut, testKey));
    Assert.assertEquals(725, (int) execute(sut, "1" + testKey.substring(1, 10)));
    // 异号,单越界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-8:15");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(388, (int) execute(sut, "1q" + testKey.substring(2, 10)));
    Assert.assertEquals(388, (int) execute(sut, "sd" + testKey.substring(2, 10)));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-15:6");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(99, (int) execute(sut, testKey.substring(0, 6) + "abcd"));
    Assert.assertEquals(99, (int) execute(sut, testKey.substring(0, 6) + "efgh"));
}
Also used : PlaceHolder(com.alibaba.cobar.parser.ast.expression.primary.PlaceHolder) Expression(com.alibaba.cobar.parser.ast.expression.Expression) List(java.util.List)

Example 5 with PlaceHolder

use of com.alibaba.cobar.parser.ast.expression.primary.PlaceHolder in project cobar by alibaba.

the class PartitionByStringTest method testPartitionStartLtEnd.

/**
     * if end==0, then end = key.length
     */
@SuppressWarnings("unchecked")
public void testPartitionStartLtEnd() {
    // 同号,不越界
    PartitionByString sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("6:1");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-5:-8");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    // 异号,不越界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("9:-9");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-1:2");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    // 同号,边界值, 双边界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("9:0");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(119, (int) execute(sut, "qiycgsrmkw"));
    Assert.assertEquals(104, (int) execute(sut, "tbctwicjyh"));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-1:-10");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    // 同号,边界值, 单边界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("5:0");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(176, (int) execute(sut, "kducgalemc"));
    Assert.assertEquals(182, (int) execute(sut, "1icuwixjsn"));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("9:5");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-7:-10");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-1:-4");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    // 异号,边界值,双边界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("9:-10");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-1:0");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(108, (int) execute(sut, "tcjsyckxhl"));
    Assert.assertEquals(106, (int) execute(sut, "1uxhklsycj"));
    // 异号,边界值,单边界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("4:-10");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-6:0");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(631, (int) execute(sut, "1kckdlxhxw"));
    Assert.assertEquals(864, (int) execute(sut, "nhyjklouqj"));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("9:-5");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-1:5");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    // 同号,双越界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("15:11");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-15:-20");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    // 同号,单越界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-8:-15");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("15:6");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    // 异号,双越界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("19:-20");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    // 异号,单越界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("15:-8");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("6:-15");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
    Assert.assertEquals(0, (int) execute(sut, UUID.randomUUID().toString().substring(0, 10)));
}
Also used : PlaceHolder(com.alibaba.cobar.parser.ast.expression.primary.PlaceHolder) Expression(com.alibaba.cobar.parser.ast.expression.Expression) List(java.util.List)

Aggregations

PlaceHolder (com.alibaba.cobar.parser.ast.expression.primary.PlaceHolder)7 Expression (com.alibaba.cobar.parser.ast.expression.Expression)5 List (java.util.List)5 ConfigException (com.alibaba.cobar.config.util.ConfigException)1 SQLStatement (com.alibaba.cobar.parser.ast.stmt.SQLStatement)1 EmptySQLASTVisitor (com.alibaba.cobar.parser.visitor.EmptySQLASTVisitor)1 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1