org.eclipse.rdf4j.common.iteration.CloseableIteration Java Examples

The following examples show how to use org.eclipse.rdf4j.common.iteration.CloseableIteration. 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: EvaluationStrategyWithRDFStarTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public CloseableIteration<? extends Triple, QueryEvaluationException> getRdfStarTriples(Resource subj,
		IRI pred, Value obj)
		throws QueryEvaluationException {
	return new AbstractCloseableIteration<Triple, QueryEvaluationException>() {
		Iterator<Triple> iter = triples.iterator();

		@Override
		public boolean hasNext()
				throws QueryEvaluationException {
			return iter.hasNext();
		}

		@Override
		public Triple next()
				throws QueryEvaluationException {
			return iter.next();
		}

		@Override
		public void remove()
				throws QueryEvaluationException {
		}
	};
}
 
Example #2
Source File: SynchronousJoin.java    From CostFed with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
protected void handleBindings() {
	
	int totalBindings=0;
	
	while (!closed && leftIter.hasNext()) {
		addTask(new Callable<CloseableIteration<BindingSet,QueryEvaluationException>>() {
			@Override
			public CloseableIteration<BindingSet, QueryEvaluationException> call() throws Exception {
				return strategy.evaluate(rightArg, leftIter.next());
			}
		});
		totalBindings++;
	}
		
	// XXX remove output if not needed anymore
	log.debug("JoinStats: left iter of join #" + this.joinId + " had " + totalBindings + " results.");
}
 
Example #3
Source File: LuceneSailConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public synchronized CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluate(TupleExpr tupleExpr,
		Dataset dataset, BindingSet bindings, boolean includeInferred) throws SailException {
	QueryContext qctx = new QueryContext();
	SearchIndexQueryContextInitializer.init(qctx, luceneIndex);

	final CloseableIteration<? extends BindingSet, QueryEvaluationException> iter;
	qctx.begin();
	try {
		iter = evaluateInternal(tupleExpr, dataset, bindings, includeInferred);
	} finally {
		qctx.end();
	}

	// NB: Iteration methods may do on-demand evaluation hence need to wrap
	// these too
	return new QueryContextIteration(iter, qctx);
}
 
Example #4
Source File: LimitedSizeEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(Join join, BindingSet bindings)
		throws QueryEvaluationException {
	// efficient computation of a SERVICE join using vectored evaluation
	// TODO maybe we can create a ServiceJoin node already in the parser?
	if (join.getRightArg() instanceof Service) {
		CloseableIteration<BindingSet, QueryEvaluationException> leftIter = evaluate(join.getLeftArg(), bindings);
		return new ServiceJoinIterator(leftIter, (Service) join.getRightArg(), bindings, this);
	}

	if (TupleExprs.containsSubquery(join.getRightArg())) {
		return new LimitedSizeHashJoinIteration(this, join, bindings, used, maxSize);
	} else {
		return new JoinIterator(this, join, bindings);
	}
}
 
Example #5
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Value evaluate(In node, BindingSet bindings) throws QueryEvaluationException {
	Value leftValue = evaluate(node.getArg(), bindings);

	// Result is false until a match has been found
	boolean result = false;

	// Use first binding name from tuple expr to compare values
	String bindingName = node.getSubQuery().getBindingNames().iterator().next();

	try (CloseableIteration<BindingSet, QueryEvaluationException> iter = evaluate(node.getSubQuery(), bindings)) {
		while (!result && iter.hasNext()) {
			BindingSet bindingSet = iter.next();

			Value rightValue = bindingSet.getValue(bindingName);

			result = leftValue == null && rightValue == null || leftValue != null && leftValue.equals(rightValue);
		}
	}

	return BooleanLiteral.valueOf(result);
}
 
Example #6
Source File: RyaDaoQueryWrapper.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Handles all results of a query. Closes the query iterator when done.
 * @param subject the subject {@link Resource} to query for.
 * @param predicate the predicate {@link IRI} to query for.
 * @param object the object {@link Value} to query for.
 * @param rdfStatementHandler the {@link RDFHandler} to use for handling
 * each statement returned. (not {@code null})
 * @param contexts the context {@link Resource}s to query for.
 * @throws QueryEvaluationException
 */
