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

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

The newest version!
package org.infinispan.client.hotrod.impl.operations;

import java.util.concurrent.TimeUnit;

import org.infinispan.client.hotrod.MetadataValue;
import org.infinispan.client.hotrod.impl.ClientStatistics;
import org.infinispan.client.hotrod.impl.InternalRemoteCache;
import org.infinispan.client.hotrod.impl.protocol.Codec;
import org.infinispan.client.hotrod.impl.protocol.HotRodConstants;
import org.infinispan.client.hotrod.impl.transport.netty.HeaderDecoder;
import org.infinispan.client.hotrod.logging.LogFactory;
import org.jboss.logging.BasicLogger;

import io.netty.buffer.ByteBuf;

/**
 * Implements "putIfAbsent" operation as described in  Hot Rod
 * protocol specification.
 *
 * @author [email protected]
 * @since 4.1
 */
public class PutIfAbsentOperation extends AbstractKeyValueOperation> {

   private static final BasicLogger log = LogFactory.getLog(PutIfAbsentOperation.class);

   public PutIfAbsentOperation(InternalRemoteCache remoteCache, byte[] keyBytes, byte[] value, long lifespan,
                               TimeUnit lifespanTimeUnit, long maxIdleTime, TimeUnit maxIdleTimeUnit) {
      super(remoteCache, keyBytes, value, lifespan, lifespanTimeUnit, maxIdleTime, maxIdleTimeUnit);
   }

   @Override
   public MetadataValue createResponse(ByteBuf buf, short status, HeaderDecoder decoder, Codec codec, CacheUnmarshaller unmarshaller) {
      if (HotRodConstants.isNotExecuted(status)) {
         MetadataValue prevValue = returnMetadataValue(buf, status, codec, unmarshaller);
         if (log.isTraceEnabled()) {
            log.tracef("Returning from putIfAbsent: %s", prevValue);
         }
         return prevValue;
      }
      return null;
   }

   @Override
   public void handleStatsCompletion(ClientStatistics statistics, long startTime, short status, MetadataValue responseValue) {
      if (HotRodConstants.isNotExecuted(status)) {
         if (HotRodConstants.hasPrevious(status)) {
            statistics.dataRead(true, startTime, 1);
         }
      } else {
         statistics.dataStore(startTime, 1);
      }
   }

   @Override
   public short requestOpCode() {
      return PUT_IF_ABSENT_REQUEST;
   }

   @Override
   public short responseOpCode() {
      return PUT_IF_ABSENT_RESPONSE;
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy