Class LocalCacheStream<R>
- java.lang.Object
-
- org.infinispan.stream.impl.local.AbstractLocalCacheStream<R,Stream<R>,CacheStream<R>>
-
- org.infinispan.stream.impl.local.LocalCacheStream<R>
-
- Type Parameters:
R- type of the stream
- All Implemented Interfaces:
AutoCloseable,BaseStream<R,Stream<R>>,Stream<R>,BaseCacheStream<R,Stream<R>>,CacheStream<R>
public class LocalCacheStream<R> extends AbstractLocalCacheStream<R,Stream<R>,CacheStream<R>> implements CacheStream<R>
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.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.infinispan.stream.impl.local.AbstractLocalCacheStream
AbstractLocalCacheStream.StreamSupplier<T,S extends BaseStream<T,S>>
-
Nested classes/interfaces inherited from interface org.infinispan.BaseCacheStream
BaseCacheStream.SegmentCompletionListener
-
Nested classes/interfaces inherited from interface java.util.stream.Stream
Stream.Builder<T extends Object>
-
-
Field Summary
-
Fields inherited from class org.infinispan.stream.impl.local.AbstractLocalCacheStream
intermediateOperations, keysToFilter, onCloseRunnables, parallel, registry, segmentsToFilter, streamSupplier
-
-
Constructor Summary
Constructors Constructor Description LocalCacheStream(AbstractLocalCacheStream.StreamSupplier<R,Stream<R>> streamSupplier, boolean parallel, ComponentRegistry registry)LocalCacheStream(AbstractLocalCacheStream<?,?,?> other)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanallMatch(Predicate<? super R> predicate)booleananyMatch(Predicate<? super R> predicate)<R1> R1collect(Supplier<Collector<? super R,?,R1>> supplier)Performs a mutable reduction operation on the elements of this stream using aCollectorthat is lazily created from theSupplierprovided.<R1> R1collect(Supplier<R1> supplier, BiConsumer<R1,? super R> accumulator, BiConsumer<R1,R1> combiner)<R1,A>
R1collect(Collector<? super R,A,R1> collector)<R1> R1collect(SerializableSupplier<Collector<? super R,?,R1>> supplier)Performs a mutable reduction operation on the elements of this stream using aCollectorthat is lazily created from theSerializableSupplierprovided.longcount()LocalCacheStream<R>disableRehashAware()Disables tracking of rehash events that could occur to the underlying cache.LocalCacheStream<R>distinct()LocalCacheStream<R>distributedBatchSize(int batchSize)Controls how many keys are returned from a remote node when using a stream terminal operation with a distributed cache to back this stream.LocalCacheStream<R>filter(Predicate<? super R> predicate)LocalCacheStream<R>filterKeys(Set<?> keys)Filters which entries are returned by only returning ones that map to the given key.LocalCacheStream<R>filterKeySegments(Set<Integer> segments)Filters which entries are returned by what segment they are present in.LocalCacheStream<R>filterKeySegments(org.infinispan.commons.util.IntSet segments)Filters which entries are returned by what segment they are present in.Optional<R>findAny()Optional<R>findFirst()<R1> LocalCacheStream<R1>flatMap(Function<? super R,? extends Stream<? extends R1>> mapper)LocalDoubleCacheStreamflatMapToDouble(Function<? super R,? extends DoubleStream> mapper)LocalIntCacheStreamflatMapToInt(Function<? super R,? extends IntStream> mapper)LocalLongCacheStreamflatMapToLong(Function<? super R,? extends LongStream> mapper)<K,V>
voidforEach(BiConsumer<Cache<K,V>,? super R> action)Same asCacheStream.forEach(Consumer)except that it takes aBiConsumerthat provides access to the underlyingCachethat is backing this stream.voidforEach(Consumer<? super R> action)voidforEachOrdered(Consumer<? super R> action)Iterator<R>iterator()LocalCacheStream<R>limit(long maxSize)<R1> LocalCacheStream<R1>map(Function<? super R,? extends R1> mapper)LocalDoubleCacheStreammapToDouble(ToDoubleFunction<? super R> mapper)LocalIntCacheStreammapToInt(ToIntFunction<? super R> mapper)LocalLongCacheStreammapToLong(ToLongFunction<? super R> mapper)Optional<R>max(Comparator<? super R> comparator)Optional<R>min(Comparator<? super R> comparator)booleannoneMatch(Predicate<? super R> predicate)LocalCacheStream<R>parallelDistribution()This would enable sending requests to all other remote nodes when a terminal operator is performed.LocalCacheStream<R>peek(Consumer<? super R> action)Optional<R>reduce(BinaryOperator<R> accumulator)Rreduce(R identity, BinaryOperator<R> accumulator)<U> Ureduce(U identity, BiFunction<U,? super R,U> accumulator, BinaryOperator<U> combiner)LocalCacheStream<R>segmentCompletionListener(BaseCacheStream.SegmentCompletionListener listener)Allows registration of a segment completion listener that is notified when a segment has completed processing.LocalCacheStream<R>sequentialDistribution()This would disable sending requests to all other remote nodes compared to one at a time.LocalCacheStream<R>skip(long n)LocalCacheStream<R>sorted()LocalCacheStream<R>sorted(Comparator<? super R> comparator)Spliterator<R>spliterator()LocalCacheStream<R>timeout(long timeout, TimeUnit unit)Sets a given time to wait for a remote operation to respond by.Object[]toArray()<A> A[]toArray(IntFunction<A[]> generator)-
Methods inherited from class org.infinispan.stream.impl.local.AbstractLocalCacheStream
close, createStream, isParallel, onClose, parallel, sequential, unordered
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.stream.BaseStream
close, isParallel
-
Methods inherited from interface org.infinispan.CacheStream
allMatch, anyMatch, collect, filter, flatMap, flatMapToDouble, flatMapToInt, flatMapToLong, forEach, forEach, map, mapToDouble, mapToInt, mapToLong, max, min, noneMatch, onClose, parallel, peek, reduce, reduce, reduce, sequential, sorted, toArray, unordered
-
-
-
-
Constructor Detail
-
LocalCacheStream
public LocalCacheStream(AbstractLocalCacheStream.StreamSupplier<R,Stream<R>> streamSupplier, boolean parallel, ComponentRegistry registry)
-
LocalCacheStream
public LocalCacheStream(AbstractLocalCacheStream<?,?,?> other)
-
-
Method Detail
-
sequentialDistribution
public LocalCacheStream<R> sequentialDistribution()
Description copied from interface:CacheStreamThis would disable sending requests to all other remote nodes compared to one at a time. This can reduce memory pressure on the originator node at the cost of performance.Parallel distribution is enabled by default except for
CacheStream.iterator()andCacheStream.spliterator()- Specified by:
sequentialDistributionin interfaceBaseCacheStream<R,Stream<R>>- Specified by:
sequentialDistributionin interfaceCacheStream<R>- Returns:
- a stream with parallel distribution disabled.
-
parallelDistribution
public LocalCacheStream<R> parallelDistribution()
Description copied from interface:BaseCacheStreamThis would enable sending requests to all other remote nodes when a terminal operator is performed. This requires additional overhead as it must process results concurrently from various nodes, but should perform faster in the majority of cases.Parallel distribution is enabled by default except for
CacheStream.iterator()andCacheStream.spliterator()- Specified by:
parallelDistributionin interfaceBaseCacheStream<R,Stream<R>>- Specified by:
parallelDistributionin interfaceCacheStream<R>- Returns:
- a stream with parallel distribution enabled.
-
filterKeySegments
public LocalCacheStream<R> filterKeySegments(Set<Integer> segments)
Description copied from interface:CacheStreamFilters which entries are returned by what segment they are present in. This method can be substantially more efficient than using a regularCacheStream.filter(Predicate)method as this can control what nodes are asked for data and what entries are read from the underlying CacheStore if present.- Specified by:
filterKeySegmentsin interfaceBaseCacheStream<R,Stream<R>>- Specified by:
filterKeySegmentsin interfaceCacheStream<R>- Parameters:
segments- The segments to use for this stream operation. Any segments not in this set will be ignored.- Returns:
- a stream with the segments filtered.
-
filterKeySegments
public LocalCacheStream<R> filterKeySegments(org.infinispan.commons.util.IntSet segments)
Description copied from interface:CacheStreamFilters which entries are returned by what segment they are present in. This method can be substantially more efficient than using a regularCacheStream.filter(Predicate)method as this can control what nodes are asked for data and what entries are read from the underlying CacheStore if present.- Specified by:
filterKeySegmentsin interfaceBaseCacheStream<R,Stream<R>>- Specified by:
filterKeySegmentsin interfaceCacheStream<R>- Parameters:
segments- The segments to use for this stream operation. Any segments not in this set will be ignored.- Returns:
- a stream with the segments filtered.
-
filterKeys
public LocalCacheStream<R> filterKeys(Set<?> keys)
Description copied from interface:CacheStreamFilters which entries are returned by only returning ones that map to the given key. This method will be faster than a regularCacheStream.filter(Predicate)if the filter is holding references to the same keys.- Specified by:
filterKeysin interfaceBaseCacheStream<R,Stream<R>>- Specified by:
filterKeysin interfaceCacheStream<R>- Parameters:
keys- The keys that this stream will only operate on.- Returns:
- a stream with the keys filtered.
-
distributedBatchSize
public LocalCacheStream<R> distributedBatchSize(int batchSize)
Description copied from interface:CacheStreamControls how many keys are returned from a remote node when using a stream terminal operation with a distributed cache to back this stream. This value is ignored when terminal operators that don't track keys are used. Key tracking terminal operators areCacheStream.iterator(),CacheStream.spliterator(),CacheStream.forEach(Consumer). Please see those methods for additional information on how this value may affect them.This value may be used in the case of a a terminal operator that doesn't track keys if an intermediate operation is performed that requires bringing keys locally to do computations. Examples of such intermediate operations are
CacheStream.sorted(),CacheStream.sorted(Comparator),CacheStream.distinct(),CacheStream.limit(long),CacheStream.skip(long)This value is always ignored when this stream is backed by a cache that is not distributed as all values are already local.
- Specified by:
distributedBatchSizein interfaceBaseCacheStream<R,Stream<R>>- Specified by:
distributedBatchSizein interfaceCacheStream<R>- Parameters:
batchSize- The size of each batch. This defaults to the state transfer chunk size.- Returns:
- a stream with the batch size updated
-
segmentCompletionListener
public LocalCacheStream<R> segmentCompletionListener(BaseCacheStream.SegmentCompletionListener listener)
Description copied from interface:CacheStreamAllows registration of a segment completion listener that is notified when a segment has completed processing. If the terminal operator has a short circuit this listener may never be called.This method is designed for the sole purpose of use with the
CacheStream.iterator()to allow for a user to track completion of segments as they are returned from the iterator. Behavior of other methods is not specified. Please seeCacheStream.iterator()for more information.Multiple listeners may be registered upon multiple invocations of this method. The ordering of notified listeners is not specified.
This is only used if this stream did not invoke
BaseCacheStream.disableRehashAware()and has no flat map based operations. If this is done no segments will be notified.- Specified by:
segmentCompletionListenerin interfaceBaseCacheStream<R,Stream<R>>- Specified by:
segmentCompletionListenerin interfaceCacheStream<R>- Parameters:
listener- The listener that will be called back as segments are completed.- Returns:
- a stream with the listener registered.
-
disableRehashAware
public LocalCacheStream<R> disableRehashAware()
Description copied from interface:CacheStreamDisables tracking of rehash events that could occur to the underlying cache. If a rehash event occurs while a terminal operation is being performed it is possible for some values that are in the cache to not be found. Note that you will never have an entry duplicated when rehash awareness is disabled, only lost values.Most terminal operations will run faster with rehash awareness disabled even without a rehash occuring. However if a rehash occurs with this disabled be prepared to possibly receive only a subset of values.
- Specified by:
disableRehashAwarein interfaceBaseCacheStream<R,Stream<R>>- Specified by:
disableRehashAwarein interfaceCacheStream<R>- Returns:
- a stream with rehash awareness disabled.
-
filter
public LocalCacheStream<R> filter(Predicate<? super R> predicate)
Description copied from interface:CacheStream
-
map
public <R1> LocalCacheStream<R1> map(Function<? super R,? extends R1> mapper)
Description copied from interface:CacheStream
-
mapToInt
public LocalIntCacheStream mapToInt(ToIntFunction<? super R> mapper)
Description copied from interface:CacheStream
-
mapToLong
public LocalLongCacheStream mapToLong(ToLongFunction<? super R> mapper)
Description copied from interface:CacheStream
-
mapToDouble
public LocalDoubleCacheStream mapToDouble(ToDoubleFunction<? super R> mapper)
Description copied from interface:CacheStream- Specified by:
mapToDoublein interfaceCacheStream<R>- Specified by:
mapToDoublein interfaceStream<R>- Parameters:
mapper- a non-interfering, stateless function to apply to each element- Returns:
- the new double cache stream
-
flatMap
public <R1> LocalCacheStream<R1> flatMap(Function<? super R,? extends Stream<? extends R1>> mapper)
Description copied from interface:CacheStream
-
flatMapToInt
public LocalIntCacheStream flatMapToInt(Function<? super R,? extends IntStream> mapper)
Description copied from interface:CacheStream- Specified by:
flatMapToIntin interfaceCacheStream<R>- Specified by:
flatMapToIntin interfaceStream<R>- Returns:
- the new cache stream
-
flatMapToLong
public LocalLongCacheStream flatMapToLong(Function<? super R,? extends LongStream> mapper)
Description copied from interface:CacheStream- Specified by:
flatMapToLongin interfaceCacheStream<R>- Specified by:
flatMapToLongin interfaceStream<R>- Returns:
- the new cache stream
-
flatMapToDouble
public LocalDoubleCacheStream flatMapToDouble(Function<? super R,? extends DoubleStream> mapper)
Description copied from interface:CacheStream- Specified by:
flatMapToDoublein interfaceCacheStream<R>- Specified by:
flatMapToDoublein interfaceStream<R>- Returns:
- the new cache stream
-
distinct
public LocalCacheStream<R> distinct()
Description copied from interface:CacheStreamThis operation will be invoked both remotely and locally when used with a distributed cache backing this stream. This operation will act as an intermediate iterator operation requiring data be brought locally for proper behavior. This is described in more detail in the
CacheStreamdocumentationThis intermediate iterator operation will be performed locally and remotely requiring possibly a subset of all elements to be in memory
Any subsequent intermediate operations and the terminal operation are then performed locally.
-
sorted
public LocalCacheStream<R> sorted()
Description copied from interface:CacheStreamThis operation is performed entirely on the local node irrespective of the backing cache. This operation will act as an intermediate iterator operation requiring data be brought locally for proper behavior. Beware this means it will require having all entries of this cache into memory at one time. This is described in more detail at
CacheStreamAny subsequent intermediate operations and the terminal operation are also performed locally.
-
sorted
public LocalCacheStream<R> sorted(Comparator<? super R> comparator)
Description copied from interface:CacheStreamThis operation is performed entirely on the local node irrespective of the backing cache. This operation will act as an intermediate iterator operation requiring data be brought locally for proper behavior. Beware this means it will require having all entries of this cache into memory at one time. This is described in more detail at
CacheStreamAny subsequent intermediate operations and the terminal operation are then performed locally.
-
peek
public LocalCacheStream<R> peek(Consumer<? super R> action)
Description copied from interface:CacheStream
-
limit
public LocalCacheStream<R> limit(long maxSize)
Description copied from interface:CacheStreamThis intermediate operation will be performed both remotely and locally to reduce how many elements are sent back from each node. More specifically this operation is applied remotely on each node to only return up to the maxSize value and then the aggregated results are limited once again on the local node.
This operation will act as an intermediate iterator operation requiring data be brought locally for proper behavior. This is described in more detail in the
CacheStreamdocumentationAny subsequent intermediate operations and the terminal operation are then performed locally.
-
skip
public LocalCacheStream<R> skip(long n)
Description copied from interface:CacheStreamThis operation is performed entirely on the local node irrespective of the backing cache. This operation will act as an intermediate iterator operation requiring data be brought locally for proper behavior. This is described in more detail in the
CacheStreamdocumentationDepending on the terminal operator this may or may not require all entries or a subset after skip is applied to be in memory all at once.
Any subsequent intermediate operations and the terminal operation are then performed locally.
-
forEach
public void forEach(Consumer<? super R> action)
Description copied from interface:CacheStreamThis operation is performed remotely on the node that is the primary owner for the key tied to the entry(s) in this stream.
NOTE: This method while being rehash aware has the lowest consistency of all of the operators. This operation will be performed on every entry at least once in the cluster, as long as the originator doesn't go down while it is being performed. This is due to how the distributed action is performed. Essentially the
CacheStream.distributedBatchSize(int)value controls how many elements are processed per node at a time when rehash is enabled. After those are complete the keys are sent to the originator to confirm that those were processed. If that node goes down during/before the response those keys will be processed a second time.It is possible to have the cache local to each node injected into this instance if the provided Consumer also implements the
CacheAwareinterface. This method will be invoked before the consumeraccept()method is invoked.This method is ran distributed by default with a distributed backing cache. However if you wish for this operation to run locally you can use the
stream().iterator().forEachRemaining(action)for a single threaded variant. If you wish to have a parallel variant you can useStreamSupport.stream(Spliterator, boolean)passing in the spliterator from the stream. In either case remember you must close the stream after you are done processing the iterator or spliterator..
-
forEach
public <K,V> void forEach(BiConsumer<Cache<K,V>,? super R> action)
Description copied from interface:CacheStreamSame asCacheStream.forEach(Consumer)except that it takes aBiConsumerthat provides access to the underlyingCachethat is backing this stream.Note that the
CacheAwareinterface is not supported for injection using this method as the cache is provided in the consumer directly.- Specified by:
forEachin interfaceCacheStream<R>- Type Parameters:
K- key type of the cacheV- value type of the cache- Parameters:
action- consumer to be ran for each element in the stream
-
forEachOrdered
public void forEachOrdered(Consumer<? super R> action)
- Specified by:
forEachOrderedin interfaceStream<R>
-
toArray
public <A> A[] toArray(IntFunction<A[]> generator)
-
reduce
public R reduce(R identity, BinaryOperator<R> accumulator)
-
reduce
public Optional<R> reduce(BinaryOperator<R> accumulator)
-
reduce
public <U> U reduce(U identity, BiFunction<U,? super R,U> accumulator, BinaryOperator<U> combiner)
-
collect
public <R1> R1 collect(Supplier<R1> supplier, BiConsumer<R1,? super R> accumulator, BiConsumer<R1,R1> combiner)
-
collect
public <R1,A> R1 collect(Collector<? super R,A,R1> collector)
Description copied from interface:CacheStreamNote when using a distributed backing cache for this stream the collector must be marshallable. This prevents the usage of
Collectorsclass. However you can use theCacheCollectorsstatic factory methods to create a serializable wrapper, which then creates the actual collector lazily after being deserialized. This is useful to use any method from theCollectorsclass as you would normally. Alternatively, you can callCacheStream.collect(SerializableSupplier)too.- Specified by:
collectin interfaceCacheStream<R>- Specified by:
collectin interfaceStream<R>- Type Parameters:
R1- collected typeA- intermediate collected type if applicable- Returns:
- the collected value
- See Also:
CacheCollectors
-
collect
public <R1> R1 collect(SerializableSupplier<Collector<? super R,?,R1>> supplier)
Description copied from interface:CacheStreamPerforms a mutable reduction operation on the elements of this stream using aCollectorthat is lazily created from theSerializableSupplierprovided. This method behaves exactly the same asCacheStream.collect(Collector)with the enhanced capability of working even when the mutable reduction operation has to run in a remote node and the operation is notSerializableor otherwise marshallable. So, this method is specially designed for situations when the user wants to use aCollectorinstance that has been created byCollectorsstatic factory methods. In this particular case, the function that instantiates theCollectorwill be marshalled according to theSerializablerules.- Specified by:
collectin interfaceCacheStream<R>- Type Parameters:
R1- The resulting type of the collector- Parameters:
supplier- The supplier to create the collector that is specifically serializable- Returns:
- the collected value
-
collect
public <R1> R1 collect(Supplier<Collector<? super R,?,R1>> supplier)
Description copied from interface:CacheStreamPerforms a mutable reduction operation on the elements of this stream using aCollectorthat is lazily created from theSupplierprovided. This method behaves exactly the same asCacheStream.collect(Collector)with the enhanced capability of working even when the mutable reduction operation has to run in a remote node and the operation is notSerializableor otherwise marshallable. So, this method is specially designed for situations when the user wants to use aCollectorinstance that has been created byCollectorsstatic factory methods. In this particular case, the function that instantiates theCollectorwill be marshalled using InfinispanExternalizerclass or one of its subtypes.- Specified by:
collectin interfaceCacheStream<R>- Type Parameters:
R1- The resulting type of the collector- Parameters:
supplier- The supplier to create the collector- Returns:
- the collected value
-
min
public Optional<R> min(Comparator<? super R> comparator)
-
max
public Optional<R> max(Comparator<? super R> comparator)
-
iterator
public Iterator<R> iterator()
Description copied from interface:CacheStreamUsage of this operator requires closing this stream after you are done with the iterator. The preferred usage is to use a try with resource block on the stream.
This method has special usage with the
BaseCacheStream.SegmentCompletionListenerin that as entries are retrieved from the next method it will complete segments.This method obeys the
CacheStream.distributedBatchSize(int). Note that when using methods such asCacheStream.flatMap(Function)that you will have possibly more than 1 element mapped to a given key so this doesn't guarantee that many number of entries are returned per batch.Note that the
Iterator.remove()method is only supported if no intermediate operations have been applied to the stream and this is not a stream created from aCache.values()collection.- Specified by:
iteratorin interfaceBaseStream<R,Stream<R>>- Specified by:
iteratorin interfaceCacheStream<R>- Returns:
- the element iterator for this stream
-
spliterator
public Spliterator<R> spliterator()
Description copied from interface:CacheStreamUsage of this operator requires closing this stream after you are done with the spliterator. The preferred usage is to use a try with resource block on the stream.
- Specified by:
spliteratorin interfaceBaseStream<R,Stream<R>>- Specified by:
spliteratorin interfaceCacheStream<R>- Returns:
- the element spliterator for this stream
-
timeout
public LocalCacheStream<R> timeout(long timeout, TimeUnit unit)
Description copied from interface:CacheStreamSets a given time to wait for a remote operation to respond by. This timeout does nothing if the terminal operation does not go remote.If a timeout does occur then a
TimeoutExceptionis thrown from the terminal operation invoking thread or on the next call to theIteratororSpliterator.Note that if a rehash occurs this timeout value is reset for the subsequent retry if rehash aware is enabled.
- Specified by:
timeoutin interfaceBaseCacheStream<R,Stream<R>>- Specified by:
timeoutin interfaceCacheStream<R>- Parameters:
timeout- the maximum time to waitunit- the time unit of the timeout argument- Returns:
- a stream with the timeout set
-
-