You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by sb...@apache.org on 2015/11/24 08:11:09 UTC

[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 <sb...@gridgain.com>
Authored: Tue Nov 24 10:10:28 2015 +0300
Committer: sboikov <sb...@gridgain.com>
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<SmallLRUCache<String, PreparedStatement>> stmtCache =
-        new ThreadLocal<SmallLRUCache<String, PreparedStatement>>() {
-            @Override protected SmallLRUCache<String, PreparedStatement> initialValue() {
-                return SmallLRUCache.newInstance(256);
+    private final ThreadLocal<StatementCache<String, PreparedStatement>> stmtCache =
+        new ThreadLocal<StatementCache<String, PreparedStatement>>() {
+            @Override protected StatementCache<String, PreparedStatement> 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<String, PreparedStatement> cache = stmtCache.get();
+            StatementCache<String, PreparedStatement> 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<K, V> extends LinkedHashMap<K, V> {
+        /** */
+        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<K, V> 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} */