add variadic arguments support to lpush and rpush

This commit is contained in:
Shaofeng Niu
2012-02-14 17:42:06 +08:00
parent 72ca494362
commit f010bc0f32
10 changed files with 50 additions and 25 deletions

View File

@@ -248,12 +248,18 @@ public class BinaryClient extends Connection {
sendCommand(HGETALL, key);
}
public void rpush(final byte[] key, final byte[] string) {
sendCommand(RPUSH, key, string);
public void rpush(final byte[] key, final byte[]... vals) {
byte[][] args = new byte[vals.length+1][];
args[0] = key;
System.arraycopy(vals, 0, args, 1, vals.length);
sendCommand(RPUSH, args);
}
public void lpush(final byte[] key, final byte[] string) {
sendCommand(LPUSH, key, string);
public void lpush(final byte[] key, final byte[]... vals) {
byte [][] args = new byte[vals.length+1][];
args[0] = key;
System.arraycopy(vals, 0, args, 1, vals.length);
sendCommand(LPUSH, args);
}
public void llen(final byte[] key) {

View File

@@ -888,7 +888,7 @@ public class BinaryJedis implements BinaryJedisCommands {
* @return Integer reply, specifically, the number of elements inside the
* list after the push operation.
*/
public Long rpush(final byte[] key, final byte[] string) {
public Long rpush(final byte[] key, final byte[]... string) {
checkIsInMulti();
client.rpush(key, string);
return client.getIntegerReply();
@@ -909,7 +909,7 @@ public class BinaryJedis implements BinaryJedisCommands {
* @return Integer reply, specifically, the number of elements inside the
* list after the push operation.
*/
public Long lpush(final byte[] key, final byte[] string) {
public Long lpush(final byte[] key, final byte[]... string) {
checkIsInMulti();
client.lpush(key, string);
return client.getIntegerReply();

View File

@@ -67,9 +67,9 @@ public interface BinaryJedisCommands {
Map<byte[], byte[]> hgetAll(byte[] key);
Long rpush(byte[] key, byte[] string);
Long rpush(byte[] key, byte[]... string);
Long lpush(byte[] key, byte[] string);
Long lpush(byte[] key, byte[]... string);
Long llen(byte[] key);

View File

@@ -180,12 +180,12 @@ public class BinaryShardedJedis extends Sharded<Jedis, JedisShardInfo>
return j.hgetAll(key);
}
public Long rpush(byte[] key, byte[] string) {
public Long rpush(byte[] key, byte[]... string) {
Jedis j = getShard(key);
return j.rpush(key, string);
}
public Long lpush(byte[] key, byte[] string) {
public Long lpush(byte[] key, byte[]... string) {
Jedis j = getShard(key);
return j.lpush(key, string);
}
@@ -407,4 +407,4 @@ public class BinaryShardedJedis extends Sharded<Jedis, JedisShardInfo>
pipeline.setShardedJedis(this);
return pipeline;
}
}
}

View File

@@ -189,12 +189,22 @@ public class Client extends BinaryClient implements Commands {
hgetAll(SafeEncoder.encode(key));
}
public void rpush(final String key, final String string) {
rpush(SafeEncoder.encode(key), SafeEncoder.encode(string));
public void rpush(final String key, final String... vals) {
final byte[][] bvals = new byte[vals.length][];
for (int i = 0; i < bvals.length; i++) {
bvals[i] = SafeEncoder.encode(vals[i]);
}
rpush(SafeEncoder.encode(key), bvals);
}
public void lpush(final String key, final String string) {
lpush(SafeEncoder.encode(key), SafeEncoder.encode(string));
public void lpush(final String key, final String... vals) {
final byte[][] bvals = new byte[vals.length][];
for (int i = 0; i < bvals.length; i++) {
bvals[i] = SafeEncoder.encode(vals[i]);
}
lpush(SafeEncoder.encode(key), bvals);
}
public void llen(final String key) {

View File

@@ -86,9 +86,9 @@ public interface Commands {
public void hgetAll(final String key);
public void rpush(final String key, final String string);
public void rpush(final String key, final String... string);
public void lpush(final String key, final String string);
public void lpush(final String key, final String... string);
public void llen(final String key);
@@ -259,4 +259,4 @@ public interface Commands {
public void exec();
public void discard();
}
}

View File

@@ -865,7 +865,7 @@ public class Jedis extends BinaryJedis implements JedisCommands {
* @return Integer reply, specifically, the number of elements inside the
* list after the push operation.
*/
public Long rpush(final String key, final String string) {
public Long rpush(final String key, final String... string) {
checkIsInMulti();
client.rpush(key, string);
return client.getIntegerReply();
@@ -886,7 +886,7 @@ public class Jedis extends BinaryJedis implements JedisCommands {
* @return Integer reply, specifically, the number of elements inside the
* list after the push operation.
*/
public Long lpush(final String key, final String string) {
public Long lpush(final String key, final String... string) {
checkIsInMulti();
client.lpush(key, string);
return client.getIntegerReply();

View File

@@ -72,9 +72,9 @@ public interface JedisCommands {
Map<String, String> hgetAll(String key);
Long rpush(String key, String string);
Long rpush(String key, String... string);
Long lpush(String key, String string);
Long lpush(String key, String... string);
Long llen(String key);

View File

@@ -198,12 +198,12 @@ public class ShardedJedis extends BinaryShardedJedis implements JedisCommands {
return j.hgetAll(key);
}
public Long rpush(String key, String string) {
public Long rpush(String key, String... string) {
Jedis j = getShard(key);
return j.rpush(key, string);
}
public Long lpush(String key, String string) {
public Long lpush(String key, String... string) {
Jedis j = getShard(key);
return j.lpush(key, string);
}

View File

@@ -30,12 +30,17 @@ public class ListCommandsTest extends JedisCommandTestBase {
assertEquals(1, size);
size = jedis.rpush("foo", "foo");
assertEquals(2, size);
size = jedis.rpush("foo", "bar", "foo");
assertEquals(4, size);
// Binary
long bsize = jedis.rpush(bfoo, bbar);
assertEquals(1, bsize);
bsize = jedis.rpush(bfoo, bfoo);
assertEquals(2, bsize);
bsize = jedis.rpush(bfoo, bbar, bfoo);
assertEquals(4, bsize);
}
@@ -45,12 +50,16 @@ public class ListCommandsTest extends JedisCommandTestBase {
assertEquals(1, size);
size = jedis.lpush("foo", "foo");
assertEquals(2, size);
size = jedis.lpush("foo", "bar", "foo");
assertEquals(4, size);
// Binary
long bsize = jedis.lpush(bfoo, bbar);
assertEquals(1, bsize);
bsize = jedis.lpush(bfoo, bfoo);
assertEquals(2, bsize);
bsize = jedis.lpush(bfoo, bbar, bfoo);
assertEquals(4, bsize);
}
@@ -620,4 +629,4 @@ public class ListCommandsTest extends JedisCommandTestBase {
assertEquals("a", jedis.lrange("bar", 0, -1).get(0));
}
}
}