Search in sources :

Example 1 with AbstractIterator

use of org.infinispan.commons.util.AbstractIterator in project infinispan by infinispan.

the class DistributedCacheStream method iterator.

// The next ones are key tracking terminal operators
@Override
public Iterator<R> iterator() {
    log.tracef("Distributed iterator invoked with rehash: %s", rehashAware);
    Function usedTransformer;
    if (intermediateOperations.isEmpty()) {
        usedTransformer = MarshallableFunctions.identity();
    } else {
        usedTransformer = new CacheIntermediatePublisher(intermediateOperations);
    }
    DeliveryGuarantee deliveryGuarantee = rehashAware ? DeliveryGuarantee.EXACTLY_ONCE : DeliveryGuarantee.AT_MOST_ONCE;
    Publisher<R> publisherToSubscribeTo;
    SegmentPublisherSupplier<R> publisher;
    if (toKeyFunction == null) {
        publisher = cpm.keyPublisher(segmentsToFilter, keysToFilter, invocationContext, explicitFlags, deliveryGuarantee, distributedBatchSize, usedTransformer);
    } else {
        publisher = cpm.entryPublisher(segmentsToFilter, keysToFilter, invocationContext, explicitFlags, deliveryGuarantee, distributedBatchSize, usedTransformer);
    }
    CompletionSegmentTracker<R> segmentTracker;
    if (segmentCompletionListener != null) {
        // Tracker relies on ordering that a segment completion occurs
        segmentTracker = new CompletionSegmentTracker<>(segmentCompletionListener);
        publisherToSubscribeTo = Flowable.fromPublisher(publisher.publisherWithSegments()).mapOptional(segmentTracker);
    } else {
        segmentTracker = null;
        publisherToSubscribeTo = publisher.publisherWithoutSegments();
    }
    CloseableIterator<R> realIterator = Closeables.iterator(Flowable.fromPublisher(publisherToSubscribeTo).onErrorResumeNext(RxJavaInterop.cacheExceptionWrapper()), distributedBatchSize);
    onClose(realIterator::close);
    if (segmentTracker != null) {
        return new AbstractIterator<R>() {

            @Override
            protected R getNext() {
                if (realIterator.hasNext()) {
                    R value = realIterator.next();
                    segmentTracker.returningObject(value);
                    return value;
                } else {
                    segmentTracker.onComplete();
                }
                return null;
            }
        };
    }
    return realIterator;
}
Also used : BiFunction(java.util.function.BiFunction) ToLongFunction(java.util.function.ToLongFunction) ToDoubleFunction(java.util.function.ToDoubleFunction) Function(java.util.function.Function) IntFunction(java.util.function.IntFunction) ToIntFunction(java.util.function.ToIntFunction) DeliveryGuarantee(org.infinispan.reactive.publisher.impl.DeliveryGuarantee) AbstractIterator(org.infinispan.commons.util.AbstractIterator)

Example 2 with AbstractIterator

use of org.infinispan.commons.util.AbstractIterator in project infinispan by infinispan.

the class RocksDBStore method actualPurgeExpired.

