use of org.eclipse.rdf4j.common.iteration.CloseableIteration in project incubator-rya by apache.
the class RyaDAOHelper method query.
public static CloseableIteration<Statement, QueryEvaluationException> query(RyaDAO ryaDAO, Statement stmt, RdfCloudTripleStoreConfiguration conf) throws QueryEvaluationException {
final CloseableIteration<RyaStatement, RyaDAOException> query;
try {
query = ryaDAO.getQueryEngine().query(RdfToRyaConversions.convertStatement(stmt), conf);
} catch (RyaDAOException e) {
throw new QueryEvaluationException(e);
}
// TODO: only support one context for now
return new // TODO: Create a new class struct for this
CloseableIteration<Statement, QueryEvaluationException>() {
private boolean isClosed = false;
@Override
public void close() throws QueryEvaluationException {
try {
isClosed = true;
query.close();
} catch (RyaDAOException e) {
throw new QueryEvaluationException(e);
}
}
@Override
public boolean hasNext() throws QueryEvaluationException {
try {
return query.hasNext();
} catch (RyaDAOException e) {
throw new QueryEvaluationException(e);
}
}
@Override
public Statement next() throws QueryEvaluationException {
if (!hasNext() || isClosed) {
throw new NoSuchElementException();
}
try {
RyaStatement next = query.next();
if (next == null) {
return null;
}
return RyaToRdfConversions.convertStatement(next);
} catch (RyaDAOException e) {
throw new QueryEvaluationException(e);
}
}
@Override
public void remove() throws QueryEvaluationException {
try {
query.remove();
} catch (RyaDAOException e) {
throw new QueryEvaluationException(e);
}
}
};
}
use of org.eclipse.rdf4j.common.iteration.CloseableIteration in project incubator-rya by apache.
the class RyaDAOHelper method query.
public static CloseableIteration<? extends Map.Entry<Statement, BindingSet>, QueryEvaluationException> query(RyaDAO ryaDAO, Collection<Map.Entry<Statement, BindingSet>> statements, RdfCloudTripleStoreConfiguration conf) throws QueryEvaluationException {
Collection<Map.Entry<RyaStatement, BindingSet>> ryaStatements = new ArrayList<Map.Entry<RyaStatement, BindingSet>>(statements.size());
for (Map.Entry<Statement, BindingSet> entry : statements) {
ryaStatements.add(new RdfCloudTripleStoreUtils.CustomEntry<RyaStatement, BindingSet>(RdfToRyaConversions.convertStatement(entry.getKey()), entry.getValue()));
}
final CloseableIteration<? extends Map.Entry<RyaStatement, BindingSet>, RyaDAOException> query;
try {
query = ryaDAO.getQueryEngine().queryWithBindingSet(ryaStatements, conf);
} catch (RyaDAOException e) {
throw new QueryEvaluationException(e);
}
return new // TODO: Create a new class struct for this
CloseableIteration<Map.Entry<Statement, BindingSet>, QueryEvaluationException>() {
private boolean isClosed = false;
@Override
public void close() throws QueryEvaluationException {
isClosed = true;
try {
query.close();
} catch (RyaDAOException e) {
throw new QueryEvaluationException(e);
}
}
@Override
public boolean hasNext() throws QueryEvaluationException {
try {
return query.hasNext();
} catch (RyaDAOException e) {
throw new QueryEvaluationException(e);
}
}
@Override
public Map.Entry<Statement, BindingSet> next() throws QueryEvaluationException {
if (!hasNext() || isClosed) {
throw new NoSuchElementException();
}
try {
Map.Entry<RyaStatement, BindingSet> next = query.next();
if (next == null) {
return null;
}
return new RdfCloudTripleStoreUtils.CustomEntry<Statement, BindingSet>(RyaToRdfConversions.convertStatement(next.getKey()), next.getValue());
} catch (RyaDAOException e) {
throw new QueryEvaluationException(e);
}
}
@Override
public void remove() throws QueryEvaluationException {
try {
query.remove();
} catch (RyaDAOException e) {
throw new QueryEvaluationException(e);
}
}
};
}
use of org.eclipse.rdf4j.common.iteration.CloseableIteration in project incubator-rya by apache.
the class MergeJoin method join.
/**
* Return all subjects that have the predicate objects associated. Predicate and objects must be not null or ranges
* to ensure sorting
*
* @param predObjs
* @return
* @throws RyaDAOException
*/
@Override
public CloseableIteration<RyaIRI, RyaDAOException> join(C conf, Map.Entry<RyaIRI, RyaType>... predObjs) throws RyaDAOException {
Preconditions.checkNotNull(predObjs);
Preconditions.checkArgument(predObjs.length > 1, "Must join 2 or more");
// TODO: Reorder predObjs based on statistics
final List<CloseableIteration<RyaStatement, RyaDAOException>> iters = new ArrayList<CloseableIteration<RyaStatement, RyaDAOException>>();
RyaIRI earliest_subject = null;
for (Map.Entry<RyaIRI, RyaType> predObj : predObjs) {
RyaIRI predicate = predObj.getKey();
RyaType object = predObj.getValue();
Preconditions.checkArgument(predicate != null && !(predicate instanceof RyaRange));
Preconditions.checkArgument(object != null && !(object instanceof RyaRange));
PeekingCloseableIteration<RyaStatement, RyaDAOException> iter = null;
if (earliest_subject == null) {
iter = new PeekingCloseableIteration<RyaStatement, RyaDAOException>(ryaQueryEngine.query(new RyaStatement(null, predicate, object), conf));
} else {
iter = new PeekingCloseableIteration<RyaStatement, RyaDAOException>(ryaQueryEngine.query(new RyaStatement(new RyaIRIRange(earliest_subject, RyaIRIRange.LAST_IRI), predicate, object), conf));
}
if (!iter.hasNext()) {
return new EmptyIteration<RyaIRI, RyaDAOException>();
}
// setting up range to make performant query
earliest_subject = iter.peek().getSubject();
iters.add(iter);
}
Preconditions.checkArgument(iters.size() > 1, "Must join 2 or more");
final CloseableIteration<RyaStatement, RyaDAOException> first = iters.remove(0);
return new CloseableIteration<RyaIRI, RyaDAOException>() {
private RyaIRI first_subj;
@Override
public void close() throws RyaDAOException {
for (CloseableIteration<RyaStatement, RyaDAOException> iter : iters) {
iter.close();
}
}
@Override
public boolean hasNext() throws RyaDAOException {
return first_subj != null || check();
}
@Override
public RyaIRI next() throws RyaDAOException {
if (first_subj != null) {
RyaIRI temp = first_subj;
first_subj = null;
return temp;
}
if (check()) {
RyaIRI temp = first_subj;
first_subj = null;
return temp;
}
return null;
}
@Override
public void remove() throws RyaDAOException {
this.next();
}
protected boolean check() throws RyaDAOException {
if (!first.hasNext())
return false;
first_subj = first.next().getSubject();
for (CloseableIteration<RyaStatement, RyaDAOException> iter : iters) {
// no more left to join
if (!iter.hasNext())
return false;
RyaIRI iter_subj = iter.next().getSubject();
while (first_subj.compareTo(iter_subj) < 0) {
if (!first.hasNext())
return false;
first_subj = first.next().getSubject();
}
while (first_subj.compareTo(iter_subj) > 0) {
if (!iter.hasNext())
return false;
iter_subj = iter.next().getSubject();
}
}
return true;
}
};
}
use of org.eclipse.rdf4j.common.iteration.CloseableIteration in project incubator-rya by apache.
the class MergeJoin method join.
/**
* Return all statements that have input predicates. Predicates must not be null or ranges
*
* @param preds
* @return
*/
@Override
public CloseableIteration<RyaStatement, RyaDAOException> join(C conf, RyaIRI... preds) throws RyaDAOException {
Preconditions.checkNotNull(preds);
Preconditions.checkArgument(preds.length > 1, "Must join 2 or more");
// TODO: Reorder predObjs based on statistics
final List<CloseableIteration<RyaStatement, RyaDAOException>> iters = new ArrayList<CloseableIteration<RyaStatement, RyaDAOException>>();
for (RyaIRI predicate : preds) {
Preconditions.checkArgument(predicate != null && !(predicate instanceof RyaRange));
CloseableIteration<RyaStatement, RyaDAOException> iter = ryaQueryEngine.query(new RyaStatement(null, predicate, null), conf);
iters.add(iter);
}
Preconditions.checkArgument(iters.size() > 1, "Must join 2 or more");
final CloseableIteration<RyaStatement, RyaDAOException> first = iters.remove(0);
return new CloseableIteration<RyaStatement, RyaDAOException>() {
private RyaStatement first_stmt;
private RyaType first_obj;
@Override
public void close() throws RyaDAOException {
for (CloseableIteration<RyaStatement, RyaDAOException> iter : iters) {
iter.close();
}
}
@Override
public boolean hasNext() throws RyaDAOException {
return first_stmt != null || check();
}
@Override
public RyaStatement next() throws RyaDAOException {
if (first_stmt != null) {
RyaStatement temp = first_stmt;
first_stmt = null;
return temp;
}
if (check()) {
RyaStatement temp = first_stmt;
first_stmt = null;
return temp;
}
return null;
}
@Override
public void remove() throws RyaDAOException {
this.next();
}
protected boolean check() throws RyaDAOException {
if (!first.hasNext())
return false;
first_stmt = first.next();
first_obj = first_stmt.getObject();
for (CloseableIteration<RyaStatement, RyaDAOException> iter : iters) {
// no more left to join
if (!iter.hasNext())
return false;
RyaType iter_obj = iter.next().getObject();
while (first_obj.compareTo(iter_obj) < 0) {
if (!first.hasNext())
return false;
first_obj = first.next().getObject();
}
while (first_obj.compareTo(iter_obj) > 0) {
if (!iter.hasNext())
return false;
iter_obj = iter.next().getObject();
}
}
return true;
}
};
}
use of org.eclipse.rdf4j.common.iteration.CloseableIteration in project incubator-rya by apache.
the class AccumuloFreeTextIndexer method getIteratorWrapper.
private static CloseableIteration<Statement, QueryEvaluationException> getIteratorWrapper(final Scanner s) {
final Iterator<Entry<Key, Value>> i = s.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", e);
throw new QueryEvaluationException(e);
} catch (final IOException e) {
logger.error("Error deserializing statement", e);
throw new QueryEvaluationException(e);
}
}
@Override
public void remove() {
throw new UnsupportedOperationException("Remove not implemented");
}
@Override
public void close() throws QueryEvaluationException {
if (s != null) {
s.close();
}
}
};
}
Aggregations