Support srandmember with negative count

DATAREDIS-116
This commit is contained in:
Jennifer Hickey
2013-07-01 11:33:02 -07:00
parent 62d2fe26cb
commit ba8ab92b0f
17 changed files with 101 additions and 48 deletions

View File

@@ -456,7 +456,7 @@ public class DefaultStringRedisConnection implements StringRedisConnection {
return delegate.sRandMember(key);
}
public Set<byte[]> sRandMember(byte[] key, long count) {
public List<byte[]> sRandMember(byte[] key, long count) {
return delegate.sRandMember(key, count);
}
@@ -1072,14 +1072,13 @@ public class DefaultStringRedisConnection implements StringRedisConnection {
return deserialize(delegate.sRandMember(serialize(key)));
}
public Set<String> sRandMember(String key, long count) {
public List<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));
}
public Long strLen(String key) {
return delegate.strLen(serialize(key));

View File

@@ -16,6 +16,7 @@
package org.springframework.data.redis.connection;
import java.util.List;
import java.util.Set;
/**
@@ -53,5 +54,5 @@ public interface RedisSetCommands {
byte[] sRandMember(byte[] key);
Set<byte[]> sRandMember(byte[] key, long count);
List<byte[]> sRandMember(byte[] key, long count);
}

View File

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

View File

@@ -1696,7 +1696,7 @@ public class JedisConnection implements RedisConnection {
}
}
public Set<byte[]> sRandMember(byte[] key, long count) {
public List<byte[]> sRandMember(byte[] key, long count) {
throw new UnsupportedOperationException();
}

View File

@@ -888,7 +888,7 @@ public class JredisConnection implements RedisConnection {
}
public Set<byte[]> sRandMember(byte[] key, long count) {
public List<byte[]> sRandMember(byte[] key, long count) {
throw new UnsupportedOperationException();
}

View File

@@ -1484,13 +1484,17 @@ public class LettuceConnection implements RedisConnection {
}
}
public Set<byte[]> sRandMember(byte[] key, long count) {
public List<byte[]> sRandMember(byte[] key, long count) {
if(count < 0) {
throw new UnsupportedOperationException("sRandMember with a negative count is not supported");
}
try {
if (isPipelined()) {
pipeline(getAsyncConnection().srandmember(key, count));
return null;
}
return getConnection().srandmember(key, count);
Set<byte[]> results = getConnection().srandmember(key, count);
return results != null ? new ArrayList<byte[]>(results) : null;
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}

View File

@@ -1397,13 +1397,13 @@ public class SrpConnection implements RedisConnection {
}
}
public Set<byte[]> sRandMember(byte[] key, long count) {
public List<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());
return SrpUtils.toBytesList(((MultiBulkReply)client.srandmember(key, count)).data());
} catch (Exception ex) {
throw convertSrpAccessException(ex);
}

View File

@@ -17,6 +17,7 @@
package org.springframework.data.redis.core;
import java.util.Collection;
import java.util.List;
import java.util.Set;
/**
@@ -62,7 +63,9 @@ public interface BoundSetOperations<K, V> extends BoundKeyOperations<K> {
V randomMember();
Set<V> randomMembers(long count);
Set<V> distinctRandomMembers(long count);
List<V> randomMembers(long count);
Boolean remove(Object o);

View File

@@ -17,6 +17,7 @@
package org.springframework.data.redis.core;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.springframework.data.redis.connection.DataType;
@@ -114,11 +115,16 @@ class DefaultBoundSetOperations<K, V> extends DefaultBoundKeyOperations<K> imple
}
public Set<V> randomMembers(long count) {
return ops.randomMembers(getKey(), count);
public Set<V> distinctRandomMembers(long count) {
return ops.distinctRandomMembers(getKey(), count);
}
public List<V> randomMembers(long count) {
return ops.randomMembers(getKey(), count);
}
public Boolean remove(Object o) {
return ops.remove(getKey(), o);
}

View File

@@ -17,6 +17,8 @@ package org.springframework.data.redis.core;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.springframework.data.redis.connection.RedisConnection;
@@ -163,18 +165,38 @@ class DefaultSetOperations<K, V> extends AbstractOperations<K, V> implements Set
}
public Set<V> randomMembers(K key, final long count) {
public Set<V> distinctRandomMembers(K key, final long count) {
if(count < 0) {
throw new IllegalArgumentException("Negative count not supported. " +
"Use randomMembers to allow duplicate elements.");
}
final byte[] rawKey = rawKey(key);
Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
public Set<byte[]> doInRedis(RedisConnection connection) {
return connection.sRandMember(rawKey, count);
return new HashSet<byte[]>(connection.sRandMember(rawKey, count));
}
}, true);
return deserializeValues(rawValues);
}
public List<V> randomMembers(K key, final long count) {
if(count < 0) {
throw new IllegalArgumentException("Use a positive number for count. " +
"This method is already allowing duplicate elements.");
}
final byte[] rawKey = rawKey(key);
List<byte[]> rawValues = execute(new RedisCallback<List<byte[]>>() {
public List<byte[]> doInRedis(RedisConnection connection) {
return connection.sRandMember(rawKey, - count);
}
}, true);
return deserializeValues(rawValues);
}
public Boolean remove(K key, Object o) {
final byte[] rawKey = rawKey(key);
final byte[] rawValue = rawValue(o);

View File

@@ -17,6 +17,7 @@
package org.springframework.data.redis.core;
import java.util.Collection;
import java.util.List;
import java.util.Set;
/**
@@ -60,7 +61,9 @@ public interface SetOperations<K, V> {
V randomMember(K key);
Set<V> randomMembers(K key, long count);
Set<V> distinctRandomMembers(K key, long count);
List<V> randomMembers(K key, long count);
Boolean remove(K key, Object o);