use of io.mycat.cache.CacheService in project Mycat-Server by MyCATApache.
the class ShowCache method execute.
public static void execute(ManagerConnection c) {
ByteBuffer buffer = c.allocate();
// write header
buffer = header.write(buffer, c, true);
// write fields
for (FieldPacket field : fields) {
buffer = field.write(buffer, c, true);
}
// write eof
buffer = eof.write(buffer, c, true);
// write rows
byte packetId = eof.packetId;
CacheService cacheService = MycatServer.getInstance().getCacheService();
for (Map.Entry<String, CachePool> entry : cacheService.getAllCachePools().entrySet()) {
String cacheName = entry.getKey();
CachePool cachePool = entry.getValue();
if (cachePool instanceof LayerCachePool) {
for (Map.Entry<String, CacheStatic> staticsEntry : ((LayerCachePool) cachePool).getAllCacheStatic().entrySet()) {
RowDataPacket row = getRow(cacheName + '.' + staticsEntry.getKey(), staticsEntry.getValue(), c.getCharset());
row.packetId = ++packetId;
buffer = row.write(buffer, c, true);
}
} else {
RowDataPacket row = getRow(cacheName, cachePool.getCacheStatic(), c.getCharset());
row.packetId = ++packetId;
buffer = row.write(buffer, c, true);
}
}
// write last eof
EOFPacket lastEof = new EOFPacket();
lastEof.packetId = ++packetId;
buffer = lastEof.write(buffer, c, true);
// write buffer
c.write(buffer);
}
use of io.mycat.cache.CacheService 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);
}
use of io.mycat.cache.CacheService in project Mycat-Server by MyCATApache.
the class DDLRouteTest method testSpecialCharDDL.
@Test
public void testSpecialCharDDL() throws Exception {
SchemaConfig schema = schemaMap.get("TESTDB");
CacheService cacheService = new CacheService();
RouteService routerService = new RouteService(cacheService);
// alter table test
String sql = " ALTER TABLE COMPANY\r\nADD COLUMN TEST VARCHAR(255) NULL AFTER CREATE_DATE,\r\n CHARACTER SET = UTF8";
sql = RouterUtil.getFixedSql(sql);
List<String> dataNodes = new ArrayList<>();
String tablename = RouterUtil.getTableName(sql, RouterUtil.getAlterTablePos(sql, 0));
Map<String, TableConfig> tables = schema.getTables();
TableConfig tc;
if (tables != null && (tc = tables.get(tablename)) != null) {
dataNodes = tc.getDataNodes();
}
int nodeSize = dataNodes.size();
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);
}
use of io.mycat.cache.CacheService in project Mycat-Server by MyCATApache.
the class HintDBTypeTest method testHint.
/**
* 测试注解
*
* @throws Exception
*/
@Test
public void testHint() throws Exception {
SchemaConfig schema = schemaMap.get("TESTDB");
//使用注解(新注解,/*!mycat*/),runOnSlave=false 强制走主节点
String sql = "/*!mycat:db_type=master*/select * from employee where sharding_id=1";
CacheService cacheService = new CacheService();
RouteService routerService = new RouteService(cacheService);
RouteResultset rrs = routerService.route(new SystemConfig(), schema, ServerParse.SELECT, sql, "UTF-8", null);
Assert.assertTrue(!rrs.getRunOnSlave());
//使用注解(新注解,/*#mycat*/),runOnSlave=false 强制走主节点
sql = "/*#mycat:db_type=master*/select * from employee where sharding_id=1";
rrs = routerService.route(new SystemConfig(), schema, ServerParse.SELECT, sql, "UTF-8", null);
Assert.assertTrue(!rrs.getRunOnSlave());
//使用注解(新注解,/*mycat*/),runOnSlave=false 强制走主节点
sql = "/*mycat:db_type=master*/select * from employee where sharding_id=1";
rrs = routerService.route(new SystemConfig(), schema, ServerParse.SELECT, sql, "UTF-8", null);
Assert.assertTrue(!rrs.getRunOnSlave());
//不使用注解,runOnSlave=null, 根据读写分离策略走主从库
sql = "select * from employee where sharding_id=1";
rrs = routerService.route(new SystemConfig(), schema, ServerParse.SELECT, sql, "UTF-8", null);
Assert.assertTrue(rrs.getRunOnSlave() == null);
}
use of io.mycat.cache.CacheService in project Mycat-Server by MyCATApache.
the class DDLRouteTest method testDDL.
/**
* ddl deal test
*
* @throws Exception
*/
@Test
public void testDDL() throws Exception {
SchemaConfig schema = schemaMap.get("TESTDB");
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();
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();
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();
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();
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