Permalink
Browse files

Suport redis APPEND

  • Loading branch information...
justinsb committed Dec 13, 2013
1 parent d969092 commit b81e4459b2f707d4a092dbc827077fbb98701d25
@@ -0,0 +1,34 @@
package com.cloudata.keyvalue.btree.operation;
import java.nio.ByteBuffer;
public class AppendOperation extends KeyOperation {
final ByteBuffer appendValue;
private int newLength;
public AppendOperation(ByteBuffer appendValue) {
this.appendValue = appendValue;
}
@Override
public ByteBuffer doAction(ByteBuffer oldValue) {
if (oldValue == null) {
this.newLength = appendValue.remaining();
return appendValue;
} else {
int n = oldValue.remaining() + appendValue.remaining();
ByteBuffer appended = ByteBuffer.allocate(n);
appended.put(oldValue.duplicate());
appended.put(appendValue.duplicate());
appended.flip();
this.newLength = n;
return appended;
}
}
public int getNewLength() {
return newLength;
}
}
@@ -9,6 +9,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cloudata.keyvalue.redis.commands.AppendCommand;
import com.cloudata.keyvalue.redis.commands.DecrByCommand;
import com.cloudata.keyvalue.redis.commands.DecrCommand;
import com.cloudata.keyvalue.redis.commands.DelCommand;
@@ -46,6 +47,7 @@
addMethod("get", new GetCommand());
addMethod("exists", new ExistsCommand());
addMethod("append", new AppendCommand());
addMethod("set", new SetCommand());
addMethod("del", new DelCommand());
@@ -0,0 +1,29 @@
package com.cloudata.keyvalue.redis.commands;
import java.nio.ByteBuffer;
import com.cloudata.keyvalue.btree.operation.AppendOperation;
import com.cloudata.keyvalue.redis.RedisException;
import com.cloudata.keyvalue.redis.RedisRequest;
import com.cloudata.keyvalue.redis.RedisServer;
import com.cloudata.keyvalue.redis.response.ErrorRedisReponse;
import com.cloudata.keyvalue.redis.response.IntegerRedisResponse;
import com.cloudata.keyvalue.redis.response.RedisResponse;
public class AppendCommand implements RedisCommand {
@Override
public RedisResponse execute(RedisServer server, RedisRequest command) throws RedisException {
int argc = command.getArgc();
if (argc != 3) {
return ErrorRedisReponse.NOT_IMPLEMENTED;
}
byte[] key = command.get(1);
byte[] value = command.get(2);
AppendOperation operation = new AppendOperation(ByteBuffer.wrap(value));
server.getKeyValueStore().doAction(ByteBuffer.wrap(key), operation);
return IntegerRedisResponse.valueOf(operation.getNewLength());
}
}
@@ -41,6 +41,25 @@ public void testSetAndGet() throws Exception {
}
}
@Test
public void testAppend() throws Exception {
byte[] key = UUID.randomUUID().toString().getBytes();
for (int i = 1; i < 100; i++) {
byte[] value = new byte[] { (byte) i };
Long newLength = jedis.append(key, value);
Assert.assertEquals(i, newLength.longValue());
}
byte[] finalValue = jedis.get(key);
for (int i = 1; i < 100; i++) {
Assert.assertEquals(i, finalValue[i - 1]);
}
}
@Test
public void testSetAndDelete() throws Exception {
List<byte[]> allKeys = Lists.newArrayList();

0 comments on commit b81e445

Please sign in to comment.