Exemple #1
0
 public void finish() {
   // prev command is the last command,last command must be getk,ensure
   // getq commands send response back
   Command lastGetKCommand =
       new BinaryGetCommand(
           prevCommand.getKey(),
           prevCommand.getKeyBytes(),
           CommandType.GET_ONE,
           new CountDownLatch(1),
           OpCode.GET_KEY,
           false);
   lastGetKCommand.encode();
   bufferList.add(lastGetKCommand.getIoBuffer());
   totalBytes += lastGetKCommand.getIoBuffer().remaining();
 }
Exemple #2
0
 public void visit(Command command) {
   // Encode prev command
   if (prevCommand != null) {
     // first n-1 send getq command
     Command getqCommand =
         new BinaryGetCommand(
             prevCommand.getKey(),
             prevCommand.getKeyBytes(),
             null,
             null,
             OpCode.GET_KEY_QUIETLY,
             true);
     getqCommand.encode();
     totalBytes += getqCommand.getIoBuffer().remaining();
     bufferList.add(getqCommand.getIoBuffer());
   }
   prevCommand = command;
 }
  public void testAddEncodeAndDecode() {

    Command command =
        this.commandFactory.createCASCommand(
            this.key, this.keyBytes, 0, this.value, 9L, this.noreply, this.transcoder);

    command.encode();
    ByteBuffer encodeBuffer = command.getIoBuffer().buf();
    assertNotNull(encodeBuffer);
    assertEquals(42, encodeBuffer.capacity());

    byte opCode = encodeBuffer.get(1);
    // cas use set command
    assertEquals(OpCode.SET.fieldValue(), opCode);

    ByteBuffer buffer =
        constructResponse(
            OpCode.SET.fieldValue(),
            (short) 0,
            (byte) 0,
            (byte) 0,
            (short) 0,
            0,
            0,
            10L,
            null,
            null,
            null);

    assertTrue(command.decode(null, buffer));
    assertTrue((Boolean) command.getResult());
    assertEquals(0, buffer.remaining());

    buffer =
        constructResponse(
            OpCode.SET.fieldValue(),
            (short) 0,
            (byte) 0,
            (byte) 0,
            (short) 0x0005,
            0,
            0,
            0L,
            null,
            null,
            null);
    command =
        this.commandFactory.createCASCommand(
            this.key, this.keyBytes, 0, this.value, 9L, this.noreply, this.transcoder);
    assertTrue(command.decode(null, buffer));
    assertFalse((Boolean) command.getResult());
    assertEquals(0, buffer.remaining());
  }
Exemple #4
0
 /**
  * merge buffers to fit socket's send buffer size
  *
  * @param currentCommand
  * @return
  * @throws InterruptedException
  */
 @SuppressWarnings("unchecked")
 public final Command optimiezeMergeBuffer(
     Command optimiezeCommand,
     final Queue writeQueue,
     final Queue<Command> executingCmds,
     int sendBufferSize) {
   if (log.isDebugEnabled()) {
     log.debug("Optimieze merge buffer:" + optimiezeCommand.toString());
   }
   if (optimiezeMergeBuffer && optimiezeCommand.getIoBuffer().remaining() < sendBufferSize) {
     optimiezeCommand = mergeBuffer(optimiezeCommand, writeQueue, executingCmds, sendBufferSize);
   }
   return optimiezeCommand;
 }
Exemple #5
0
  @SuppressWarnings("unchecked")
  private final Command mergeBuffer(
      final Command firstCommand,
      final Queue writeQueue,
      final Queue<Command> executingCmds,
      final int sendBufferSize) {
    Command lastCommand = firstCommand; // ��苟������涓�ommand
    Command nextCmd = (Command) writeQueue.peek();
    if (nextCmd == null) {
      return lastCommand;
    }

    final List<Command> commands = getLocalList();
    final ByteBuffer firstBuffer = firstCommand.getIoBuffer().buf();
    int totalBytes = firstBuffer.remaining();
    commands.add(firstCommand);
    boolean wasFirst = true;
    while (totalBytes + nextCmd.getIoBuffer().remaining() <= sendBufferSize
        && (nextCmd = (Command) writeQueue.peek()) != null) {
      if (nextCmd.getStatus() == OperationStatus.WRITING) {
        break;
      }
      if (nextCmd.isCancel()) {
        writeQueue.remove();
        continue;
      }
      nextCmd.setStatus(OperationStatus.WRITING);

      writeQueue.remove();

      if (wasFirst) {
        wasFirst = false;
      }
      // if it is get_one command,try to merge get commands
      if ((nextCmd.getCommandType() == CommandType.GET_ONE
              || nextCmd.getCommandType() == CommandType.GETS_ONE)
          && optimiezeGet) {
        nextCmd = mergeGetCommands(nextCmd, writeQueue, executingCmds, nextCmd.getCommandType());
      }

      commands.add(nextCmd);
      lastCommand = nextCmd;
      totalBytes += nextCmd.getIoBuffer().remaining();
      if (totalBytes > sendBufferSize) {
        break;
      }
    }
    if (commands.size() > 1) {
      byte[] buf = new byte[totalBytes];
      int offset = 0;
      for (Command command : commands) {
        byte[] ba = command.getIoBuffer().array();
        System.arraycopy(ba, 0, buf, offset, ba.length);
        offset += ba.length;
        if (command != lastCommand
            && (!command.isNoreply() || command instanceof BaseBinaryCommand)) {
          executingCmds.add(command);
        }
      }
      lastCommand.setIoBuffer(IoBuffer.wrap(buf));
    }
    return lastCommand;
  }