Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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...
commit bbae7588b9404e35ad5adeb1d3931e8f705e7336 1 parent c80c975
@avsej avsej authored trondn committed
View
15 src/main/java/org/couchbase/mock/memcached/AppendCommandExecutor.java
@@ -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()));
}
}
View
22 src/main/java/org/couchbase/mock/memcached/Item.java
@@ -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;
}
}
View
15 src/main/java/org/couchbase/mock/memcached/PrependCommandExecutor.java
@@ -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()));
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.