Java源码示例:org.eclipse.rdf4j.common.iteration.LookAheadIteration

示例1
synchronized private CloseableIteration<? extends ExtensibleStatement, SailException> getCached(
		PartialStatement partialStatement) {
	List<ExtensibleStatement> statements = cache.get(partialStatement);

	if (statements != null) {

		return new LookAheadIteration<ExtensibleStatement, SailException>() {
			Iterator<ExtensibleStatement> iterator = statements.iterator();

			@Override
			protected ExtensibleStatement getNextElement() throws SailException {
				if (iterator.hasNext()) {
					return iterator.next();
				}
				return null;
			}
		};

	}

	return null;
}
 
示例2
@Override
public CloseableIteration<? extends ExtensibleStatement, SailException> getStatements(Resource subject,
		IRI predicate,
		Value object, boolean inferred, Resource... context) {

	QueryBuilder queryBuilder = getQueryBuilder(subject, predicate, object, inferred, context);

	return new LookAheadIteration<ExtensibleStatement, SailException>() {

		CloseableIteration<SearchHit, RuntimeException> iterator = ElasticsearchHelper
				.getScrollingIterator(queryBuilder, clientProvider.getClient(), index, scrollTimeout);

		@Override
		protected ExtensibleStatement getNextElement() throws SailException {

			ExtensibleStatement next = null;

			while (next == null && iterator.hasNext()) {
				SearchHit nextSearchHit = iterator.next();

				Map<String, Object> sourceAsMap = nextSearchHit.getSourceAsMap();

				String id = nextSearchHit.getId();

				ExtensibleStatement statement = sourceToStatement(sourceAsMap, id, subject, predicate, object);

				// we use hash to lookup the object value because the object can be bigger than what elasticsearch
				// allows as max for keyword (32766 bytes), so it needs to be stored in a text field that is not
				// index. The hash is stored in an integer field and is index. The code below does hash collision
				// check.
				if (object != null
						&& object.stringValue().hashCode() == statement.getObject().stringValue().hashCode()
						&& !object.equals(statement.getObject())) {
					continue;
				}

				next = statement;

			}

			return next;
		}

		@Override
		public void remove() throws SailException {

			throw new IllegalStateException("Does not support removing from iterator");

		}

		@Override
		protected void handleClose() throws SailException {
			super.handleClose();
			iterator.close();
		}

	};

}
 
示例3
@Override
public CloseableIteration<? extends Statement, SailException> getStatements(Resource subj, IRI pred, Value obj,
		boolean includeInferred, Resource... contexts) throws SailException {

	if (rdfsSubClassOfReasoner != null && includeInferred && obj instanceof Resource
			&& RDF.TYPE.equals(pred)) {
		Set<Resource> inferredTypes = rdfsSubClassOfReasoner.backwardsChain((Resource) obj);
		if (!inferredTypes.isEmpty()) {

			CloseableIteration<Statement, SailException>[] statementsMatchingInferredTypes = inferredTypes.stream()
					.map(r -> super.getStatements(subj, pred, r, false, contexts))
					.toArray(CloseableIteration[]::new);

			return new LookAheadIteration<Statement, SailException>() {

				UnionIteration<Statement, SailException> unionIteration = new UnionIteration<>(
						statementsMatchingInferredTypes);

				HashSet<Statement> dedupe = new HashSet<>();

				@Override
				protected Statement getNextElement() throws SailException {
					Statement next = null;

					while (next == null && unionIteration.hasNext()) {
						Statement temp = unionIteration.next();
						temp = SimpleValueFactory.getInstance()
								.createStatement(temp.getSubject(), temp.getPredicate(), obj, temp.getContext());

						if (!dedupe.isEmpty()) {
							boolean contains = dedupe.contains(temp);
							if (!contains) {
								next = temp;
								dedupe.add(next);
							}
						} else {
							next = temp;
							dedupe.add(next);
						}

					}

					return next;
				}

				@Override
				public void remove() throws SailException {
					throw new IllegalStateException("Not implemented");
				}

				@Override
				protected void handleClose() throws SailException {
					try {
						unionIteration.close();
					} finally {
						super.handleClose();
					}
				}

			};

		}
	}

	return super.getStatements(subj, pred, obj, includeInferred, contexts);
}
 
示例4
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(BindingSetAssignment bsa,
		BindingSet bindings) throws QueryEvaluationException {
	final Iterator<BindingSet> iter = bsa.getBindingSets().iterator();
	if (bindings.size() == 0) { // empty binding set
		return new CloseableIteratorIteration<>(iter);
	}

	CloseableIteration<BindingSet, QueryEvaluationException> result;

	final QueryBindingSet b = new QueryBindingSet(bindings);

	result = new LookAheadIteration<BindingSet, QueryEvaluationException>() {

		@Override
		protected BindingSet getNextElement() throws QueryEvaluationException {
			QueryBindingSet result = null;
			while (result == null && iter.hasNext()) {
				final BindingSet assignedBindings = iter.next();
				for (String name : assignedBindings.getBindingNames()) {
					final Value assignedValue = assignedBindings.getValue(name);
					if (assignedValue != null) { // can be null if set to
						// UNDEF
						// check that the binding assignment does not
						// overwrite
						// existing bindings.
						Value bValue = b.getValue(name);
						if (bValue == null || assignedValue.equals(bValue)) {
							if (result == null) {
								result = new QueryBindingSet(b);
							}
							if (bValue == null) {
								// we are not overwriting an existing
								// binding.
								result.addBinding(name, assignedValue);
							}
						} else {
							// if values are not equal there is no
							// compatible
							// merge and we should return no next element.
							result = null;
							break;
						}
					}
				}
			}
			return result;
		}

	};

	return result;
}