Permalink
Browse files

Implement redis DEL command

  • Loading branch information...
justinsb committed Dec 13, 2013
1 parent 783af5d commit d96909265b7b0175d42053580db10b714cd642fe
@@ -4,9 +4,18 @@
public class DeleteOperation extends KeyOperation {
private int deleteCount;
@Override
public ByteBuffer doAction(ByteBuffer oldValue) {
if (oldValue != null) {
deleteCount++;
}
return null; // Delete the value
}
public int getDeleteCount() {
return deleteCount;
}
}
@@ -11,6 +11,7 @@
import com.cloudata.keyvalue.redis.commands.DecrByCommand;
import com.cloudata.keyvalue.redis.commands.DecrCommand;
import com.cloudata.keyvalue.redis.commands.DelCommand;
import com.cloudata.keyvalue.redis.commands.EchoCommand;
import com.cloudata.keyvalue.redis.commands.ExistsCommand;
import com.cloudata.keyvalue.redis.commands.GetCommand;
@@ -47,6 +48,8 @@
addMethod("set", new SetCommand());
addMethod("del", new DelCommand());
addMethod("incr", new IncrCommand());
addMethod("incrby", new IncrByCommand());
addMethod("decr", new DecrCommand());
@@ -0,0 +1,29 @@
package com.cloudata.keyvalue.redis.commands;
import java.nio.ByteBuffer;
import com.cloudata.keyvalue.btree.operation.DeleteOperation;
import com.cloudata.keyvalue.redis.RedisException;
import com.cloudata.keyvalue.redis.RedisRequest;
import com.cloudata.keyvalue.redis.RedisServer;
import com.cloudata.keyvalue.redis.response.IntegerRedisResponse;
import com.cloudata.keyvalue.redis.response.RedisResponse;
public class DelCommand implements RedisCommand {
@Override
public RedisResponse execute(RedisServer server, RedisRequest command) throws RedisException {
int count = 0;
// TODO: Put into one transaction
for (int i = 1; i < command.getArgc(); i++) {
byte[] key = command.get(i);
DeleteOperation operation = new DeleteOperation();
server.getKeyValueStore().doAction(ByteBuffer.wrap(key), operation);
count += operation.getDeleteCount();
}
return IntegerRedisResponse.valueOf(count);
}
}
@@ -1,6 +1,8 @@
package com.cloudata.keyvalue;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.UUID;
@@ -10,6 +12,9 @@
import redis.clients.jedis.Jedis;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
public class RedisIntegrationTest extends IntegrationTestBase {
private Jedis jedis;
@@ -36,6 +41,29 @@ public void testSetAndGet() throws Exception {
}
}
@Test
public void testSetAndDelete() throws Exception {
List<byte[]> allKeys = Lists.newArrayList();
for (int i = 1; i < 100; i++) {
byte[] key = Integer.toString(i).getBytes();
byte[] value = buildValue(i);
jedis.set(key, value);
allKeys.add(key);
}
Collections.shuffle(allKeys);
for (List<byte[]> partition : Iterables.partition(allKeys, 8)) {
byte[][] array = partition.toArray(new byte[partition.size()][]);
Long count = jedis.del(array);
Assert.assertEquals(partition.size(), count.longValue());
}
}
@Test
public void testIncrement() throws Exception {

0 comments on commit d969092

Please sign in to comment.