long initValue, final CommandType cmdType, boolean noreply,
long operationTimeout, int exp) throws InterruptedException,
TimeoutException, MemcachedException {
final byte[] keyBytes = ByteUtils.getBytes(key);
ByteUtils.checkKey(keyBytes);
final Command command = this.commandFactory.createIncrDecrCommand(key,
keyBytes, delta, initValue, exp, cmdType, noreply);
this.sendCommand(command);
if (!command.isNoreply()) {
this.latchWait(command, operationTimeout);
command.getIoBuffer().free();
this.checkException(command);
if (command.getResult() == null) {
throw new MemcachedException(
"Operation fail,may be caused by networking or timeout");
}
final Object result = command.getResult();
if (result instanceof String) {
if (((String) result).equals("NOT_FOUND")) {
if (this.add(key, exp, String.valueOf(initValue),
this.opTimeout)) {
return initValue;
} else {
return this.sendIncrOrDecrCommand(key, delta,
initValue, cmdType, noreply, operationTimeout,
exp);
}
} else {
throw new MemcachedException(
"Unknown result type for incr/decr:"
+ result.getClass() + ",result=" + result);
}
} else {
return (Long) command.getResult();
}
} else {
return -1;
}
}