Support sAdd with multiple values

DATAREDIS-99
This commit is contained in:
Jennifer Hickey
2013-08-01 12:23:47 -07:00
parent 6ad3bcf688
commit d4377b4d17
22 changed files with 106 additions and 59 deletions

View File

@@ -661,8 +661,8 @@ public class DefaultStringRedisConnection implements StringRedisConnection {
return result;
}
public Boolean sAdd(byte[] key, byte[] value) {
Boolean result = delegate.sAdd(key, value);
public Long sAdd(byte[] key, byte[]... values) {
Long result = delegate.sAdd(key, values);
if(isFutureConversion()) {
addResultConverter(identityConverter);
}
@@ -1676,8 +1676,8 @@ public class DefaultStringRedisConnection implements StringRedisConnection {
}
public Boolean sAdd(String key, String value) {
Boolean result = delegate.sAdd(serialize(key), serialize(value));
public Long sAdd(String key, String... values) {
Long result = delegate.sAdd(serialize(key), serializeMulti(values));
if(isFutureConversion()) {
addResultConverter(identityConverter);
}

View File

@@ -26,7 +26,7 @@ import java.util.Set;
*/
public interface RedisSetCommands {
Boolean sAdd(byte[] key, byte[] value);
Long sAdd(byte[] key, byte[]... values);
Boolean sRem(byte[] key, byte[] value);

View File

@@ -159,7 +159,7 @@ public interface StringRedisConnection extends RedisConnection {
String bRPopLPush(int timeout, String srcKey, String dstKey);
Boolean sAdd(String key, String value);
Long sAdd(String key, String... values);
Boolean sRem(String key, String value);

View File

@@ -1631,19 +1631,21 @@ public class JedisConnection implements RedisConnection {
//
public Boolean sAdd(byte[] key, byte[] value) {
public Long sAdd(byte[] key, byte[]... values) {
if((isPipelined() || isQueueing()) && values.length > 1) {
throw new UnsupportedOperationException("sAdd of multiple fields not supported " +
"in pipeline or transaction");
}
try {
if (isPipelined()) {
pipeline(new JedisResult(pipeline.sadd(key, value),
JedisConverters.longToBoolean()));
pipeline(new JedisResult(pipeline.sadd(key, values[0])));
return null;
}
if (isQueueing()) {
transaction(new JedisResult(transaction.sadd(key, value),
JedisConverters.longToBoolean()));
transaction(new JedisResult(transaction.sadd(key, values[0])));
return null;
}
return JedisConverters.toBoolean(jedis.sadd(key, value));
return jedis.sadd(key, values);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}

View File

@@ -785,9 +785,12 @@ public class JredisConnection implements RedisConnection {
//
public Boolean sAdd(byte[] key, byte[] value) {
public Long sAdd(byte[] key, byte[]... values) {
if(values.length > 1) {
throw new UnsupportedOperationException("sAdd of multiple fields not supported");
}
try {
return jredis.sadd(key, value);
return JredisUtils.toLong(jredis.sadd(key, values[0]));
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}

View File

@@ -44,7 +44,6 @@ import org.springframework.data.redis.connection.ReturnType;
import org.springframework.data.redis.connection.SortParameters;
import org.springframework.data.redis.connection.Subscription;
import org.springframework.data.redis.connection.convert.TransactionResultConverter;
import org.springframework.data.redis.connection.srp.SrpConverters;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
@@ -1707,17 +1706,17 @@ public class LettuceConnection implements RedisConnection {
//
public Boolean sAdd(byte[] key, byte[] value) {
public Long sAdd(byte[] key, byte[]... values) {
try {
if (isPipelined()) {
pipeline(new LettuceResult(getAsyncConnection().sadd(key, value), SrpConverters.longToBoolean()));
pipeline(new LettuceResult(getAsyncConnection().sadd(key, values)));
return null;
}
if (isQueueing()) {
transaction(new LettuceTxResult(getConnection().sadd(key, value), SrpConverters.longToBoolean()));
transaction(new LettuceTxResult(getConnection().sadd(key, values)));
return null;
}
return SrpConverters.toBoolean(getConnection().sadd(key, value));
return getConnection().sadd(key, values);
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}

View File

@@ -1335,13 +1335,13 @@ public class SrpConnection implements RedisConnection {
//
public Boolean sAdd(byte[] key, byte[] value) {
public Long sAdd(byte[] key, byte[]... values) {
try {
if (isPipelined()) {
pipeline(new SrpResult(pipeline.sadd(key, new Object[] { value }), SrpConverters.longToBoolean()));
pipeline(new SrpResult(pipeline.sadd(key, (Object[]) values)));
return null;
}
return SrpConverters.toBoolean(client.sadd(key, new Object[] { value }).data());
return client.sadd(key, (Object[]) values).data();
} catch (Exception ex) {
throw convertSrpAccessException(ex);
}

View File

@@ -53,7 +53,7 @@ public interface BoundSetOperations<K, V> extends BoundKeyOperations<K> {
void unionAndStore(Collection<K> keys, K destKey);
Boolean add(V value);
Long add(V... values);
Boolean isMember(Object o);

View File

@@ -44,8 +44,8 @@ class DefaultBoundSetOperations<K, V> extends DefaultBoundKeyOperations<K> imple
}
public Boolean add(V value) {
return ops.add(getKey(), value);
public Long add(V... values) {
return ops.add(getKey(), values);
}

View File

@@ -35,13 +35,13 @@ class DefaultSetOperations<K, V> extends AbstractOperations<K, V> implements Set
}
public Boolean add(K key, V value) {
public Long add(K key, V... values) {
final byte[] rawKey = rawKey(key);
final byte[] rawValue = rawValue(value);
return execute(new RedisCallback<Boolean>() {
final byte[][] rawValues = rawValues(values);
return execute(new RedisCallback<Long>() {
public Boolean doInRedis(RedisConnection connection) {
return connection.sAdd(rawKey, rawValue);
public Long doInRedis(RedisConnection connection) {
return connection.sAdd(rawKey, rawValues);
}
}, true);
}

View File

@@ -51,7 +51,7 @@ public interface SetOperations<K, V> {
Long unionAndStore(K key, Collection<K> otherKeys, K destKey);
Boolean add(K key, V value);
Long add(K key, V... values);
Boolean isMember(K key, Object o);

View File

@@ -139,10 +139,11 @@ public class DefaultRedisSet<E> extends AbstractRedisCollection<E> implements Re
}
@SuppressWarnings("unchecked")
public boolean add(E e) {
Boolean result = boundSetOps.add(e);
Long result = boundSetOps.add(e);
checkResult(result);
return result;
return result == 1;
}