Package net.rubyeye.xmemcached.command

Examples of net.rubyeye.xmemcached.command.Command


    memcachedClient.set("name", 0, "dennis");
    assertEquals("dennis", memcachedClient.get("name"));
    long writeMessageCount = memcachedClient.getConnector().getStatistics()
        .getWriteMessageCount();
    CountDownLatch latch = new CountDownLatch(1);
    Command errorCommand = null;
    if (memcachedClient.getProtocol() == Protocol.Text) {
      errorCommand = new MockEncodeTimeoutTextGetOneCommand("name",
          "name".getBytes(), CommandType.GET_ONE, latch, 1000);
    } else {
      errorCommand = new MockEncodeTimeoutBinaryGetCommand("name",
          "name".getBytes(), CommandType.GET_ONE, latch, OpCode.GET,
          false, 1000);
    }

    memcachedClient.getConnector().send(errorCommand);
    // Force write thread to encode command
    errorCommand.setIoBuffer(null);
    // wait 100 milliseconds,the operation will be timeout
    if (!latch.await(100, TimeUnit.MILLISECONDS)) {
      errorCommand.cancel();
    }
    Thread.sleep(1000);
    // It is not written to channel,because it is canceled.
    assertEquals(writeMessageCount, memcachedClient.getConnector()
        .getStatistics().getWriteMessageCount());
View Full Code Here


      System.out.println("gets:" + (newGets - oldGets));
    }
  }

  public void _testErrorCommand() throws Exception {
    Command nonexisCmd = new Command() {

      @Override
      public boolean decode(MemcachedTCPSession session, ByteBuffer buffer) {
        return decodeError(ByteUtils.nextLine(buffer));
      }

      @Override
      public void encode() {
        ioBuffer = IoBuffer
            .wrap(ByteBuffer.wrap("test\r\n".getBytes()));
      }

    };
    nonexisCmd.setKey("test");
    nonexisCmd.setLatch(new CountDownLatch(1));
    memcachedClient.getConnector().send(nonexisCmd);
    // this.memcachedClient.flushAll();
    nonexisCmd.getLatch().await();

    assertNotNull(nonexisCmd.getException());
    assertEquals("Nonexist command,check your memcached version please.",
        nonexisCmd.getException().getMessage());
    assertTrue(nonexisCmd.getException() instanceof UnknownCommandException);

    memcachedClient.set("name", 0, "dennis");
    assertEquals("dennis", memcachedClient.get("name"));
  }
View Full Code Here

  public void testOperationDecodeTimeOut() throws Exception {
    memcachedClient.set("name", 0, "dennis");
    assertEquals("dennis", memcachedClient.get("name"));
    CountDownLatch latch = new CountDownLatch(1);
    Command errorCommand = null;
    if (memcachedClient.getProtocol() == Protocol.Text) {
      errorCommand = new MockDecodeTimeoutTextGetOneCommand("name",
          "name".getBytes(), CommandType.GET_ONE, latch, 1000);
    } else {
      errorCommand = new MockDecodeTimeoutBinaryGetOneCommand("name",
          "name".getBytes(), CommandType.GET_ONE, latch, OpCode.GET,
          false, 1000);
    }
    memcachedClient.getConnector().send(errorCommand);
    // wait 100 milliseconds,the operation will be timeout
    latch.await(100, TimeUnit.MILLISECONDS);
    assertNull(errorCommand.getResult());
    Thread.sleep(1000);
    // It works.
    assertNotNull(errorCommand.getResult());
    assertEquals("dennis", memcachedClient.get("name"));
  }
View Full Code Here

    memcachedClient.set("name", 0, "dennis");
    assertEquals("dennis", memcachedClient.get("name"));
    long writeMessageCount = memcachedClient.getConnector().getStatistics()
        .getWriteMessageCount();
    CountDownLatch latch = new CountDownLatch(1);
    Command errorCommand = null;
    if (memcachedClient.getProtocol() == Protocol.Text) {
      errorCommand = new MockEncodeTimeoutTextGetOneCommand("name",
          "name".getBytes(), CommandType.GET_ONE, latch, 1000);
    } else {
      errorCommand = new MockEncodeTimeoutBinaryGetCommand("name", "name"
          .getBytes(), CommandType.GET_ONE, latch, OpCode.GET, false,
          1000);
    }

    memcachedClient.getConnector().send(errorCommand);
    // Force write thread to encode command
    errorCommand.setIoBuffer(null);
    // wait 100 milliseconds,the operation will be timeout
    if (!latch.await(100, TimeUnit.MILLISECONDS)) {
      errorCommand.cancel();
    }
    Thread.sleep(1000);
    // It is not written to channel,because it is canceled.
    assertEquals(writeMessageCount, memcachedClient.getConnector()
        .getStatistics().getWriteMessageCount());
