Search in sources :

Example 16 with CloseableIteration

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();
        }
    };
}
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)

Example 17 with CloseableIteration

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);
// }
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) EmptyBindingSet(org.eclipse.rdf4j.query.impl.EmptyBindingSet) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) IRI(org.eclipse.rdf4j.model.IRI) RyaIRI(org.apache.rya.api.domain.RyaIRI) Var(org.eclipse.rdf4j.query.algebra.Var) Resource(org.eclipse.rdf4j.model.Resource) SailException(org.eclipse.rdf4j.sail.SailException) CloseableIteration(org.eclipse.rdf4j.common.iteration.CloseableIteration) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) Value(org.eclipse.rdf4j.model.Value) NoSuchElementException(java.util.NoSuchElementException)

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