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

org.infinispan.client.hotrod.impl.operations.AbstractKeyOperation 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.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.VersionedOperationResponse;
import org.infinispan.client.hotrod.impl.protocol.Codec;
import org.infinispan.client.hotrod.impl.protocol.HotRodConstants;
import org.infinispan.client.hotrod.impl.transport.netty.ChannelFactory;
import org.infinispan.client.hotrod.marshall.MediaTypeMarshaller;
import org.infinispan.client.hotrod.telemetry.impl.TelemetryService;
import org.infinispan.commons.util.Util;

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

/**
 * Basic class for all hot rod operations that manipulate a key.
 *
 * @author [email protected]
 * @since 4.1
 */
@Immutable
public abstract class AbstractKeyOperation extends StatsAffectingRetryingOperation {
   protected final Object key;
   protected final byte[] keyBytes;

   protected AbstractKeyOperation(short requestCode, short responseCode, Codec codec, ChannelFactory channelFactory,
                                  Object key, byte[] keyBytes, byte[] cacheName, AtomicReference clientTopology, int flags,
                                  Configuration cfg, DataFormat dataFormat, ClientStatistics clientStatistics,
                                  TelemetryService telemetryService) {
      super(requestCode, responseCode, codec, channelFactory, cacheName, clientTopology, flags, cfg, dataFormat,
            clientStatistics, telemetryService);
      this.key = key;
      this.keyBytes = keyBytes;
   }

   @Override
   protected void fetchChannelAndInvoke(int retryCount, Set failedServers) {
      if (retryCount == 0) {
         channelFactory.fetchChannelAndInvoke(commandKey(), failedServers, cacheName(), this);
      } else {
         channelFactory.fetchChannelAndInvoke(failedServers, cacheName(), this);
      }
   }

   private Object commandKey() {
      DataFormat df = dataFormat();
      if (df == null) {
         return key != null
               ? key
               : keyBytes;
      }

      return df.isObjectStorage()
            ? key
            : keyBytes;
   }

   protected T returnPossiblePrevValue(ByteBuf buf, short status) {
      return (T) codec.returnPossiblePrevValue(buf, status, dataFormat(), flags(), cfg.getClassAllowList(), channelFactory.getMarshaller());
   }

   protected VersionedOperationResponse returnVersionedOperationResponse(ByteBuf buf, short status) {
      VersionedOperationResponse.RspCode code;
      if (HotRodConstants.isSuccess(status)) {
         code = VersionedOperationResponse.RspCode.SUCCESS;
      } else if (HotRodConstants.isNotExecuted(status)) {
         code = VersionedOperationResponse.RspCode.MODIFIED_KEY;
      } else if (HotRodConstants.isNotExist(status)) {
         code = VersionedOperationResponse.RspCode.NO_SUCH_KEY;
      } else {
         throw new IllegalStateException("Unknown response status: " + Integer.toHexString(status));
      }
      Object prevValue = returnPossiblePrevValue(buf, status);
      return new VersionedOperationResponse(prevValue, code);
   }

   @Override
   protected void addParams(StringBuilder sb) {
      sb.append(", key=").append(key == null ? Util.printArray(keyBytes) : key);
   }

   @Override
   public Object routingObject(Object defaultObject) {
      // We need to serialize the object and hash the bytes to retrieve the segment.
      // To map to the correct segment, we need to utilize the server's target media-type.
      DataFormat df = dataFormat();
      if (df != null) {
         MediaTypeMarshaller mtm = df.server();
         if (mtm != null) return mtm.keyToBytes(key);
      }

      return defaultObject;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy