Search in sources :

Example 1 with CassandraRow

use of org.locationtech.geowave.datastore.cassandra.CassandraRow in project geowave by locationtech.

the class CassandraReader method initScanner.

protected void initScanner() {
    final Collection<SinglePartitionQueryRanges> ranges = readerParams.getQueryRanges().getPartitionQueryRanges();
    if ((ranges != null) && !ranges.isEmpty()) {
        iterator = operations.getBatchedRangeRead(readerParams.getIndex().getName(), readerParams.getAdapterIds(), ranges, DataStoreUtils.isMergingIteratorRequired(readerParams, visibilityEnabled), rowTransformer, new ClientVisibilityFilter(Sets.newHashSet(readerParams.getAdditionalAuthorizations()))).results();
    } else {
        // TODO figure out the query select by adapter IDs here
        final Select select = operations.getSelect(readerParams.getIndex().getName());
        CloseableIterator<CassandraRow> results = operations.executeQuery(select.build());
        if ((readerParams.getAdapterIds() != null) && (readerParams.getAdapterIds().length > 0)) {
            // TODO because we aren't filtering server-side by adapter ID,
            // we will need to filter here on the client
            results = new CloseableIteratorWrapper<>(results, Iterators.filter(results, input -> ArrayUtils.contains(readerParams.getAdapterIds(), input.getAdapterId())));
        }
        iterator = wrapResults(results, readerParams);
    }
}
Also used : SinglePartitionQueryRanges(org.locationtech.geowave.core.index.SinglePartitionQueryRanges) Select(com.datastax.oss.driver.api.querybuilder.select.Select) CassandraRow(org.locationtech.geowave.datastore.cassandra.CassandraRow) ClientVisibilityFilter(org.locationtech.geowave.core.store.query.filter.ClientVisibilityFilter)

Example 2 with CassandraRow

use of org.locationtech.geowave.datastore.cassandra.CassandraRow in project geowave by locationtech.

the class BatchedRangeRead method executeQueryAsync.

