Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query caching and prepared statements for SPARQL #520

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions blueprints-sail-graph/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
<artifactId>blueprints-core</artifactId>
<version>${tinkerpop.version}</version>
</dependency>
<!-- query caching -->
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-api</artifactId>
<version>0.20</version>
</dependency>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-core</artifactId>
<version>0.20</version>
<scope>runtime</scope>
</dependency>
<!-- SESAME SUPPORT -->
<dependency>
<groupId>org.openrdf.sesame</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -572,12 +576,36 @@ public GraphQuery query() {
* @throws RuntimeException if an error occurs in the SPARQL query engine
*/
public List<Map<String, Vertex>> executeSparql(String sparqlQuery) throws RuntimeException {
return executeSparql(sparqlQuery, new MapBindingSet());
}

private static class QueryCacheSource implements CacheSource<String, ParsedQuery> {
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<String, ParsedQuery> makeCache() {
return CacheBuilder.newCache(String.class, ParsedQuery.class).source(queryCacheSource).build();
}
private static Cache<String, ParsedQuery> queryCache = makeCache();

public List<Map<String, Vertex>> 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<? extends BindingSet, QueryEvaluationException> results = this.sailConnection.get().evaluate(query.getTupleExpr(), query.getDataset(), new MapBindingSet(), includeInferred);
final CloseableIteration<? extends BindingSet, QueryEvaluationException> results =
this.sailConnection.get().evaluate(
query.getTupleExpr(),
query.getDataset(),
mapBindingSet,
includeInferred);
final List<Map<String, Vertex>> returnList = new ArrayList<Map<String, Vertex>>();
try {
while (results.hasNext()) {
Expand Down