View Full Code Here

      System.out.println("gets:" + (newGets - oldGets));
    }
  }

  public void _testErrorCommand() throws Exception {
    Command nonexisCmd = new Command() {

      @Override
      public boolean decode(MemcachedTCPSession session, ByteBuffer buffer) {
        return decodeError(ByteUtils.nextLine(buffer));
      }

      @Override
      public void encode() {
        ioBuffer = IoBuffer
            .wrap(ByteBuffer.wrap("test\r\n".getBytes()));
      }

    };
    nonexisCmd.setKey("test");
    nonexisCmd.setLatch(new CountDownLatch(1));
    memcachedClient.getConnector().send(nonexisCmd);
    // this.memcachedClient.flushAll();
    nonexisCmd.getLatch().await();

    assertNotNull(nonexisCmd.getException());
    assertEquals("Nonexist command,check your memcached version please.",
        nonexisCmd.getException().getMessage());
    assertTrue(nonexisCmd.getException() instanceof UnknownCommandException);

    memcachedClient.set("name", 0, "dennis");
    assertEquals("dennis", memcachedClient.get("name"));
  }
View Full Code Here

  @SuppressWarnings("unchecked")
  private final <T> Object fetch0(final String key, final byte[] keyBytes,
      final CommandType cmdType, final long timeout,
      Transcoder<T> transcoder) throws InterruptedException,
      TimeoutException, MemcachedException, MemcachedException {
    final Command command = this.commandFactory.createGetCommand(key,
        keyBytes, cmdType, this.transcoder);
    this.sendCommand(command);
    this.latchWait(command, timeout);
    command.getIoBuffer().free(); // free buffer
    this.checkException(command);
    CachedData data = (CachedData) command.getResult();
    if (data == null) {
      return null;
    }
    if (transcoder == null) {
      transcoder = this.transcoder;
View Full Code Here

  private final <T> Command sendGetMultiCommand(
      final Collection<String> keys, final CountDownLatch latch,
      final CommandType cmdType, final Transcoder<T> transcoder)
      throws InterruptedException, TimeoutException, MemcachedException {
    final Command command = this.commandFactory.createGetMultiCommand(keys,
        latch, cmdType, transcoder);
    this.sendCommand(command);
    return command;
  }
View Full Code Here

      long opTimeout) throws MemcachedException, InterruptedException,
      TimeoutException {
    key = this.sanitizeKey(key);
    final byte[] keyBytes = ByteUtils.getBytes(key);
    ByteUtils.checkKey(keyBytes);
    final Command command = this.commandFactory.createDeleteCommand(key,
        keyBytes, time, noreply);
    this.sendCommand(command);
    if (!command.isNoreply()) {
      this.latchWait(command, opTimeout);
      command.getIoBuffer().free();
      this.checkException(command);
      if (command.getResult() == null) {
        throw new MemcachedException(
            "Operation fail,may be caused by networking or timeout");
      }
    } else {
      return false;
    }
    return (Boolean) command.getResult();
  }
View Full Code Here

      throws TimeoutException, InterruptedException, MemcachedException {
    key = this.sanitizeKey(key);
    final byte[] keyBytes = ByteUtils.getBytes(key);
    ByteUtils.checkKey(keyBytes);
    CountDownLatch latch = new CountDownLatch(1);
    final Command command = this.commandFactory.createTouchCommand(key,
        keyBytes, latch, exp, false);
    this.sendCommand(command);
    this.latchWait(command, opTimeout);
    command.getIoBuffer().free();
    this.checkException(command);
    if (command.getResult() == null) {
      throw new MemcachedException(
          "Operation fail,may be caused by networking or timeout");
    }
    return (Boolean) command.getResult();
  }
View Full Code Here

    int totalLength = 0;
    while (it.hasNext()) {
      key = it.next();
      if (it.hasNext()) {
        // first n-1 send gatq command
        Command command = this.commandFactory.createGetAndTouchCommand(
            key, ByteUtils.getBytes(key), null, innerKeyExpMap
                .get(key), true);
        command.encode();
        totalLength += command.getIoBuffer().remaining();
        bufferList.add(command.getIoBuffer());
      }
    }
    // last key,create a gat command
    Command lastCommand = this.commandFactory.createGetAndTouchCommand(key,
        ByteUtils.getBytes(key), null, innerKeyExpMap.get(key), false);
    lastCommand.encode();
    bufferList.add(lastCommand.getIoBuffer());
    totalLength += lastCommand.getIoBuffer().remaining();

    IoBuffer mergedBuffer = IoBuffer.allocate(totalLength);
    for (IoBuffer buffer : bufferList) {
      mergedBuffer.put(buffer.buf());
    }
    mergedBuffer.flip();

    Command resultCommand = new BinaryGetMultiCommand(key,
        CommandType.GET_MANY, latch);
    resultCommand.setIoBuffer(mergedBuffer);
    this.sendCommand(resultCommand);
    return resultCommand;
  }
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.