public CloseableIterator<T> executeQueryAsync(final Statement... statements) {
    // first create a list of asynchronous query executions
    final List<CompletionStage<AsyncResultSet>> futures = Lists.newArrayListWithExpectedSize(statements.length);
    final BlockingQueue<Object> results = new LinkedBlockingQueue<>(MAX_BOUNDED_READS_ENQUEUED);
    new Thread(new Runnable() {

        @Override
        public void run() {
            // set it to 1 to make sure all queries are submitted in
            // the loop
            final AtomicInteger queryCount = new AtomicInteger(1);
            for (final Statement s : statements) {
                try {
                    readSemaphore.acquire();
                    final CompletionStage<AsyncResultSet> f = operations.getSession().executeAsync(s);
                    synchronized (futures) {
                        futures.add(f);
                    }
                    queryCount.incrementAndGet();
                    f.whenCompleteAsync((result, t) -> {
                        if (result != null) {
                            try {
                                final Iterator<GeoWaveRow> iterator = (Iterator) Streams.stream(ResultSets.newInstance(result)).map(row -> new CassandraRow(row)).filter(filter).iterator();
                                rowTransformer.apply(rowMerging ? new GeoWaveRowMergingIterator(iterator) : iterator).forEachRemaining(row -> {
                                    try {
                                        results.put(row);
                                    } catch (final InterruptedException e) {
                                        LOGGER.warn("interrupted while waiting to enqueue a cassandra result", e);
                                    }
                                });
                            } finally {
                                checkFinalize(queryCount, results, readSemaphore);
                            }
                        } else if (t != null) {
                            checkFinalize(queryCount, results, readSemaphore);
                            // can do logging or start counting errors.
                            if (!(t instanceof CancellationException)) {
                                LOGGER.error("Failure from async query", t);
                                throw new RuntimeException(t);
                            }
                        }
                    });
                } catch (final InterruptedException e) {
                    LOGGER.warn("Exception while executing query", e);
                    readSemaphore.release();
                }
            }
            // then decrement
            if (queryCount.decrementAndGet() <= 0) {
                // statements submitted
                try {
                    results.put(RowConsumer.POISON);
                } catch (final InterruptedException e) {
                    LOGGER.error("Interrupted while finishing blocking queue, this may result in deadlock!");
                }
            }
        }
    }, "Cassandra Query Executor").start();
    return new CloseableIteratorWrapper<T>(new Closeable() {

        @Override
        public void close() throws IOException {
            synchronized (futures) {
                for (final CompletionStage<AsyncResultSet> f : futures) {
                    f.toCompletableFuture().cancel(true);
                }
            }
        }
    }, new RowConsumer(results));
}
Also used : CassandraRow(org.locationtech.geowave.datastore.cassandra.CassandraRow) Arrays(java.util.Arrays) ResultSets(com.datastax.oss.driver.internal.core.cql.ResultSets) SinglePartitionQueryRanges(org.locationtech.geowave.core.index.SinglePartitionQueryRanges) GeoWaveRowIteratorTransformer(org.locationtech.geowave.core.store.entities.GeoWaveRowIteratorTransformer) LoggerFactory(org.slf4j.LoggerFactory) ArrayUtils(org.apache.commons.lang3.ArrayUtils) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) Lists(com.google.common.collect.Lists) CloseableIteratorWrapper(org.locationtech.geowave.core.store.CloseableIteratorWrapper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteArrayRange(org.locationtech.geowave.core.index.ByteArrayRange) GeoWaveRow(org.locationtech.geowave.core.store.entities.GeoWaveRow) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) CancellationException(java.util.concurrent.CancellationException) GeoWaveRowMergingIterator(org.locationtech.geowave.core.store.entities.GeoWaveRowMergingIterator) BoundStatementBuilder(com.datastax.oss.driver.api.core.cql.BoundStatementBuilder) Semaphore(java.util.concurrent.Semaphore) Predicate(java.util.function.Predicate) Collection(java.util.Collection) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) Streams(com.google.common.collect.Streams) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) RowConsumer(org.locationtech.geowave.core.store.util.RowConsumer) TypeCodecs(com.datastax.oss.driver.api.core.type.codec.TypeCodecs) CassandraUtils(org.locationtech.geowave.datastore.cassandra.util.CassandraUtils) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) CloseableIterator(org.locationtech.geowave.core.store.CloseableIterator) Closeable(java.io.Closeable) CassandraField(org.locationtech.geowave.datastore.cassandra.CassandraRow.CassandraField) Statement(com.datastax.oss.driver.api.core.cql.Statement) GeoWaveRow(org.locationtech.geowave.core.store.entities.GeoWaveRow) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) Closeable(java.io.Closeable) GeoWaveRowMergingIterator(org.locationtech.geowave.core.store.entities.GeoWaveRowMergingIterator) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Iterator(java.util.Iterator) GeoWaveRowMergingIterator(org.locationtech.geowave.core.store.entities.GeoWaveRowMergingIterator) CloseableIterator(org.locationtech.geowave.core.store.CloseableIterator) CompletionStage(java.util.concurrent.CompletionStage) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) Statement(com.datastax.oss.driver.api.core.cql.Statement) CassandraRow(org.locationtech.geowave.datastore.cassandra.CassandraRow) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CancellationException(java.util.concurrent.CancellationException) RowConsumer(org.locationtech.geowave.core.store.util.RowConsumer) CloseableIteratorWrapper(org.locationtech.geowave.core.store.CloseableIteratorWrapper)

Aggregations

SinglePartitionQueryRanges (org.locationtech.geowave.core.index.SinglePartitionQueryRanges)2 CassandraRow (org.locationtech.geowave.datastore.cassandra.CassandraRow)2 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)1 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)1 BoundStatementBuilder (com.datastax.oss.driver.api.core.cql.BoundStatementBuilder)1 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)1 Statement (com.datastax.oss.driver.api.core.cql.Statement)1 TypeCodecs (com.datastax.oss.driver.api.core.type.codec.TypeCodecs)1 Select (com.datastax.oss.driver.api.querybuilder.select.Select)1 ResultSets (com.datastax.oss.driver.internal.core.cql.ResultSets)1 Lists (com.google.common.collect.Lists)1 Streams (com.google.common.collect.Streams)1 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 List (java.util.List)1