Search in sources :

Example 1 with CloseableIterator

use of org.apache.rya.api.utils.CloseableIterator in project incubator-rya by apache.

the class KeyValueJoinStateStore method getJoinedValues.

@Override
public CloseableIterator<VisibilityBindingSet> getJoinedValues(final BinaryResult result) {
    requireNonNull(result);
    // Get an iterator over the values that start with the join variables for the other side.
    final Side otherSide = result.getSide() == Side.LEFT ? Side.RIGHT : Side.LEFT;
    final VisibilityBindingSet bs = result.getResult();
    final String joinKeyPrefix = makeCommaDelimitedValues(otherSide, joinVars, bs);
    final String startKey = joinKeyPrefix + START_RANGE_SUFFIX;
    final String endKey = joinKeyPrefix + END_RANGE_SUFFIX;
    final KeyValueIterator<String, VisibilityBindingSet> rangeIt = store.range(startKey, endKey);
    // Return a CloseableIterator over the range's value fields, skipping the start and end entry.
    return new CloseableIterator<VisibilityBindingSet>() {

        private Optional<VisibilityBindingSet> next = null;

        @Override
        public boolean hasNext() {
            // If the iterator has not been initialized yet, read a value in.
            if (next == null) {
                next = readNext();
            }
            // Return true if there is a next value, otherwise false.
            return next.isPresent();
        }

        @Override
        public VisibilityBindingSet next() {
            // If the iterator has not been initialized yet, read a value in.
            if (next == null) {
                next = readNext();
            }
            // It's illegal to call next() when there is no next value.
            if (!next.isPresent()) {
                throw new IllegalStateException("May not invoke next() when there is nothing left in the Iterator.");
            }
            // Update and return the next value.
            final VisibilityBindingSet ret = next.get();
            log.debug("\nReturning: {}", ret);
            next = readNext();
            return ret;
        }

        private Optional<VisibilityBindingSet> readNext() {
            // Check to see if there's anything left in the iterator.
            if (!rangeIt.hasNext()) {
                return Optional.empty();
            }
            // Read a candidate key/value pair from the iterator.
            KeyValue<String, VisibilityBindingSet> candidate = rangeIt.next();
            // If we are initializing, then the first thing we must read is a start of range marker.
            if (next == null) {
                if (!candidate.key.endsWith(START_RANGE_SUFFIX)) {
                    throw new IllegalStateException("The first key encountered must be a start of range key.");
                }
                log.debug("Read the start of range markers.\n");
                // Read a new candidate to skip this one.
                if (!rangeIt.hasNext()) {
                    throw new IllegalStateException("There must be another entry after the start of range key.");
                }
                candidate = rangeIt.next();
            } else // If that value is an end of range key, then we are finished. Otherwise, return it.
            if (candidate.key.endsWith(END_RANGE_SUFFIX)) {
                log.debug("Read the end of range marker.\n");
                // If there are more messages, that's a problem.
                if (rangeIt.hasNext()) {
                    throw new IllegalStateException("The end of range marker must be the last key in the iterator.");
                }
                return Optional.empty();
            }
            // Otherwise we found a new value.
            return Optional.of(candidate.value);
        }

        @Override
        public void close() throws Exception {
            rangeIt.close();
        }
    };
}
Also used : Side(org.apache.rya.streams.kafka.processors.ProcessorResult.BinaryResult.Side) CloseableIterator(org.apache.rya.api.utils.CloseableIterator) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) Optional(java.util.Optional)

Example 2 with CloseableIterator

use of org.apache.rya.api.utils.CloseableIterator in project incubator-rya by apache.

the class MongoPcjDocuments method queryForBindings.

private CloseableIterator<BindingSet> queryForBindings(final Document query) {
    final FindIterable<Document> rez = pcjCollection.find(query);
    final Iterator<Document> resultsIter = rez.iterator();
    return new CloseableIterator<BindingSet>() {

        @Override
        public boolean hasNext() {
            return resultsIter.hasNext();
        }

        @Override
        public BindingSet next() {
            final Document bs = resultsIter.next();
            final MapBindingSet binding = new MapBindingSet();
            for (final String key : bs.keySet()) {
                if (key.equals(VISIBILITIES_FIELD)) {
                // has auths, is a visibility binding set.
                } else if (!key.equals("_id") && !key.equals(PCJ_ID)) {
                    // is the binding value.
                    final Document typeDoc = (Document) bs.get(key);
                    final URI dataType = new URIImpl(typeDoc.getString(BINDING_TYPE));
                    final RyaType type = new RyaType(dataType, typeDoc.getString(BINDING_VALUE));
                    final Value value = RyaToRdfConversions.convertValue(type);
                    binding.addBinding(key, value);
                }
            }
            return binding;
        }

        @Override
        public void close() throws Exception {
        }
    };
}
Also used : CloseableIterator(org.apache.rya.api.utils.CloseableIterator) Value(org.openrdf.model.Value) URIImpl(org.openrdf.model.impl.URIImpl) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Document(org.bson.Document) RyaType(org.apache.rya.api.domain.RyaType) URI(org.openrdf.model.URI)

