use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.
the class RepositoryFederatedService method select.
/**
* Evaluate the provided sparqlQueryString at the initialized {@link Repository} of this
* {@link FederatedService}. Insert bindings into SELECT query and evaluate
*/
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> select(Service service, Set<String> projectionVars, BindingSet bindings, String baseUri) throws QueryEvaluationException {
try {
String sparqlQueryString = service.getSelectQueryString(projectionVars);
TupleQuery query = getConnection().prepareTupleQuery(QueryLanguage.SPARQL, sparqlQueryString, baseUri);
Iterator<Binding> bIter = bindings.iterator();
while (bIter.hasNext()) {
Binding b = bIter.next();
if (service.getServiceVars().contains(b.getName()))
query.setBinding(b.getName(), b.getValue());
}
TupleQueryResult res = query.evaluate();
// insert original bindings again
InsertBindingSetCursor result = new InsertBindingSetCursor(res, bindings);
if (service.isSilent())
return new SilentIteration(result);
else
return result;
} catch (MalformedQueryException e) {
throw new QueryEvaluationException(e);
} catch (RepositoryException e) {
throw new QueryEvaluationException("Repository for endpoint " + rep.toString() + " could not be initialized.", e);
}
}
use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.
the class RepositoryFederatedService method evaluateInternal.
/**
* Evaluate the SPARQL query that can be constructed from the SERVICE node at the initialized
* {@link Repository} of this {@link FederatedService}. Use specified bindings as constraints to the
* query. Try to evaluate using BINDINGS clause, if this yields an exception fall back to the naive
* implementation. This method deals with SILENT SERVICEs.
*/
protected CloseableIteration<BindingSet, QueryEvaluationException> evaluateInternal(Service service, CloseableIteration<BindingSet, QueryEvaluationException> bindings, String baseUri) throws QueryEvaluationException {
// materialize all bindings (to allow for fallback in case of errors)
// note that this may be blocking depending on the underlying iterator
List<BindingSet> allBindings = new LinkedList<BindingSet>();
while (bindings.hasNext()) {
allBindings.add(bindings.next());
}
if (allBindings.size() == 0) {
return new EmptyIteration<BindingSet, QueryEvaluationException>();
}
// projection vars
Set<String> projectionVars = new HashSet<String>(service.getServiceVars());
projectionVars.removeAll(allBindings.get(0).getBindingNames());
// below we need to take care for SILENT services
CloseableIteration<BindingSet, QueryEvaluationException> result = null;
try {
// fallback to simple evaluation (just a single binding)
if (allBindings.size() == 1) {
result = select(service, projectionVars, allBindings.get(0), baseUri);
result = service.isSilent() ? new SilentIteration(result) : result;
return result;
}
// To be able to insert the input bindings again later on, we need some
// means to identify the row of each binding. hence, we use an
// additional
// projection variable, which is also passed in the BINDINGS clause
// with the value of the actual row. The value corresponds to the index
// of the binding in the index list
projectionVars.add("__rowIdx");
String queryString = service.getSelectQueryString(projectionVars);
List<String> relevantBindingNames = getRelevantBindingNames(allBindings, service.getServiceVars());
if (relevantBindingNames.size() != 0) {
// append the VALUES clause to the query
queryString += buildVALUESClause(allBindings, relevantBindingNames);
}
TupleQuery query = getConnection().prepareTupleQuery(QueryLanguage.SPARQL, queryString, baseUri);
TupleQueryResult res = null;
// TODO how to retrieve max query value
query.setMaxQueryTime(60);
// from actual setting?
res = query.evaluate();
if (relevantBindingNames.size() == 0)
// cross
result = new SPARQLCrossProductIteration(res, allBindings);
else
// product
// common
result = new ServiceJoinConversionIteration(res, allBindings);
// join
result = service.isSilent() ? new SilentIteration(result) : result;
return result;
} catch (RepositoryException e) {
Iterations.closeCloseable(result);
if (service.isSilent())
return new CollectionIteration<BindingSet, QueryEvaluationException>(allBindings);
throw new QueryEvaluationException("Repository for endpoint " + rep.toString() + " could not be initialized.", e);
} catch (MalformedQueryException e) {
// this exception must not be silenced, bug in our code
throw new QueryEvaluationException(e);
} catch (QueryEvaluationException e) {
Iterations.closeCloseable(result);
if (service.isSilent())
return new CollectionIteration<BindingSet, QueryEvaluationException>(allBindings);
throw e;
} catch (RuntimeException e) {
Iterations.closeCloseable(result);
// QueryEval) if silent
if (service.isSilent())
return new CollectionIteration<BindingSet, QueryEvaluationException>(allBindings);
throw e;
}
}
use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.
the class RepositoryFederatedService method ask.
/**
* Evaluate the provided sparqlQueryString at the initialized {@link Repository} of this
* {@link FederatedService}. Insert bindings, send ask query and return final result
*/
@Override
public boolean ask(Service service, BindingSet bindings, String baseUri) throws QueryEvaluationException {
try {
String sparqlQueryString = service.getAskQueryString();
BooleanQuery query = getConnection().prepareBooleanQuery(QueryLanguage.SPARQL, sparqlQueryString, baseUri);
Iterator<Binding> bIter = bindings.iterator();
while (bIter.hasNext()) {
Binding b = bIter.next();
if (service.getServiceVars().contains(b.getName()))
query.setBinding(b.getName(), b.getValue());
}
return query.evaluate();
} catch (MalformedQueryException e) {
throw new QueryEvaluationException(e);
} catch (RepositoryException e) {
throw new QueryEvaluationException("Repository for endpoint " + rep.toString() + " could not be initialized.", e);
}
}
use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.
the class HTTPRepository method useCompatibleMode.
/**
* Verify if transaction handling should be done in backward-compatible mode (this is the case when
* communicating with an older Sesame Server).
*
* @return <code>true</code> if the Server does not support the extended transaction protocol,
* <code>false</code> otherwise.
* @throws RepositoryException
* if something went wrong while querying the server for the protocol version.
*/
boolean useCompatibleMode() throws RepositoryException {
Boolean result = compatibleMode;
if (result == null) {
synchronized (this) {
result = compatibleMode;
if (result == null) {
try (RDF4JProtocolSession client = createHTTPClient()) {
final String serverProtocolVersion = client.getServerProtocol();
// protocol version 7 supports the new transaction
// handling. If the server is older, we need to run in
// backward-compatible mode.
result = compatibleMode = (Integer.parseInt(serverProtocolVersion) < 7);
} catch (NumberFormatException e) {
throw new RepositoryException("could not read protocol version from server: ", e);
} catch (IOException e) {
throw new RepositoryException("could not read protocol version from server: ", e);
}
}
}
}
return result;
}
use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.
the class HTTPRepository method isWritable.
@Override
public boolean isWritable() throws RepositoryException {
if (!isInitialized()) {
throw new IllegalStateException("HTTPRepository not initialized.");
}
boolean isWritable = false;
try (RDF4JProtocolSession client = createHTTPClient()) {
final String repositoryURL = client.getRepositoryURL();
final TupleQueryResult repositoryList = client.getRepositoryList();
try {
while (repositoryList.hasNext()) {
final BindingSet bindingSet = repositoryList.next();
final Value uri = bindingSet.getValue("uri");
if (uri != null && uri.stringValue().equals(repositoryURL)) {
isWritable = Literals.getBooleanValue(bindingSet.getValue("writable"), false);
break;
}
}
} finally {
repositoryList.close();
}
} catch (QueryEvaluationException e) {
throw new RepositoryException(e);
} catch (IOException e) {
throw new RepositoryException(e);
}
return isWritable;
}
Aggregations