Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix arithmetic command executor

Change-Id: I2e60b1ff4605d648298898efdd9030881a6079ea
Reviewed-on: http://review.couchbase.org/11990
Tested-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Reviewed-by: Trond Norbye <trond.norbye@gmail.com>
  • Loading branch information...
commit 947e003d70cce718fb7adc5ed0db8ab53223d08a 1 parent e33824e
@avsej avsej authored trondn committed
View
78 src/main/java/org/couchbase/mock/memcached/ArithmeticCommandExecutor.java
@@ -35,49 +35,53 @@ public void execute(BinaryCommand command, MemcachedServer server, MemcachedConn
Item item = server.getDatastore().get(server, cmd.getVBucketId(), cmd.getKey());
ComCode cc = cmd.getComCode();
- if (item == null && cmd.create()) {
- item = new Item(cmd.getKey(), 0, cmd.getExpiration(), Long.toString(cmd.getInitial()).getBytes(), 0);
- ErrorCode err = server.getDatastore().add(server, cmd.getVBucketId(), item);
+ if (item == null) {
+ if (cmd.create()) {
+ item = new Item(cmd.getKey(), 0, cmd.getExpiration(), Long.toString(cmd.getInitial()).getBytes(), 0);
+ ErrorCode err = server.getDatastore().add(server, cmd.getVBucketId(), item);
- switch (err) {
- case KEY_EEXISTS:
- execute(command, server, client);
- break;
- case SUCCESS:
- if (cc == ComCode.INCREMENT || cc == ComCode.DECREMENT) {
- // return value
- }
- break;
- default:
- client.sendResponse(new BinaryResponse(command, err));
+ switch (err) {
+ case KEY_EEXISTS:
+ execute(command, server, client);
+ break;
+ case SUCCESS:
+ if (cc == ComCode.INCREMENT || cc == ComCode.DECREMENT) {
+ client.sendResponse(new BinaryArithmeticResponse(cmd, cmd.getInitial(), item.getCas()));
+ }
+ break;
+ default:
+ client.sendResponse(new BinaryResponse(command, err));
+ }
+ } else {
+ client.sendResponse(new BinaryResponse(command, ErrorCode.KEY_ENOENT));
}
-
- return;
- }
-
- long value;
- try {
- value = Long.parseLong(new String(item.getValue()));
- } catch (NumberFormatException ex) {
- client.sendResponse(new BinaryResponse(command, ErrorCode.DELTA_BADVAL));
return;
- }
-
- if (cc == ComCode.INCREMENT || cc == ComCode.INCREMENTQ) {
- value += cmd.getDelta();
} else {
- value -= cmd.getDelta();
- }
+ long value;
+ try {
+ value = Long.parseLong(new String(item.getValue()));
+ } catch (NumberFormatException ex) {
+ client.sendResponse(new BinaryResponse(command, ErrorCode.DELTA_BADVAL));
+ return;
+ }
- Item nval = new Item(cmd.getKey(), 0, cmd.getExpiration(), Long.toString(value).getBytes(), item.getCas());
- ErrorCode err = server.getDatastore().set(server, cmd.getVBucketId(), nval);
- if (err == ErrorCode.SUCCESS) {
- if (cc == ComCode.INCREMENT || cc == ComCode.DECREMENT) {
- // return value
- client.sendResponse(new BinaryArithmeticResponse(cmd, value, nval.getCas()));
+ if (cc == ComCode.INCREMENT || cc == ComCode.INCREMENTQ) {
+ value += cmd.getDelta();
+ } else {
+ value -= cmd.getDelta();
+ }
+
+ int exp = cmd.getExpiration() > 0 ? cmd.getExpiration() : item.getExptime();
+ Item nval = new Item(cmd.getKey(), 0, exp, Long.toString(value).getBytes(), item.getCas());
+ ErrorCode err = server.getDatastore().set(server, cmd.getVBucketId(), nval);
+ if (err == ErrorCode.SUCCESS) {
+ if (cc == ComCode.INCREMENT || cc == ComCode.DECREMENT) {
+ // return value
+ client.sendResponse(new BinaryArithmeticResponse(cmd, value, nval.getCas()));
+ }
+ } else {
+ client.sendResponse(new BinaryResponse(command, err));
}
- } else {
- client.sendResponse(new BinaryResponse(command, err));
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.