private Flowable<MarshallableEntry<K, V>> actualPurgeExpired(long now) {
    // The following flowable is responsible for emitting entries that have expired from expiredDb and removing the
    // given entries
    Flowable<byte[]> expiredFlowable = Flowable.using(() -> {
        ReadOptions readOptions = new ReadOptions().setFillCache(false);
        return new AbstractMap.SimpleImmutableEntry<>(readOptions, expiredDb.newIterator(readOptions));
    }, entry -> {
        if (entry.getValue() == null) {
            return Flowable.empty();
        }
        RocksIterator iterator = entry.getValue();
        iterator.seekToFirst();
        return Flowable.fromIterable(() -> new AbstractIterator<byte[]>() {

            @Override
            protected byte[] getNext() {
                if (!iterator.isValid()) {
                    return null;
                }
                byte[] keyBytes = iterator.key();
                Long time = unmarshall(keyBytes);
                if (time > now)
                    return null;
                try {
                    expiredDb.delete(keyBytes);
                } catch (RocksDBException e) {
                    throw new PersistenceException(e);
                }
                byte[] value = iterator.value();
                iterator.next();
                return value;
            }
        });
    }, entry -> {
        entry.getKey().close();
        RocksIterator rocksIterator = entry.getValue();
        if (rocksIterator != null) {
            rocksIterator.close();
        }
    });
    Flowable<MarshallableEntry<K, V>> expiredEntryFlowable = expiredFlowable.flatMap(expiredBytes -> {
        Object bucketKey = unmarshall(expiredBytes);
        if (bucketKey instanceof ExpiryBucket) {
            return Flowable.fromIterable(((ExpiryBucket) bucketKey).entries).flatMapMaybe(marshalledKey -> {
                ColumnFamilyHandle columnFamilyHandle = handler.getHandleForMarshalledKey(marshalledKey);
                MarshalledValue mv = handlePossiblyExpiredKey(columnFamilyHandle, marshalledKey, now);
                return mv == null ? Maybe.empty() : Maybe.just(entryFactory.create(unmarshall(marshalledKey), mv));
            });
        } else {
            // The bucketKey is an actual key
            ColumnFamilyHandle columnFamilyHandle = handler.getHandle(bucketKey);
            MarshalledValue mv = handlePossiblyExpiredKey(columnFamilyHandle, marshall(bucketKey), now);
            return mv == null ? Flowable.empty() : Flowable.just(entryFactory.create(bucketKey, mv));
        }
    });
    if (log.isTraceEnabled()) {
        // Note this tracing only works properly for one subscriber
        FlowableProcessor<MarshallableEntry<K, V>> mirrorEntries = UnicastProcessor.create();
        expiredEntryFlowable = expiredEntryFlowable.doOnEach(mirrorEntries).doOnSubscribe(subscription -> log.tracef("Purging entries from RocksDBStore"));
        mirrorEntries.count().subscribe(count -> log.tracef("Purged %d entries from RocksDBStore", count));
    }
    return expiredEntryFlowable;
}
Also used : Arrays(java.util.Arrays) MarshalledValue(org.infinispan.persistence.spi.MarshalledValue) IntSets(org.infinispan.commons.util.IntSets) LogFactory(org.infinispan.util.logging.LogFactory) MarshallableEntryFactory(org.infinispan.persistence.spi.MarshallableEntryFactory) ColumnFamilyDescriptor(org.rocksdb.ColumnFamilyDescriptor) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) AdvancedCache(org.infinispan.AdvancedCache) Log(org.infinispan.persistence.rocksdb.logging.Log) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) ColumnFamilyOptions(org.rocksdb.ColumnFamilyOptions) Map(java.util.Map) ConfiguredBy(org.infinispan.commons.configuration.ConfiguredBy) RocksDBStoreConfiguration(org.infinispan.persistence.rocksdb.configuration.RocksDBStoreConfiguration) RocksDBException(org.rocksdb.RocksDBException) Path(java.nio.file.Path) ProtoField(org.infinispan.protostream.annotations.ProtoField) EnumSet(java.util.EnumSet) PERSISTENCE(org.infinispan.util.logging.Log.PERSISTENCE) AbstractIterator(org.infinispan.commons.util.AbstractIterator) ReadOptions(org.rocksdb.ReadOptions) RxJavaInterop(org.infinispan.commons.reactive.RxJavaInterop) Predicate(java.util.function.Predicate) CacheConfigurationException(org.infinispan.commons.CacheConfigurationException) MethodHandles(java.lang.invoke.MethodHandles) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) UnicastProcessor(io.reactivex.rxjava3.processors.UnicastProcessor) WriteOptions(org.rocksdb.WriteOptions) Objects(java.util.Objects) IntSet(org.infinispan.commons.util.IntSet) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle) Options(org.rocksdb.Options) InitializationContext(org.infinispan.persistence.spi.InitializationContext) ProtoTypeId(org.infinispan.protostream.annotations.ProtoTypeId) PersistenceException(org.infinispan.persistence.spi.PersistenceException) WriteBatch(org.rocksdb.WriteBatch) Metadata(org.infinispan.metadata.Metadata) CompletionStages(org.infinispan.util.concurrent.CompletionStages) Function(java.util.function.Function) ProtoStreamTypeIds(org.infinispan.commons.marshall.ProtoStreamTypeIds) MarshallableEntryImpl(org.infinispan.marshall.persistence.impl.MarshallableEntryImpl) RocksIterator(org.rocksdb.RocksIterator) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) ProtoFactory(org.infinispan.protostream.annotations.ProtoFactory) HashSet(java.util.HashSet) Maybe(io.reactivex.rxjava3.core.Maybe) Version(org.infinispan.commons.util.Version) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) RocksDB(org.rocksdb.RocksDB) PrivateMetadata(org.infinispan.metadata.impl.PrivateMetadata) MarshallableEntry(org.infinispan.persistence.spi.MarshallableEntry) Properties(java.util.Properties) Flowable(io.reactivex.rxjava3.core.Flowable) Files(java.nio.file.Files) NonBlockingStore(org.infinispan.persistence.spi.NonBlockingStore) DBOptions(org.rocksdb.DBOptions) Publisher(org.reactivestreams.Publisher) Util(org.infinispan.commons.util.Util) IOException(java.io.IOException) MarshallUtil(org.infinispan.commons.marshall.MarshallUtil) File(java.io.File) BuiltinComparator(org.rocksdb.BuiltinComparator) FlowableProcessor(io.reactivex.rxjava3.processors.FlowableProcessor) PersistenceUtil(org.infinispan.persistence.internal.PersistenceUtil) AbstractMap(java.util.AbstractMap) PersistenceMarshaller(org.infinispan.marshall.persistence.PersistenceMarshaller) BlockingManager(org.infinispan.util.concurrent.BlockingManager) Marshaller(org.infinispan.commons.marshall.Marshaller) TimeService(org.infinispan.commons.time.TimeService) RocksDBException(org.rocksdb.RocksDBException) MarshalledValue(org.infinispan.persistence.spi.MarshalledValue) RocksIterator(org.rocksdb.RocksIterator) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle) ReadOptions(org.rocksdb.ReadOptions) PersistenceException(org.infinispan.persistence.spi.PersistenceException) MarshallableEntry(org.infinispan.persistence.spi.MarshallableEntry)

Aggregations

Function (java.util.function.Function)2 AbstractIterator (org.infinispan.commons.util.AbstractIterator)2 Flowable (io.reactivex.rxjava3.core.Flowable)1 Maybe (io.reactivex.rxjava3.core.Maybe)1 FlowableProcessor (io.reactivex.rxjava3.processors.FlowableProcessor)1 UnicastProcessor (io.reactivex.rxjava3.processors.UnicastProcessor)1 File (java.io.File)1 IOException (java.io.IOException)1 MethodHandles (java.lang.invoke.MethodHandles)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 StandardCopyOption (java.nio.file.StandardCopyOption)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 EnumSet (java.util.EnumSet)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1