Search in sources :

Example 31 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat-Server by MyCATApache.

the class DruidLockTableParser method statementParse.

@Override
public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) throws SQLNonTransientException {
    MySqlLockTableStatement lockTableStat = (MySqlLockTableStatement) stmt;
    String table = lockTableStat.getTableSource().toString().toUpperCase();
    TableConfig tableConfig = schema.getTables().get(table);
    if (tableConfig == null) {
        String msg = "can't find table define of " + table + " in schema:" + schema.getName();
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    LockType lockType = lockTableStat.getLockType();
    if (LockType.WRITE != lockType && LockType.READ != lockType) {
        String msg = "lock type must be write or read";
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    List<String> dataNodes = tableConfig.getDataNodes();
    RouteResultsetNode[] nodes = new RouteResultsetNode[dataNodes.size()];
    for (int i = 0; i < dataNodes.size(); i++) {
        nodes[i] = new RouteResultsetNode(dataNodes.get(i), ServerParse.LOCK, stmt.toString());
    }
    rrs.setNodes(nodes);
    rrs.setFinishedRoute(true);
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) MySqlLockTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement) RouteResultsetNode(io.mycat.route.RouteResultsetNode) TableConfig(io.mycat.config.model.TableConfig) LockType(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement.LockType)

Example 32 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat-Server by MyCATApache.

the class RuleFunctionSuitTableTest method testPartitionByPrefixPattern.

@Test
public void testPartitionByPrefixPattern() {
    PartitionByPrefixPattern partition = new PartitionByPrefixPattern();
    partition.setMapFile("partition_prefix_pattern.txt");
    partition.init();
    /*
		 * partition_prefix_pattern.txt
		 * 1-4=0
         * 5-8=1
         * 9-12=2
         * 13-16=3
         * 17-20=4
	     * 21-24=5
         * 25-28=6
         * 29-32=7
         * 0-0=7
		 */
    Assert.assertEquals(8, partition.getPartitionNum());
    RuleConfig rule = new RuleConfig("id", "partition-prefix-pattern");
    rule.setRuleAlgorithm(partition);
    TableConfig tableConf = new TableConfig("test", "id", true, false, -1, "dn1,dn2", null, rule, true, null, false, null, null, null);
    int suit1 = partition.suitableFor(tableConf);
    Assert.assertEquals(-1, suit1);
    tableConf.getDataNodes().clear();
    String[] dataNodes = SplitUtil.split("dn$1-8", ',', '$', '-');
    tableConf.getDataNodes().addAll(Arrays.asList(dataNodes));
    int suit2 = partition.suitableFor(tableConf);
    Assert.assertEquals(0, suit2);
    tableConf.getDataNodes().clear();
    dataNodes = SplitUtil.split("dn$1-10", ',', '$', '-');
    tableConf.getDataNodes().addAll(Arrays.asList(dataNodes));
    int suit3 = partition.suitableFor(tableConf);
    Assert.assertEquals(1, suit3);
}
Also used : TableConfig(io.mycat.config.model.TableConfig) RuleConfig(io.mycat.config.model.rule.RuleConfig) Test(org.junit.Test)

Example 33 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat-Server by MyCATApache.

the class RuleFunctionSuitTableTest method testPartitionByRangeMod.

@Test
public void testPartitionByRangeMod() {
    PartitionByRangeMod partition = new PartitionByRangeMod();
    partition.setMapFile("partition-range-mod.txt");
    partition.init();
    // partition = 20
    Assert.assertEquals(20, partition.getPartitionNum());
    RuleConfig rule = new RuleConfig("id", "partition-range-mod");
    rule.setRuleAlgorithm(partition);
    TableConfig tableConf = new TableConfig("test", "id", true, false, -1, "dn$1-10", null, rule, true, null, false, null, null, null);
    int suit1 = partition.suitableFor(tableConf);
    Assert.assertEquals(-1, suit1);
    tableConf.getDataNodes().clear();
    String[] dataNodes = SplitUtil.split("dn$1-20", ',', '$', '-');
    tableConf.getDataNodes().addAll(Arrays.asList(dataNodes));
    int suit2 = partition.suitableFor(tableConf);
    Assert.assertEquals(0, suit2);
    tableConf.getDataNodes().clear();
    dataNodes = SplitUtil.split("dn$1-30", ',', '$', '-');
    tableConf.getDataNodes().addAll(Arrays.asList(dataNodes));
    int suit3 = partition.suitableFor(tableConf);
    Assert.assertEquals(1, suit3);
}
Also used : TableConfig(io.mycat.config.model.TableConfig) RuleConfig(io.mycat.config.model.rule.RuleConfig) Test(org.junit.Test)

Example 34 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat-Server by MyCATApache.

the class RuleFunctionSuitTableTest method testAutoPartitionByLong.

@Test
public void testAutoPartitionByLong() {
    AutoPartitionByLong autoPartition = new AutoPartitionByLong();
    autoPartition.setMapFile("autopartition-long.txt");
    // partition = 3
    autoPartition.init();
    Assert.assertEquals(3, autoPartition.getPartitionNum());
    RuleConfig rule = new RuleConfig("id", "auto-partition-long");
    rule.setRuleAlgorithm(autoPartition);
    TableConfig tableConf = new TableConfig("test", "id", true, false, -1, "dn1,dn2", null, rule, true, null, false, null, null, null);
    int suit1 = autoPartition.suitableFor(tableConf);
    Assert.assertEquals(-1, suit1);
    tableConf.getDataNodes().clear();
    tableConf.getDataNodes().addAll(Arrays.asList("dn1", "dn2", "dn3"));
    int suit2 = autoPartition.suitableFor(tableConf);
    Assert.assertEquals(0, suit2);
    tableConf.getDataNodes().clear();
    tableConf.getDataNodes().addAll(Arrays.asList("dn1", "dn2", "dn3", "dn4"));
    int suit3 = autoPartition.suitableFor(tableConf);
    Assert.assertEquals(1, suit3);
    /* 
		 * autopartition-long-dupl.txt
		 * 0-1000=0
		 * 1001-2000=1
		 * 2001-3000=0
		 * 3001-4000=1
		*/
    AutoPartitionByLong autoPartition2 = new AutoPartitionByLong();
    autoPartition2.setMapFile("autopartition-long-dupl.txt");
    autoPartition2.init();
    Assert.assertEquals(2, autoPartition2.getPartitionNum());
    RuleConfig rule2 = new RuleConfig("id", "auto-partition-long-dupl");
    rule2.setRuleAlgorithm(autoPartition2);
    TableConfig tableConf2 = new TableConfig("test2", "id", true, false, -1, "dn1,dn2", null, rule, true, null, false, null, null, null);
    Assert.assertEquals(0, autoPartition2.suitableFor(tableConf2));
    Assert.assertEquals(0, autoPartition2.calculate("500").intValue());
    Assert.assertEquals(1, autoPartition2.calculate("1500").intValue());
    Assert.assertEquals(1, autoPartition2.calculate("2000").intValue());
    Assert.assertEquals(0, autoPartition2.calculate("3000").intValue());
    Assert.assertEquals(1, autoPartition2.calculate("3001").intValue());
}
Also used : TableConfig(io.mycat.config.model.TableConfig) RuleConfig(io.mycat.config.model.rule.RuleConfig) Test(org.junit.Test)

Example 35 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat-Server by MyCATApache.

the class DDLRouteTest method testDDLDefaultNode.

@Test
public void testDDLDefaultNode() throws Exception {
    SchemaConfig schema = schemaMap.get("solo1");
    CacheService cacheService = new CacheService();
    RouteService routerService = new RouteService(cacheService);
    // create table/view/function/..
    String sql = " create table company(idd int)";
    sql = RouterUtil.getFixedSql(sql);
    String upsql = sql.toUpperCase();
    // TODO:modify by zhuam 小写表名,转为大写比较
    String tablename = RouterUtil.getTableName(sql, RouterUtil.getCreateTablePos(upsql, 0));
    tablename = tablename.toUpperCase();
    List<String> dataNodes = new ArrayList<>();
    Map<String, TableConfig> tables = schema.getTables();
    TableConfig tc;
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    int nodeSize = dataNodes.size();
    if (nodeSize == 0 && schema.getDataNode() != null) {
        nodeSize = 1;
    }
    int rs = ServerParse.parse(sql);
    int sqlType = rs & 0xff;
    RouteResultset rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
    // drop table test
    sql = " drop table COMPANY";
    sql = RouterUtil.getFixedSql(sql);
    upsql = sql.toUpperCase();
    tablename = RouterUtil.getTableName(sql, RouterUtil.getDropTablePos(upsql, 0));
    tables = schema.getTables();
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    nodeSize = dataNodes.size();
    if (nodeSize == 0 && schema.getDataNode() != null) {
        nodeSize = 1;
    }
    rs = ServerParse.parse(sql);
    sqlType = rs & 0xff;
    rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
    // drop table test
    sql = " drop table if exists COMPANY";
    sql = RouterUtil.getFixedSql(sql);
    upsql = sql.toUpperCase();
    tablename = RouterUtil.getTableName(sql, RouterUtil.getDropTablePos(upsql, 0));
    tables = schema.getTables();
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    nodeSize = dataNodes.size();
    if (nodeSize == 0 && schema.getDataNode() != null) {
        nodeSize = 1;
    }
    rs = ServerParse.parse(sql);
    sqlType = rs & 0xff;
    rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
    // alter table
    sql = "   alter table COMPANY add COLUMN name int ;";
    sql = RouterUtil.getFixedSql(sql);
    upsql = sql.toUpperCase();
    tablename = RouterUtil.getTableName(sql, RouterUtil.getAlterTablePos(upsql, 0));
    tables = schema.getTables();
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    nodeSize = dataNodes.size();
    if (nodeSize == 0 && schema.getDataNode() != null) {
        nodeSize = 1;
    }
    rs = ServerParse.parse(sql);
    sqlType = rs & 0xff;
    rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
    // truncate table;
    sql = " truncate table COMPANY";
    sql = RouterUtil.getFixedSql(sql);
    upsql = sql.toUpperCase();
    tablename = RouterUtil.getTableName(sql, RouterUtil.getTruncateTablePos(upsql, 0));
    tables = schema.getTables();
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    nodeSize = dataNodes.size();
    if (nodeSize == 0 && schema.getDataNode() != null) {
        nodeSize = 1;
    }
    rs = ServerParse.parse(sql);
    sqlType = rs & 0xff;
    rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) ArrayList(java.util.ArrayList) TableConfig(io.mycat.config.model.TableConfig) RouteService(io.mycat.route.RouteService) CacheService(io.mycat.cache.CacheService) RouteResultset(io.mycat.route.RouteResultset) Test(org.junit.Test)

Aggregations

TableConfig (io.mycat.config.model.TableConfig)84 SchemaConfig (io.mycat.config.model.SchemaConfig)26 SQLNonTransientException (java.sql.SQLNonTransientException)26 Test (org.junit.Test)18 RuleConfig (io.mycat.config.model.rule.RuleConfig)16 RouteResultset (io.mycat.route.RouteResultset)14 AbstractPartitionAlgorithm (io.mycat.route.function.AbstractPartitionAlgorithm)14 HashMap (java.util.HashMap)14 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)12 SlotFunction (io.mycat.route.function.SlotFunction)12 ArrayList (java.util.ArrayList)12 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)10 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)8 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)8 ConfigException (io.mycat.config.util.ConfigException)8 RouteResultsetNode (io.mycat.route.RouteResultsetNode)8 HashSet (java.util.HashSet)8 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)6 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)6 CacheService (io.mycat.cache.CacheService)6