Search in sources :

Example 1 with GeoWaveRedisPersistedRow

use of org.locationtech.geowave.datastore.redis.util.GeoWaveRedisPersistedRow in project geowave by locationtech.

the class RedisReader method createIteratorForReader.

private CloseableIterator<T> createIteratorForReader(final RedissonClient client, final Compression compression, final ReaderParams<T> readerParams, final GeoWaveRowIteratorTransformer<T> rowTransformer, final String namespace, final boolean visibilityEnabled, final boolean async) {
    final Collection<SinglePartitionQueryRanges> ranges = readerParams.getQueryRanges().getPartitionQueryRanges();
    final Set<String> authorizations = Sets.newHashSet(readerParams.getAdditionalAuthorizations());
    if ((ranges != null) && !ranges.isEmpty()) {
        return createIterator(client, compression, readerParams, readerParams.getRowTransformer(), namespace, ranges, authorizations, visibilityEnabled, async);
    } else {
        final Iterator<GeoWaveRedisRow>[] iterators = new Iterator[readerParams.getAdapterIds().length];
        int i = 0;
        for (final short adapterId : readerParams.getAdapterIds()) {
            final Pair<Boolean, Boolean> groupByRowAndSortByTime = RedisUtils.isGroupByRowAndIsSortByTime(readerParams, adapterId);
            final String setNamePrefix = RedisUtils.getRowSetPrefix(namespace, readerParams.getInternalAdapterStore().getTypeName(adapterId), readerParams.getIndex().getName());
            final Stream<Pair<ByteArray, Iterator<ScoredEntry<GeoWaveRedisPersistedRow>>>> streamIt = RedisUtils.getPartitions(client, setNamePrefix).stream().map(p -> {
                final Iterator<ScoredEntry<GeoWaveRedisPersistedRow>> result = RedisUtils.getRowSet(client, compression, setNamePrefix, p.getBytes(), groupByRowAndSortByTime.getRight(), visibilityEnabled).entryRange(Double.NEGATIVE_INFINITY, true, Double.POSITIVE_INFINITY, true);
                final Iterator<ScoredEntry<GeoWaveRedisPersistedRow>> it = groupByRowAndSortByTime.getLeft() ? RedisUtils.groupByRow(result, groupByRowAndSortByTime.getRight()) : result;
                return ImmutablePair.of(p, it);
            });
            iterators[i++] = Iterators.concat(streamIt.map(p -> Iterators.transform(p.getRight(), pr -> new GeoWaveRedisRow(pr.getValue(), adapterId, p.getLeft().getBytes(), RedisUtils.getFullSortKey(pr.getScore(), pr.getValue().getSortKeyPrecisionBeyondScore())))).iterator());
        }
        return wrapResults(Iterators.concat(iterators), readerParams, rowTransformer, authorizations, visibilityEnabled);
    }
}
Also used : ByteArray(org.locationtech.geowave.core.index.ByteArray) Arrays(java.util.Arrays) GeoWaveRedisRow(org.locationtech.geowave.datastore.redis.util.GeoWaveRedisRow) RecordReaderParams(org.locationtech.geowave.mapreduce.splits.RecordReaderParams) SinglePartitionQueryRanges(org.locationtech.geowave.core.index.SinglePartitionQueryRanges) GeoWaveRowIteratorTransformer(org.locationtech.geowave.core.store.entities.GeoWaveRowIteratorTransformer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayUtils(org.apache.commons.lang3.ArrayUtils) RedisUtils(org.locationtech.geowave.datastore.redis.util.RedisUtils) Iterators(com.google.common.collect.Iterators) Compression(org.locationtech.geowave.datastore.redis.config.RedisOptions.Compression) Pair(org.apache.commons.lang3.tuple.Pair) CloseableIteratorWrapper(org.locationtech.geowave.core.store.CloseableIteratorWrapper) ReaderParams(org.locationtech.geowave.core.store.operations.ReaderParams) ByteArrayRange(org.locationtech.geowave.core.index.ByteArrayRange) ClientVisibilityFilter(org.locationtech.geowave.core.store.query.filter.ClientVisibilityFilter) RedissonClient(org.redisson.api.RedissonClient) DataStoreUtils(org.locationtech.geowave.core.store.util.DataStoreUtils) GeoWaveRow(org.locationtech.geowave.core.store.entities.GeoWaveRow) Iterator(java.util.Iterator) GeoWaveRowMergingIterator(org.locationtech.geowave.core.store.entities.GeoWaveRowMergingIterator) DataIndexReaderParams(org.locationtech.geowave.core.store.operations.DataIndexReaderParams) RowReader(org.locationtech.geowave.core.store.operations.RowReader) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) Streams(com.google.common.collect.Streams) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) ScoredEntry(org.redisson.client.protocol.ScoredEntry) Sets(com.google.common.collect.Sets) RangeReaderParams(org.locationtech.geowave.core.store.operations.RangeReaderParams) Stream(java.util.stream.Stream) CloseableIterator(org.locationtech.geowave.core.store.CloseableIterator) GeoWaveRedisPersistedRow(org.locationtech.geowave.datastore.redis.util.GeoWaveRedisPersistedRow) Closeable(java.io.Closeable) Serialization(org.locationtech.geowave.datastore.redis.config.RedisOptions.Serialization) GeoWaveRowRange(org.locationtech.geowave.mapreduce.splits.GeoWaveRowRange) Collections(java.util.Collections) SinglePartitionQueryRanges(org.locationtech.geowave.core.index.SinglePartitionQueryRanges) GeoWaveRedisRow(org.locationtech.geowave.datastore.redis.util.GeoWaveRedisRow) Iterator(java.util.Iterator) GeoWaveRowMergingIterator(org.locationtech.geowave.core.store.entities.GeoWaveRowMergingIterator) CloseableIterator(org.locationtech.geowave.core.store.CloseableIterator) ScoredEntry(org.redisson.client.protocol.ScoredEntry) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair)