public void queryAll(final Resource subject, final IRI predicate, final Value object, final RDFHandler rdfStatementHandler, final Resource... contexts) throws QueryEvaluationException {
    checkNotNull(rdfStatementHandler);
    final CloseableIteration<Statement, QueryEvaluationException> iter = RyaDAOHelper.query(ryaDao, subject, predicate, object, conf, contexts);
    try {
        while (iter.hasNext()) {
            final Statement statement = iter.next();
            try {
                rdfStatementHandler.handleStatement(statement);
            } catch (final Exception e) {
                throw new QueryEvaluationException("Error handling statement.", e);
            }
        }
    } finally {
        if (iter != null) {
            iter.close();
        }
    }
}
 
Example #7
Source File: AccumuloTemporalIndexer.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * find intervals stored in the repository before the given Interval. Find interval endings that are
 * before the given beginning.
 * Indexing Intervals  will probably change or be removed.
 * Currently predicate and subject constraints are filtered on the client.
 */
@Override
public CloseableIteration<Statement, QueryEvaluationException> queryIntervalBefore(
        final TemporalInterval queryInterval, final StatementConstraints constraints) throws QueryEvaluationException
{
    final Scanner scanner = getScanner();
    if (scanner != null) {
        // get rows where the end date is less than the queryInterval.getBefore()
        final Range range = new Range(null, false, new Key(new Text(queryInterval.getHasBeginning().getAsKeyBytes())), false);
        scanner.setRange(range);
         if (constraints.hasContext()) {
            scanner.fetchColumn(new Text(constraints.getContext().toString()), new Text(KeyParts.CQ_END));
        } else {
            scanner.fetchColumn(new Text(""), new Text(KeyParts.CQ_END));
        }
    }
    return getIteratorWrapper(scanner);
}
 
Example #8
Source File: ParallelEvaluationStrategyImpl.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
    public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(TupleExpr expr, BindingSet bindings) throws QueryEvaluationException {
        if (expr instanceof QueryRoot) {
            if (displayQueryPlan) {
//                System.out.println("Tables: ");
//                System.out.println("--SPO: \t" + RdfCloudTripleStoreConstants.TBL_SPO);
//                System.out.println("--PO: \t" + RdfCloudTripleStoreConstants.TBL_PO);
//                System.out.println("--OSP: \t" + RdfCloudTripleStoreConstants.TBL_OSP);
                logger.info("=================== Rya Query ===================");
                for (String str : expr.toString().split("\\r?\\n")) {
                    logger.info(str);
                }
                logger.info("================= End Rya Query =================");
            }
        }
        return super.evaluate(expr, bindings);
    }
 
