All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.infinispan.stream.impl.local.LocalCacheStream Maven / Gradle / Ivy

There is a newer version: 9.1.7.Final
Show newest version
package org.infinispan.stream.impl.local;

import java.util.Comparator;
import java.util.Optional;
import java.util.Set;
import java.util.Spliterator;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.stream.Collector;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;

import org.infinispan.Cache;
import org.infinispan.CacheStream;
import org.infinispan.commons.util.CloseableIterator;
import org.infinispan.commons.util.Closeables;
import org.infinispan.factories.ComponentRegistry;
import org.infinispan.stream.CacheAware;
import org.infinispan.stream.impl.intops.IntermediateOperation;
import org.infinispan.stream.impl.intops.object.DistinctOperation;
import org.infinispan.stream.impl.intops.object.FilterOperation;
import org.infinispan.stream.impl.intops.object.FlatMapOperation;
import org.infinispan.stream.impl.intops.object.FlatMapToDoubleOperation;
import org.infinispan.stream.impl.intops.object.FlatMapToIntOperation;
import org.infinispan.stream.impl.intops.object.FlatMapToLongOperation;
import org.infinispan.stream.impl.intops.object.LimitOperation;
import org.infinispan.stream.impl.intops.object.MapOperation;
import org.infinispan.stream.impl.intops.object.MapToDoubleOperation;
import org.infinispan.stream.impl.intops.object.MapToIntOperation;
import org.infinispan.stream.impl.intops.object.MapToLongOperation;
import org.infinispan.stream.impl.intops.object.PeekOperation;
import org.infinispan.stream.impl.intops.object.SkipOperation;
import org.infinispan.stream.impl.intops.object.SortedComparatorOperation;
import org.infinispan.stream.impl.intops.object.SortedOperation;
import org.infinispan.util.function.RemovableFunction;

/**
 * CacheStream that is to be used locally.  This allows for full functionality of a regular stream but also has options
 * to filter by keys and other functionality.
 * @param  type of the stream
 */
public class LocalCacheStream extends AbstractLocalCacheStream, CacheStream> implements CacheStream {

   public LocalCacheStream(StreamSupplier> streamSupplier, boolean parallel, ComponentRegistry registry) {
      super(streamSupplier, parallel, registry);
   }

   public LocalCacheStream(AbstractLocalCacheStream other) {
      super(other);
   }

   @Override
   public LocalCacheStream sequentialDistribution() {
      return this;
   }

   @Override
   public LocalCacheStream parallelDistribution() {
      return this;
   }

   @Override
   public LocalCacheStream filterKeySegments(Set segments) {
      segmentsToFilter = segments;
      return this;
   }

   @Override
   public LocalCacheStream filterKeys(Set keys) {
      keysToFilter = keys;
      return this;
   }

   @Override
   public LocalCacheStream distributedBatchSize(int batchSize) {
      // TODO: Does this change cache loader?
      return this;
   }

   @Override
   public LocalCacheStream segmentCompletionListener(SegmentCompletionListener listener) {
      // All segments are completed when the getStream() is completed so we don't track them
      return this;
   }

   @Override
   public LocalCacheStream disableRehashAware() {
      // Local stream doesn't matter for rehash
      return this;
   }

   @Override
   public LocalCacheStream filter(Predicate predicate) {
      registry.wireDependencies(predicate);
      intermediateOperations.add(new FilterOperation<>(predicate));
      return this;
   }

   @Override
   public  LocalCacheStream map(Function mapper) {
      registry.wireDependencies(mapper);
      intermediateOperations.add(new MapOperation<>(mapper));
      return (LocalCacheStream) this;
   }

   @Override
   public LocalIntCacheStream mapToInt(ToIntFunction mapper) {
      intermediateOperations.add(new MapToIntOperation<>(mapper));
      return new LocalIntCacheStream(this);
   }

   @Override
   public LocalLongCacheStream mapToLong(ToLongFunction mapper) {
      intermediateOperations.add(new MapToLongOperation<>(mapper));
      return new LocalLongCacheStream(this);
   }

   @Override
   public LocalDoubleCacheStream mapToDouble(ToDoubleFunction mapper) {
      intermediateOperations.add(new MapToDoubleOperation<>(mapper));
      return new LocalDoubleCacheStream(this);
   }

   @Override
   public  LocalCacheStream flatMap(Function> mapper) {
      intermediateOperations.add(new FlatMapOperation<>(mapper));
      return (LocalCacheStream) this;
   }

   @Override
   public LocalIntCacheStream flatMapToInt(Function mapper) {
      intermediateOperations.add(new FlatMapToIntOperation<>(mapper));
      return new LocalIntCacheStream(this);
   }

