Java Code Examples for org.eclipse.rdf4j.common.iteration.Iterations#closeCloseable()

The following examples show how to use org.eclipse.rdf4j.common.iteration.Iterations#closeCloseable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TripleSourceBase.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Convenience method to perform an operation on a {@link RepositoryConnection}. This method takes care for closing
 * resources as well error handling. The resulting iteration has to be supplied to the {@link ResultHolder}.
 *
 * @param operation the {@link ConnectionOperation}
 * @return the resulting iteration
 */
protected <T> CloseableIteration<T, QueryEvaluationException> withConnection(ConnectionOperation<T> operation) {

	ResultHolder<T> resultHolder = new ResultHolder<>();
	RepositoryConnection conn = endpoint.getConnection();
	try {

		operation.perform(conn, resultHolder);

		CloseableIteration<T, QueryEvaluationException> res = resultHolder.get();

		// do not wrap Empty Iterations
		if (res instanceof EmptyIteration) {
			conn.close();
			return res;
		}

		return closeConn(conn, res);

	} catch (Throwable t) {
		// handle all other exception case
		Iterations.closeCloseable(resultHolder.get());
		conn.close();
		throw ExceptionUtil.traceExceptionSource(endpoint, t, "");
	}
}
 
Example 2
Source File: SparqlTripleSource.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> getStatements(
		String preparedQuery, RepositoryConnection conn, BindingSet bindings, FilterValueExpr filterExpr)
{
	
	TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, preparedQuery, null);
	//query.setMaxExecutionTime(10);
	disableInference(query);
	
	CloseableIteration<BindingSet, QueryEvaluationException> res=null;
	try {			
		
		// evaluate the query
		monitorRemoteRequest();
		res = query.evaluate();
		
		// apply filter and/or insert original bindings
		if (filterExpr!=null) {
			if (bindings.size()>0) 
				res = new FilteringInsertBindingsIteration(strategy, filterExpr, bindings, res);
			else
				res = new FilteringIteration(strategy, filterExpr, res);
			if (!res.hasNext()) {
				Iterations.closeCloseable(res);
				return new EmptyIteration<BindingSet, QueryEvaluationException>();
			}
		} else if (bindings.size()>0) {
			res = new InsertBindingsIteration(res, bindings);
		}

		// in order to avoid licking http route while iteration
		return new BufferedCloseableIterator<BindingSet, QueryEvaluationException>(res);
		
	} catch (QueryEvaluationException ex) {
		Iterations.closeCloseable(res);
		throw ExceptionUtil.traceExceptionSourceAndRepair(strategy.getFedXConnection().getEndpointManager(), conn, ex, "Subquery: " + preparedQuery);			
	}
}
 
Example 3
Source File: ProviderUtil.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Checks the connection by submitting a SPARQL SELECT query:
 * 
 * SELECT * WHERE { ?s ?p ?o } LIMIT 1
 * 
 * Throws an exception if the query cannot be evaluated
 * successfully for some reason (indicating that the 
 * endpoint is not ok)
 * 
 * @param repo
 * @throws RepositoryException
 * @throws QueryEvaluationException
 * @throws MalformedQueryException
 */
public static long checkConnectionIfConfigured(Config cfg, Repository repo) {
	
	if (!cfg.isValidateRepositoryConnections()) {
		return 0;
	}
		
	long startTime = System.currentTimeMillis();
	
	RepositoryConnection conn = repo.getConnection();		
	try {
		TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, "SELECT * WHERE { ?s ?p ?o } LIMIT 1");
		TupleQueryResult qRes = null;
		try {
			qRes = query.evaluate();
			if (!qRes.hasNext()) {
				log.warn("No data in provided repository (" + repo + ")");
			}
			while (qRes.hasNext()) qRes.next();
			
		} finally {
			if (qRes != null) {
				Iterations.closeCloseable(qRes);
			}
		}			
	} finally {			
		conn.close();
	}
	return System.currentTimeMillis() - startTime;
}
 
Example 4
Source File: RDFStoreTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private <T> T first(Iteration<T, ?> iter) throws Exception {
	try {
		if (iter.hasNext()) {
			return iter.next();
		}
	} finally {
		Iterations.closeCloseable(iter);
	}

	return null;
}
 
Example 5
Source File: RDFStoreTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private int countElements(Iteration<?, ?> iter) throws Exception {
	int count = 0;

	try {
		while (iter.hasNext()) {
			iter.next();
			count++;
		}
	} finally {
		Iterations.closeCloseable(iter);
	}

	return count;
}
 
Example 6
Source File: SparqlFederationEvalStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluateBoundJoinStatementPattern(
		StatementTupleExpr stmt, List<BindingSet> bindings)
		throws QueryEvaluationException {

	// we can omit the bound join handling
	if (bindings.size() == 1) {
		return evaluate(stmt, bindings.get(0));
	}

	FilterValueExpr filterExpr = null;
	if (stmt instanceof FilterTuple) {
		filterExpr = ((FilterTuple) stmt).getFilterExpr();
	}

	AtomicBoolean isEvaluated = new AtomicBoolean(false);
	String preparedQuery = QueryStringUtil.selectQueryStringBoundJoinVALUES((StatementPattern) stmt, bindings,
			filterExpr, isEvaluated, stmt.getQueryInfo().getDataset());

	CloseableIteration<BindingSet, QueryEvaluationException> result = null;
	try {
		result = evaluateAtStatementSources(preparedQuery, stmt.getStatementSources(), stmt.getQueryInfo());

		// apply filter and/or convert to original bindings
		if (filterExpr != null && !isEvaluated.get()) {
			result = new BoundJoinVALUESConversionIteration(result, bindings); // apply conversion
			result = new FilteringIteration(filterExpr, result, this); // apply filter
			if (!result.hasNext()) {
				return new EmptyIteration<>();
			}
		} else {
			result = new BoundJoinVALUESConversionIteration(result, bindings);
		}

		return result;
	} catch (Throwable t) {
		Iterations.closeCloseable(result);
		throw ExceptionUtil.toQueryEvaluationException(t);
	}
}
 
Example 7
Source File: PathIteration.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void handleClose() throws QueryEvaluationException {
	try {
		super.handleClose();
	} finally {
		Iterations.closeCloseable(currentIter);
	}

}
 
Example 8
Source File: SPARQLMinusIteration.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void handleClose() throws X {
	try {
		super.handleClose();
	} finally {
		Iterations.closeCloseable(getRightArg());
	}
}
 
Example 9
Source File: RepositoryResult.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void handleClose() throws RepositoryException {
	try {
		super.handleClose();
	} finally {
		Iterations.closeCloseable(wrappedIter);
	}
}
 
Example 10
Source File: ConsumingIteration.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void close() throws QueryEvaluationException {
	Iterations.closeCloseable(innerIter);
}
 
Example 11
Source File: CloseableIterationIterator.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void close() throws IOException {
	Iterations.closeCloseable(iteration);
}