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);
}
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);
}
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);
}
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());
}
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);
}
Aggregations