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.AdvancedCache;
import org.infinispan.Cache;
import org.infinispan.cache.impl.AbstractDelegatingCache;
import org.infinispan.commands.VisitableCommand;
import org.infinispan.commands.Visitor;
import org.infinispan.commons.util.EnumUtil;
import org.infinispan.context.Flag;
import org.infinispan.context.InvocationContext;

/**
 * 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 AdvancedCache cache;

   public SizeCommand(Cache cache, long flags) {
      setFlagsBitSet(flags);
      AdvancedCache advancedCache = AbstractDelegatingCache.unwrapCache(cache).getAdvancedCache();
      if (flags != EnumUtil.EMPTY_BIT_SET) {
         this.cache = advancedCache.withFlags(EnumUtil.enumArrayOf(flags, Flag.class));
      } else {
         this.cache = advancedCache;
      }
   }

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

   @Override
   public LoadType loadType() {
      throw new UnsupportedOperationException();
   }

   @Override
   public Integer perform(InvocationContext ctx) throws Throwable {
      long size = cache.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