changed the strategy, becuase Query objects cannot be shared accross different repository connections
Project: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/commit/7150b1a7 Tree: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/tree/7150b1a7 Diff: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/diff/7150b1a7 Branch: refs/heads/master Commit: 7150b1a73ad710757e24ecc94513b8d5825d3441 Parents: b92d74f Author: Sergio Fernández <[email protected]> Authored: Fri Apr 5 14:02:21 2013 +0200 Committer: Sergio Fernández <[email protected]> Committed: Fri Apr 5 14:02:21 2013 +0200 ---------------------------------------------------------------------- .../platform/sparql/api/sparql/SparqlService.java | 83 ++------------- .../sparql/services/sparql/SparqlServiceImpl.java | 63 ++++++++---- .../sparql/webservices/SparqlWebService.java | 2 +- 3 files changed, 53 insertions(+), 95 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/7150b1a7/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/api/sparql/SparqlService.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/api/sparql/SparqlService.java b/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/api/sparql/SparqlService.java index 4fc372d..3a226b3 100644 --- a/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/api/sparql/SparqlService.java +++ b/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/api/sparql/SparqlService.java @@ -17,26 +17,23 @@ */ package org.apache.marmotta.platform.sparql.api.sparql; +import java.io.OutputStream; +import java.util.List; +import java.util.Map; + +import org.apache.marmotta.platform.core.exception.InvalidArgumentException; import org.apache.marmotta.platform.core.exception.MarmottaException; import org.apache.marmotta.platform.sparql.services.sparqlio.rdf.SPARQLGraphResultWriter; -import org.apache.marmotta.platform.core.exception.InvalidArgumentException; import org.openrdf.model.Value; -import org.openrdf.query.BooleanQuery; -import org.openrdf.query.GraphQuery; import org.openrdf.query.MalformedQueryException; import org.openrdf.query.Query; import org.openrdf.query.QueryEvaluationException; import org.openrdf.query.QueryLanguage; -import org.openrdf.query.TupleQuery; import org.openrdf.query.UpdateExecutionException; import org.openrdf.query.resultio.BooleanQueryResultWriter; import org.openrdf.query.resultio.TupleQueryResultWriter; import org.openrdf.repository.RepositoryException; -import java.io.OutputStream; -import java.util.List; -import java.util.Map; - /** * Add file description here! * <p/> @@ -77,72 +74,12 @@ public interface SparqlService { * Evaluate a SPARQL query on the KiWi TripleStore. Writes the query results * to the stream passed in the format requested. * - * @param query - * @param output - * @param format - * @throws QueryEvaluationException - */ - void query(Query query, OutputStream output, String format) throws QueryEvaluationException; - - /** - * Evaluate a SPARQL tuple query on the KiWi TripleStore. Writes the query results - * to the writer passed. - * - * @param tuple query - * @param writer the writer to use to write results - * @throws QueryEvaluationException + * @param query query + * @param output strem to write + * @param format mimetype + * @throws MarmottaException */ - void query(TupleQuery query, TupleQueryResultWriter writer) throws QueryEvaluationException; - - /** - * Evaluate a SPARQL tuple query on the KiWi TripleStore. Writes the query results - * to the stream passed in the format requested. - * - * @param query - * @param output - * @param format - */ - void query(TupleQuery query, OutputStream output, String format) throws QueryEvaluationException; - - /** - * Evaluate a SPARQL boolean query on the KiWi TripleStore. Writes the query results - * to the writer passed. - * - * @param query boolean query - * @param writer the writer to use to write results - * @throws QueryEvaluationException - */ - void query(BooleanQuery query, BooleanQueryResultWriter writer) throws QueryEvaluationException; - - /** - * Evaluate a SPARQL boolean query on the KiWi TripleStore. Writes the query results - * to the stream passed in the format requested. - * - * @param query - * @param output - * @param format - */ - void query(BooleanQuery query, OutputStream output, String format) throws QueryEvaluationException; - - /** - * Evaluate a SPARQL graph query on the KiWi TripleStore. Writes the query results - * to the writer passed. - * - * @param query graph query - * @param writer the writer to use to write results - * @throws QueryEvaluationException - */ - void query(GraphQuery query, SPARQLGraphResultWriter writer) throws QueryEvaluationException; - - /** - * Evaluate a SPARQL graph query on the KiWi TripleStore. Writes the query results - * to the stream passed in the format requested. - * - * @param query - * @param output - * @param format - */ - void query(GraphQuery query, OutputStream output, String format) throws QueryEvaluationException; + void query(Query query, OutputStream output, String format) throws MarmottaException; /** * Evaluate a SPARQL ASK query on the KiWi TripleStore http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/7150b1a7/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/services/sparql/SparqlServiceImpl.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/services/sparql/SparqlServiceImpl.java b/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/services/sparql/SparqlServiceImpl.java index bfefd4d..93f0bff 100644 --- a/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/services/sparql/SparqlServiceImpl.java +++ b/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/services/sparql/SparqlServiceImpl.java @@ -127,20 +127,46 @@ public class SparqlServiceImpl implements SparqlService { } @Override - public void query(Query query, OutputStream output, String format) throws QueryEvaluationException { - if (query instanceof TupleQuery) { - query((TupleQuery)query, output, format); - } else if (query instanceof BooleanQuery) { - query((BooleanQuery)query, output, format); - } else if (query instanceof GraphQuery) { - query((GraphQuery)query, output, format); - } else { - throw new InvalidArgumentException("SPARQL query type " + query.getClass() + " not supported!"); + public void query(Query query, OutputStream output, String format) throws MarmottaException { + long start = System.currentTimeMillis(); + + log.debug("executing SPARQL query:\n{}", query); + + try { + RepositoryConnection connection = sesameService.getConnection(); + try { + connection.begin(); + Query sparqlQuery = connection.prepareQuery(QueryLanguage.SPARQL, query.toString()); + + if (sparqlQuery instanceof TupleQuery) { + query((TupleQuery)sparqlQuery, output, format); + } else if (sparqlQuery instanceof BooleanQuery) { + query((BooleanQuery)sparqlQuery, output, format); + } else if (sparqlQuery instanceof GraphQuery) { + query((GraphQuery)sparqlQuery, output, format); + } else { + throw new InvalidArgumentException("SPARQL query type " + sparqlQuery.getClass() + " not supported!"); + } + + connection.commit(); + } finally { + connection.close(); + } + } catch(RepositoryException e) { + log.error("error while getting repository connection: {}", e); + throw new MarmottaException("error while getting repository connection", e); + } catch (QueryEvaluationException e) { + log.error("error while evaluating query: {}", e); + throw new MarmottaException("error while writing query result in format ", e); + } catch (MalformedQueryException e) { + log.error("error because malformed query: {}", e); + throw new MarmottaException("error because malformed query result in format ", e); } + + log.debug("SPARQL execution took {}ms", System.currentTimeMillis()-start); } - @Override - public void query(TupleQuery query, TupleQueryResultWriter writer) throws QueryEvaluationException { + private void query(TupleQuery query, TupleQueryResultWriter writer) throws QueryEvaluationException { try { query.evaluate(writer); } catch (TupleQueryResultHandlerException e) { @@ -148,13 +174,11 @@ public class SparqlServiceImpl implements SparqlService { } } - @Override - public void query(TupleQuery query, OutputStream output, String format) throws QueryEvaluationException { + private void query(TupleQuery query, OutputStream output, String format) throws QueryEvaluationException { query(query, SparqlWritersHelper.getTupleResultWriter(format, output)); } - @Override - public void query(BooleanQuery query, BooleanQueryResultWriter writer) throws QueryEvaluationException { + private void query(BooleanQuery query, BooleanQueryResultWriter writer) throws QueryEvaluationException { try { writer.write(query.evaluate()); } catch (IOException e) { @@ -162,13 +186,11 @@ public class SparqlServiceImpl implements SparqlService { } } - @Override - public void query(BooleanQuery query, OutputStream output, String format) throws QueryEvaluationException { + private void query(BooleanQuery query, OutputStream output, String format) throws QueryEvaluationException { query(query, SparqlWritersHelper.getBooleanResultWriter(format, output)); } - @Override - public void query(GraphQuery query, SPARQLGraphResultWriter writer) throws QueryEvaluationException { + private void query(GraphQuery query, SPARQLGraphResultWriter writer) throws QueryEvaluationException { try { writer.write(query.evaluate()); } catch (IOException e) { @@ -176,8 +198,7 @@ public class SparqlServiceImpl implements SparqlService { } } - @Override - public void query(GraphQuery query, OutputStream output, String format) throws QueryEvaluationException { + private void query(GraphQuery query, OutputStream output, String format) throws QueryEvaluationException { query(query, SparqlWritersHelper.getGraphResultWriter(format, output)); } http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/7150b1a7/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/webservices/SparqlWebService.java ---------------------------------------------------------------------- diff --git a/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/webservices/SparqlWebService.java b/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/webservices/SparqlWebService.java index 58a1ee7..56dd26a 100644 --- a/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/webservices/SparqlWebService.java +++ b/platform/marmotta-sparql/src/main/java/org/apache/marmotta/platform/sparql/webservices/SparqlWebService.java @@ -515,7 +515,7 @@ public class SparqlWebService { public void write(OutputStream output) throws IOException, WebApplicationException { try { sparqlService.query(query, output, resultType); - } catch (QueryEvaluationException e) { + } catch (MarmottaException e) { throw new WebApplicationException(e.getCause(), Response.status(Response.Status.BAD_REQUEST).entity(WebServiceUtil.jsonErrorResponse(e)).build()); } }
