Return-Path: X-Original-To: apmail-ignite-commits-archive@minotaur.apache.org Delivered-To: apmail-ignite-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 3AABC18791 for ; Tue, 24 Nov 2015 07:10:52 +0000 (UTC) Received: (qmail 86110 invoked by uid 500); 24 Nov 2015 07:10:52 -0000 Delivered-To: apmail-ignite-commits-archive@ignite.apache.org Received: (qmail 86014 invoked by uid 500); 24 Nov 2015 07:10:52 -0000 Mailing-List: contact commits-help@ignite.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.apache.org Delivered-To: mailing list commits@ignite.apache.org Received: (qmail 85544 invoked by uid 99); 24 Nov 2015 07:10:51 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 24 Nov 2015 07:10:51 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 733C8E03AD; Tue, 24 Nov 2015 07:10:51 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sboikov@apache.org To: commits@ignite.apache.org Date: Tue, 24 Nov 2015 07:11:09 -0000 Message-Id: <166872bbb7cb475cb620d60614d2e755@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [19/19] ignite git commit: ignite-sql-cache-stmt ignite-sql-cache-stmt Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/5e000f85 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/5e000f85 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/5e000f85 Branch: refs/heads/ignite-sql-cache-stmt Commit: 5e000f85674209e78ecc24b8db927c1f984e1b8d Parents: 70b9559 Author: sboikov Authored: Tue Nov 24 10:10:28 2015 +0300 Committer: sboikov Committed: Tue Nov 24 10:10:28 2015 +0300 ---------------------------------------------------------------------- .../processors/query/h2/IgniteH2Indexing.java | 63 ++++++++++++++++++-- .../h2/twostep/GridReduceQueryExecutor.java | 3 +- 2 files changed, 59 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/5e000f85/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java index 7e1c415..4ee1773 100644 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java +++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java @@ -269,10 +269,10 @@ public class IgniteH2Indexing implements GridQueryIndexing { private volatile GridKernalContext ctx; /** */ - private final ThreadLocal> stmtCache = - new ThreadLocal>() { - @Override protected SmallLRUCache initialValue() { - return SmallLRUCache.newInstance(256); + private final ThreadLocal> stmtCache = + new ThreadLocal>() { + @Override protected StatementCache initialValue() { + return new StatementCache(256); } }; @@ -303,7 +303,7 @@ public class IgniteH2Indexing implements GridQueryIndexing { */ private PreparedStatement prepareStatement(Connection c, String sql, boolean useStmtCache) throws SQLException { if (useStmtCache) { - SmallLRUCache cache = stmtCache.get(); + StatementCache cache = stmtCache.get(); PreparedStatement stmt = cache.get(sql); @@ -315,9 +315,13 @@ public class IgniteH2Indexing implements GridQueryIndexing { stmt = c.prepareStatement(sql); - // TODO close the statement on evict from cache cache.put(sql, stmt); + PreparedStatement rmvd = cache.getAndClearRemoved(); + + if (rmvd != null) + U.closeQuiet(rmvd); + return stmt; } else @@ -2371,4 +2375,51 @@ public class IgniteH2Indexing implements GridQueryIndexing { return preferSwapVal; } } + + /** + * + */ + private static class StatementCache extends LinkedHashMap { + /** */ + private int size; + + /** */ + private V rmvd; + + /** + * @param size Size. + */ + private StatementCache(int size) { + super(size, (float)0.75, true); + + this.size = size; + } + + /** {@inheritDoc} */ + @Override protected boolean removeEldestEntry(Map.Entry eldest) { + assert rmvd == null; + + boolean rmv = size() > size; + + if (rmv) + this.rmvd = eldest.getValue(); + + return rmv; + } + + /** + * @return Removed value. + */ + @Nullable V getAndClearRemoved() { + if (rmvd != null) { + V ret = rmvd; + + rmvd = null; + + return ret; + } + + return null; + } + } } http://git-wip-us.apache.org/repos/asf/ignite/blob/5e000f85/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridReduceQueryExecutor.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridReduceQueryExecutor.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridReduceQueryExecutor.java index 000d249..f5868cc 100644 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridReduceQueryExecutor.java +++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridReduceQueryExecutor.java @@ -19,6 +19,7 @@ package org.apache.ignite.internal.processors.query.h2.twostep; import java.lang.reflect.Constructor; import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -1184,7 +1185,7 @@ public class GridReduceQueryExecutor { * @throws IgniteCheckedException If failed. */ protected Iter(ResultSet data) throws IgniteCheckedException { - super(data, false); + super(data, true); } /** {@inheritDoc} */