Permalink
Browse files

Support exists redis command

  • Loading branch information...
justinsb committed Dec 13, 2013
1 parent 4f46052 commit 783af5d1652cb9348774f1e3e34e2b2bce18dc19
@@ -12,6 +12,7 @@
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.ExistsCommand;
import com.cloudata.keyvalue.redis.commands.GetCommand;
import com.cloudata.keyvalue.redis.commands.IncrByCommand;
import com.cloudata.keyvalue.redis.commands.IncrCommand;
@@ -41,8 +42,10 @@
addMethod("ping", new PingCommand());
addMethod("quit", new QuitCommand());

addMethod("set", new SetCommand());
addMethod("get", new GetCommand());
addMethod("exists", new ExistsCommand());

addMethod("set", new SetCommand());

addMethod("incr", new IncrCommand());
addMethod("incrby", new IncrByCommand());
@@ -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.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 ExistsCommand implements RedisCommand {
private static final Logger log = LoggerFactory.getLogger(ExistsCommand.class);

@Override
public RedisResponse execute(RedisServer server, RedisRequest command) throws RedisException {
byte[] key = command.get(1);

ByteBuffer value = server.getKeyValueStore().get(ByteBuffer.wrap(key));

if (value == null) {
return IntegerRedisResponse.ZERO;
} else {
return IntegerRedisResponse.ONE;
}
}
}
@@ -20,11 +20,18 @@
cache[i] = new IntegerRedisResponse(i + cacheLow);
}
}

static IntegerRedisResponse get(long v) {
return Cache.cache[(int) (v - Cache.cacheLow)];
}
}

public static final RedisResponse ZERO = Cache.get(0);
public static final RedisResponse ONE = Cache.get(1);

public static IntegerRedisResponse valueOf(long v) {
if (v >= Cache.cacheLow && v <= Cache.cacheHigh) {
return Cache.cache[(int) (v - Cache.cacheLow)];
return Cache.get(v);
} else {
return new IntegerRedisResponse(v);
}
@@ -2,19 +2,27 @@

import java.net.InetSocketAddress;
import java.util.Random;
import java.util.UUID;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import redis.clients.jedis.Jedis;

public class RedisIntegrationTest extends IntegrationTestBase {

@Test
public void testSetAndGet() throws Exception {
private Jedis jedis;

@Before
public void buildJedisClient() {
InetSocketAddress redisSocketAddress = (InetSocketAddress) SERVERS[0].getRedisSocketAddress();

Jedis jedis = new Jedis(redisSocketAddress.getHostName(), redisSocketAddress.getPort());
this.jedis = new Jedis(redisSocketAddress.getHostName(), redisSocketAddress.getPort());
}

@Test
public void testSetAndGet() throws Exception {

for (int i = 1; i < 100; i++) {
byte[] key = Integer.toString(i).getBytes();
@@ -30,9 +38,6 @@ 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 = "INCR".getBytes();
for (int i = 1; i < 100; i++) {
@@ -44,9 +49,6 @@ public void testIncrement() throws Exception {

@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();
@@ -64,9 +66,6 @@ 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++) {
@@ -78,9 +77,6 @@ public void testDecrement() throws Exception {

@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();
@@ -96,4 +92,15 @@ public void testDecrementBy() throws Exception {
}
}

@Test
public void testExists() throws Exception {

byte[] key = UUID.randomUUID().toString().getBytes();
Assert.assertFalse(jedis.exists(key));

jedis.set(key, key);

Assert.assertTrue(jedis.exists(key));
}

}

0 comments on commit 783af5d

Please sign in to comment.