camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [5/9] camel git commit: Caching templates.
Date Sat, 09 Jan 2016 10:44:48 GMT
Caching templates.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c8cbcde3
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c8cbcde3
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c8cbcde3

Branch: refs/heads/master
Commit: c8cbcde397dde05a9bba18fa79cd81e8aa984708
Parents: f0738bb
Author: Sami Nurminen <snurmine@gmail.com>
Authored: Wed Jan 6 22:25:38 2016 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sat Jan 9 10:59:47 2016 +0100

----------------------------------------------------------------------
 .../sql/stored/SqlStoredComponent.java          |  6 ++--
 .../component/sql/stored/SqlStoredEndpoint.java | 13 ++++----
 .../component/sql/stored/SqlStoredProducer.java | 13 +++++---
 .../template/TemplateStoredProcedure.java       |  7 +++--
 .../TemplateStoredProcedureFactory.java         | 32 +++++++++++++++++---
 .../sql/stored/template/grammar/sspt.jj         |  2 ++
 .../camel/component/sql/stored/ParserTest.java  |  2 +-
 .../sql/stored/TemplateStoredProcedureTest.java |  9 ++++--
 8 files changed, 58 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/c8cbcde3/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredComponent.java
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredComponent.java
index 9fdb45d..20aedf1 100644
--- a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredComponent.java
+++ b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredComponent.java
@@ -5,6 +5,7 @@ import org.apache.camel.Endpoint;
 import org.apache.camel.component.sql.stored.template.TemplateStoredProcedureFactory;
 import org.apache.camel.impl.UriEndpointComponent;
 import org.apache.camel.util.CamelContextHelper;
+import org.springframework.jdbc.core.JdbcTemplate;
 
 import javax.sql.DataSource;
 import java.util.Map;
@@ -20,9 +21,6 @@ public class SqlStoredComponent extends UriEndpointComponent {
         super(SqlStoredEndpoint.class);
     }
 
-
-    TemplateStoredProcedureFactory templateStoredProcedureFactory = new TemplateStoredProcedureFactory();
-
     public SqlStoredComponent(CamelContext context, Class<? extends Endpoint> endpointClass)
{
         super(context, endpointClass);
     }
@@ -49,7 +47,7 @@ public class SqlStoredComponent extends UriEndpointComponent {
         }
 
 
-        return new SqlStoredEndpoint(templateStoredProcedureFactory, target, remaining);
+        return new SqlStoredEndpoint(new JdbcTemplate(target), remaining);
     }
 
     public DataSource getDataSource() {

http://git-wip-us.apache.org/repos/asf/camel/blob/c8cbcde3/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredEndpoint.java
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredEndpoint.java
index 844e9e5..ae7348c 100644
--- a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredEndpoint.java
+++ b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredEndpoint.java
@@ -7,8 +7,7 @@ import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.spi.UriPath;
 import org.apache.camel.util.UnsafeUriCharactersEncoder;
-
-import javax.sql.DataSource;
+import org.springframework.jdbc.core.JdbcTemplate;
 
 @UriEndpoint(scheme = "sql-stored", title = "SQL stored", syntax = "sql-stored:template",
label = "database,sql")
 public class SqlStoredEndpoint extends DefaultPollingEndpoint {
@@ -19,19 +18,19 @@ public class SqlStoredEndpoint extends DefaultPollingEndpoint {
 
     private final TemplateStoredProcedureFactory templateStoredProcedureFactory;
 
-    private final DataSource dataSource;
+    private final JdbcTemplate jdbcTemplate;
 
 
-    public SqlStoredEndpoint(TemplateStoredProcedureFactory templateStoredProcedureFactory,
DataSource dataSource,
+    public SqlStoredEndpoint(JdbcTemplate jdbcTemplate,
                              String template) {
-        this.templateStoredProcedureFactory = templateStoredProcedureFactory;
-        this.dataSource = dataSource;
+        this.templateStoredProcedureFactory = new TemplateStoredProcedureFactory(jdbcTemplate);
+        this.jdbcTemplate = jdbcTemplate;
         this.template = template;
     }
 
     @Override
     public Producer createProducer() throws Exception {
-        return new SqlStoredProducer(this, templateStoredProcedureFactory.createFromString(template,
dataSource));
+        return new SqlStoredProducer(this, template, templateStoredProcedureFactory);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/c8cbcde3/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredProducer.java
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredProducer.java
index 9d2210d..f306a4c 100644
--- a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredProducer.java
+++ b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredProducer.java
@@ -3,6 +3,7 @@ package org.apache.camel.component.sql.stored;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.component.sql.stored.template.TemplateStoredProcedure;
+import org.apache.camel.component.sql.stored.template.TemplateStoredProcedureFactory;
 import org.apache.camel.impl.DefaultProducer;
 
 /**
@@ -10,16 +11,20 @@ import org.apache.camel.impl.DefaultProducer;
  */
 public class SqlStoredProducer extends DefaultProducer {
 
-    final TemplateStoredProcedure templateStoredProcedure;
+    final TemplateStoredProcedureFactory templateStoredProcedureFactory;
 
-    public SqlStoredProducer(Endpoint endpoint, TemplateStoredProcedure templateStoredProcedure)
{
+    final TemplateStoredProcedure defaultTemplateStoredProcedure;
+
+    public SqlStoredProducer(Endpoint endpoint, String template, TemplateStoredProcedureFactory
+            templateStoredProcedureFactory) {
         super(endpoint);
-        this.templateStoredProcedure = templateStoredProcedure;
+        this.defaultTemplateStoredProcedure = templateStoredProcedureFactory.createFromString(template);
+        this.templateStoredProcedureFactory = templateStoredProcedureFactory;
     }
 
     @Override
     public void process(Exchange exchange) throws Exception {
-        templateStoredProcedure.execute(exchange);
+        this.defaultTemplateStoredProcedure.execute(exchange);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/c8cbcde3/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/TemplateStoredProcedure.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/TemplateStoredProcedure.java
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/TemplateStoredProcedure.java
index 7c1f415..44103d8 100644
--- a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/TemplateStoredProcedure.java
+++ b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/TemplateStoredProcedure.java
@@ -4,6 +4,7 @@ import org.apache.camel.Exchange;
 import org.apache.camel.component.sql.stored.template.ast.InputParameter;
 import org.apache.camel.component.sql.stored.template.ast.OutParameter;
 import org.apache.camel.component.sql.stored.template.ast.Template;
+import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.jdbc.core.SqlOutParameter;
 import org.springframework.jdbc.core.SqlParameter;
 import org.springframework.jdbc.object.StoredProcedure;
@@ -17,9 +18,9 @@ public class TemplateStoredProcedure extends StoredProcedure {
 
     Template template;
 
-    public TemplateStoredProcedure(DataSource dataSource, Template template) {
+    public TemplateStoredProcedure(JdbcTemplate jdbcTemplate, Template template) {
         this.template = template;
-        setDataSource(dataSource);
+        setDataSource(jdbcTemplate.getDataSource());
 
         setSql(template.getProcedureName());
 
@@ -33,9 +34,11 @@ public class TemplateStoredProcedure extends StoredProcedure {
         }
 
         compile();
+
     }
 
 
+
     public void execute(Exchange exchange) {
 
         Map<String, Object> params = new HashMap<>();

http://git-wip-us.apache.org/repos/asf/camel/blob/c8cbcde3/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/TemplateStoredProcedureFactory.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/TemplateStoredProcedureFactory.java
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/TemplateStoredProcedureFactory.java
index 2229a4e..947a6a5 100644
--- a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/TemplateStoredProcedureFactory.java
+++ b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/TemplateStoredProcedureFactory.java
@@ -4,25 +4,47 @@ import org.apache.camel.component.sql.stored.template.ast.ParseRuntimeException;
 import org.apache.camel.component.sql.stored.template.ast.Template;
 import org.apache.camel.component.sql.stored.template.generated.ParseException;
 import org.apache.camel.component.sql.stored.template.generated.SSPTParser;
+import org.apache.camel.util.LRUCache;
+import org.springframework.jdbc.core.JdbcTemplate;
 
 import javax.sql.DataSource;
 import java.io.StringReader;
 
 public class TemplateStoredProcedureFactory {
 
+    final int TEMPLATE_CACHE_DEFAULT_SIZE = 200;
+
+    final JdbcTemplate jdbcTemplate;
+
+    LRUCache<String, TemplateStoredProcedure> templateCache = new LRUCache<String,
TemplateStoredProcedure>(200);
+
+
+    public TemplateStoredProcedureFactory(JdbcTemplate jdbcTemplate) {
+        this.jdbcTemplate = jdbcTemplate;
+    }
+
+    public TemplateStoredProcedure createFromString(String string) {
+        TemplateStoredProcedure fromCache = templateCache.get(string);
+
+        if (fromCache != null) {
+            return fromCache;
+        }
 
-    public TemplateStoredProcedure createFromString(String string, DataSource dataSource)
 {
         Template sptpRootNode = parseTemplate(string);
-        return new TemplateStoredProcedure(dataSource, sptpRootNode);
+        TemplateStoredProcedure ret = new TemplateStoredProcedure(jdbcTemplate, sptpRootNode);
+
+        templateCache.put(string, ret);
+
+        return ret;
 
     }
 
-    public Template parseTemplate(String template)  {
+    public Template parseTemplate(String template) {
         try {
-            SSPTParser parser = new SSPTParser(new StringReader(template));
 
+            SSPTParser parser = new SSPTParser(new StringReader(template));
             return validate(parser.parse());
-        }catch(ParseException parseException) {
+        } catch (ParseException parseException) {
             throw new ParseRuntimeException(parseException);
         }
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/c8cbcde3/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/grammar/sspt.jj
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/grammar/sspt.jj
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/grammar/sspt.jj
index edd9f17..e43d17b 100644
--- a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/grammar/sspt.jj
+++ b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/template/grammar/sspt.jj
@@ -2,6 +2,8 @@
 
 options {
     STATIC = false;
+    FORCE_LA_CHECK = true;
+    SANITY_CHECK = true;
 }
 
 

http://git-wip-us.apache.org/repos/asf/camel/blob/c8cbcde3/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ParserTest.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ParserTest.java
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ParserTest.java
index 81b6b5a..9982138 100644
--- a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ParserTest.java
+++ b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/ParserTest.java
@@ -17,7 +17,7 @@ import java.sql.Types;
 
 public class ParserTest extends CamelTestSupport {
 
-    TemplateStoredProcedureFactory parser = new TemplateStoredProcedureFactory();
+    TemplateStoredProcedureFactory parser = new TemplateStoredProcedureFactory(null);
 
 
     @Test

http://git-wip-us.apache.org/repos/asf/camel/blob/c8cbcde3/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TemplateStoredProcedureTest.java
----------------------------------------------------------------------
diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TemplateStoredProcedureTest.java
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TemplateStoredProcedureTest.java
index 73e9711..3b06390 100644
--- a/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TemplateStoredProcedureTest.java
+++ b/components/camel-sql/src/test/java/org/apache/camel/component/sql/stored/TemplateStoredProcedureTest.java
@@ -16,7 +16,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
 
 public class TemplateStoredProcedureTest extends CamelTestSupport {
 
-    TemplateStoredProcedureFactory parser = new TemplateStoredProcedureFactory();
+    TemplateStoredProcedureFactory parser;
 
     private EmbeddedDatabase db;
     private JdbcTemplate jdbcTemplate;
@@ -28,13 +28,16 @@ public class TemplateStoredProcedureTest extends CamelTestSupport {
 
         jdbcTemplate = new JdbcTemplate(db);
 
+
+        parser = new TemplateStoredProcedureFactory(jdbcTemplate);
+
         super.setUp();
     }
 
 
     @Test
     public void shouldExecuteStoredProcedure() {
-        TemplateStoredProcedure sp = new TemplateStoredProcedure(db, parser.parseTemplate("ADDNUMBERS"
+
+        TemplateStoredProcedure sp = new TemplateStoredProcedure(jdbcTemplate, parser.parseTemplate("ADDNUMBERS"
+
                 "(INTEGER ${header.v1},INTEGER ${header.v2},OUT INTEGER resultofsum)"));
 
         Exchange exchange = createExchangeWithBody(null);
@@ -52,7 +55,7 @@ public class TemplateStoredProcedureTest extends CamelTestSupport {
 
     @Test
     public void shouldExecuteNilacidProcedure() {
-        TemplateStoredProcedure sp = new TemplateStoredProcedure(db, parser.parseTemplate("NILADIC"
+
+        TemplateStoredProcedure sp = new TemplateStoredProcedure(jdbcTemplate, parser.parseTemplate("NILADIC"
+
                 "()"));
 
         Exchange exchange = createExchangeWithBody(null);


Mime
View raw message