Permalink
Browse files

Implement decr & decrby

  • Loading branch information...
justinsb committed Dec 13, 2013
1 parent 14974d5 commit 16c5cb840f291e32172a8896fe722ae735e726cf
@@ -9,6 +9,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cloudata.keyvalue.redis.commands.DecrByCommand;
import com.cloudata.keyvalue.redis.commands.DecrCommand;
import com.cloudata.keyvalue.redis.commands.EchoCommand;
import com.cloudata.keyvalue.redis.commands.GetCommand;
import com.cloudata.keyvalue.redis.commands.IncrByCommand;
@@ -44,6 +46,8 @@
addMethod("incr", new IncrCommand());
addMethod("incrby", new IncrByCommand());
addMethod("decr", new DecrCommand());
addMethod("decrby", new DecrByCommand());
}
static void addMethod(String name, RedisCommand action) {
@@ -0,0 +1,26 @@
package com.cloudata.keyvalue.redis.commands;
import java.nio.ByteBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cloudata.keyvalue.btree.ByteBuffers;
import com.cloudata.keyvalue.redis.RedisException;
import com.cloudata.keyvalue.redis.RedisRequest;
import com.cloudata.keyvalue.redis.RedisServer;
import com.cloudata.keyvalue.redis.response.RedisResponse;
public class DecrByCommand extends IncrByCommand {
private static final Logger log = LoggerFactory.getLogger(DecrByCommand.class);
@Override
public RedisResponse execute(RedisServer server, RedisRequest command) throws RedisException {
byte[] delta = command.get(2);
long deltaLong = ByteBuffers.parseLong(ByteBuffer.wrap(delta));
deltaLong = -deltaLong;
return execute(server, command, deltaLong);
}
}
@@ -0,0 +1,18 @@
package com.cloudata.keyvalue.redis.commands;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cloudata.keyvalue.redis.RedisException;
import com.cloudata.keyvalue.redis.RedisRequest;
import com.cloudata.keyvalue.redis.RedisServer;
import com.cloudata.keyvalue.redis.response.RedisResponse;
public class DecrCommand extends IncrByCommand {
private static final Logger log = LoggerFactory.getLogger(DecrCommand.class);
@Override
public RedisResponse execute(RedisServer server, RedisRequest command) throws RedisException {
return execute(server, command, -1);
}
}
@@ -62,4 +62,38 @@ public void testIncrementBy() throws Exception {
}
}
@Test
public void testDecrement() throws Exception {
InetSocketAddress redisSocketAddress = (InetSocketAddress) SERVERS[0].getRedisSocketAddress();
Jedis jedis = new Jedis(redisSocketAddress.getHostName(), redisSocketAddress.getPort());
byte[] key = "DECR".getBytes();
for (int i = 1; i < 100; i++) {
Long value = jedis.decr(key);
Assert.assertEquals(-i, value.longValue());
}
}
@Test
public void testDecrementBy() throws Exception {
InetSocketAddress redisSocketAddress = (InetSocketAddress) SERVERS[0].getRedisSocketAddress();
Jedis jedis = new Jedis(redisSocketAddress.getHostName(), redisSocketAddress.getPort());
long counter = 0;
Random r = new Random();
byte[] key = "DECRBY".getBytes();
for (int i = 1; i < 100; i++) {
long delta = r.nextInt();
Long value = jedis.decrBy(key, delta);
counter -= delta;
Assert.assertEquals(counter, value.longValue());
}
}
}

0 comments on commit 16c5cb8

Please sign in to comment.