Permalink
Browse files

Implement incrby command

  • Loading branch information...
justinsb committed Dec 13, 2013
1 parent e971df5 commit 8abc1b96593e374116b568384bdc5c063d17e491
@@ -11,6 +11,7 @@
import com.cloudata.keyvalue.redis.commands.EchoCommand;
import com.cloudata.keyvalue.redis.commands.GetCommand;
import com.cloudata.keyvalue.redis.commands.IncrByCommand;
import com.cloudata.keyvalue.redis.commands.IncrCommand;
import com.cloudata.keyvalue.redis.commands.PingCommand;
import com.cloudata.keyvalue.redis.commands.QuitCommand;
@@ -42,6 +43,7 @@
addMethod("get", new GetCommand());
addMethod("incr", new IncrCommand());
addMethod("incrby", new IncrByCommand());
}
static void addMethod(String name, RedisCommand action) {
@@ -0,0 +1,37 @@
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.btree.operation.IncrementOperation;
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 IncrByCommand implements RedisCommand {
private static final Logger log = LoggerFactory.getLogger(IncrByCommand.class);
@Override
public RedisResponse execute(RedisServer server, RedisRequest command) throws RedisException {
byte[] delta = command.get(2);
long deltaLong = ByteBuffers.parseLong(ByteBuffer.wrap(delta));
return execute(server, command, deltaLong);
}
protected IntegerRedisResponse execute(RedisServer server, RedisRequest command, long deltaLong) {
byte[] key = command.get(1);
IncrementOperation operation = new IncrementOperation(deltaLong);
ByteBuffer value = (ByteBuffer) server.getKeyValueStore().doAction(ByteBuffer.wrap(key), operation);
long v = ByteBuffers.parseLong(value);
return new IntegerRedisResponse(v);
}
}
@@ -1,30 +1,18 @@
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.btree.operation.IncrementOperation;
import com.cloudata.keyvalue.btree.operation.KeyOperation;
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 IncrCommand implements RedisCommand {
public class IncrCommand extends IncrByCommand {
private static final Logger log = LoggerFactory.getLogger(IncrCommand.class);
@Override
public RedisResponse execute(RedisServer server, RedisRequest command) throws RedisException {
byte[] key = command.get(1);
KeyOperation operation = new IncrementOperation(1);
ByteBuffer value = (ByteBuffer) server.getKeyValueStore().doAction(ByteBuffer.wrap(key), operation);
long v = ByteBuffers.parseLong(value);
return new IntegerRedisResponse(v);
return execute(server, command, 1);
}
}
@@ -1,6 +1,7 @@
package com.cloudata.keyvalue;
import java.net.InetSocketAddress;
import java.util.Random;
import org.junit.Assert;
import org.junit.Test;
@@ -33,11 +34,31 @@ public void testIncrement() throws Exception {
Jedis jedis = new Jedis(redisSocketAddress.getHostName(), redisSocketAddress.getPort());
byte[] key = "A".getBytes();
byte[] key = "INCR".getBytes();
for (int i = 1; i < 100; i++) {
Long value = jedis.incr(key);
Assert.assertEquals(i, value.intValue());
Assert.assertEquals(i, value.longValue());
}
}
@Test
public void testIncrementBy() 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 = "INCRBY".getBytes();
for (int i = 1; i < 100; i++) {
long delta = r.nextInt();
Long value = jedis.incrBy(key, delta);
counter += delta;
Assert.assertEquals(counter, value.longValue());
}
}

0 comments on commit 8abc1b9

Please sign in to comment.