   @Override
   public LocalLongCacheStream flatMapToLong(Function mapper) {
      intermediateOperations.add(new FlatMapToLongOperation<>(mapper));
      return new LocalLongCacheStream(this);
   }

   @Override
   public LocalDoubleCacheStream flatMapToDouble(Function mapper) {
      intermediateOperations.add(new FlatMapToDoubleOperation<>(mapper));
      return new LocalDoubleCacheStream(this);
   }

   @Override
   public LocalCacheStream distinct() {
      intermediateOperations.add(DistinctOperation.getInstance());
      return this;
   }

   @Override
   public LocalCacheStream sorted() {
      intermediateOperations.add(SortedOperation.getInstance());
      return this;
   }

   @Override
   public LocalCacheStream sorted(Comparator comparator) {
      intermediateOperations.add(new SortedComparatorOperation<>(comparator));
      return this;
   }

   @Override
   public LocalCacheStream peek(Consumer action) {
      intermediateOperations.add(new PeekOperation<>(action));
      return this;
   }

   @Override
   public LocalCacheStream limit(long maxSize) {
      intermediateOperations.add(new LimitOperation<>(maxSize));
      return this;
   }

   @Override
   public LocalCacheStream skip(long n) {
      intermediateOperations.add(new SkipOperation<>(n));
      return this;
   }

   @Override
   public void forEach(Consumer action) {
      injectCache(action);
      createStream().forEach(action);
   }

   @Override
   public  void forEach(BiConsumer, ? super R> action) {
      Cache cache = registry.getComponent(Cache.class);
      registry.wireDependencies(action);
      createStream().forEach(e -> action.accept(cache, e));
   }

   @Override
   public void forEachOrdered(Consumer action) {
      injectCache(action);
      createStream().forEachOrdered(action);
   }

   /**
    * Method to inject a cache into a consumer.  Note we only support this for the consumer at this
    * time.
    * @param cacheAware the instance that may be a {@link CacheAware}
    */
   private void injectCache(Consumer cacheAware) {
      if (cacheAware instanceof CacheAware) {
         ((CacheAware) cacheAware).injectCache(registry.getComponent(Cache.class));
      }
   }

   @Override
   public Object[] toArray() {
      return createStream().toArray();
   }

   @Override
   public  A[] toArray(IntFunction generator) {
      return createStream().toArray(generator);
   }

   @Override
   public R reduce(R identity, BinaryOperator accumulator) {
      return createStream().reduce(identity, accumulator);
   }

   @Override
   public Optional reduce(BinaryOperator accumulator) {
      return createStream().reduce(accumulator);
   }

   @Override
   public  U reduce(U identity, BiFunction accumulator, BinaryOperator combiner) {
      return createStream().reduce(identity, accumulator, combiner);
   }

   @Override
   public  R1 collect(Supplier supplier, BiConsumer accumulator, BiConsumer combiner) {
      return createStream().collect(supplier, accumulator, combiner);
   }

   @Override
   public  R1 collect(Collector collector) {
      return createStream().collect(collector);
   }

   @Override
   public Optional min(Comparator comparator) {
      return createStream().min(comparator);
   }

   @Override
   public Optional max(Comparator comparator) {
      return createStream().max(comparator);
   }

   @Override
   public long count() {
      return createStream().count();
   }

   @Override
   public boolean anyMatch(Predicate predicate) {
      return createStream().anyMatch(predicate);
   }

   @Override
   public boolean allMatch(Predicate predicate) {
      return createStream().allMatch(predicate);
   }

   @Override
   public boolean noneMatch(Predicate predicate) {
      return createStream().noneMatch(predicate);
   }

   @Override
   public Optional findFirst() {
      return createStream().findFirst();
   }

   @Override
   public Optional findAny() {
      return createStream().findAny();
   }

   @Override
   public CloseableIterator iterator() {
      int size = intermediateOperations.size();
      if (size == 0) {
         // If no intermediate operations we can support remove
         return streamSupplier.removableIterator(Closeables.iterator(createStream()));
      }
      else if (size == 1) {
         IntermediateOperation intOp = intermediateOperations.peek();
         if (intOp instanceof MapOperation) {
            MapOperation map = (MapOperation) intOp;
            if (map.getFunction() instanceof RemovableFunction) {
               // If function was removable means we can just use remove as is
               return streamSupplier.removableIterator(Closeables.iterator(createStream()));
            }
         }
      }
      return Closeables.iterator(createStream());
   }

   @Override
   public Spliterator spliterator() {
      return createStream().spliterator();
   }

   @Override
   public LocalCacheStream timeout(long timeout, TimeUnit unit) {
      // Timeout does nothing for a local cache stream
      return this;
   }
}