Example 2 with GeoWaveRedisPersistedRow

use of org.locationtech.geowave.datastore.redis.util.GeoWaveRedisPersistedRow in project geowave by locationtech.

the class BatchedRangeRead method executeQueryAsync.

private CloseableIterator<T> executeQueryAsync(final List<RangeReadInfo> reads) {
    // first create a list of asynchronous query executions
    final List<RFuture<Collection<ScoredEntry<GeoWaveRedisPersistedRow>>>> futures = Lists.newArrayListWithExpectedSize(reads.size());
    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 RangeReadInfo r : reads) {
                try {
                    ByteArray partitionKey;
                    if ((r.partitionKey == null) || (r.partitionKey.length == 0)) {
                        partitionKey = EMPTY_PARTITION_KEY;
                    } else {
                        partitionKey = new ByteArray(r.partitionKey);
                    }
                    readSemaphore.acquire();
                    final RFuture<Collection<ScoredEntry<GeoWaveRedisPersistedRow>>> f = setCache.get(partitionKey).entryRangeAsync(r.startScore, true, r.endScore, // sure the end is inclusive and do more precise client-side filtering
                    ((r.endScore <= r.startScore) || (r.explicitEndCheck != null)));
                    queryCount.incrementAndGet();
                    f.handle((result, throwable) -> {
                        if (!f.isSuccess()) {
                            if (!f.isCancelled()) {
                                LOGGER.warn("Async Redis query failed", throwable);
                            }
                            checkFinalize(readSemaphore, results, queryCount);
                            return result;
                        } else {
                            try {
                                result.forEach(i -> i.getValue().setPartitionKey(r.partitionKey));
                                transformAndFilter(result.stream().filter(e -> r.passesExplicitRowChecks(e)).iterator()).forEachRemaining(row -> {
                                    try {
                                        results.put(row);
                                    } catch (final InterruptedException e) {
                                        LOGGER.warn("interrupted while waiting to enqueue a redis result", e);
                                    }
                                });
                            } finally {
                                checkFinalize(readSemaphore, results, queryCount);
                            }
                            return result;
                        }
                    });
                    synchronized (futures) {
                        futures.add(f);
                    }
                } 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!");
                }
            }
        }
    }, "Redis Query Executor").start();
    return new CloseableIteratorWrapper<>(new Closeable() {

        @Override
        public void close() throws IOException {
            List<RFuture<Collection<ScoredEntry<GeoWaveRedisPersistedRow>>>> newFutures;
            synchronized (futures) {
                newFutures = new ArrayList<>(futures);
            }
            for (final RFuture<Collection<ScoredEntry<GeoWaveRedisPersistedRow>>> f : newFutures) {
                f.cancel(true);
            }
        }
    }, new RowConsumer<>(results));
}
Also used : ByteArray(org.locationtech.geowave.core.index.ByteArray) GeoWaveRedisRow(org.locationtech.geowave.datastore.redis.util.GeoWaveRedisRow) SinglePartitionQueryRanges(org.locationtech.geowave.core.index.SinglePartitionQueryRanges) GeoWaveRowIteratorTransformer(org.locationtech.geowave.core.store.entities.GeoWaveRowIteratorTransformer) LoggerFactory(org.slf4j.LoggerFactory) RedisUtils(org.locationtech.geowave.datastore.redis.util.RedisUtils) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) RFuture(org.redisson.api.RFuture) Compression(org.locationtech.geowave.datastore.redis.config.RedisOptions.Compression) Lists(com.google.common.collect.Lists) Pair(org.apache.commons.lang3.tuple.Pair) CloseableIteratorWrapper(org.locationtech.geowave.core.store.CloseableIteratorWrapper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RedissonClient(org.redisson.api.RedissonClient) GeoWaveRow(org.locationtech.geowave.core.store.entities.GeoWaveRow) UnsignedBytes(com.google.common.primitives.UnsignedBytes) RedisScoredSetWrapper(org.locationtech.geowave.datastore.redis.util.RedisScoredSetWrapper) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) GeoWaveRowMergingIterator(org.locationtech.geowave.core.store.entities.GeoWaveRowMergingIterator) LoadingCache(com.github.benmanes.caffeine.cache.LoadingCache) Semaphore(java.util.concurrent.Semaphore) Predicate(java.util.function.Predicate) Collection(java.util.Collection) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) Streams(com.google.common.collect.Streams) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) RowConsumer(org.locationtech.geowave.core.store.util.RowConsumer) ScoredEntry(org.redisson.client.protocol.ScoredEntry) Serializable(java.io.Serializable) List(java.util.List) CloseableIterator(org.locationtech.geowave.core.store.CloseableIterator) GeoWaveRedisPersistedRow(org.locationtech.geowave.datastore.redis.util.GeoWaveRedisPersistedRow) Closeable(java.io.Closeable) Comparator(java.util.Comparator) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RFuture(org.redisson.api.RFuture) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) GeoWaveRedisPersistedRow(org.locationtech.geowave.datastore.redis.util.GeoWaveRedisPersistedRow) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScoredEntry(org.redisson.client.protocol.ScoredEntry) ByteArray(org.locationtech.geowave.core.index.ByteArray) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) CloseableIteratorWrapper(org.locationtech.geowave.core.store.CloseableIteratorWrapper)

