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

org.infinispan.stream.impl.interceptor.AbstractDelegatingEntryCacheSet Maven / Gradle / Ivy

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

import org.infinispan.Cache;
import org.infinispan.CacheSet;
import org.infinispan.CacheStream;
import org.infinispan.commons.util.CloseableSpliterator;
import org.infinispan.container.entries.CacheEntry;
import org.infinispan.distribution.DistributionManager;
import org.infinispan.stream.impl.AbstractDelegatingCacheSet;
import org.infinispan.stream.impl.local.EntryStreamSupplier;
import org.infinispan.stream.impl.local.LocalCacheStream;

import java.util.stream.StreamSupport;

/**
 * Abstract cache entry set that delegates to the underlying cache for stream usage
 * @param  key type of the cache
 * @param  value type of the cache
 */
public abstract class AbstractDelegatingEntryCacheSet extends AbstractDelegatingCacheSet> {
   private final Cache cache;
   private final CacheSet> set;

   protected AbstractDelegatingEntryCacheSet(Cache cache, CacheSet> set) {
      this.cache = cache;
      this.set = set;
   }

   @Override
   protected final CacheSet> delegate() {
      return set;
   }

   @Override
   public CacheStream> stream() {
      return getStream(false);
   }

   @Override
   public CacheStream> parallelStream() {
      return getStream(true);
   }

   protected CacheStream> getStream(boolean parallel) {
      DistributionManager dm = cache.getAdvancedCache().getDistributionManager();
      CloseableSpliterator> closeableSpliterator = spliterator();
      CacheStream> stream = new LocalCacheStream<>(new EntryStreamSupplier<>(cache, dm != null ?
              dm.getConsistentHash() : null, () -> StreamSupport.stream(closeableSpliterator, false)), parallel,
              cache.getAdvancedCache().getComponentRegistry());
      // We rely on the fact that on close returns the same instance
      stream.onClose(() -> closeableSpliterator.close());
      return stream;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy