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

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

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

import org.infinispan.AdvancedCache;
import org.infinispan.Cache;
import org.infinispan.commons.util.CloseableIterator;
import org.infinispan.context.Flag;
import org.infinispan.distribution.ch.ConsistentHash;
import org.infinispan.stream.impl.RemovableCloseableIterator;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;

import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * Stream supplier that is to be used when the underlying stream is composed by key instances.  This supplier will do
 * the proper filtering by assuming each element is the key itself.
 */
public class KeyStreamSupplier implements AbstractLocalCacheStream.StreamSupplier {
   private static final Log log = LogFactory.getLog(KeyStreamSupplier.class);

   private final Cache cache;
   private final ConsistentHash hash;
   private final Supplier> supplier;

   public KeyStreamSupplier(Cache cache, ConsistentHash hash, Supplier> supplier) {
      this.cache = cache;
      this.hash = hash;
      this.supplier = supplier;
   }

   @Override
   public Stream buildStream(Set segmentsToFilter, Set keysToFilter) {
      Stream stream;
      if (keysToFilter != null) {
         log.tracef("Applying key filtering %s", keysToFilter);
         // Make sure we aren't going remote to retrieve these
         AdvancedCache advancedCache = cache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL);
         stream = (Stream) keysToFilter.stream().filter(k -> advancedCache.containsKey(k));
      } else {
         stream = supplier.get();
      }
      if (segmentsToFilter != null && hash != null) {
         log.tracef("Applying segment filter %s", segmentsToFilter);
         stream = stream.filter(k -> segmentsToFilter.contains(hash.getSegment(k)));
      }
      return stream;
   }

   @Override
   public CloseableIterator removableIterator(CloseableIterator realIterator) {
      return new RemovableCloseableIterator<>(realIterator, cache, Function.identity());
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy