Skip to content

Commit

Permalink
Variadic commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ivowiblo committed Apr 18, 2012
1 parent e4f7f61 commit 8aca0b7
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 102 deletions.
50 changes: 38 additions & 12 deletions src/main/java/redis/clients/jedis/BinaryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static redis.clients.jedis.Protocol.Keyword.WITHSCORES;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -44,6 +45,15 @@ public BinaryClient(final String host) {
public BinaryClient(final String host, final int port) {
super(host, port);
}

private byte[][] joinParameters(byte[] first, byte[][] rest){
byte[][] result = new byte[rest.length+1][];
result[0] = first;
for(int i=0;i<rest.length;i++){
result[i+1] = rest[i];
}
return result;
}

public void setPassword(final String password) {
this.password = password;
Expand Down Expand Up @@ -228,8 +238,8 @@ public void hexists(final byte[] key, final byte[] field) {
sendCommand(HEXISTS, key, field);
}

public void hdel(final byte[] key, final byte[] field) {
sendCommand(HDEL, key, field);
public void hdel(final byte[] key, final byte[]... fields) {
sendCommand(HDEL, joinParameters(key, fields));
}

public void hlen(final byte[] key) {
Expand All @@ -248,12 +258,12 @@ public void hgetAll(final byte[] key) {
sendCommand(HGETALL, key);
}

public void rpush(final byte[] key, final byte[] string) {
sendCommand(RPUSH, key, string);
public void rpush(final byte[] key, final byte[]... strings) {
sendCommand(RPUSH, joinParameters(key, strings));
}

public void lpush(final byte[] key, final byte[] string) {
sendCommand(LPUSH, key, string);
public void lpush(final byte[] key, final byte[]... strings) {
sendCommand(LPUSH, joinParameters(key, strings));
}

public void llen(final byte[] key) {
Expand Down Expand Up @@ -292,16 +302,16 @@ public void rpoplpush(final byte[] srckey, final byte[] dstkey) {
sendCommand(RPOPLPUSH, srckey, dstkey);
}

public void sadd(final byte[] key, final byte[] member) {
sendCommand(SADD, key, member);
public void sadd(final byte[] key, final byte[]... members) {
sendCommand(SADD, joinParameters(key, members));
}

public void smembers(final byte[] key) {
sendCommand(SMEMBERS, key);
}

public void srem(final byte[] key, final byte[] member) {
sendCommand(SREM, key, member);
public void srem(final byte[] key, final byte[]... members) {
sendCommand(SREM, joinParameters(key, members));
}

public void spop(final byte[] key) {
Expand Down Expand Up @@ -362,12 +372,28 @@ public void zadd(final byte[] key, final double score, final byte[] member) {
sendCommand(ZADD, key, toByteArray(score), member);
}

public void zaddBinary(final byte[] key, Map<Double, byte[]> scoreMembers) {
ArrayList<byte[]> args = new ArrayList<byte[]>(scoreMembers.size() * 2 + 1);

args.add(key);

for(Map.Entry<Double,byte[]> entry : scoreMembers.entrySet()){
args.add(toByteArray(entry.getKey()));
args.add(entry.getValue());
}

byte[][] argsArray = new byte[args.size()][];
args.toArray(argsArray);

sendCommand(ZADD, argsArray);
}

public void zrange(final byte[] key, final int start, final int end) {
sendCommand(ZRANGE, key, toByteArray(start), toByteArray(end));
}

public void zrem(final byte[] key, final byte[] member) {
sendCommand(ZREM, key, member);
public void zrem(final byte[] key, final byte[]... members) {
sendCommand(ZREM, joinParameters( key, members));
}

public void zincrby(final byte[] key, final double score,
Expand Down
42 changes: 24 additions & 18 deletions src/main/java/redis/clients/jedis/BinaryJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -796,13 +796,13 @@ public Boolean hexists(final byte[] key, final byte[] field) {
* <b>Time complexity:</b> O(1)
*
* @param key
* @param field
* @param fields
* @return If the field was present in the hash it is deleted and 1 is
* returned, otherwise 0 is returned and no operation is performed.
*/
public Long hdel(final byte[] key, final byte[] field) {
public Long hdel(final byte[] key, final byte[]... fields) {
checkIsInMulti();
client.hdel(key, field);
client.hdel(key, fields);
return client.getIntegerReply();
}

Expand Down Expand Up @@ -881,16 +881,16 @@ public Map<byte[], byte[]> hgetAll(final byte[] key) {
* <p>
* Time complexity: O(1)
*
* @see BinaryJedis#lpush(byte[], byte[])
* @see BinaryJedis#rpush(byte[], byte[]...)
*
* @param key
* @param string
* @param strings
* @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[]... strings) {
checkIsInMulti();
client.rpush(key, string);
client.rpush(key, strings);
return client.getIntegerReply();
}

Expand All @@ -902,16 +902,16 @@ public Long rpush(final byte[] key, final byte[] string) {
* <p>
* Time complexity: O(1)
*
* @see BinaryJedis#rpush(byte[], byte[])
* @see BinaryJedis#rpush(byte[], byte[]...)
*
* @param key
* @param string
* @param strings
* @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[]... strings) {
checkIsInMulti();
client.lpush(key, string);
client.lpush(key, strings);
return client.getIntegerReply();
}

Expand Down Expand Up @@ -1162,13 +1162,13 @@ public byte[] rpoplpush(final byte[] srckey, final byte[] dstkey) {
* Time complexity O(1)
*
* @param key
* @param member
* @param members
* @return Integer reply, specifically: 1 if the new element was added 0 if
* the element was already a member of the set
*/
public Long sadd(final byte[] key, final byte[] member) {
public Long sadd(final byte[] key, final byte[]... members) {
checkIsInMulti();
client.sadd(key, member);
client.sadd(key, members);
return client.getIntegerReply();
}

Expand Down Expand Up @@ -1200,7 +1200,7 @@ public Set<byte[]> smembers(final byte[] key) {
* @return Integer reply, specifically: 1 if the new element was removed 0
* if the new element was not a member of the set
*/
public Long srem(final byte[] key, final byte[] member) {
public Long srem(final byte[] key, final byte[]... member) {
checkIsInMulti();
client.srem(key, member);
return client.getIntegerReply();
Expand Down Expand Up @@ -1458,6 +1458,12 @@ public Long zadd(final byte[] key, final double score, final byte[] member) {
client.zadd(key, score, member);
return client.getIntegerReply();
}

public Long zadd(final byte[] key, final Map<Double, byte[]> scoreMembers) {
checkIsInMulti();
client.zaddBinary(key, scoreMembers);
return client.getIntegerReply();
}

public Set<byte[]> zrange(final byte[] key, final int start, final int end) {
checkIsInMulti();
Expand All @@ -1477,13 +1483,13 @@ public Set<byte[]> zrange(final byte[] key, final int start, final int end) {
*
*
* @param key
* @param member
* @param members
* @return Integer reply, specifically: 1 if the new element was removed 0
* if the new element was not a member of the set
*/
public Long zrem(final byte[] key, final byte[] member) {
public Long zrem(final byte[] key, final byte[]... members) {
checkIsInMulti();
client.zrem(key, member);
client.zrem(key, members);
return client.getIntegerReply();
}

Expand Down
14 changes: 8 additions & 6 deletions src/main/java/redis/clients/jedis/BinaryJedisCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public interface BinaryJedisCommands {

Boolean hexists(byte[] key, byte[] field);

Long hdel(byte[] key, byte[] field);
Long hdel(byte[] key, byte[]... field);

Long hlen(byte[] key);

Expand All @@ -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);

Expand All @@ -87,11 +87,11 @@ public interface BinaryJedisCommands {

byte[] rpop(byte[] key);

Long sadd(byte[] key, byte[] member);
Long sadd(byte[] key, byte[]... member);

Set<byte[]> smembers(byte[] key);

Long srem(byte[] key, byte[] member);
Long srem(byte[] key, byte[]... member);

byte[] spop(byte[] key);

Expand All @@ -102,10 +102,12 @@ public interface BinaryJedisCommands {
byte[] srandmember(byte[] key);

Long zadd(byte[] key, double score, byte[] member);

Long zadd(byte[] key, Map<Double, byte[]> scoreMembers);

Set<byte[]> zrange(byte[] key, int start, int end);

Long zrem(byte[] key, byte[] member);
Long zrem(byte[] key, byte[]... member);

Double zincrby(byte[] key, double score, byte[] member);

Expand Down
29 changes: 17 additions & 12 deletions src/main/java/redis/clients/jedis/BinaryShardedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ public Boolean hexists(byte[] key, byte[] field) {
return j.hexists(key, field);
}

public Long hdel(byte[] key, byte[] field) {
public Long hdel(byte[] key, byte[]... fields) {
Jedis j = getShard(key);
return j.hdel(key, field);
return j.hdel(key, fields);
}

public Long hlen(byte[] key) {
Expand All @@ -180,14 +180,14 @@ public Map<byte[], byte[]> hgetAll(byte[] key) {
return j.hgetAll(key);
}

public Long rpush(byte[] key, byte[] string) {
public Long rpush(byte[] key, byte[]... strings) {
Jedis j = getShard(key);
return j.rpush(key, string);
return j.rpush(key, strings);
}

public Long lpush(byte[] key, byte[] string) {
public Long lpush(byte[] key, byte[]... strings) {
Jedis j = getShard(key);
return j.lpush(key, string);
return j.lpush(key, strings);
}

public Long llen(byte[] key) {
Expand Down Expand Up @@ -230,19 +230,19 @@ public byte[] rpop(byte[] key) {
return j.rpop(key);
}

public Long sadd(byte[] key, byte[] member) {
public Long sadd(byte[] key, byte[]... members) {
Jedis j = getShard(key);
return j.sadd(key, member);
return j.sadd(key, members);
}

public Set<byte[]> smembers(byte[] key) {
Jedis j = getShard(key);
return j.smembers(key);
}

public Long srem(byte[] key, byte[] member) {
public Long srem(byte[] key, byte[]... members) {
Jedis j = getShard(key);
return j.srem(key, member);
return j.srem(key, members);
}

public byte[] spop(byte[] key) {
Expand All @@ -269,15 +269,20 @@ public Long zadd(byte[] key, double score, byte[] member) {
Jedis j = getShard(key);
return j.zadd(key, score, member);
}

public Long zadd(byte[] key, Map<Double,byte[]> scoreMembers) {
Jedis j = getShard(key);
return j.zadd(key, scoreMembers);
}

public Set<byte[]> zrange(byte[] key, int start, int end) {
Jedis j = getShard(key);
return j.zrange(key, start, end);
}

public Long zrem(byte[] key, byte[] member) {
public Long zrem(byte[] key, byte[]... members) {
Jedis j = getShard(key);
return j.zrem(key, member);
return j.zrem(key, members);
}

public Double zincrby(byte[] key, double score, byte[] member) {
Expand Down
Loading

0 comments on commit 8aca0b7

Please sign in to comment.