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

org.infinispan.commands.read.SizeCommand Maven / Gradle / Ivy

There is a newer version: 9.1.7.Final
Show newest version
package org.infinispan.commands.read;

import org.infinispan.Cache;
import org.infinispan.commands.VisitableCommand;
import org.infinispan.commands.Visitor;
import org.infinispan.commons.util.CloseableIterable;
import org.infinispan.container.entries.CacheEntry;
import org.infinispan.context.Flag;
import org.infinispan.context.InvocationContext;
import org.infinispan.filter.AcceptAllKeyValueFilter;
import org.infinispan.filter.NullValueConverter;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Command to calculate the size of the cache
 *
 * @author Manik Surtani ([email protected])
 * @author [email protected]
 * @author Trustin Lee
 * @since 4.0
 */
public class SizeCommand extends AbstractLocalCommand implements VisitableCommand {
   private final Cache cache;

   public SizeCommand(Cache cache, Set flags) {
      setFlags(flags);
      this.cache = cache;
   }

   @Override
   public Object acceptVisitor(InvocationContext ctx, Visitor visitor) throws Throwable {
      return visitor.visitSizeCommand(ctx, this);
   }

   Cache getFlagRespectingCache() {
      Set flags = getFlags();
      if (flags != null  && !flags.isEmpty()) {
         return cache.getAdvancedCache().withFlags(flags.toArray(new Flag[flags.size()]));
      } else {
         return cache;
      }
   }

   @Override
   public boolean readsExistingValues() {
      return false;
   }

   @Override
   public Integer perform(InvocationContext ctx) throws Throwable {
      long size = getFlagRespectingCache().keySet().stream().count();
      if (size > Integer.MAX_VALUE) {
         return Integer.MAX_VALUE;
      } else {
         return (int) size;
      }
   }

   @Override
   public String toString() {
      return "SizeCommand{}";
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy