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

org.infinispan.commands.functional.ReadOnlyManyCommand Maven / Gradle / Ivy

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

import org.infinispan.commands.LocalCommand;
import org.infinispan.commands.Visitor;
import org.infinispan.commands.read.AbstractDataCommand;
import org.infinispan.commons.api.functional.EntryView.ReadEntryView;
import org.infinispan.container.entries.CacheEntry;
import org.infinispan.container.entries.InternalCacheEntry;
import org.infinispan.context.InvocationContext;
import org.infinispan.distribution.ch.ConsistentHash;
import org.infinispan.functional.impl.EntryViews;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

import static org.infinispan.functional.impl.EntryViews.snapshot;

public final class ReadOnlyManyCommand extends AbstractDataCommand implements LocalCommand {

   private Set keys;
   private Function, R> f;

   private ConsistentHash ch;
   // TODO: remotely fetched are because of compatibility - can't we just always return InternalCacheEntry and have
   //       the unboxing executed as the topmost interceptor?
   private Map remotelyFetched;

   public ReadOnlyManyCommand(Set keys, Function, R> f) {
      this.keys = keys;
      this.f = f;
   }

   public ReadOnlyManyCommand() {
   }

   public Set getKeys() {
      return keys;
   }

   @Override
   public byte getCommandId() {
      return -1;
   }

   @Override
   public void writeTo(ObjectOutput output) throws IOException {
      // Not really replicated
   }

   @Override
   public void readFrom(ObjectInput input) throws IOException, ClassNotFoundException {
      // Not really replicated
   }

   public ConsistentHash getConsistentHash() {
      return ch;
   }

   public void setConsistentHash(ConsistentHash ch) {
      this.ch = ch;
   }

   public Map getRemotelyFetched() {
      return remotelyFetched;
   }

   public void setRemotelyFetched(Map remotelyFetched) {
      this.remotelyFetched = remotelyFetched;
   }

   @Override
   public Object perform(InvocationContext ctx) throws Throwable {
      return keys.stream().map(k -> {
         CacheEntry me = lookupCacheEntry(ctx, k);
         R ret = f.apply(me == null ? EntryViews.noValue(k) : EntryViews.readOnly(me));
         return snapshot(ret);
      });
   }

   private CacheEntry lookupCacheEntry(InvocationContext ctx, Object key) {
      return ctx.lookupEntry(key);
   }

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

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

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

   @Override
   public String toString() {
      return "ReadOnlyManyCommand{" +
         "keys=" + keys +
         ", f=" + f +
         ", ch=" + ch +
         ", remotelyFetched=" + remotelyFetched +
         '}';
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy