use of org.eclipse.rdf4j.common.iteration.CloseableIteration in project incubator-rya by apache.
the class AccumuloDocIdIndexer method queryDocIndex.
@Override
public CloseableIteration<BindingSet, QueryEvaluationException> queryDocIndex(final StarQuery query, final Collection<BindingSet> constraints) throws TableNotFoundException, QueryEvaluationException {
final StarQuery starQ = query;
final Iterator<BindingSet> bs = constraints.iterator();
final Iterator<BindingSet> bs2 = constraints.iterator();
final Set<String> unCommonVarNames;
final Set<String> commonVarNames;
if (bs2.hasNext()) {
final BindingSet currBs = bs2.next();
commonVarNames = StarQuery.getCommonVars(query, currBs);
unCommonVarNames = Sets.difference(currBs.getBindingNames(), commonVarNames);
} else {
commonVarNames = Sets.newHashSet();
unCommonVarNames = Sets.newHashSet();
}
if (commonVarNames.size() == 1 && !query.commonVarConstant() && commonVarNames.contains(query.getCommonVarName())) {
final HashMultimap<String, BindingSet> map = HashMultimap.create();
final String commonVar = starQ.getCommonVarName();
final Iterator<Entry<Key, Value>> intersections;
final BatchScanner scan;
final Set<Range> ranges = Sets.newHashSet();
while (bs.hasNext()) {
final BindingSet currentBs = bs.next();
if (currentBs.getBinding(commonVar) == null) {
continue;
}
final String row = currentBs.getBinding(commonVar).getValue().stringValue();
ranges.add(new Range(row));
map.put(row, currentBs);
}
scan = runQuery(starQ, ranges);
intersections = scan.iterator();
return new CloseableIteration<BindingSet, QueryEvaluationException>() {
private QueryBindingSet currentSolutionBs = null;
private boolean hasNextCalled = false;
private boolean isEmpty = false;
private Iterator<BindingSet> inputSet = new ArrayList<BindingSet>().iterator();
private BindingSet currentBs;
private Key key;
@Override
public boolean hasNext() throws QueryEvaluationException {
if (!hasNextCalled && !isEmpty) {
while (inputSet.hasNext() || intersections.hasNext()) {
if (!inputSet.hasNext()) {
key = intersections.next().getKey();
inputSet = map.get(key.getRow().toString()).iterator();
}
currentBs = inputSet.next();
currentSolutionBs = deserializeKey(key, starQ, currentBs, unCommonVarNames);
if (currentSolutionBs.size() == unCommonVarNames.size() + starQ.getUnCommonVars().size() + 1) {
hasNextCalled = true;
return true;
}
}
isEmpty = true;
return false;
} else {
return !isEmpty;
}
}
@Override
public BindingSet next() throws QueryEvaluationException {
if (hasNextCalled) {
hasNextCalled = false;
} else if (isEmpty) {
throw new NoSuchElementException();
} else {
if (this.hasNext()) {
hasNextCalled = false;
} else {
throw new NoSuchElementException();
}
}
return currentSolutionBs;
}
@Override
public void remove() throws QueryEvaluationException {
throw new UnsupportedOperationException();
}
@Override
public void close() throws QueryEvaluationException {
scan.close();
}
};
} else {
return new CloseableIteration<BindingSet, QueryEvaluationException>() {
@Override
public void remove() throws QueryEvaluationException {
throw new UnsupportedOperationException();
}
private Iterator<Entry<Key, Value>> intersections = null;
private QueryBindingSet currentSolutionBs = null;
private boolean hasNextCalled = false;
private boolean isEmpty = false;
private boolean init = false;
private BindingSet currentBs;
private StarQuery sq = new StarQuery(starQ);
private final Set<Range> emptyRangeSet = Sets.newHashSet();
private BatchScanner scan;
@Override
public BindingSet next() throws QueryEvaluationException {
if (hasNextCalled) {
hasNextCalled = false;
} else if (isEmpty) {
throw new NoSuchElementException();
} else {
if (this.hasNext()) {
hasNextCalled = false;
} else {
throw new NoSuchElementException();
}
}
return currentSolutionBs;
}
@Override
public boolean hasNext() throws QueryEvaluationException {
if (!init) {
if (intersections == null && bs.hasNext()) {
currentBs = bs.next();
sq = StarQuery.getConstrainedStarQuery(sq, currentBs);
scan = runQuery(sq, emptyRangeSet);
intersections = scan.iterator();
// binding set empty
} else if (intersections == null && !bs.hasNext()) {
currentBs = new QueryBindingSet();
scan = runQuery(starQ, emptyRangeSet);
intersections = scan.iterator();
}
init = true;
}
if (!hasNextCalled && !isEmpty) {
while (intersections.hasNext() || bs.hasNext()) {
if (!intersections.hasNext()) {
scan.close();
currentBs = bs.next();
sq = StarQuery.getConstrainedStarQuery(sq, currentBs);
scan = runQuery(sq, emptyRangeSet);
intersections = scan.iterator();
}
if (intersections.hasNext()) {
currentSolutionBs = deserializeKey(intersections.next().getKey(), sq, currentBs, unCommonVarNames);
} else {
continue;
}
if (sq.commonVarConstant() && currentSolutionBs.size() == unCommonVarNames.size() + sq.getUnCommonVars().size()) {
hasNextCalled = true;
return true;
} else if (currentSolutionBs.size() == unCommonVarNames.size() + sq.getUnCommonVars().size() + 1) {
hasNextCalled = true;
return true;
}
}
isEmpty = true;
return false;
} else {
return !isEmpty;
}
}
@Override
public void close() throws QueryEvaluationException {
scan.close();
}
};
}
}
use of org.eclipse.rdf4j.common.iteration.CloseableIteration in project incubator-rya by apache.
the class IteratorFactory method getIterator.
public static CloseableIteration<BindingSet, QueryEvaluationException> getIterator(final StatementPattern match, final BindingSet bindings, final String queryText, final SearchFunction searchFunction) {
return new CloseableIteration<BindingSet, QueryEvaluationException>() {
private boolean isClosed = false;
private CloseableIteration<Statement, QueryEvaluationException> statementIt = null;
private String subjectBinding = match.getSubjectVar().getName();
private String predicateBinding = match.getPredicateVar().getName();
private String objectBinding = match.getObjectVar().getName();
private String contextBinding = null;
private void performQuery() throws QueryEvaluationException {
StatementConstraints contraints = new StatementConstraints();
// get the context (i.e. named graph) of the statement and use that in the query
QueryModelNode parentNode = match.getSubjectVar().getParentNode();
if (parentNode instanceof StatementPattern) {
StatementPattern parentStatement = (StatementPattern) parentNode;
Var contextVar = parentStatement.getContextVar();
if (contextVar != null) {
contextBinding = contextVar.getName();
Resource context = (Resource) contextVar.getValue();
contraints.setContext(context);
}
}
// get the subject constraint
if (match.getSubjectVar().isConstant()) {
// get the subject binding from the filter/statement pair
Resource subject = (Resource) match.getSubjectVar().getValue();
contraints.setSubject(subject);
} else if (bindings.hasBinding(subjectBinding)) {
// get the subject binding from the passed in bindings (eg from other statements/parts of the tree)
Resource subject = (Resource) bindings.getValue(subjectBinding);
contraints.setSubject(subject);
}
// get the predicate constraint
if (match.getPredicateVar().isConstant()) {
// get the predicate binding from the filter/statement pair
Set<IRI> predicates = new HashSet<IRI>(getPredicateRestrictions(match.getPredicateVar()));
contraints.setPredicates(predicates);
} else if (bindings.hasBinding(predicateBinding)) {
// get the predicate binding from the passed in bindings (eg from other statements/parts of the tree)
IRI predicateUri = (IRI) bindings.getValue(predicateBinding);
Set<IRI> predicates = Collections.singleton(predicateUri);
contraints.setPredicates(predicates);
}
statementIt = searchFunction.performSearch(queryText, contraints);
}
@Override
public boolean hasNext() throws QueryEvaluationException {
if (statementIt == null) {
performQuery();
}
return statementIt.hasNext();
}
@Override
public BindingSet next() throws QueryEvaluationException {
if (!hasNext() || isClosed) {
throw new NoSuchElementException();
}
Statement statment = statementIt.next();
MapBindingSet bset = new MapBindingSet();
if (!subjectBinding.startsWith("-const"))
bset.addBinding(subjectBinding, statment.getSubject());
if (!predicateBinding.startsWith("-const"))
bset.addBinding(predicateBinding, statment.getPredicate());
if (!objectBinding.startsWith("-const"))
bset.addBinding(objectBinding, statment.getObject());
if (contextBinding != null && !contextBinding.startsWith("-const"))
bset.addBinding(contextBinding, statment.getContext());
// merge with other bindings.
for (String name : bindings.getBindingNames()) {
bset.addBinding(name, bindings.getValue(name));
}
return bset;
}
@Override
public void remove() throws QueryEvaluationException {
throw new UnsupportedOperationException();
}
@Override
public void close() throws QueryEvaluationException {
if (statementIt != null) {
statementIt.close();
}
isClosed = true;
}
};
}
use of org.eclipse.rdf4j.common.iteration.CloseableIteration in project AJAN-service by aantakli.
the class RDFBeanManager method getAll.
/**
* Obtain an iterator over all instances of specified RDFBean class stored
* in the RDF model
*
* <p>
* The returned Iterator performs lazy unmarshalling of RDFBean objects (on
* every <code>next()</code> call) without any specific order. When
* iterating is done, the caller must invoke the <code>close()</code> method
* of CloseableIterator to release the resources of the underlying RDF model
* implementation.
*
* @param rdfBeanClass
* Java class of RDFBeans
* @return Iterator over RDFBean instances
* @throws RDFBeanException
* If the class is not a valid RDFBean
* @throws RepositoryException
*/
public <T> CloseableIteration<T, Exception> getAll(final Class<T> rdfBeanClass) throws RDFBeanException, RepositoryException {
RDFBeanInfo rbi = RDFBeanInfo.get(rdfBeanClass);
IRI type = rbi.getRDFType();
if (type == null) {
return new CloseableIteration<T, Exception>() {
@Override
public boolean hasNext() throws Exception {
return false;
}
@Override
public T next() throws Exception {
return null;
}
@Override
public void remove() throws Exception {
throw new UnsupportedOperationException();
}
@Override
public void close() throws Exception {
}
};
}
final CloseableIteration<Statement, RepositoryException> sts = conn.getStatements(null, RDF.TYPE, type, false);
return new CloseableIteration<T, Exception>() {
@Override
public boolean hasNext() throws Exception {
return sts.hasNext();
}
@Override
public T next() throws Exception {
return _get(sts.next().getSubject(), rdfBeanClass);
}
@Override
public void remove() throws Exception {
throw new UnsupportedOperationException();
}
@Override
public void close() throws Exception {
sts.close();
}
};
}
use of org.eclipse.rdf4j.common.iteration.CloseableIteration in project incubator-rya by apache.
the class GeoWaveGeoIndexer method getIteratorWrapper.
private CloseableIteration<Statement, QueryEvaluationException> getIteratorWrapper(final String filterString) {
return new CloseableIteration<Statement, QueryEvaluationException>() {
private CloseableIterator<SimpleFeature> featureIterator = null;
CloseableIterator<SimpleFeature> getIterator() throws QueryEvaluationException {
if (featureIterator == null) {
Filter cqlFilter;
try {
cqlFilter = ECQL.toFilter(filterString);
} catch (final CQLException e) {
logger.error("Error parsing query: " + LogUtils.clean(filterString), e);
throw new QueryEvaluationException(e);
}
final CQLQuery cqlQuery = new CQLQuery(null, cqlFilter, featureDataAdapter);
final QueryOptions queryOptions = new QueryOptions(featureDataAdapter, index);
try {
featureIterator = geoWaveDataStore.query(queryOptions, cqlQuery);
} catch (final Exception e) {
logger.error("Error performing query: " + filterString, e);
throw new QueryEvaluationException(e);
}
}
return featureIterator;
}
@Override
public boolean hasNext() throws QueryEvaluationException {
return getIterator().hasNext();
}
@Override
public Statement next() throws QueryEvaluationException {
final SimpleFeature feature = getIterator().next();
final String subjectString = feature.getAttribute(SUBJECT_ATTRIBUTE).toString();
final String predicateString = feature.getAttribute(PREDICATE_ATTRIBUTE).toString();
final String objectString = feature.getAttribute(OBJECT_ATTRIBUTE).toString();
final Object context = feature.getAttribute(CONTEXT_ATTRIBUTE);
final String contextString = context != null ? context.toString() : "";
final Statement statement = StatementSerializer.readStatement(subjectString, predicateString, objectString, contextString);
return statement;
}
@Override
public void remove() {
throw new UnsupportedOperationException("Remove not implemented");
}
@Override
public void close() throws QueryEvaluationException {
try {
getIterator().close();
} catch (final IOException e) {
throw new QueryEvaluationException(e);
}
}
};
}
use of org.eclipse.rdf4j.common.iteration.CloseableIteration in project incubator-rya by apache.
the class GeoMesaGeoIndexer method getIteratorWrapper.
private CloseableIteration<Statement, QueryEvaluationException> getIteratorWrapper(final String filterString) {
return new CloseableIteration<Statement, QueryEvaluationException>() {
private FeatureIterator<SimpleFeature> featureIterator = null;
FeatureIterator<SimpleFeature> getIterator() throws QueryEvaluationException {
if (featureIterator == null) {
Filter cqlFilter;
try {
cqlFilter = ECQL.toFilter(filterString);
} catch (final CQLException e) {
logger.error("Error parsing query: " + LogUtils.clean(filterString), e);
throw new QueryEvaluationException(e);
}
final Query query = new Query(featureType.getTypeName(), cqlFilter);
try {
featureIterator = featureSource.getFeatures(query).features();
} catch (final IOException e) {
logger.error("Error performing query: " + LogUtils.clean(filterString), e);
throw new QueryEvaluationException(e);
}
}
return featureIterator;
}
@Override
public boolean hasNext() throws QueryEvaluationException {
return getIterator().hasNext();
}
@Override
public Statement next() throws QueryEvaluationException {
final SimpleFeature feature = getIterator().next();
final String subjectString = feature.getAttribute(SUBJECT_ATTRIBUTE).toString();
final String predicateString = feature.getAttribute(PREDICATE_ATTRIBUTE).toString();
final String objectString = feature.getAttribute(OBJECT_ATTRIBUTE).toString();
final String contextString = feature.getAttribute(CONTEXT_ATTRIBUTE).toString();
final Statement statement = StatementSerializer.readStatement(subjectString, predicateString, objectString, contextString);
return statement;
}
@Override
public void remove() {
throw new UnsupportedOperationException("Remove not implemented");
}
@Override
public void close() throws QueryEvaluationException {
getIterator().close();
}
};
}
Aggregations