Aggregations

Iterators (com.google.common.collect.Iterators)2 Streams (com.google.common.collect.Streams)2 Closeable (java.io.Closeable)2 IOException (java.io.IOException)2 Collection (java.util.Collection)2 Iterator (java.util.Iterator)2 Pair (org.apache.commons.lang3.tuple.Pair)2 ByteArray (org.locationtech.geowave.core.index.ByteArray)2 SinglePartitionQueryRanges (org.locationtech.geowave.core.index.SinglePartitionQueryRanges)2 CloseableIterator (org.locationtech.geowave.core.store.CloseableIterator)2 CloseableIteratorWrapper (org.locationtech.geowave.core.store.CloseableIteratorWrapper)2 GeoWaveRow (org.locationtech.geowave.core.store.entities.GeoWaveRow)2 GeoWaveRowIteratorTransformer (org.locationtech.geowave.core.store.entities.GeoWaveRowIteratorTransformer)2 GeoWaveRowMergingIterator (org.locationtech.geowave.core.store.entities.GeoWaveRowMergingIterator)2 Compression (org.locationtech.geowave.datastore.redis.config.RedisOptions.Compression)2 GeoWaveRedisPersistedRow (org.locationtech.geowave.datastore.redis.util.GeoWaveRedisPersistedRow)2 GeoWaveRedisRow (org.locationtech.geowave.datastore.redis.util.GeoWaveRedisRow)2 RedisUtils (org.locationtech.geowave.datastore.redis.util.RedisUtils)2 RedissonClient (org.redisson.api.RedissonClient)2 ScoredEntry (org.redisson.client.protocol.ScoredEntry)2