private Operation optimizeGets() {
    // make sure there are at least two get operations in a row before
    // attempting to optimize them.
    Operation optimizedOp = writeQ.remove();
    if (writeQ.peek() instanceof GetOperation) {
      OptimizedGetImpl og = new OptimizedGetImpl((GetOperation) optimizedOp);
      optimizedOp = og;

      while (writeQ.peek() instanceof GetOperation) {
        GetOperation o = (GetOperation) writeQ.remove();
        if (!o.isCancelled()) {
          og.addOperation(o);
        }
      }

      // Initialize the new mega get
      optimizedOp.initialize();
      assert optimizedOp.getState() == OperationState.WRITING;
      ProxyCallback pcb = (ProxyCallback) og.getCallback();
      getLogger()
          .debug(
              "Set up %s with %s keys and %s callbacks", this, pcb.numKeys(), pcb.numCallbacks());
    }

    return optimizedOp;
  }
  public void testMultipleGetOperationFanout() {
    Collection<String> keys = Arrays.asList("k1", "k2", "k3");
    Mock m = mock(GetOperation.Callback.class);
    OperationStatus st = new OperationStatus(true, "blah");
    m.expects(once()).method("complete");
    m.expects(once()).method("receivedStatus").with(same(st));
    m.expects(once()).method("gotData").with(eq("k1"), eq(1), isA(byte[].class));
    m.expects(once()).method("gotData").with(eq("k2"), eq(2), isA(byte[].class));
    m.expects(once()).method("gotData").with(eq("k3"), eq(3), isA(byte[].class));

    GetOperation.Callback callback = (GetOperation.Callback) m.proxy();
    GetOperation op = ofact.get(keys, callback);

    // Transition each operation callback into the complete state.
    Iterator<String> ki = keys.iterator();
    int i = 0;
    for (Operation o : ofact.clone(op)) {
      GetOperation.Callback cb = (GetOperation.Callback) o.getCallback();
      cb.gotData(ki.next(), ++i, new byte[3]);
      cb.receivedStatus(st);
      cb.complete();
    }
  }
  private Operation optimizeSets() {
    // make sure there are at least two get operations in a row before
    // attempting to optimize them.
    Operation optimizedOp = writeQ.remove();
    if (writeQ.peek() instanceof CASOperation) {
      OptimizedSetImpl og = new OptimizedSetImpl((CASOperation) optimizedOp);
      optimizedOp = og;

      while (writeQ.peek() instanceof StoreOperation
          && og.size() < MAX_SET_OPTIMIZATION_COUNT
          && og.bytes() < MAX_SET_OPTIMIZATION_BYTES) {
        CASOperation o = (CASOperation) writeQ.remove();
        if (!o.isCancelled()) {
          og.addOperation(o);
        }
      }

      // Initialize the new mega set
      optimizedOp.initialize();
      assert optimizedOp.getState() == OperationState.WRITING;
    }

    return optimizedOp;
  }
 protected void assertCallback(Operation op) {
   assertSame(genericCallback, op.getCallback());
 }