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

com.gemstone.gemfire.internal.memcached.Command Maven / Gradle / Ivy

There is a newer version: 2.0-BETA
Show newest version
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You
 * may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License. See accompanying
 * LICENSE file.
 */
package com.gemstone.gemfire.internal.memcached;

import java.nio.ByteBuffer;

import com.gemstone.gemfire.internal.memcached.commands.AddCommand;
import com.gemstone.gemfire.internal.memcached.commands.AddQCommand;
import com.gemstone.gemfire.internal.memcached.commands.AppendCommand;
import com.gemstone.gemfire.internal.memcached.commands.AppendQCommand;
import com.gemstone.gemfire.internal.memcached.commands.CASCommand;
import com.gemstone.gemfire.internal.memcached.commands.DecrementCommand;
import com.gemstone.gemfire.internal.memcached.commands.DecrementQCommand;
import com.gemstone.gemfire.internal.memcached.commands.DeleteCommand;
import com.gemstone.gemfire.internal.memcached.commands.DeleteQCommand;
import com.gemstone.gemfire.internal.memcached.commands.FlushAllCommand;
import com.gemstone.gemfire.internal.memcached.commands.FlushAllQCommand;
import com.gemstone.gemfire.internal.memcached.commands.GATCommand;
import com.gemstone.gemfire.internal.memcached.commands.GATQCommand;
import com.gemstone.gemfire.internal.memcached.commands.GetCommand;
import com.gemstone.gemfire.internal.memcached.commands.GetKCommand;
import com.gemstone.gemfire.internal.memcached.commands.GetKQCommand;
import com.gemstone.gemfire.internal.memcached.commands.GetQCommand;
import com.gemstone.gemfire.internal.memcached.commands.IncrementCommand;
import com.gemstone.gemfire.internal.memcached.commands.IncrementQCommand;
import com.gemstone.gemfire.internal.memcached.commands.NoOpCommand;
import com.gemstone.gemfire.internal.memcached.commands.NotSupportedCommand;
import com.gemstone.gemfire.internal.memcached.commands.PrependCommand;
import com.gemstone.gemfire.internal.memcached.commands.PrependQCommand;
import com.gemstone.gemfire.internal.memcached.commands.QuitCommand;
import com.gemstone.gemfire.internal.memcached.commands.QuitQCommand;
import com.gemstone.gemfire.internal.memcached.commands.ReplaceCommand;
import com.gemstone.gemfire.internal.memcached.commands.ReplaceQCommand;
import com.gemstone.gemfire.internal.memcached.commands.SetCommand;
import com.gemstone.gemfire.internal.memcached.commands.SetQCommand;
import com.gemstone.gemfire.internal.memcached.commands.StatsCommand;
import com.gemstone.gemfire.internal.memcached.commands.TouchCommand;
import com.gemstone.gemfire.internal.memcached.commands.VerbosityCommand;
import com.gemstone.gemfire.internal.memcached.commands.VersionCommand;

/**
 * Represents all commands a memcached client
 * can issue
 * @author Swapnil Bawaskar
 *
 */
public enum Command {

  SET {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new SetCommand();
      }
      return processor;
    }
  },
  SETQ {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new SetQCommand();
      }
      return processor;
    }
  },
  ADD {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new AddCommand();
      }
      return processor;
    }
  },
  ADDQ {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new AddQCommand();
      }
      return processor;
    }
  },
  REPLACE {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new ReplaceCommand();
      }
      return processor;
    }
  },
  REPLACEQ {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new ReplaceQCommand();
      }
      return processor;
    }
  },
  APPEND {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new AppendCommand();
      }
      return processor;
    }
  },
  APPENDQ {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new AppendQCommand();
      }
      return processor;
    }
  },
  PREPEND {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new PrependCommand();
      }
      return processor;
    }
  },
  PREPENDQ {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new PrependQCommand();
      }
      return processor;
    }
  },
  CAS {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new CASCommand();
      }
      return processor;
    }
  },
  GET {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new GetCommand();
      }
      return processor;
    }
  },
  GETS {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new GetCommand();
      }
      return processor;
    }
  },
  GETQ {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new GetQCommand();
      }
      return processor;
    }
  },
  GETK {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new GetKCommand();
      }
      return processor;
    }
  },
  GETKQ {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new GetKQCommand();
      }
      return processor;
    }
  },
  DELETE {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new DeleteCommand();
      }
      return processor;
    }
  },
  DELETEQ {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new DeleteQCommand();
      }
      return processor;
    }
  },
  INCR {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new IncrementCommand();
      }
      return processor;
    }
  },
  INCRQ {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new IncrementQCommand();
      }
      return processor;
    }
  },
  DECR {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new DecrementCommand();
      }
      return processor;
    }
  },
  DECRQ {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new DecrementQCommand();
      }
      return processor;
    }
  },
  STATS {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new StatsCommand();
      }
      return processor;
    }
  },
  FLUSH_ALL {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new FlushAllCommand();
      }
      return processor;
    }
  },
  FLUSH_ALLQ {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new FlushAllQCommand();
      }
      return processor;
    }
  },
  VERSION {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new VersionCommand();
      }
      return processor;
    }
  },
  VERBOSITY {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new VerbosityCommand();
      }
      return processor;
    }
  },
  QUIT {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new QuitCommand();
      }
      return processor;
    }
  },
  QUITQ {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new QuitQCommand();
      }
      return processor;
    }
  },
  NOOP {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new NoOpCommand();
      }
      return processor;
    }
  },
  TOUCH {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new TouchCommand();
      }
      return processor;
    }
  },
  GAT {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new GATCommand();
      }
      return processor;
    }
  },
  GATQ {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new GATQCommand();
      }
      return processor;
    }
  },
  NOT_SUPPORTED {
    private CommandProcessor processor;
    @Override
    public CommandProcessor getCommandProcessor() {
      if (processor == null) {
        processor = new NotSupportedCommand();
      }
      return processor;
    }
      
  };

  /**
   * Returns a command processor to process the given {@link #Command}
   * @return a command processor
   */
  public abstract CommandProcessor getCommandProcessor();

  private static final int HEADER_LENGTH = 24;
  private static final byte REQUEST_MAGIC = (byte) 0x80;

  /**
   * Reads the request header and returns the corresponding binary op
   * @param header
   * @return the binary command
   */
  public static Command getBinaryCommand(ByteBuffer header) {
    if (header.remaining() < HEADER_LENGTH) {
      throw new IllegalStateException("Not a vaild request, header is not complete");
    }
    byte magic = header.get();
    if (magic != REQUEST_MAGIC) {
      throw new IllegalStateException("Not a valid request, magic byte incorrect "+magic+" b:"+Integer.toHexString(magic)+" u:"+byteToHex(magic));
    }
    byte opCode = header.get();
    if (ConnectionHandler.getLogger().finerEnabled()) {
      String str = buffertoString(header);
      ConnectionHandler.getLogger().finer("Request:"+header+str.toString());
    }
    return getCommandFromOpCode(opCode);
  }

  public static String buffertoString(ByteBuffer header) {
    StringBuilder str = new StringBuilder("\n0: ");
    for (int i=0; i> 4) & 0x0f], hexDigit[b & 0x0f] };
    return "0x" + new String(array);
 }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy