Permalink
Browse files

Fix append/prepend commands

Change-Id: I4a6c12bbf29790abb8ef76d7807e8da99584195f
Reviewed-on: http://review.couchbase.org/11956
Tested-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Reviewed-by: Trond Norbye <trond.norbye@gmail.com>
  • Loading branch information...
avsej authored and trondn committed Dec 29, 2011
1 parent c80c975 commit bbae7588b9404e35ad5adeb1d3931e8f705e7336
@@ -28,6 +28,7 @@
* @author Trond Norbye <trond.norbye@gmail.com>
*/
class AppendCommandExecutor implements CommandExecutor {
+
@Override
public void execute(BinaryCommand cmd, MemcachedServer server, MemcachedConnection client) {
BinaryStoreCommand command = (BinaryStoreCommand) cmd;
@@ -36,17 +37,15 @@ public void execute(BinaryCommand cmd, MemcachedServer server, MemcachedConnecti
Item item = command.getItem();
Item existing = server.getDatastore().get(server, cmd.getVBucketId(), cmd.getKey());
- if (item == null) {
- client.sendResponse(new BinaryResponse(cmd, ErrorCode.KEY_ENOENT));
- return;
+ if (existing == null) {
+ client.sendResponse(new BinaryResponse(cmd, ErrorCode.NOT_STORED));
+ return;
}
- // We're supposed to append this to the existing object, that
- // means we need to prepend the existing object to this..
- item.prepend(existing);
- err = server.getDatastore().replace(server, cmd.getVBucketId(), item);
+ existing.append(item);
+ err = server.getDatastore().replace(server, cmd.getVBucketId(), existing);
if (err == ErrorCode.SUCCESS && cmd.getComCode() == ComCode.APPENDQ) {
return;
}
- client.sendResponse(new BinaryStoreResponse(command, err, item.getCas()));
+ client.sendResponse(new BinaryStoreResponse(command, err, existing.getCas()));
}
}
@@ -60,16 +60,22 @@ void setCas(long l) {
}
public void append(Item i) {
- byte[] b = new byte[value.length + i.getValue().length];
- System.arraycopy(b, 0, value, 0, value.length);
- System.arraycopy(b, value.length, i.getValue(), 0, i.getValue().length);
- value = b;
+ byte[] s1 = value;
+ byte[] s2 = i.getValue();
+ byte[] dst = new byte[s1.length + s2.length];
+
+ System.arraycopy(s1, 0, dst, 0, s1.length);
+ System.arraycopy(s2, 0, dst, s1.length, s2.length);
+ value = dst;
}
public void prepend(Item i) {
- byte[] b = new byte[value.length + i.getValue().length];
- System.arraycopy(b, 0, i.getValue(), 0, i.getValue().length);
- System.arraycopy(b, i.getValue().length, value, 0, value.length);
- value = b;
+ byte[] s1 = value;
+ byte[] s2 = i.getValue();
+ byte[] dst = new byte[s1.length + s2.length];
+
+ System.arraycopy(s2, 0, dst, 0, s2.length);
+ System.arraycopy(s1, 0, dst, s2.length, s1.length);
+ value = dst;
}
}
@@ -28,6 +28,7 @@
* @author Trond Norbye <trond.norbye@gmail.com>
*/
class PrependCommandExecutor implements CommandExecutor {
+
@Override
public void execute(BinaryCommand cmd, MemcachedServer server, MemcachedConnection client) {
BinaryStoreCommand command = (BinaryStoreCommand) cmd;
@@ -36,17 +37,15 @@ public void execute(BinaryCommand cmd, MemcachedServer server, MemcachedConnecti
Item item = command.getItem();
Item existing = server.getDatastore().get(server, cmd.getVBucketId(), cmd.getKey());
- if (item == null) {
- client.sendResponse(new BinaryResponse(cmd, ErrorCode.KEY_ENOENT));
- return;
+ if (existing == null) {
+ client.sendResponse(new BinaryResponse(cmd, ErrorCode.NOT_STORED));
+ return;
}
- // We're supposed to prepend this to the existing object, that
- // means we need to append the existing object to this..
- item.append(existing);
- err = server.getDatastore().replace(server, cmd.getVBucketId(), item);
+ existing.prepend(item);
+ err = server.getDatastore().replace(server, cmd.getVBucketId(), existing);
if (err == ErrorCode.SUCCESS && cmd.getComCode() == ComCode.PREPENDQ) {
return;
}
- client.sendResponse(new BinaryStoreResponse(command, err, item.getCas()));
+ client.sendResponse(new BinaryStoreResponse(command, err, existing.getCas()));
}
}

0 comments on commit bbae758

Please sign in to comment.