use of org.eclipse.rdf4j.common.iteration.CloseableIteration in project incubator-rya by apache.
the class AccumuloTemporalIndexer method getIteratorWrapper.
/**
* An iteration wrapper for a loaded scanner that is returned for each query above.
*
* @param scanner
* the results to iterate, then close.
* @return an anonymous object that will iterate the resulting statements from a given scanner.
*/
private static CloseableIteration<Statement, QueryEvaluationException> getIteratorWrapper(final ScannerBase scanner) {
final Iterator<Entry<Key, Value>> i = scanner.iterator();
return new CloseableIteration<Statement, QueryEvaluationException>() {
@Override
public boolean hasNext() {
return i.hasNext();
}
@Override
public Statement next() throws QueryEvaluationException {
final Entry<Key, Value> entry = i.next();
final Value v = entry.getValue();
try {
final String dataString = Text.decode(v.get(), 0, v.getSize());
final Statement s = StatementSerializer.readStatement(dataString);
return s;
} catch (final CharacterCodingException e) {
logger.error("Error decoding value=" + Arrays.toString(v.get()), e);
throw new QueryEvaluationException(e);
} catch (final IOException e) {
logger.error("Error de-serializing statement, string=" + v.get(), e);
throw new QueryEvaluationException(e);
}
}
@Override
public void remove() {
throw new UnsupportedOperationException("Remove not implemented");
}
@Override
public void close() throws QueryEvaluationException {
scanner.close();
}
};
}
use of org.eclipse.rdf4j.common.iteration.CloseableIteration in project incubator-rya by apache.
the class RdfCloudTripleStoreConnection method getStatementsInternal.
@Override
protected CloseableIteration<? extends Statement, SailException> getStatementsInternal(final Resource subject, final IRI predicate, final Value object, final boolean flag, final Resource... contexts) throws SailException {
// try {
// have to do this to get the inferred values
// TODO: Will this method reduce performance?
final Var subjVar = decorateValue(subject, "s");
final Var predVar = decorateValue(predicate, "p");
final Var objVar = decorateValue(object, "o");
StatementPattern sp = null;
final boolean hasContext = contexts != null && contexts.length > 0;
final Resource context = (hasContext) ? contexts[0] : null;
final Var cntxtVar = decorateValue(context, "c");
// TODO: Only using one context here
sp = new StatementPattern(subjVar, predVar, objVar, cntxtVar);
// return new StoreTripleSource(store.getConf()).getStatements(resource, uri, value, contexts);
final CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluate = evaluate(sp, null, null, false);
return new // TODO: Use a util class to do this
CloseableIteration<Statement, SailException>() {
private boolean isClosed = false;
@Override
public void close() throws SailException {
isClosed = true;
try {
evaluate.close();
} catch (final QueryEvaluationException e) {
throw new SailException(e);
}
}
@Override
public boolean hasNext() throws SailException {
try {
return evaluate.hasNext();
} catch (final QueryEvaluationException e) {
throw new SailException(e);
}
}
@Override
public Statement next() throws SailException {
if (!hasNext() || isClosed) {
throw new NoSuchElementException();
}
try {
final BindingSet next = evaluate.next();
final Resource bs_subj = (Resource) ((subjVar.hasValue()) ? subjVar.getValue() : next.getBinding(subjVar.getName()).getValue());
final IRI bs_pred = (IRI) ((predVar.hasValue()) ? predVar.getValue() : next.getBinding(predVar.getName()).getValue());
final Value bs_obj = (objVar.hasValue()) ? objVar.getValue() : next.getBinding(objVar.getName()).getValue();
final Binding b_cntxt = next.getBinding(cntxtVar.getName());
// convert BindingSet to Statement
if (b_cntxt != null) {
return SimpleValueFactory.getInstance().createStatement(bs_subj, bs_pred, bs_obj, (Resource) b_cntxt.getValue());
} else {
return SimpleValueFactory.getInstance().createStatement(bs_subj, bs_pred, bs_obj);
}
} catch (final QueryEvaluationException e) {
throw new SailException(e);
}
}
@Override
public void remove() throws SailException {
try {
evaluate.remove();
} catch (final QueryEvaluationException e) {
throw new SailException(e);
}
}
};
// } catch (QueryEvaluationException e) {
// throw new SailException(e);
// }
}
Aggregations