Package net.rubyeye.xmemcached.command

Examples of net.rubyeye.xmemcached.command.Command


    assertEquals("1000", new String(((CachedData) command.getResult())
        .getData()));
  }

  public void testGetsOneDecode() {
    Command command = this.commandFactory.createGetCommand("test", "test"
        .getBytes(), CommandType.GETS_ONE, null);
    checkDecodeNullAndNotLineByteBuffer(command);
    checkDecodeInvalidLine(command, "STORED\r\n");
    checkDecodeInvalidLine(command, "NOT_FOUND\r\n");
    checkDecodeInvalidLine(command, "NOT_STORED\r\n");
    checkDecodeInvalidLine(command, "DELETED\r\n");

    checkDecodeValidLine(command, "END\r\n");
    assertNull(command.getResult());
    assertEquals(0, command.getLatch().getCount());

    command = this.commandFactory.createGetCommand("test", "test"
        .getBytes(), CommandType.GET_ONE, null);
    assertFalse(command.decode(null, ByteBuffer
        .wrap("VALUE test 0 2 999\r\n10\r\n".getBytes())));
    assertNull(command.getResult());
    checkDecodeValidLine(command, "VALUE test 0 2 999\r\n10\r\nEND\r\n");

    assertEquals("10", new String(((CachedData) command.getResult())
        .getData()));
    assertEquals(999, ((CachedData) command.getResult()).getCas());
  }
View Full Code Here


  /**
   * On receive message from memcached server
   */
  @Override
  public final void onMessageReceived(final Session session, final Object msg) {
    Command command = (Command) msg;
    if (this.statisticsHandler.isStatistics()) {
      if (command.getMergeCount() > 0) {
        int size = ((MapReturnValueAware) command).getReturnValues()
            .size();
        this.statisticsHandler.statistics(CommandType.GET_HIT, size);
        this.statisticsHandler.statistics(CommandType.GET_MISS, command
            .getMergeCount()
            - size);
      } else if (command instanceof TextGetOneCommand
          || command instanceof BinaryGetCommand) {
        if (command.getResult() != null) {
          this.statisticsHandler.statistics(CommandType.GET_HIT);
        } else {
          this.statisticsHandler.statistics(CommandType.GET_MISS);
        }
      } else {
        this.statisticsHandler.statistics(command.getCommandType());
      }
    }
  }
View Full Code Here

  /**
   * put command which have been sent to queue
   */
  @Override
  public final void onMessageSent(Session session, Object msg) {
    Command command = (Command) msg;
    command.setStatus(OperationStatus.SENT);
    if (!command.isNoreply()
        || this.client.getProtocol() == Protocol.Binary) {
      ((MemcachedTCPSession) session).addCommand(command);
    }
  }
View Full Code Here

    if (this.enableHeartBeat) {
      log.debug("Session (%s) is idle,send heartbeat", session
          .getRemoteSocketAddress() == null ? "unknown" : SystemUtils
          .getRawAddress(session.getRemoteSocketAddress())
          + ":" + session.getRemoteSocketAddress().getPort());
      Command versionCommand = null;
      CountDownLatch latch = new CountDownLatch(1);
      if (this.client.getProtocol() == Protocol.Binary) {
        versionCommand = new BinaryVersionCommand(latch, session
            .getRemoteSocketAddress());
View Full Code Here

    keys.add("test3");
    keys.add("test4");
  }

  public void testGetManyEncode() {
    Command command = this.commandFactory.createGetMultiCommand(keys,
        new CountDownLatch(1), CommandType.GET_MANY, transcoder);
    assertNull(command.getIoBuffer());
    command.encode();

    checkByteBufferEquals(command, "get test1 test2 test3 test4\r\n");

  }
View Full Code Here

    checkByteBufferEquals(command, "get test1 test2 test3 test4\r\n");

  }

  public void testGetsManyEncode() {
    Command command = this.commandFactory.createGetMultiCommand(keys,
        new CountDownLatch(1), CommandType.GETS_MANY, transcoder);
    assertNull(command.getIoBuffer());
    command.encode();

    checkByteBufferEquals(command, "gets test1 test2 test3 test4\r\n");
  }
View Full Code Here

  @SuppressWarnings("unchecked")
  public Command optimize(final Command currentCommand,
      final Queue writeQueue, final Queue<Command> executingCmds,
      int sendBufferSize) {
    Command optimiezeCommand = currentCommand;
    optimiezeCommand = optimiezeGet(writeQueue, executingCmds,
        optimiezeCommand);
    optimiezeCommand = optimiezeMergeBuffer(optimiezeCommand, writeQueue,
        executingCmds, sendBufferSize);
    return optimiezeCommand;
View Full Code Here

  @SuppressWarnings("unchecked")
  private final Command mergeBuffer(final Command firstCommand,
      final Queue writeQueue, final Queue<Command> executingCmds,
      final int sendBufferSize) {
    Command lastCommand = firstCommand; // 合并的最后一个command
    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;
      }

    }
View Full Code Here

    final CommandCollector commandCollector = creatCommandCollector();
    currentCmd.setStatus(OperationStatus.WRITING);

    commandCollector.visit(currentCmd);
    while (mergeCount < mergeFactor) {
      Command nextCmd = (Command) writeQueue.peek();
      if (nextCmd == null) {
        break;
      }
      if (nextCmd.isCancel()) {
        writeQueue.remove();
        continue;
      }
      if (nextCmd.getCommandType() == expectedCommandType) {
        if (mergeCommands == null) { // lazy initialize
          mergeCommands = new HashMap<Object, Command>(
              mergeFactor / 2);
          mergeCommands.put(currentCmd.getKey(), currentCmd);
        }
        if (log.isDebugEnabled()) {
          log.debug("Merge get command:" + nextCmd.toString());
        }
        nextCmd.setStatus(OperationStatus.WRITING);
        Command removedCommand = (Command) writeQueue.remove();
        // If the key is exists,add the command to associated list.
        if (mergeCommands.containsKey(removedCommand.getKey())) {
          final AssocCommandAware mergedGetCommand = (AssocCommandAware) mergeCommands
              .get(removedCommand.getKey());
          if (mergedGetCommand.getAssocCommands() == null) {
            mergedGetCommand
                .setAssocCommands(new ArrayList<Command>(5));
          }
          mergedGetCommand.getAssocCommands().add(removedCommand);
        } else {
          commandCollector.visit(nextCmd);
          mergeCommands.put(removedCommand.getKey(), removedCommand);
        }
        mergeCount++;
      } else {
        break;
      }
View Full Code Here

    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;
    }
View Full Code Here

TOP

Related Classes of net.rubyeye.xmemcached.command.Command

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.