Example 3 with CloseableIterator

use of org.apache.rya.api.utils.CloseableIterator in project incubator-rya by apache.

the class AccumuloPcjStorageIT method listResults.

@Test
public void listResults() throws Exception {
    // Setup the PCJ storage that will be tested against.
    final Connector connector = super.getClusterInstance().getConnector();
    final String ryaInstanceName = super.getRyaInstanceName();
    try (final PrecomputedJoinStorage pcjStorage = new AccumuloPcjStorage(connector, ryaInstanceName)) {
        // Create a PCJ.
        final String sparql = "SELECT * WHERE { ?a <http://isA> ?b }";
        final String pcjId = pcjStorage.createPcj(sparql);
        // Add some binding sets to it.
        final Set<VisibilityBindingSet> storedResults = new HashSet<>();
        final MapBindingSet aliceBS = new MapBindingSet();
        aliceBS.addBinding("a", new URIImpl("http://Alice"));
        aliceBS.addBinding("b", new URIImpl("http://Person"));
        storedResults.add(new VisibilityBindingSet(aliceBS, ""));
        final MapBindingSet charlieBS = new MapBindingSet();
        charlieBS.addBinding("a", new URIImpl("http://Charlie"));
        charlieBS.addBinding("b", new URIImpl("http://Comedian"));
        storedResults.add(new VisibilityBindingSet(charlieBS, ""));
        pcjStorage.addResults(pcjId, storedResults);
        // List the results that were stored.
        final Set<BindingSet> results = new HashSet<>();
        try (CloseableIterator<BindingSet> resultsIt = pcjStorage.listResults(pcjId)) {
            while (resultsIt.hasNext()) {
                results.add(resultsIt.next());
            }
        }
        // The stored results are returned as normal binding sets, so unwrap them.
        final Set<BindingSet> expectedResults = storedResults.stream().map(visBs -> visBs.getBindingSet()).collect(Collectors.toSet());
        assertEquals(expectedResults, results);
    }
}
Also used : AccumuloRyaITBase(org.apache.rya.accumulo.AccumuloRyaITBase) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) URIImpl(org.openrdf.model.impl.URIImpl) PCJDetails(org.apache.rya.api.instance.RyaDetails.PCJIndexDetails.PCJDetails) NotInitializedException(org.apache.rya.api.instance.RyaDetailsRepository.NotInitializedException) ShiftVarOrderFactory(org.apache.rya.indexing.pcj.storage.accumulo.ShiftVarOrderFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Connector(org.apache.accumulo.core.client.Connector) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RyaDetailsRepository(org.apache.rya.api.instance.RyaDetailsRepository) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) BindingSet(org.openrdf.query.BindingSet) VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) MalformedQueryException(org.openrdf.query.MalformedQueryException) PCJStorageException(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException) CloseableIterator(org.apache.rya.api.utils.CloseableIterator) ImmutableMap(com.google.common.collect.ImmutableMap) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) AccumuloException(org.apache.accumulo.core.client.AccumuloException) PrecomputedJoinStorage(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage) List(java.util.List) AccumuloRyaInstanceDetailsRepository(org.apache.rya.accumulo.instance.AccumuloRyaInstanceDetailsRepository) RyaDetailsRepositoryException(org.apache.rya.api.instance.RyaDetailsRepository.RyaDetailsRepositoryException) Assert.assertFalse(org.junit.Assert.assertFalse) AccumuloPcjStorage(org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Connector(org.apache.accumulo.core.client.Connector) MapBindingSet(org.openrdf.query.impl.MapBindingSet) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) BindingSet(org.openrdf.query.BindingSet) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) AccumuloPcjStorage(org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage) URIImpl(org.openrdf.model.impl.URIImpl) PrecomputedJoinStorage(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage) MapBindingSet(org.openrdf.query.impl.MapBindingSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

CloseableIterator (org.apache.rya.api.utils.CloseableIterator)3 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)2 URIImpl (org.openrdf.model.impl.URIImpl)2 MapBindingSet (org.openrdf.query.impl.MapBindingSet)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 AccumuloException (org.apache.accumulo.core.client.AccumuloException)1 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)1 Connector (org.apache.accumulo.core.client.Connector)1 AccumuloRyaITBase (org.apache.rya.accumulo.AccumuloRyaITBase)1 AccumuloRyaInstanceDetailsRepository (org.apache.rya.accumulo.instance.AccumuloRyaInstanceDetailsRepository)1 RyaType (org.apache.rya.api.domain.RyaType)1 PCJDetails (org.apache.rya.api.instance.RyaDetails.PCJIndexDetails.PCJDetails)1 RyaDetailsRepository (org.apache.rya.api.instance.RyaDetailsRepository)1