Example #9
Source File: InferenceEngine.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Query for all triples involving a given predicate and add corresponding edges to a
 * {@link Graph} in one or both directions.
 * @param predicate Find all connections via this predicate URI
 * @param dir Direction of interest: for a matching triple, if {@link Direction#OUT} add an edge
 *      from subject to object; if {@link Direction#IN} add an edge from object to subject;
 *      if {@link Direction#BOTH} add both.
 * @param graph A TinkerPop graph
 * @param edgeName Label that will be given to all added edges
 * @throws QueryEvaluationException
 */
private void addPredicateEdges(final IRI predicate, final Direction dir, final Graph graph, final String edgeName)
        throws QueryEvaluationException {
    final CloseableIteration<Statement, QueryEvaluationException> iter = RyaDAOHelper.query(ryaDAO,
            null, predicate, null, conf);
    try {
        while (iter.hasNext()) {
            final Statement st = iter.next();
            if (Direction.OUT.equals(dir) || Direction.BOTH.equals(dir)) {
                addStatementEdge(graph, edgeName, st);
            }
            if (Direction.IN.equals(dir) || Direction.BOTH.equals(dir)) {
                addStatementEdge(graph, edgeName, VF.createStatement((Resource) st.getObject(),
                        st.getPredicate(), st.getSubject()));
            }
        }
    } finally {
        if (iter != null) {
            iter.close();
        }
    }
}
 
Example #10
Source File: AccumuloIndexSetColumnVisibilityTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void variableInstantiationTest() throws Exception {
    // Setup the object that will be tested.
    final String pcjTableName = new PcjTableNameFactory().makeTableName(ryaInstanceName, pcjId);
    final AccumuloIndexSet ais = new AccumuloIndexSet(conf, pcjTableName);

    // Setup the binding sets that will be evaluated.
    final QueryBindingSet bs = new QueryBindingSet();
    bs.addBinding("name", VF.createIRI("http://Alice"));
    final QueryBindingSet bs2 = new QueryBindingSet();
    bs2.addBinding("name", VF.createIRI("http://Bob"));

    final Set<BindingSet> bSets = Sets.newHashSet(bs, bs2);
    final CloseableIteration<BindingSet, QueryEvaluationException> results = ais.evaluate(bSets);

    final Set<BindingSet> fetchedResults = new HashSet<>();
    while (results.hasNext()) {
        final BindingSet next = results.next();
        fetchedResults.add(next);
    }

    final Set<BindingSet> expected = Sets.newHashSet(pcjBs1, pcjBs2);
    assertEquals(expected, fetchedResults);
}
 
Example #11
Source File: Show.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Show namespaces
 */
private void showNamespaces() {
	Repository repository = state.getRepository();
	if (repository == null) {
		writeUnopenedError();
		return;
	}

	try (RepositoryConnection con = repository.getConnection()) {
		try (CloseableIteration<? extends Namespace, RepositoryException> namespaces = con.getNamespaces()) {
			if (namespaces.hasNext()) {
				writeln(OUTPUT_SEPARATOR);
				while (namespaces.hasNext()) {
					final Namespace namespace = namespaces.next();
					writeln("|" + namespace.getPrefix() + "  " + namespace.getName());
				}
				writeln(OUTPUT_SEPARATOR);
			} else {
				writeln("No namespaces found");
			}
		}
	} catch (RepositoryException e) {
		writeError("Failed to show namespaces", e);
	}
}
 
Example #12
Source File: AccumuloRyaDAO.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void delete(final Iterator<RyaStatement> statements, final AccumuloRdfConfiguration conf) throws RyaDAOException {
    try {
        while (statements.hasNext()) {
            final RyaStatement stmt = statements.next();
            //query first
            final CloseableIteration<RyaStatement, RyaDAOException> query = this.queryEngine.query(stmt, conf);
            while (query.hasNext()) {
                deleteSingleRyaStatement(query.next());
            }

            for (final AccumuloIndexer index : secondaryIndexers) {
                index.deleteStatement(stmt);
            }
        }
        if (flushEachUpdate.get()) {
            mt_bw.flush();
        }
    } catch (final Exception e) {
        throw new RyaDAOException(e);
    }
}
 
Example #13
Source File: SailFederationEvalStrategy.java    From CostFed with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluateIndependentJoinGroup(
		IndependentJoinGroup joinGroup, BindingSet bindings)
		throws QueryEvaluationException {
		
	TupleExpr preparedQuery = QueryAlgebraUtil.selectQueryIndependentJoinGroup(joinGroup, bindings);
	
	try {
		List<StatementSource> statementSources = joinGroup.getMembers().get(0).getStatementSources();	// TODO this is only correct for the prototype (=> different endpoints)
		CloseableIteration<BindingSet, QueryEvaluationException> result = evaluateAtStatementSources(preparedQuery, statementSources, joinGroup.getQueryInfo());
					
		// return only those elements which evaluated positively at the endpoint
		result = new IndependentJoingroupBindingsIteration(result, bindings);
		
		return result;
	} catch (Exception e) {
		throw new QueryEvaluationException(e);
	}

}
 
Example #14
Source File: RyaDaoQueryWrapper.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Handles only the first result of a query. Closes the query iterator when
 * done.
 * @param subject the subject {@link Resource} to query for.
 * @param predicate the predicate {@link IRI} to query for.
 * @param object the object {@link Value} to query for.
 * @param rdfStatementHandler the {@link RDFHandler} to use for handling the
 * first statement returned. (not {@code null})
 * @param contexts the context {@link Resource}s to query for.
 * @throws QueryEvaluationException
 */
public void queryFirst(final Resource subject, final IRI predicate, final Value object, final RDFHandler rdfStatementHandler, final Resource... contexts) throws QueryEvaluationException {
    checkNotNull(rdfStatementHandler);
    final CloseableIteration<Statement, QueryEvaluationException> iter = RyaDAOHelper.query(ryaDao, subject, predicate, object, conf, contexts);
    try {
        if (iter.hasNext()) {
            final Statement statement = iter.next();
            try {
                rdfStatementHandler.handleStatement(statement);
            } catch (final Exception e) {
                throw new QueryEvaluationException("Error handling statement.", e);
            }
        }
    } finally {
        if (iter != null) {
            iter.close();
        }
    }
}
 
Example #15
Source File: SailRepositoryConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void exportStatements(Resource subj, IRI pred, Value obj, boolean includeInferred, RDFHandler handler,
		Resource... contexts) throws RepositoryException, RDFHandlerException {
	handler.startRDF();

	try ( // Export namespace information
			CloseableIteration<? extends Namespace, RepositoryException> nsIter = getNamespaces()) {
		while (nsIter.hasNext()) {
			Namespace ns = nsIter.next();
			handler.handleNamespace(ns.getPrefix(), ns.getName());
		}
	}

	// Export statements

	try (CloseableIteration<? extends Statement, RepositoryException> stIter = getStatements(subj, pred, obj,
			includeInferred, contexts)) {
		while (stIter.hasNext()) {
			handler.handleStatement(stIter.next());
		}
	}

	handler.endRDF();
}
 
Example #16
Source File: MemorySailStore.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public synchronized void prepare() throws SailException {
	acquireExclusiveTransactionLock();
	if (observations != null) {
		for (StatementPattern p : observations) {
			Resource subj = (Resource) p.getSubjectVar().getValue();
			IRI pred = (IRI) p.getPredicateVar().getValue();
			Value obj = p.getObjectVar().getValue();
			Var ctxVar = p.getContextVar();
			Resource[] contexts;
			if (ctxVar == null) {
				contexts = new Resource[0];
			} else {
				contexts = new Resource[] { (Resource) ctxVar.getValue() };
			}
			try (CloseableIteration<MemStatement, SailException> iter = createStatementIterator(subj, pred, obj,
					null, -1, contexts);) {
				while (iter.hasNext()) {
					MemStatement st = iter.next();
					int since = st.getSinceSnapshot();
					int till = st.getTillSnapshot();
					if (serializable < since && since < nextSnapshot
							|| serializable < till && till < nextSnapshot) {
						throw new SailConflictException("Observed State has Changed");
					}
				}
			}
		}
	}
}
 
Example #17
Source File: MemTripleSourceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test method for
 * {@link org.eclipse.rdf4j.sail.memory.MemTripleSource#getStatements(org.eclipse.rdf4j.model.Resource, org.eclipse.rdf4j.model.IRI, org.eclipse.rdf4j.model.Value, org.eclipse.rdf4j.model.Resource[])}
 * .
 */
@Test
public final void testGetStatementsOneContextOnePredicateOneContext() throws Exception {
	loadTestData("/alp-testdata.ttl", this.alice);
	TripleSource source = getTripleSourceCommitted();

	try (CloseableIteration<? extends Statement, QueryEvaluationException> statements = source.getStatements(null,
			RDFS.SUBCLASSOF, null, this.alice)) {
		List<Statement> list = Iterations.asList(statements);

		assertEquals(4, list.size());
	}
}
 
Example #18
Source File: SailDatasetTripleSource.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public CloseableIteration<? extends Triple, QueryEvaluationException> getRdfStarTriples(Resource subj, IRI pred,
		Value obj) throws QueryEvaluationException {
	try {
		return new TriplesIteration(dataset.getTriples(subj, pred, obj));
	} catch (SailException e) { // TODO is this necessary?
		throw new QueryEvaluationException(e);
	}
}
 
Example #19
Source File: MongoTemporalIndexer.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public CloseableIteration<Statement, QueryEvaluationException> queryInstantAfterInstant(
        final TemporalInstant queryInstant, final StatementConstraints constraints) throws QueryEvaluationException {
    final QueryBuilder qb = QueryBuilder.start(INSTANT)
        .greaterThan(queryInstant.getAsDateTime().toDate());
    return withConstraints(constraints, qb.get());
}
 
Example #20
Source File: UnionExecutorBase.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void callAsync(CloseableIteration<T, QueryEvaluationException> res) {
	/* optimization: avoid adding empty results */
	if (res instanceof EmptyIteration<?,?>) {
		result.onRemoveIterator();
		return;
	}

	//log.info("union: " + res.getClass().toString());
	if (res instanceof org.eclipse.rdf4j.query.resultio.helpers.BackgroundTupleResult) {
		result.add_release(new BufferedCloseableIterator<T, QueryEvaluationException>(res));
	} else {
		result.add_release(res);
	}
}
 
Example #21
Source File: IsolationLevelTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Query results must not include statements added after the first result is read
 */
private void snapshotRead(IsolationLevel level) throws RepositoryException {
	clear(store);
	try (RepositoryConnection con = store.getConnection();) {
		con.begin(level);
		int size = 1;
		for (int i = 0; i < size; i++) {
			insertTestStatement(con, i);
		}
		int counter = 0;
		try (CloseableIteration<? extends Statement, RepositoryException> stmts = con.getStatements(null, null,
				null, false);) {
			while (stmts.hasNext()) {
				Statement st = stmts.next();
				counter++;
				if (counter < size) {
					// remove observed statement to force new state
					con.remove(st.getSubject(), st.getPredicate(), st.getObject(), st.getContext());
					insertTestStatement(con, size + counter);
					insertTestStatement(con, size + size + counter);
				}
			}
		}
		try {
			con.commit();
		} catch (RepositoryException e) {
			// it is okay to abort after a dirty read
			e.printStackTrace();
			return;
		}
		assertEquals(size, counter);
	}
}
 
Example #22
Source File: MongoTemporalIndexer.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public CloseableIteration<Statement, QueryEvaluationException> queryInstantEqualsInstant(
        final TemporalInstant queryInstant, final StatementConstraints constraints) throws QueryEvaluationException {
    final QueryBuilder qb = QueryBuilder.start(INSTANT)
        .is(queryInstant.getAsDateTime().toDate());
    return withConstraints(constraints, qb.get());
}
 
Example #23
Source File: GeoTupleSet.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public CloseableIteration<Statement, QueryEvaluationException> performSearch(final String queryText,
        final StatementConstraints contraints) throws QueryEvaluationException {
    try {
        final WKTReader reader = new WKTReader();
        final Geometry geometry = reader.read(queryText);
        final CloseableIteration<Statement, QueryEvaluationException> statements = geoIndexer.queryCrosses(
                geometry, contraints);
        return statements;
    } catch (final ParseException e) {
        throw new QueryEvaluationException(e);
    }
}
 
Example #24
Source File: HashJoinTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testMergeJoinMultiWayNone2() throws Exception {
    //add data
    RyaIRI pred = new RyaIRI(litdupsNS, "pred1");
    RyaType zero = new RyaType("0");
    RyaType one = new RyaType("1");
    RyaType two = new RyaType("2");
    RyaType three = new RyaType("3");
    RyaType four = new RyaType("4");
    RyaIRI subj1 = new RyaIRI(litdupsNS, "subj1");
    RyaIRI subj2 = new RyaIRI(litdupsNS, "subj2");
    RyaIRI subj3 = new RyaIRI(litdupsNS, "subj3");
    RyaIRI subj4 = new RyaIRI(litdupsNS, "subj4");

    dao.add(new RyaStatement(subj1, pred, one));
    dao.add(new RyaStatement(subj1, pred, four));
    dao.add(new RyaStatement(subj2, pred, zero));
    dao.add(new RyaStatement(subj2, pred, one));
    dao.add(new RyaStatement(subj2, pred, four));
    dao.add(new RyaStatement(subj3, pred, two));
    dao.add(new RyaStatement(subj3, pred, four));
    dao.add(new RyaStatement(subj4, pred, one));
    dao.add(new RyaStatement(subj4, pred, two));
    

    //1 join
    HashJoin hjoin = new HashJoin(dao.getQueryEngine());
    CloseableIteration<RyaIRI, RyaDAOException> join = hjoin.join(null,
            new RdfCloudTripleStoreUtils.CustomEntry<RyaIRI, RyaType>(pred, one),
            new RdfCloudTripleStoreUtils.CustomEntry<RyaIRI, RyaType>(pred, two),
            new RdfCloudTripleStoreUtils.CustomEntry<RyaIRI, RyaType>(pred, three),
            new RdfCloudTripleStoreUtils.CustomEntry<RyaIRI, RyaType>(pred, four)
    );

    assertFalse(join.hasNext());
    join.close();
}
 
Example #25
Source File: TBSSFederationEvalStrategy.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(TupleExpr expr, BindingSet bindings)
{
	if (expr instanceof BindJoin) {
		return evaluateBindJoin((BindJoin)expr, bindings);
	} else if (expr instanceof HashJoin) {
		return evaluateHashJoin((HashJoin)expr, bindings);
	} else if (expr instanceof JoinRestarter) {
		return evaluateJoinRestarter((JoinRestarter)expr, bindings);
	} else if (expr instanceof TopKSourceStatementPattern) {
		return evaluateTopKSourceStatement((TopKSourceStatementPattern)expr, bindings);
	}
	return super.evaluate(expr, bindings);
}
 
Example #26
Source File: TripleSourceBase.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> getStatements(
		String preparedQuery, BindingSet bindings, FilterValueExpr filterExpr, QueryInfo queryInfo)
		throws RepositoryException, MalformedQueryException,
		QueryEvaluationException {

	return withConnection((conn, resultHolder) -> {

		TupleQuery query = conn.prepareTupleQuery(QueryLanguage.SPARQL, preparedQuery, null);
		applyMaxExecutionTimeUpperBound(query);
		configureInference(query, queryInfo);

		// evaluate the query
		monitorRemoteRequest();
		CloseableIteration<BindingSet, QueryEvaluationException> res = query.evaluate();
		resultHolder.set(res);

		// apply filter and/or insert original bindings
		if (filterExpr != null) {
			if (bindings.size() > 0) {
				res = new FilteringInsertBindingsIteration(filterExpr, bindings, res,
						this.strategy);
			} else {
				res = new FilteringIteration(filterExpr, res, this.strategy);
			}
			if (!res.hasNext()) {
				Iterations.closeCloseable(res);
				conn.close();
				resultHolder.set(new EmptyIteration<>());
				return;
			}
		} else if (bindings.size() > 0) {
			res = new InsertBindingsIteration(res, bindings);
		}

		resultHolder.set(new ConsumingIteration(res));

	});
}
 
Example #27
Source File: SailSourceConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private <T, X extends Exception> CloseableIteration<T, QueryEvaluationException> interlock(
		CloseableIteration<T, QueryEvaluationException> iter, SailClosable... closes) {
	return new SailClosingIteration<T, QueryEvaluationException>(iter, closes) {

		@Override
		protected void handleSailException(SailException e) throws QueryEvaluationException {
			throw new QueryEvaluationException(e);
		}
	};
}
 
Example #28
Source File: SailIsolationLevelTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected long count(SailConnection con, Resource subj, IRI pred, Value obj, boolean includeInferred,
		Resource... contexts) throws SailException {
	try (CloseableIteration<? extends Statement, SailException> stmts = con.getStatements(subj, pred, obj,
			includeInferred, contexts);) {
		long counter = 0;
		while (stmts.hasNext()) {
			stmts.next();
			counter++;
		}
		return counter;
	}
}
 
Example #29
Source File: MongoGeoIndexer.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public CloseableIteration<Statement, QueryEvaluationException> queryEquals(
        final Geometry query, final StatementConstraints constraints) {
    try {
        final Document queryObj = storageStrategy.getQuery(new GeoQuery(EQUALS, query));
        return withConstraints(constraints, queryObj);
    } catch (final MalformedQueryException e) {
        logger.error(e.getMessage(), e);
        return null;
    }
}
 
Example #30
Source File: TopKSourceStatementPattern.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
private void replayCache() {
	if (iteration == null) {
		iteration = new QueueIteration<BindingSet>();
	} else if (iteration.isClosed()) {
		iteration.restart();
	}
	List<StatementSource> srcs = getStatementSources();
	List<StatementSource> replaySrcs = new ArrayList<StatementSource>();
	for (int i = 0; i < srcs.size(); ++i) {
		if (bindingCache.size() <= i) {
			replaySrcs.add(srcs.get(i));
			bindingCache.add(new ArrayList<List<BindingSet>>());
		}
	}
	
	if (!replaySrcs.isEmpty()) {
		for (List<List<BindingSet>> levelcache : bindingCache) {
			for (List<BindingSet> item : levelcache) {
				iteration.executeTask(getScheduler(), new Callable<CloseableIteration<BindingSet,QueryEvaluationException>>() {
					@Override
					public CloseableIteration<BindingSet, QueryEvaluationException> call() throws Exception {
						return evaluate(item, replaySrcs);
					}
				});
			}
		}
	}
}