Skip to content

Commit

Permalink
fix #701: cache sql parser result for (prepare) statement
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Apr 5, 2018
1 parent 41999da commit 0e6c960
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
@@ -0,0 +1,67 @@
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package io.shardingjdbc.core.parsing;

import io.shardingjdbc.core.parsing.parser.sql.SQLStatement;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Map;
import java.util.WeakHashMap;

/**
* Parsing result cache.
*
* @author zhangliang
*/
@NoArgsConstructor(access = AccessLevel.NONE)
public final class ParsingResultCache {

private static final ParsingResultCache INSTANCE = new ParsingResultCache();

private volatile Map<String, SQLStatement> cache = new WeakHashMap<>(65535, 1);

/**
* Get parsing result cache instance.
*
* @return parsing result cache instance
*/
public static ParsingResultCache getInstance() {
return INSTANCE;
}

/**
* Put SQL and parsing result into cache.
*
* @param sql SQL
* @param sqlStatement SQL statement
*/
public void put(final String sql, final SQLStatement sqlStatement) {
cache.put(sql, sqlStatement);
}

/**
* Get SQL statement.
*
* @param sql SQL
* @return SQL statement
*/
public SQLStatement getSQLStatement(final String sql) {
return cache.get(sql);
}
}
Expand Up @@ -45,6 +45,10 @@ public final class SQLParsingEngine {
* @return parsed SQL statement
*/
public SQLStatement parse() {
SQLStatement cachedSQLStatement = ParsingResultCache.getInstance().getSQLStatement(sql);
if (null != cachedSQLStatement) {
return cachedSQLStatement;
}
LexerEngine lexerEngine = LexerEngineFactory.newInstance(dbType, sql);
lexerEngine.nextToken();
return SQLParserFactory.newInstance(dbType, lexerEngine.getCurrentToken().getType(), shardingRule, lexerEngine).parse();
Expand Down
Expand Up @@ -34,7 +34,7 @@ public final class PreparedStatementRegistry {

private static final PreparedStatementRegistry INSTANCE = new PreparedStatementRegistry();

private final ConcurrentMap<String, Integer> sqlAndStatementIdMap = new ConcurrentHashMap<>(Integer.MAX_VALUE, 1);
private final ConcurrentMap<String, Integer> sqlAndStatementIdMap = new ConcurrentHashMap<>(65535, 1);

private final AtomicInteger sequence = new AtomicInteger();

Expand Down

0 comments on commit 0e6c960

Please sign in to comment.