Add Redis 2.6 srandmember with count to Connections

DATAREDIS-116
This commit is contained in:
Jennifer Hickey
2013-06-24 16:15:07 -07:00
parent cc9b0821a0
commit c1337d5067
12 changed files with 153 additions and 0 deletions

View File

@@ -452,6 +452,10 @@ public class DefaultStringRedisConnection implements StringRedisConnection {
return delegate.sRandMember(key);
}
public Set<byte[]> sRandMember(byte[] key, long count) {
return delegate.sRandMember(key, count);
}
public Boolean sRem(byte[] key, byte[] value) {
return delegate.sRem(key, value);
}
@@ -1064,6 +1068,9 @@ public class DefaultStringRedisConnection implements StringRedisConnection {
return deserialize(delegate.sRandMember(serialize(key)));
}
public Set<String> sRandMember(String key, long count) {
return deserialize(delegate.sRandMember(serialize(key), count));
}
public Boolean sRem(String key, String value) {
return delegate.sRem(serialize(key), serialize(value));

View File

@@ -52,4 +52,6 @@ public interface RedisSetCommands {
Set<byte[]> sMembers(byte[] key);
byte[] sRandMember(byte[] key);
Set<byte[]> sRandMember(byte[] key, long count);
}

View File

@@ -187,6 +187,8 @@ public interface StringRedisConnection extends RedisConnection {
String sRandMember(String key);
Set<String> sRandMember(String key, long count);
Boolean zAdd(String key, double score, String value);
Boolean zRem(String key, String value);

View File

@@ -1691,6 +1691,9 @@ public class JedisConnection implements RedisConnection {
}
}
public Set<byte[]> sRandMember(byte[] key, long count) {
throw new UnsupportedOperationException();
}
public Boolean sRem(byte[] key, byte[] value) {
try {

View File

@@ -883,6 +883,11 @@ public class JredisConnection implements RedisConnection {
}
public Set<byte[]> sRandMember(byte[] key, long count) {
throw new UnsupportedOperationException();
}
public Boolean sRem(byte[] key, byte[] value) {
try {
return jredis.srem(key, value);

View File

@@ -1432,6 +1432,18 @@ public class LettuceConnection implements RedisConnection {
}
}
public Set<byte[]> sRandMember(byte[] key, long count) {
try {
if (isPipelined()) {
pipeline(getAsyncConnection().srandmember(key, count));
return null;
}
return getConnection().srandmember(key, count);
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}
}
public Boolean sRem(byte[] key, byte[] value) {
try {
if (isPipelined()) {

View File

@@ -42,6 +42,7 @@ import redis.Command;
import redis.client.RedisClient;
import redis.client.RedisClient.Pipeline;
import redis.client.RedisException;
import redis.reply.MultiBulkReply;
import redis.reply.Reply;
import com.google.common.base.Charsets;
@@ -1383,6 +1384,17 @@ public class SrpConnection implements RedisConnection {
}
}
public Set<byte[]> sRandMember(byte[] key, long count) {
try {
if (isPipelined()) {
pipeline(pipeline.srandmember(key, count));
return null;
}
return SrpUtils.toSet(((MultiBulkReply)client.srandmember(key, count)).data());
} catch (Exception ex) {
throw convertSrpAccessException(ex);
}
}
public Boolean sRem(byte[] key, byte[] value) {
try {

View File

@@ -1166,6 +1166,41 @@ public abstract class AbstractConnectionIntegrationTests {
.contains(connection.sRandMember("myset")));
}
@Test
public void testSRandMemberKeyNotExists() {
actual.add(connection.sRandMember("notexist"));
assertNull(convertResults().get(0));
}
@SuppressWarnings("rawtypes")
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testSRandMemberCount() {
actual.add(connection.sAdd("myset", "foo"));
actual.add(connection.sAdd("myset", "bar"));
actual.add(connection.sAdd("myset", "baz"));
actual.add(connection.sRandMember("myset", 2));
assertTrue(((Set)convertResults().get(3)).size() == 2);
}
@SuppressWarnings("rawtypes")
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testSRandMemberCountNegative() {
actual.add(connection.sAdd("myset", "foo"));
actual.add(connection.sRandMember("myset", -2));
// APIs filter out duplicates so the negative has no effect
assertTrue(((Set)convertResults().get(1)).size() == 1);
}
@SuppressWarnings("rawtypes")
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testSRandMemberCountKeyNotExists() {
actual.add(connection.sRandMember("notexist", 2));
assertTrue(((Set)convertResults().get(0)).isEmpty());
}
@Test
public void testSRem() {
connection.sAdd("myset", "foo");

View File

@@ -158,6 +158,24 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati
super.testIncrByDouble();
}
@Test(expected=UnsupportedOperationException.class)
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testSRandMemberCount() {
super.testSRandMemberCount();
}
@Test(expected=UnsupportedOperationException.class)
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testSRandMemberCountKeyNotExists() {
super.testSRandMemberCountKeyNotExists();
}
@Test(expected=UnsupportedOperationException.class)
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testSRandMemberCountNegative() {
super.testSRandMemberCountNegative();
}
@Test
public void testIncrDecrByLong() {
String key = "test.count";

View File

@@ -345,6 +345,24 @@ public class JedisConnectionPipelineIntegrationTests extends
super.testScriptFlush();
}
@Test(expected=UnsupportedOperationException.class)
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testSRandMemberCount() {
super.testSRandMemberCount();
}
@Test(expected=UnsupportedOperationException.class)
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testSRandMemberCountKeyNotExists() {
super.testSRandMemberCountKeyNotExists();
}
@Test(expected=UnsupportedOperationException.class)
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testSRandMemberCountNegative() {
super.testSRandMemberCountNegative();
}
// Overrides, usually due to return values being Long vs Boolean or Set vs
// List

View File

@@ -495,6 +495,24 @@ public class JRedisConnectionIntegrationTests extends AbstractConnectionIntegrat
super.testScriptFlush();
}
@Test(expected=UnsupportedOperationException.class)
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testSRandMemberCount() {
super.testSRandMemberCount();
}
@Test(expected=UnsupportedOperationException.class)
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testSRandMemberCountKeyNotExists() {
super.testSRandMemberCountKeyNotExists();
}
@Test(expected=UnsupportedOperationException.class)
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testSRandMemberCountNegative() {
super.testSRandMemberCountNegative();
}
// Jredis returns null for rPush
@Test
public void testSort() {

View File

@@ -146,6 +146,27 @@ public class SrpConnectionPipelineIntegrationTests extends
verifyResults(Arrays.asList(new Object[] { 1l, 1l, 1l, 0l }), actual);
}
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testSRandMemberCount() {
convertResultToSet = true;
super.testSRandMemberCount();
}
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testSRandMemberCountKeyNotExists() {
convertResultToSet = true;
super.testSRandMemberCountKeyNotExists();
}
@Test
@IfProfileValue(name = "redisVersion", value = "2.6")
public void testSRandMemberCountNegative() {
convertResultToSet = true;
super.testSRandMemberCountNegative();
}
@Test
public void testZIncrBy() {
actual.add(connection.zAdd("myset", 2, "Bob"));