@Override
 public BasicInvocationStage visitGetKeyValueCommand(
     InvocationContext ctx, GetKeyValueCommand command) throws Throwable {
   if (streamSummaryContainer.isEnabled() && ctx.isOriginLocal()) {
     streamSummaryContainer.addGet(command.getKey(), command.getRemotelyFetchedValue() != null);
   }
   return invokeNext(ctx, command);
 }
 @Override
 public Object visitGetKeyValueCommand(InvocationContext ctx, GetKeyValueCommand command)
     throws Throwable {
   if (trace) log.trace("Executing command: " + command + ".");
   Object ret = command.perform(ctx);
   if (ret != null) {
     notifyCacheEntryVisit(ctx, command, command.getKey(), ret);
   }
   return ret;
 }
 @Override
 public Object visitGetKeyValueCommand(InvocationContext ctx, GetKeyValueCommand command)
     throws Throwable {
   Object key = command.getKey();
   if (isStoreAsBinary() || getMightGoRemote(ctx, key)) checkMarshallable(key);
   return super.visitGetKeyValueCommand(ctx, command);
 }
 private Object remoteGet(InvocationContext ctx, Object key, GetKeyValueCommand command)
     throws Throwable {
   if (trace) log.tracef("Doing a remote get for key %s", key);
   InternalCacheEntry ice = dm.retrieveFromRemoteSource(key, ctx, false, command);
   command.setRemotelyFetchedValue(ice);
   if (ice != null) {
     return ice.getValue();
   }
   return null;
 }
  @Override
  public Object visitGetKeyValueCommand(InvocationContext ctx, GetKeyValueCommand command)
      throws Throwable {
    try {
      Object returnValue = invokeNextInterceptor(ctx, command);
      // If L1 caching is enabled, this is a remote command, and we found a value in our cache
      // we store it so that we can later invalidate it
      if (returnValue != null && isL1CacheEnabled && !ctx.isOriginLocal())
        l1Manager.addRequestor(command.getKey(), ctx.getOrigin());

      // need to check in the context as well since a null retval is not necessarily an indication
      // of the entry not being
      // available.  It could just have been removed in the same tx beforehand.  Also don't bother
      // with a remote get if
      // the entry is mapped to the local node.
      if (needsRemoteGet(ctx, command.getKey(), returnValue == null))
        returnValue = remoteGetAndStoreInL1(ctx, command.getKey(), false);
      return returnValue;
    } catch (SuspectException e) {
      // retry
      return visitGetKeyValueCommand(ctx, command);
    }
  }
 @Override
 public Object visitGetKeyValueCommand(InvocationContext ctx, GetKeyValueCommand command)
     throws Throwable {
   try {
     Object returnValue = invokeNextInterceptor(ctx, command);
     if (needsRemoteGet(ctx, command, returnValue == null)) {
       Object key = command.getKey();
       returnValue = shouldFetchFromRemote(ctx, key) ? remoteGet(ctx, key, command) : null;
     }
     return returnValue;
   } catch (SuspectException e) {
     // retry
     return visitGetKeyValueCommand(ctx, command);
   }
 }
  public void testReplicableCommandsMarshalling() throws Exception {
    String cacheName = EmbeddedCacheManager.DEFAULT_CACHE_NAME;
    ClusteredGetCommand c2 =
        new ClusteredGetCommand("key", cacheName, Collections.<Flag>emptySet());
    marshallAndAssertEquality(c2);

    // SizeCommand does not have an empty constructor, so doesn't look to be one that is
    // marshallable.

    GetKeyValueCommand c4 = new GetKeyValueCommand("key", null, Collections.<Flag>emptySet());
    byte[] bytes = marshaller.objectToByteBuffer(c4);
    GetKeyValueCommand rc4 = (GetKeyValueCommand) marshaller.objectFromByteBuffer(bytes);
    assert rc4.getCommandId() == c4.getCommandId()
        : "Writen["
            + c4.getCommandId()
            + "] and read["
            + rc4.getCommandId()
            + "] objects should be the same";
    assert Arrays.equals(rc4.getParameters(), c4.getParameters())
        : "Writen["
            + c4.getParameters()
            + "] and read["
            + rc4.getParameters()
            + "] objects should be the same";

    PutKeyValueCommand c5 =
        new PutKeyValueCommand("k", "v", false, null, 0, 0, Collections.<Flag>emptySet());
    marshallAndAssertEquality(c5);

    RemoveCommand c6 = new RemoveCommand("key", null, null, Collections.<Flag>emptySet());
    marshallAndAssertEquality(c6);

    // EvictCommand does not have an empty constructor, so doesn't look to be one that is
    // marshallable.

    InvalidateCommand c7 = new InvalidateCommand(null, null, "key1", "key2");
    bytes = marshaller.objectToByteBuffer(c7);
    InvalidateCommand rc7 = (InvalidateCommand) marshaller.objectFromByteBuffer(bytes);
    assert rc7.getCommandId() == c7.getCommandId()
        : "Writen["
            + c7.getCommandId()
            + "] and read["
            + rc7.getCommandId()
            + "] objects should be the same";
    assert Arrays.equals(rc7.getParameters(), c7.getParameters())
        : "Writen["
            + c7.getParameters()
            + "] and read["
            + rc7.getParameters()
            + "] objects should be the same";

    InvalidateCommand c71 =
        new InvalidateL1Command(
            false, null, null, null, null, Collections.<Flag>emptySet(), "key1", "key2");
    bytes = marshaller.objectToByteBuffer(c71);
    InvalidateCommand rc71 = (InvalidateCommand) marshaller.objectFromByteBuffer(bytes);
    assert rc71.getCommandId() == c71.getCommandId()
        : "Writen["
            + c71.getCommandId()
            + "] and read["
            + rc71.getCommandId()
            + "] objects should be the same";
    assert Arrays.equals(rc71.getParameters(), c71.getParameters())
        : "Writen["
            + c71.getParameters()
            + "] and read["
            + rc71.getParameters()
            + "] objects should be the same";

    ReplaceCommand c8 =
        new ReplaceCommand("key", "oldvalue", "newvalue", null, 0, 0, Collections.EMPTY_SET);
    marshallAndAssertEquality(c8);

    ClearCommand c9 = new ClearCommand();
    bytes = marshaller.objectToByteBuffer(c9);
    ClearCommand rc9 = (ClearCommand) marshaller.objectFromByteBuffer(bytes);
    assert rc9.getCommandId() == c9.getCommandId()
        : "Writen["
            + c9.getCommandId()
            + "] and read["
            + rc9.getCommandId()
            + "] objects should be the same";
    assert Arrays.equals(rc9.getParameters(), c9.getParameters())
        : "Writen["
            + c9.getParameters()
            + "] and read["
            + rc9.getParameters()
            + "] objects should be the same";

    Map m1 = new HashMap();
    for (int i = 0; i < 10; i++) {
      GlobalTransaction gtx =
          gtf.newGlobalTransaction(new JGroupsAddress(new IpAddress(1000 * i)), false);
      m1.put(1000 * i, gtx);
    }
    PutMapCommand c10 = new PutMapCommand(m1, null, 0, 0, Collections.<Flag>emptySet());
    marshallAndAssertEquality(c10);

    Address local = new JGroupsAddress(new IpAddress(12345));
    GlobalTransaction gtx = gtf.newGlobalTransaction(local, false);
    PrepareCommand c11 = new PrepareCommand(cacheName, gtx, true, c5, c6, c8, c10);
    marshallAndAssertEquality(c11);

    CommitCommand c12 = new CommitCommand(cacheName, gtx);
    marshallAndAssertEquality(c12);

    RollbackCommand c13 = new RollbackCommand(cacheName, gtx);
    marshallAndAssertEquality(c13);

    MultipleRpcCommand c99 =
        new MultipleRpcCommand(
            Arrays.<ReplicableCommand>asList(c2, c5, c6, c8, c10, c12, c13), cacheName);
    marshallAndAssertEquality(c99);
  }