From 875556e83a2b812bab90533f8f599bbf421437c2 Mon Sep 17 00:00:00 2001 From: Dirk Groeneveld Date: Wed, 7 Jan 2015 16:00:10 -0800 Subject: [PATCH 1/2] Added hack version of prepared statements to SailGraph --- blueprints-sail-graph/pom.xml | 12 +++++ .../blueprints/impls/sail/SailGraph.java | 48 +++++++++++++++++-- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/blueprints-sail-graph/pom.xml b/blueprints-sail-graph/pom.xml index 07d03e89..b211a48d 100644 --- a/blueprints-sail-graph/pom.xml +++ b/blueprints-sail-graph/pom.xml @@ -17,6 +17,18 @@ blueprints-core ${tinkerpop.version} + + + org.cache2k + cache2k-api + 0.20 + + + org.cache2k + cache2k-core + 0.20 + runtime + org.openrdf.sesame diff --git a/blueprints-sail-graph/src/main/java/com/tinkerpop/blueprints/impls/sail/SailGraph.java b/blueprints-sail-graph/src/main/java/com/tinkerpop/blueprints/impls/sail/SailGraph.java index e4a912e5..b94af56a 100644 --- a/blueprints-sail-graph/src/main/java/com/tinkerpop/blueprints/impls/sail/SailGraph.java +++ b/blueprints-sail-graph/src/main/java/com/tinkerpop/blueprints/impls/sail/SailGraph.java @@ -13,6 +13,9 @@ import com.tinkerpop.blueprints.util.StringFactory; import info.aduna.iteration.CloseableIteration; import org.apache.log4j.PropertyConfigurator; +import org.cache2k.Cache; +import org.cache2k.CacheBuilder; +import org.cache2k.CacheSource; import org.openrdf.model.Literal; import org.openrdf.model.Namespace; import org.openrdf.model.Resource; @@ -50,6 +53,7 @@ import java.util.List; import java.util.Map; import java.util.UUID; +import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Logger; /** @@ -572,12 +576,50 @@ public GraphQuery query() { * @throws RuntimeException if an error occurs in the SPARQL query engine */ public List> executeSparql(String sparqlQuery) throws RuntimeException { + return executeSparql(sparqlQuery, new MapBindingSet()); + } + + private static class QueryCacheSource implements CacheSource { + private final SPARQLParser parser = new SPARQLParser(); + private AtomicInteger parseCount = new AtomicInteger(); + + @Override + public ParsedQuery get(final String sparqlQuery) throws Throwable { + parseCount.incrementAndGet(); + return parser.parseQuery(sparqlQuery, null); + } + + public int cacheMissCount() { + return parseCount.get(); + } + } + static QueryCacheSource queryCacheSource = new QueryCacheSource(); + static Cache makeCache() { + return CacheBuilder.newCache(String.class, ParsedQuery.class).source(queryCacheSource).build(); + } + private static Cache queryCache = makeCache(); + + private long showCacheMessage = 0; + public List> executeSparql(String sparqlQuery, final MapBindingSet mapBindingSet) throws RuntimeException { try { sparqlQuery = getPrefixes() + sparqlQuery; - final SPARQLParser parser = new SPARQLParser(); - final ParsedQuery query = parser.parseQuery(sparqlQuery, null); + + // try to get the parsed query from the cache + final ParsedQuery query = queryCache.get(sparqlQuery); + + final long t = System.currentTimeMillis(); + if(t > showCacheMessage) { + LOGGER.info(String.format("Cache misses: %d", queryCacheSource.cacheMissCount())); + showCacheMessage = t + 10000; + } + boolean includeInferred = false; - final CloseableIteration results = this.sailConnection.get().evaluate(query.getTupleExpr(), query.getDataset(), new MapBindingSet(), includeInferred); + final CloseableIteration results = + this.sailConnection.get().evaluate( + query.getTupleExpr(), + query.getDataset(), + mapBindingSet, + includeInferred); final List> returnList = new ArrayList>(); try { while (results.hasNext()) { From 737c55734c4e3f50c64ae9a0feef6ac7d9bd45ae Mon Sep 17 00:00:00 2001 From: Dirk Groeneveld Date: Thu, 22 Jan 2015 15:05:44 -0800 Subject: [PATCH 2/2] Remove logging output --- .../tinkerpop/blueprints/impls/sail/SailGraph.java | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/blueprints-sail-graph/src/main/java/com/tinkerpop/blueprints/impls/sail/SailGraph.java b/blueprints-sail-graph/src/main/java/com/tinkerpop/blueprints/impls/sail/SailGraph.java index b94af56a..7177ec4f 100644 --- a/blueprints-sail-graph/src/main/java/com/tinkerpop/blueprints/impls/sail/SailGraph.java +++ b/blueprints-sail-graph/src/main/java/com/tinkerpop/blueprints/impls/sail/SailGraph.java @@ -581,17 +581,11 @@ public List> executeSparql(String sparqlQuery) throws Runtim private static class QueryCacheSource implements CacheSource { private final SPARQLParser parser = new SPARQLParser(); - private AtomicInteger parseCount = new AtomicInteger(); @Override public ParsedQuery get(final String sparqlQuery) throws Throwable { - parseCount.incrementAndGet(); return parser.parseQuery(sparqlQuery, null); } - - public int cacheMissCount() { - return parseCount.get(); - } } static QueryCacheSource queryCacheSource = new QueryCacheSource(); static Cache makeCache() { @@ -599,20 +593,12 @@ static Cache makeCache() { } private static Cache queryCache = makeCache(); - private long showCacheMessage = 0; public List> executeSparql(String sparqlQuery, final MapBindingSet mapBindingSet) throws RuntimeException { try { sparqlQuery = getPrefixes() + sparqlQuery; - // try to get the parsed query from the cache final ParsedQuery query = queryCache.get(sparqlQuery); - final long t = System.currentTimeMillis(); - if(t > showCacheMessage) { - LOGGER.info(String.format("Cache misses: %d", queryCacheSource.cacheMissCount())); - showCacheMessage = t + 10000; - } - boolean includeInferred = false; final CloseableIteration results = this.sailConnection.get().evaluate(