Search in sources :

Example 6 with CloseableIteration

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();
            }
        };
    }
}
Also used : QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) Set(java.util.Set) BatchScanner(org.apache.accumulo.core.client.BatchScanner) ArrayList(java.util.ArrayList) Range(org.apache.accumulo.core.data.Range) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) CloseableIteration(org.eclipse.rdf4j.common.iteration.CloseableIteration) Entry(java.util.Map.Entry) DocumentIndexIntersectingIterator(org.apache.rya.accumulo.documentIndex.DocumentIndexIntersectingIterator) Iterator(java.util.Iterator) Value(org.apache.accumulo.core.data.Value) Key(org.apache.accumulo.core.data.Key) NoSuchElementException(java.util.NoSuchElementException)

Example 7 with CloseableIteration

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;
        }
    };
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) BindingSet(org.eclipse.rdf4j.query.BindingSet) MapBindingSet(org.eclipse.rdf4j.query.impl.MapBindingSet) Set(java.util.Set) HashSet(java.util.HashSet) Var(org.eclipse.rdf4j.query.algebra.Var) Statement(org.eclipse.rdf4j.model.Statement) Resource(org.eclipse.rdf4j.model.Resource) QueryModelNode(org.eclipse.rdf4j.query.algebra.QueryModelNode) CloseableIteration(org.eclipse.rdf4j.common.iteration.CloseableIteration) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) MapBindingSet(org.eclipse.rdf4j.query.impl.MapBindingSet) NoSuchElementException(java.util.NoSuchElementException) HashSet(java.util.HashSet)

Example 8 with CloseableIteration

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();
        }
    };
}
Also used : CloseableIteration(org.eclipse.rdf4j.common.iteration.CloseableIteration) RDFBeanInfo(org.cyberborean.rdfbeans.reflect.RDFBeanInfo) IRI(org.eclipse.rdf4j.model.IRI) Statement(org.eclipse.rdf4j.model.Statement) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException)

Example 9 with CloseableIteration

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);
            }
        }
    };
}
Also used : CloseableIterator(mil.nga.giat.geowave.core.store.CloseableIterator) RyaStatement(org.apache.rya.api.domain.RyaStatement) Statement(org.eclipse.rdf4j.model.Statement) CQLQuery(mil.nga.giat.geowave.adapter.vector.query.cql.CQLQuery) IOException(java.io.IOException) QueryOptions(mil.nga.giat.geowave.core.store.query.QueryOptions) SimpleFeature(org.opengis.feature.simple.SimpleFeature) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) GeoWavePluginException(mil.nga.giat.geowave.adapter.vector.plugin.GeoWavePluginException) ParseException(com.vividsolutions.jts.io.ParseException) SchemaException(org.geotools.feature.SchemaException) CQLException(org.geotools.filter.text.cql2.CQLException) IOException(java.io.IOException) CloseableIteration(org.eclipse.rdf4j.common.iteration.CloseableIteration) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) Filter(org.opengis.filter.Filter) CQLException(org.geotools.filter.text.cql2.CQLException)

Example 10 with CloseableIteration

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();
        }
    };
}
Also used : CloseableIteration(org.eclipse.rdf4j.common.iteration.CloseableIteration) FeatureIterator(org.geotools.feature.FeatureIterator) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) Query(org.geotools.data.Query) NearQuery(org.apache.rya.indexing.accumulo.geo.GeoTupleSet.GeoSearchFunctionFactory.NearQuery) Filter(org.opengis.filter.Filter) RyaStatement(org.apache.rya.api.domain.RyaStatement) Statement(org.eclipse.rdf4j.model.Statement) CQLException(org.geotools.filter.text.cql2.CQLException) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature)

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