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

net.spy.memcached.protocol.ascii.AsciiMemcachedNodeImpl Maven / Gradle / Ivy

The newest version!
package net.spy.memcached.protocol.ascii;

import java.net.SocketAddress;
import java.nio.channels.SocketChannel;
import java.util.concurrent.BlockingQueue;

import net.spy.memcached.ops.GetOperation;
import net.spy.memcached.ops.APIType;
import net.spy.memcached.ops.Operation;
import net.spy.memcached.ops.OperationState;
import net.spy.memcached.protocol.ProxyCallback;
import net.spy.memcached.protocol.TCPMemcachedNodeImpl;

/**
 * Memcached node for the ASCII protocol.
 */
public final class AsciiMemcachedNodeImpl extends TCPMemcachedNodeImpl {
  public AsciiMemcachedNodeImpl(String name,
                                SocketAddress sa, SocketChannel c,
                                int bufSize, BlockingQueue rq,
                                BlockingQueue wq, BlockingQueue iq,
                                Long opQueueMaxBlockTimeNs) {
    super(name, sa, c, bufSize, rq, wq, iq, opQueueMaxBlockTimeNs,
        false /* ascii never does auth */, true /* ascii protocol */);
  }

  @Override
  protected void optimize() {
    // make sure there are at least two get operations in a row before
    // attempting to optimize them.
    Operation nxtOp = writeQ.peek();
    if (nxtOp instanceof GetOperation && nxtOp.getAPIType() != APIType.MGET) {
      optimizedOp = writeQ.remove();
      nxtOp = writeQ.peek();
      if (nxtOp instanceof GetOperation && nxtOp.getAPIType() != APIType.MGET) {
        OptimizedGetImpl og = new OptimizedGetImpl(
                (GetOperation) optimizedOp);
        optimizedOp = og;

        do {
          GetOperationImpl o = (GetOperationImpl) writeQ.remove();
          if (!o.isCancelled()) {
            og.addOperation(o);
          }
          nxtOp = writeQ.peek();
        } while (nxtOp instanceof GetOperation &&
                nxtOp.getAPIType() != APIType.MGET);

        // Initialize the new mega get
        optimizedOp.initialize();
        assert optimizedOp.getState() == OperationState.WRITE_QUEUED;
        ProxyCallback pcb = (ProxyCallback) og.getCallback();
        getLogger().debug("Set up %s with %s keys and %s callbacks",
                this, pcb.numKeys(), pcb.numCallbacks());
      }
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy