Skip to content

Commit

Permalink
Merge pull request #30 from fynwin/master
Browse files Browse the repository at this point in the history
编译报错
  • Loading branch information
fynwin authored Jul 24, 2017
2 parents 6e4be44 + 991969d commit 618f6a6
Showing 1 changed file with 36 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 结果集缓存服务
*
Expand All @@ -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<String, BigSQLResult> sqlResultCacheImp;

static class InitSQLResultsCacheService {
Expand All @@ -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<String,BigSQLResult> loader,
IRemoveKeyListener<String,BigSQLResult> listener){
public void cacheSQLResult(NewSQLContext sqlContext, BigSQLResult bigSQLResult, IDataLoader<String, BigSQLResult> loader,
IRemoveKeyListener<String, BigSQLResult> listener) {

/**
* cache-time=xxx auto-refresh=true access-count=5000
*/
String realSQL = sqlContext.getRealSQL(0);
String key = "" + murmur3_32().hashUnencodedChars(realSQL);
Keyer<String,BigSQLResult> keyer = new Keyer<String,BigSQLResult>();
// String key = "" + murmur3_32().hashUnencodedChars(realSQL);
Keyer<String, BigSQLResult> keyer = new Keyer<String, BigSQLResult>();
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);
}

/**
Expand All @@ -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;
}


Expand All @@ -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);
}

Expand All @@ -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 {

Expand All @@ -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*/);
}

/**
Expand All @@ -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;
}

Expand All @@ -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语句
Expand All @@ -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);
/**
* 新建立的连接放到连接池中
*/
Expand All @@ -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;
Expand Down

0 comments on commit 618f6a6

Please sign in to comment.