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..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 @@ -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,36 @@ 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(); + + @Override + public ParsedQuery get(final String sparqlQuery) throws Throwable { + return parser.parseQuery(sparqlQuery, null); + } + } + static QueryCacheSource queryCacheSource = new QueryCacheSource(); + static Cache makeCache() { + return CacheBuilder.newCache(String.class, ParsedQuery.class).source(queryCacheSource).build(); + } + private static Cache queryCache = makeCache(); + + 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); + + final ParsedQuery query = queryCache.get(sparqlQuery); + 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()) {