Search in sources :

Example 11 with CloseableIteration

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);
            }
        }
    };
}
Also used : CloseableIteration(org.eclipse.rdf4j.common.iteration.CloseableIteration) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) RyaStatement(org.apache.rya.api.domain.RyaStatement) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) NoSuchElementException(java.util.NoSuchElementException)

Example 12 with CloseableIteration

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);
            }
        }
    };
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) RyaStatement(org.apache.rya.api.domain.RyaStatement) Statement(org.eclipse.rdf4j.model.Statement) ArrayList(java.util.ArrayList) RyaStatement(org.apache.rya.api.domain.RyaStatement) RdfCloudTripleStoreUtils(org.apache.rya.api.RdfCloudTripleStoreUtils) CloseableIteration(org.eclipse.rdf4j.common.iteration.CloseableIteration) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException)

Example 13 with CloseableIteration

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;
        }
    };
}
Also used : RyaIRI(org.apache.rya.api.domain.RyaIRI) EmptyIteration(org.eclipse.rdf4j.common.iteration.EmptyIteration) ArrayList(java.util.ArrayList) RyaStatement(org.apache.rya.api.domain.RyaStatement) RyaRange(org.apache.rya.api.domain.RyaRange) RyaIRIRange(org.apache.rya.api.domain.RyaIRIRange) RyaType(org.apache.rya.api.domain.RyaType) PeekingCloseableIteration(org.apache.rya.api.utils.PeekingCloseableIteration) CloseableIteration(org.eclipse.rdf4j.common.iteration.CloseableIteration) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) Map(java.util.Map)

Example 14 with CloseableIteration

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;
        }
    };
}
Also used : PeekingCloseableIteration(org.apache.rya.api.utils.PeekingCloseableIteration) CloseableIteration(org.eclipse.rdf4j.common.iteration.CloseableIteration) RyaIRI(org.apache.rya.api.domain.RyaIRI) ArrayList(java.util.ArrayList) RyaStatement(org.apache.rya.api.domain.RyaStatement) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) RyaRange(org.apache.rya.api.domain.RyaRange) RyaType(org.apache.rya.api.domain.RyaType)

Example 15 with CloseableIteration

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();
            }
        }
    };
}
Also used : CloseableIteration(org.eclipse.rdf4j.common.iteration.CloseableIteration) Entry(java.util.Map.Entry) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) RyaStatement(org.apache.rya.api.domain.RyaStatement) Statement(org.eclipse.rdf4j.model.Statement) Value(org.apache.accumulo.core.data.Value) CharacterCodingException(java.nio.charset.CharacterCodingException) IOException(java.io.IOException) Key(org.apache.accumulo.core.data.Key)

Aggregations

CloseableIteration (org.eclipse.rdf4j.common.iteration.CloseableIteration)17 RyaStatement (org.apache.rya.api.domain.RyaStatement)10 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)9 Statement (org.eclipse.rdf4j.model.Statement)7 BindingSet (org.eclipse.rdf4j.query.BindingSet)7 NoSuchElementException (java.util.NoSuchElementException)6 RyaDAOException (org.apache.rya.api.persist.RyaDAOException)6 ArrayList (java.util.ArrayList)5 IOException (java.io.IOException)4 Map (java.util.Map)4 RyaIRI (org.apache.rya.api.domain.RyaIRI)4 QueryBindingSet (org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet)4 Entry (java.util.Map.Entry)3 Key (org.apache.accumulo.core.data.Key)3 Value (org.apache.accumulo.core.data.Value)3 RyaType (org.apache.rya.api.domain.RyaType)3 IRI (org.eclipse.rdf4j.model.IRI)3 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)3 SailException (org.eclipse.rdf4j.sail.SailException)3 CharacterCodingException (java.nio.charset.CharacterCodingException)2