Search in sources :

Example 6 with PlaceHolder

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

the class PartitionByStringTest method testPartition.

@SuppressWarnings("unchecked")
public void testPartition() {
    PartitionByString sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-2:");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals((int) execute(sut, "12"), (int) execute(sut, "012"));
    Assert.assertEquals((int) execute(sut, "112"), (int) execute(sut, "012"));
    Assert.assertEquals((int) execute(sut, "2"), (int) execute(sut, "2"));
    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(49, (int) execute(sut, "012"));
    Assert.assertEquals(49, (int) execute(sut, "12"));
    Assert.assertEquals(49, (int) execute(sut, "15"));
    Assert.assertEquals(0, (int) execute(sut, "2"));
    Assert.assertEquals(56, (int) execute(sut, "888888"));
    Assert.assertEquals(56, (int) execute(sut, "89"));
    Assert.assertEquals(56, (int) execute(sut, "780"));
    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(49, (int) execute(sut, "012"));
    Assert.assertEquals(49, (int) execute(sut, "219"));
    Assert.assertEquals(0, (int) execute(sut, "2"));
    Assert.assertEquals(512, (int) execute(sut, "888888"));
}
Also used : PlaceHolder(com.alibaba.cobar.parser.ast.expression.primary.PlaceHolder) Expression(com.alibaba.cobar.parser.ast.expression.Expression) List(java.util.List)

Example 7 with PlaceHolder

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

the class PartitionByStringTest method testPartitionNoStartOrNoEnd.

@SuppressWarnings("unchecked")
public void testPartitionNoStartOrNoEnd() {
    String testKey = "abcdefghij";
    // 无start, 不越界
    PartitionByString sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice(":6");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(99, (int) execute(sut, testKey));
    Assert.assertEquals(99, (int) execute(sut, testKey.substring(0, 6) + "abcd"));
    Assert.assertEquals(99, (int) execute(sut, testKey.substring(0, 6) + "sila"));
    Assert.assertTrue((99 != (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(":-4");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(99, (int) execute(sut, testKey));
    Assert.assertEquals(99, (int) execute(sut, testKey.substring(0, 6) + "abcd"));
    Assert.assertEquals(99, (int) execute(sut, testKey.substring(0, 6) + "sila"));
    Assert.assertTrue((99 != (int) execute(sut, "c" + testKey.substring(1, 5) + "sil2")));
    // 无start, 越界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice(":15");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(165, (int) execute(sut, testKey));
    Assert.assertEquals(647, (int) execute(sut, "b" + testKey));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice(":-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)));
    // 无end, 不越界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("2:");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(388, (int) execute(sut, testKey));
    Assert.assertEquals(388, (int) execute(sut, "ab" + testKey.substring(2, 10)));
    Assert.assertEquals(388, (int) execute(sut, "e&" + testKey.substring(2, 10)));
    Assert.assertTrue((388 != (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("-5:");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(808, (int) execute(sut, testKey));
    Assert.assertEquals(808, (int) execute(sut, "abT*1" + testKey.substring(5, 10)));
    Assert.assertEquals(808, (int) execute(sut, "ab^^!" + testKey.substring(5, 10)));
    // 无end, 越界
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("-15:");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(165, (int) execute(sut, testKey));
    Assert.assertEquals(647, (int) execute(sut, "b" + testKey));
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice("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)));
    // 无start 无end
    sut = new PartitionByString("test   ", (List<Expression>) ListUtil.createList(new PlaceHolder("member_id", "MEMBER_ID").setCacheEvalRst(false)));
    sut.setCacheEvalRst(false);
    sut.setHashSlice(":");
    sut.setPartitionCount("1024");
    sut.setPartitionLength("1");
    sut.init();
    Assert.assertEquals(165, (int) execute(sut, testKey));
    Assert.assertEquals(452, (int) execute(sut, "b" + testKey.substring(1)));
}
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