Permalink
Browse files

Add support for increment command

  • Loading branch information...
justinsb committed Dec 13, 2013
1 parent 807d988 commit 110fde4e2ff96b256e5282c3e133ae726e87ca0e
@@ -11,6 +11,7 @@
import com.cloudata.keyvalue.redis.commands.EchoCommand;
import com.cloudata.keyvalue.redis.commands.GetCommand;
import com.cloudata.keyvalue.redis.commands.IncrCommand;
import com.cloudata.keyvalue.redis.commands.PingCommand;
import com.cloudata.keyvalue.redis.commands.QuitCommand;
import com.cloudata.keyvalue.redis.commands.RedisCommand;
@@ -39,6 +40,8 @@
addMethod("set", new SetCommand());
addMethod("get", new GetCommand());
addMethod("incr", new IncrCommand());
}
static void addMethod(String name, RedisCommand action) {
@@ -0,0 +1,29 @@
package com.cloudata.keyvalue.redis.commands;
import java.nio.ByteBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cloudata.keyvalue.KeyValueProto.KvAction;
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.IntegerRedisResponse;
import com.cloudata.keyvalue.redis.response.RedisResponse;
public class IncrCommand implements RedisCommand {
private static final Logger log = LoggerFactory.getLogger(IncrCommand.class);
@Override
public RedisResponse execute(RedisServer server, RedisRequest command) throws RedisException {
byte[] key = command.get(1);
ByteBuffer value = (ByteBuffer) server.getKeyValueStore().doAction(KvAction.INCREMENT, ByteBuffer.wrap(key),
null);
long v = ByteBuffers.parseLong(value);
return new IntegerRedisResponse(v);
}
}
@@ -1,28 +1,30 @@
package com.cloudata.keyvalue.redis.response;
import com.cloudata.keyvalue.redis.Codec;
import io.netty.buffer.ByteBuf;
import com.cloudata.keyvalue.redis.Codec;
public class IntegerRedisResponse extends RedisResponse {
public static final char MARKER = ':';
private final long value;
private static final int cacheLow = -255;
private static final int cacheHigh = 255;
private static IntegerRedisResponse[] cache;
static class Cache {
private static final int cacheLow = -255;
private static final int cacheHigh = 255;
private static IntegerRedisResponse[] cache;
static {
cache = new IntegerRedisResponse[1 + cacheHigh - cacheLow];
for (int i = 0; i < cache.length; i++) {
cache[i + cacheLow] = new IntegerRedisResponse(i);
static {
cache = new IntegerRedisResponse[1 + cacheHigh - cacheLow];
for (int i = 0; i < cache.length; i++) {
cache[i] = new IntegerRedisResponse(i + cacheLow);
}
}
}
public static IntegerRedisResponse valueOf(long v) {
if (v >= cacheLow && v <= cacheHigh) {
return cache[(int) (v - cacheLow)];
if (v >= Cache.cacheLow && v <= Cache.cacheHigh) {
return Cache.cache[(int) (v - Cache.cacheLow)];
} else {
return new IntegerRedisResponse(v);
}
@@ -27,4 +27,18 @@ public void testSetAndGet() throws Exception {
}
}
@Test
public void testIncrement() throws Exception {
InetSocketAddress redisSocketAddress = (InetSocketAddress) SERVERS[0].getRedisSocketAddress();
Jedis jedis = new Jedis(redisSocketAddress.getHostName(), redisSocketAddress.getPort());
byte[] key = "A".getBytes();
for (int i = 1; i < 100; i++) {
Long value = jedis.incr(key);
Assert.assertEquals(i, value.intValue());
}
}
}

0 comments on commit 110fde4

Please sign in to comment.