Skip to content

Commit

Permalink
Use QuerySolutionMap in DatasetRdfStoreService
Browse files Browse the repository at this point in the history
  • Loading branch information
michielrogissart-cognizone committed Oct 5, 2023
1 parent 9300943 commit 9f0f645
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 16 deletions.
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,14 @@ subprojects {
}
}


signing {
sign publishing.publications.mavenJava
if (project.hasProperty("publishToMavenCentral")) {
sign publishing.publications.mavenJava
}
}


tasks.withType(Javadoc) {
failOnError false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolutionMap;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.shared.Lock;
import org.apache.jena.update.UpdateAction;
import org.apache.jena.update.UpdateFactory;
import org.apache.jena.update.UpdateRequest;
import zone.cogni.asquare.triplestore.RdfStoreService;
import zone.cogni.libs.jena.utils.DatasetHelper;
import zone.cogni.sem.jena.template.JenaResultSetHandler;

import java.util.function.Supplier;

public class DatasetRdfStoreService implements RdfStoreService {
private final Dataset dataset;

Expand All @@ -40,36 +42,53 @@ public <R> R executeSelectQuery(Query query,
QuerySolutionMap bindings,
JenaResultSetHandler<R> resultSetHandler,
String context) {
try (QueryExecution queryExecution = QueryExecutionFactory.create(QueryFactory.create(query), dataset)) {
return resultSetHandler.handle(queryExecution.execSelect());
}
return executeInLock(Lock.READ, () -> {
traceQuery("Select", query, bindings, context);

try (QueryExecution queryExecution = QueryExecutionFactory.create(query, dataset, bindings)) {
return resultSetHandler.handle(queryExecution.execSelect());
}
});
}

@Override
public boolean executeAskQuery(Query query, QuerySolutionMap bindings) {
try (QueryExecution queryExecution = QueryExecutionFactory.create(QueryFactory.create(query), dataset)) {
return queryExecution.execAsk();
}
return executeInLock(Lock.READ, () -> {
traceQuery("Ask", query, bindings, null);

try (QueryExecution queryExecution = QueryExecutionFactory.create(query, dataset, bindings)) {
return queryExecution.execAsk();
}
});

}

@Override
public Model executeConstructQuery(Query query, QuerySolutionMap bindings) {
try (QueryExecution queryExecution = QueryExecutionFactory.create(QueryFactory.create(query), dataset)) {
return queryExecution.execConstruct();
}
return executeInLock(Lock.READ, () -> {
traceQuery("Construct", query, bindings, null);

try (QueryExecution queryExecution = QueryExecutionFactory.create(query, dataset, bindings)) {
return queryExecution.execConstruct();
}
});
}

@Override
public void executeUpdateQuery(String updateQuery) {
UpdateRequest request = UpdateFactory.create(updateQuery);
UpdateAction.execute(request, dataset);
executeInLock(Lock.WRITE, () -> {
UpdateRequest request = UpdateFactory.create(updateQuery);
UpdateAction.execute(request, dataset);
});
}

@Override
public void replaceGraph(String graphUri, Model model) {
dataset.removeNamedModel(graphUri)
.getNamedModel(graphUri)
.add(model);
executeInLock(Lock.WRITE, () -> {
dataset.removeNamedModel(graphUri)
.getNamedModel(graphUri)
.add(model);
});
}

@Override
Expand All @@ -82,6 +101,34 @@ public void delete() {
dataset.close();
}

private void executeInLock(boolean lock, Runnable toExecute) {
dataset.getLock().enterCriticalSection(lock);
try {
toExecute.run();
}
finally {
dataset.getLock().leaveCriticalSection();
}
}

private <T> T executeInLock(boolean lock, Supplier<T> toExecute) {
dataset.getLock().enterCriticalSection(lock);
try {
return toExecute.get();
}
finally {
dataset.getLock().leaveCriticalSection();
}
}

private void traceQuery(String queryType, Query query, QuerySolutionMap bindings, String context) {
if (log.isTraceEnabled()) log.trace("{} {} - {} \n{}",
queryType,
context == null ? "" : "--- " + context + " --- ",
bindings == null ? "{}" : bindings,
query);
}

/**
* Create and return an in memory dataset copy.
*
Expand Down

0 comments on commit 9f0f645

Please sign in to comment.