Support lPush with multiple values

DATAREDIS-99
This commit is contained in:
Jennifer Hickey
2013-07-31 18:09:56 -07:00
parent 8f362b481c
commit 36780d14a1
17 changed files with 87 additions and 20 deletions

View File

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

View File

@@ -34,7 +34,7 @@ public interface RedisListCommands {
Long rPush(byte[] key, byte[] value);
Long lPush(byte[] key, byte[] value);
Long lPush(byte[] key, byte[]... value);
Long rPushX(byte[] key, byte[] value);

View File

@@ -127,7 +127,7 @@ public interface StringRedisConnection extends RedisConnection {
Long rPush(String key, String value);
Long lPush(String key, String value);
Long lPush(String key, String... values);
Long rPushX(String key, String value);

View File

@@ -1322,17 +1322,21 @@ public class JedisConnection implements RedisConnection {
// List commands
//
public Long lPush(byte[] key, byte[] value) {
public Long lPush(byte[] key, byte[]... values) {
if((isPipelined() || isQueueing()) && values.length > 1) {
throw new UnsupportedOperationException("lPush of multiple fields not supported " +
"in pipeline or transaction");
}
try {
if (isPipelined()) {
pipeline(new JedisResult(pipeline.lpush(key, value)));
pipeline(new JedisResult(pipeline.lpush(key, values[0])));
return null;
}
if (isQueueing()) {
transaction(new JedisResult(transaction.lpush(key, value)));
transaction(new JedisResult(transaction.lpush(key, values[0])));
return null;
}
return jedis.lpush(key, value);
return jedis.lpush(key, values);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}

View File

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

View File

@@ -1421,17 +1421,17 @@ public class LettuceConnection implements RedisConnection {
//
public Long lPush(byte[] key, byte[] value) {
public Long lPush(byte[] key, byte[]... values) {
try {
if (isPipelined()) {
pipeline(new LettuceResult(getAsyncConnection().lpush(key, value)));
pipeline(new LettuceResult(getAsyncConnection().lpush(key, values)));
return null;
}
if (isQueueing()) {
transaction(new LettuceTxResult(getConnection().lpush(key, value)));
transaction(new LettuceTxResult(getConnection().lpush(key, values)));
return null;
}
return getConnection().lpush(key, value);
return getConnection().lpush(key, values);
} catch (Exception ex) {
throw convertLettuceAccessException(ex);
}

View File

@@ -1106,13 +1106,13 @@ public class SrpConnection implements RedisConnection {
// List commands
//
public Long lPush(byte[] key, byte[] value) {
public Long lPush(byte[] key, byte[]... values) {
try {
if (isPipelined()) {
pipeline(new SrpResult(pipeline.lpush(key, new Object[] { value })));
pipeline(new SrpResult(pipeline.lpush(key, (Object[]) values)));
return null;
}
return client.lpush(key, new Object[] { value }).data();
return client.lpush(key, (Object[]) values ).data();
} catch (Exception ex) {
throw convertSrpAccessException(ex);
}

View File

@@ -111,6 +111,15 @@ abstract class AbstractOperations<K, V> {
return valueSerializer().serialize(value);
}
byte[][] rawValues(Object... values) {
final byte[][] rawValues = new byte[values.length][];
int i = 0;
for (Object value : values) {
rawValues[i++] = rawValue(value);
}
return rawValues;
}
@SuppressWarnings("unchecked")
<HK> byte[] rawHashKey(HK hashKey) {
Assert.notNull(hashKey, "non null hash key required");

View File

@@ -35,6 +35,8 @@ public interface BoundListOperations<K, V> extends BoundKeyOperations<K> {
Long leftPush(V value);
Long leftPushAll(V... values);
Long leftPushIfPresent(V value);
Long leftPush(V pivot, V value);

View File

@@ -67,6 +67,9 @@ class DefaultBoundListOperations<K, V> extends DefaultBoundKeyOperations<K> impl
return ops.leftPush(getKey(), value);
}
public Long leftPushAll(V... values) {
return ops.leftPushAll(getKey(), values);
}
public Long leftPushIfPresent(V value) {
return ops.leftPushIfPresent(getKey(), value);

View File

@@ -76,7 +76,16 @@ class DefaultListOperations<K, V> extends AbstractOperations<K, V> implements Li
}, true);
}
public Long leftPushAll(K key, V... values) {
final byte[] rawKey = rawKey(key);
final byte[][] rawValues = rawValues(values);
return execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection) {
return connection.lPush(rawKey, rawValues);
}
}, true);
}
public Long leftPushIfPresent(K key, V value) {
final byte[] rawKey = rawKey(key);
final byte[] rawValue = rawValue(value);

View File

@@ -33,6 +33,8 @@ public interface ListOperations<K, V> {
Long leftPush(K key, V value);
Long leftPushAll(K key, V... values);
Long leftPushIfPresent(K key, V value);
Long leftPush(K key, V pivot, V value);