diff --git a/Mycat-Core/src/main/java/io/mycat/sqlcache/SQLResultsCacheService.java b/Mycat-Core/src/main/java/io/mycat/sqlcache/SQLResultsCacheService.java index 69eb2ab..925163a 100644 --- a/Mycat-Core/src/main/java/io/mycat/sqlcache/SQLResultsCacheService.java +++ b/Mycat-Core/src/main/java/io/mycat/sqlcache/SQLResultsCacheService.java @@ -22,6 +22,9 @@ import io.mycat.mysql.packet.MySQLPacket; import io.mycat.net2.states.WriteWaitingState; +import java.io.IOException; +import java.util.ArrayList; + /** * SQL 结果集缓存服务 * @@ -31,7 +34,7 @@ */ public class SQLResultsCacheService { - private static final Logger LOGGER = LoggerFactory.getLogger(SQLResultsCacheService.class); + // private static final Logger LOGGER = LoggerFactory.getLogger(SQLResultsCacheService.class); private final CacheImp sqlResultCacheImp; static class InitSQLResultsCacheService { @@ -45,29 +48,29 @@ private SQLResultsCacheService() { /** * 将sql结果集缓存起来 * - * @param sqlContext Hint SQL相关信息 - * @param bigSQLResult sql结果集缓存 - * @param loader 结果集load or reload 接口 - * @param listener key被移除时,调用的接口 + * @param sqlContext Hint SQL相关信息 + * @param bigSQLResult sql结果集缓存 + * @param loader 结果集load or reload 接口 + * @param listener key被移除时,调用的接口 */ - public void cacheSQLResult(NewSQLContext sqlContext,BigSQLResult bigSQLResult,IDataLoader loader, - IRemoveKeyListener listener){ + public void cacheSQLResult(NewSQLContext sqlContext, BigSQLResult bigSQLResult, IDataLoader loader, + IRemoveKeyListener listener) { /** * cache-time=xxx auto-refresh=true access-count=5000 */ String realSQL = sqlContext.getRealSQL(0); - String key = "" + murmur3_32().hashUnencodedChars(realSQL); - Keyer keyer = new Keyer(); +// String key = "" + murmur3_32().hashUnencodedChars(realSQL); + Keyer keyer = new Keyer(); keyer.setSql(realSQL); - keyer.setKey(key); +// keyer.setKey(key); keyer.setValue(bigSQLResult); keyer.setCacheTTL(sqlContext.getAnnotationValue(NewSQLContext.ANNOTATION_CACHE_TIME)); keyer.setAccessCount(sqlContext.getAnnotationValue(NewSQLContext.ANNOTATION_ACCESS_COUNT)); // keyer.setAutoRefresh(sqlContext.getAnnotationValue(NewSQLContext.ANNOTATION_AUTO_REFRESH)); keyer.setRemoveKeyListener(listener); keyer.setiDataLoader(loader); - sqlResultCacheImp.put(key, bigSQLResult, keyer); +// sqlResultCacheImp.put(key, bigSQLResult, keyer); } /** @@ -77,8 +80,9 @@ public void cacheSQLResult(NewSQLContext sqlContext,BigSQLResult bigSQLResult,ID * @return */ public BigSQLResult getSQLResult(String sql) { - String key = "" + murmur3_32().hashUnencodedChars(sql); - return sqlResultCacheImp.get(key); +// String key = "" + murmur3_32().hashUnencodedChars(sql); +// return sqlResultCacheImp.get(key); + return null; } @@ -89,7 +93,8 @@ public BigSQLResult getSQLResult(String sql) { */ public void remove(String sql) { - String key = "" + murmur3_32().hashUnencodedChars(sql); +// String key = "" + murmur3_32().hashUnencodedChars(sql); + String key = ""; sqlResultCacheImp.remove(key); } @@ -100,8 +105,8 @@ public void remove(String sql) { * @param frontCon * @param sqlContext * @param mySQLMessage - * @return * @throws IOException + * @return */ public boolean processHintSQL(MySQLFrontConnection frontCon, NewSQLContext sqlContext, MySQLMessage mySQLMessage) throws IOException { @@ -116,14 +121,14 @@ public boolean processHintSQL(MySQLFrontConnection frontCon, NewSQLContext sqlCo String realSql = sqlContext.getRealSQL(0); BigSQLResult sqlResultCache = getSQLResult(realSql); - if (sqlResultCache != null){ - LOGGER.error(realSql + ":====>>>> Use Local Cache SQL Resuls"); - sqlResultCacheDirectClient(frontCon,sqlResultCache); + if (sqlResultCache != null) { +// LOGGER.error(realSql + ":====>>>> Use Local Cache SQL Resuls"); + sqlResultCacheDirectClient(frontCon, sqlResultCache); return true; } else { /**从后端拉取数据进行缓存*/ - sqlResultCache = - new BigSQLResult(LocatePolicy.Normal,realSql,32*1024*1024/**TODO*/); + sqlResultCache = + new BigSQLResult(LocatePolicy.Normal, realSql, 32 * 1024 * 1024/**TODO*/); } /** @@ -147,7 +152,7 @@ public boolean processHintSQL(MySQLFrontConnection frontCon, NewSQLContext sqlCo session.removeBackCon(existCon); } if (frontCon.getMycatSchema() == null) { - frontCon.writeErrMessage(1450, "No schema selected"); +// frontCon.writeErrMessage(1450, "No schema selected"); return false; } @@ -159,14 +164,15 @@ public boolean processHintSQL(MySQLFrontConnection frontCon, NewSQLContext sqlCo /** * 如果该sql对应后端db,没有连接池,则创建连接池部分 */ - final MySQLBackendConnection newCon = - datas.getConnection(frontCon.getReactor(), dnBean.getDatabase(), true, null); + final MySQLBackendConnection newCon = null; +// datas.getConnection(frontCon.getReactor(), dnBean.getDatabase(), true, null); + /**很关键的设置前端front 与 backend session*/ newCon.setAttachement(frontCon); /**设置后端连接池结果集处理handler,sqlResultCache缓存结果集类*/ - newCon.setUserCallback(new SQLResCacheHintHandler(sqlContext,sqlResultCache)); + newCon.setUserCallback(new SQLResCacheHintHandler(sqlContext, sqlResultCache)); /** * 执行sql语句 @@ -176,8 +182,8 @@ public boolean processHintSQL(MySQLFrontConnection frontCon, NewSQLContext sqlCo * 将数据写到后端连接池中 */ command.arg = realSql.getBytes(newCon.getCharset()); - newCon.getWriteDataBuffer().putBytes(command.write(newCon)); - newCon.enableWrite(false); +// newCon.getWriteDataBuffer().putBytes(command.write(newCon)); +// newCon.enableWrite(false); /** * 新建立的连接放到连接池中 */ @@ -188,11 +194,11 @@ public boolean processHintSQL(MySQLFrontConnection frontCon, NewSQLContext sqlCo /** * 否则直接写到后端即可 */ - command.arg = realSql.getBytes(existCon.getCharset()); - existCon.getWriteDataBuffer().putBytes(command.write(existCon)); - existCon.enableWrite(false); +// command.arg = realSql.getBytes(existCon.getCharset()); +// existCon.getWriteDataBuffer().putBytes(command.write(existCon)); +// existCon.enableWrite(false); /**设置后端连接池结果集处理handler,sqlResultCache缓存结果集类*/ - existCon.setUserCallback(new SQLResCacheHintHandler(sqlContext,sqlResultCache)); + existCon.setUserCallback(new SQLResCacheHintHandler(sqlContext, sqlResultCache)); } return true;