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

org.infinispan.client.hotrod.impl.operations.GetAllOperation Maven / Gradle / Ivy

There is a newer version: 15.1.0.Dev04
Show newest version
package org.infinispan.client.hotrod.impl.operations;

import java.net.SocketAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;

import org.infinispan.client.hotrod.DataFormat;
import org.infinispan.client.hotrod.configuration.Configuration;
import org.infinispan.client.hotrod.impl.ClientStatistics;
import org.infinispan.client.hotrod.impl.ClientTopology;
import org.infinispan.client.hotrod.impl.protocol.Codec;
import org.infinispan.client.hotrod.impl.transport.netty.ByteBufUtil;
import org.infinispan.client.hotrod.impl.transport.netty.ChannelFactory;
import org.infinispan.client.hotrod.impl.transport.netty.HeaderDecoder;

import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import net.jcip.annotations.Immutable;

/**
 * Implements "getAll" as defined by  Hot Rod protocol specification.
 *
 * @author William Burns
 * @since 7.2
 */
@Immutable
public class GetAllOperation extends StatsAffectingRetryingOperation> {

   private Map result;
   private int size = -1;

   public GetAllOperation(Codec codec, ChannelFactory channelFactory,
                          Set keys, byte[] cacheName, AtomicReference clientTopology,
                          int flags, Configuration cfg, DataFormat dataFormat, ClientStatistics clientStatistics) {
      super(GET_ALL_REQUEST, GET_ALL_RESPONSE, codec, channelFactory, cacheName, clientTopology, flags, cfg, dataFormat,
            clientStatistics, null);
      this.keys = keys;
   }

   protected final Set keys;

   @Override
   protected void executeOperation(Channel channel) {
      scheduleRead(channel);

      int bufSize = codec.estimateHeaderSize(header) + ByteBufUtil.estimateVIntSize(keys.size());
      for (byte[] key : keys) {
         bufSize += ByteBufUtil.estimateArraySize(key);
      }
      ByteBuf buf = channel.alloc().buffer(bufSize);

      codec.writeHeader(buf, header);
      ByteBufUtil.writeVInt(buf, keys.size());
      for (byte[] key : keys) {
         ByteBufUtil.writeArray(buf, key);
      }
      channel.writeAndFlush(buf);
   }

   @Override
   protected void reset() {
      super.reset();
      result = null;
      size = -1;
   }

   @Override
   protected void fetchChannelAndInvoke(int retryCount, Set failedServers) {
      channelFactory.fetchChannelAndInvoke(keys.iterator().next(), failedServers, cacheName(), this);
   }

   @Override
   public void acceptResponse(ByteBuf buf, short status, HeaderDecoder decoder) {
      if (size < 0) {
         size = ByteBufUtil.readVInt(buf);
         result = new HashMap<>(size);
         decoder.checkpoint();
      }
      while (result.size() < size) {
         K key = dataFormat().keyToObj(ByteBufUtil.readArray(buf), cfg.getClassAllowList());
         V value = dataFormat().valueToObj(ByteBufUtil.readArray(buf), cfg.getClassAllowList());
         result.put(key, value);
         decoder.checkpoint();
      }
      statsDataRead(true, size);
      statsDataRead(false, keys.size() - size);
      complete(result);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy