Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,26 @@ public void zremrangeByScore(final byte[] key, final byte[] min, final byte[] ma
sendCommand(ZREMRANGEBYSCORE, key, min, max);
}

public void zunion(final ZParams params, final byte[]... keys) {
sendCommand(ZUNION, buildZunionByteParams(params, false, keys));
}

public void zunionWithScores(final ZParams params, final byte[]... keys) {
sendCommand(ZUNION, buildZunionByteParams(params, true, keys));
}

private byte[][] buildZunionByteParams(final ZParams params, final boolean withScores, final byte[]... keys) {
final List<byte[]> args = new ArrayList<>();
args.add(Protocol.toByteArray(keys.length));
Collections.addAll(args, keys);

args.addAll(params.getParams());
if (withScores) {
args.add(WITHSCORES.getRaw());
}
return args.toArray(new byte[args.size()][]);
}

public void zunionstore(final byte[] dstkey, final byte[]... sets) {
sendCommand(ZUNIONSTORE, joinParameters(dstkey, toByteArray(sets.length), sets));
}
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -3039,6 +3039,34 @@ public Long zremrangeByScore(final byte[] key, final byte[] min, final byte[] ma
return client.getIntegerReply();
}

/**
* Add multiple sorted sets, This command is similar to ZUNIONSTORE, but instead of storing the
* resulting sorted set, it is returned to the client.
* @param params
* @param keys
* @return
*/
@Override
public Set<byte[]> zunion(final ZParams params, final byte[]... keys) {
checkIsInMultiOrPipeline();
client.zunion(params, keys);
return BuilderFactory.BYTE_ARRAY_ZSET.build(client.getBinaryMultiBulkReply());
}

/**
* Add multiple sorted sets with scores, This command is similar to ZUNIONSTORE, but instead of storing the
* resulting sorted set, it is returned to the client.
* @param params
* @param keys
* @return
*/
@Override
public Set<Tuple> zunionWithScores(final ZParams params, final byte[]... keys) {
checkIsInMultiOrPipeline();
client.zunionWithScores(params, keys);
return BuilderFactory.TUPLE_ZSET.build(client.getBinaryMultiBulkReply());
}

/**
* Creates a union or intersection of N sorted sets given by keys k1 through kN, and stores it at
* dstkey. It is mandatory to provide the number of input keys N, before passing the input keys
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryJedisCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -1984,6 +1984,26 @@ public Long execute(Jedis connection) {
}.runBinary(wholeKeys.length, wholeKeys);
}

@Override
public Set<byte[]> zunion(final ZParams params, final byte[]... keys) {
return new JedisClusterCommand<Set<byte[]>>(connectionHandler, maxAttempts) {
@Override
public Set<byte[]> execute(Jedis connection) {
return connection.zunion(params, keys);
}
}.runBinary(keys.length, keys);
}

@Override
public Set<Tuple> zunionWithScores(final ZParams params, final byte[]... keys) {
return new JedisClusterCommand<Set<Tuple>>(connectionHandler, maxAttempts) {
@Override
public Set<Tuple> execute(Jedis connection) {
return connection.zunionWithScores(params, keys);
}
}.runBinary(keys.length, keys);
}

@Override
public Long zunionstore(final byte[] dstkey, final byte[]... sets) {
byte[][] wholeKeys = KeyMergeUtil.merge(dstkey, sets);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/redis/clients/jedis/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,16 @@ public void zremrangeByScore(final String key, final String min, final String ma
zremrangeByScore(SafeEncoder.encode(key), SafeEncoder.encode(min), SafeEncoder.encode(max));
}

@Override
public void zunion(final ZParams params, final String... keys) {
zunion(params, SafeEncoder.encodeMany(keys));
}

@Override
public void zunionWithScores(final ZParams params, final String... keys) {
zunionWithScores(params, SafeEncoder.encodeMany(keys));
}

@Override
public void zunionstore(final String dstkey, final String... sets) {
zunionstore(SafeEncoder.encode(dstkey), SafeEncoder.encodeMany(sets));
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -2629,6 +2629,34 @@ public Long zremrangeByScore(final String key, final String min, final String ma
return client.getIntegerReply();
}

/**
* Add multiple sorted sets, This command is similar to ZUNIONSTORE, but instead of storing the
* resulting sorted set, it is returned to the client.
* @param params
* @param keys
* @return
*/
@Override
public Set<String> zunion(ZParams params, String... keys) {
checkIsInMultiOrPipeline();
client.zunion(params, keys);
return BuilderFactory.STRING_ZSET.build(client.getBinaryMultiBulkReply());
}

/**
* Add multiple sorted sets with scores, This command is similar to ZUNIONSTORE, but instead of storing the
* resulting sorted set, it is returned to the client.
* @param params
* @param keys
* @return
*/
@Override
public Set<Tuple> zunionWithScores(ZParams params, String... keys) {
checkIsInMultiOrPipeline();
client.zunionWithScores(params, keys);
return getTupledSet();
}

/**
* Creates a union or intersection of N sorted sets given by keys k1 through kN, and stores it at
* dstkey. It is mandatory to provide the number of input keys N, before passing the input keys
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/redis/clients/jedis/JedisCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -2052,6 +2052,26 @@ public Long execute(Jedis connection) {
}.run(mergedKeys.length, mergedKeys);
}

@Override
public Set<String> zunion(final ZParams params, final String... keys) {
return new JedisClusterCommand<Set<String>>(connectionHandler, maxAttempts) {
@Override
public Set<String> execute(Jedis connection) {
return connection.zunion(params, keys);
}
}.run(keys.length, keys);
}

@Override
public Set<Tuple> zunionWithScores(final ZParams params, final String... keys) {
return new JedisClusterCommand<Set<Tuple>>(connectionHandler, maxAttempts) {
@Override
public Set<Tuple> execute(Jedis connection) {
return connection.zunionWithScores(params, keys);
}
}.run(keys.length, keys);
}

@Override
public Long zunionstore(final String dstkey, final String... sets) {
String[] mergedKeys = KeyMergeUtil.merge(dstkey, sets);
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/redis/clients/jedis/MultiKeyPipelineBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,30 @@ public Response<Long> zinterstore(byte[] dstkey, ZParams params, byte[]... sets)
return getResponse(BuilderFactory.LONG);
}

@Override
public Response<Set<byte[]>> zunion(ZParams params, byte[]... keys) {
client.zunion(params, keys);
return getResponse(BuilderFactory.BYTE_ARRAY_ZSET);
}

@Override
public Response<Set<Tuple>> zunionWithScores(ZParams params, byte[]... keys) {
client.zunionWithScores(params, keys);
return getResponse(BuilderFactory.TUPLE_ZSET);
}

@Override
public Response<Set<String>> zunion(ZParams params, String... keys) {
client.zunion(params, keys);
return getResponse(BuilderFactory.STRING_ZSET);
}

@Override
public Response<Set<Tuple>> zunionWithScores(ZParams params, String... keys) {
client.zunionWithScores(params, keys);
return getResponse(BuilderFactory.TUPLE_ZSET);
}

@Override
public Response<Long> zunionstore(String dstkey, String... sets) {
client.zunionstore(dstkey, sets);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/redis/clients/jedis/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public static enum Command implements ProtocolCommand {
ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZRANDMEMBER, ZCARD, ZSCORE, ZPOPMAX, ZPOPMIN, MULTI, DISCARD, EXEC,
WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE,
PUNSUBSCRIBE, PUBSUB, ZCOUNT, ZRANGEBYSCORE, ZREVRANGEBYSCORE, ZREMRANGEBYRANK,
ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, ZLEXCOUNT, ZRANGEBYLEX, ZREVRANGEBYLEX,
ZREMRANGEBYSCORE, ZUNION, ZUNIONSTORE, ZINTERSTORE, ZLEXCOUNT, ZRANGEBYLEX, ZREVRANGEBYLEX,
ZREMRANGEBYLEX, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG,
STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH, SETBIT, GETBIT,
BITPOS, SETRANGE, GETRANGE, EVAL, EVALSHA, SCRIPT, SLOWLOG, OBJECT, BITCOUNT, BITOP, SENTINEL,
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/redis/clients/jedis/commands/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ default void setex(String key, int seconds, String value) {

void zremrangeByScore(String key, String min, String max);

void zunion(ZParams params, String... keys);

void zunionWithScores(ZParams params, String... keys);

void zunionstore(String dstkey, String... sets);

void zunionstore(String dstkey, ZParams params, String... sets);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public interface MultiKeyBinaryCommands {

Long zinterstore(byte[] dstkey, ZParams params, byte[]... sets);

Set<byte[]> zunion(ZParams params, byte[]... keys);

Set<Tuple> zunionWithScores(ZParams params, byte[]... keys);

Long zunionstore(byte[] dstkey, byte[]... sets);

Long zunionstore(byte[] dstkey, ZParams params, byte[]... sets);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public interface MultiKeyBinaryJedisClusterCommands {

Long zinterstore(byte[] dstkey, ZParams params, byte[]... sets);

Set<byte[]> zunion(ZParams params, byte[]... keys);

Set<Tuple> zunionWithScores(ZParams params, byte[]... keys);

Long zunionstore(byte[] dstkey, byte[]... sets);

Long zunionstore(byte[] dstkey, ZParams params, byte[]... sets);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public interface MultiKeyBinaryRedisPipeline {

Response<Long> zinterstore(byte[] dstkey, ZParams params, byte[]... sets);

Response<Set<byte[]>> zunion(ZParams params, byte[]... keys);

Response<Set<Tuple>> zunionWithScores(ZParams params, byte[]... keys);

Response<Long> zunionstore(byte[] dstkey, byte[]... sets);

Response<Long> zunionstore(byte[] dstkey, ZParams params, byte[]... sets);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public interface MultiKeyCommands {

Long zinterstore(String dstkey, ZParams params, String... sets);

Set<String> zunion(ZParams params, String... keys);

Set<Tuple> zunionWithScores(ZParams params, String... keys);

Long zunionstore(String dstkey, String... sets);

Long zunionstore(String dstkey, ZParams params, String... sets);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public interface MultiKeyCommandsPipeline {

Response<Long> zinterstore(String dstkey, ZParams params, String... sets);

Response<Set<String>> zunion(ZParams params, String... keys);

Response<Set<Tuple>> zunionWithScores(ZParams params, String... keys);

Response<Long> zunionstore(String dstkey, String... sets);

Response<Long> zunionstore(String dstkey, ZParams params, String... sets);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public interface MultiKeyJedisClusterCommands {

Long zinterstore(String dstkey, ZParams params, String... sets);

Set<String> zunion(ZParams params, String... keys);

Set<Tuple> zunionWithScores(ZParams params, String... keys);

Long zunionstore(String dstkey, String... sets);

Long zunionstore(String dstkey, ZParams params, String... sets);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,46 @@ public void zremrangeByLexBinary() {
assertByteArraySetEquals(bexpected, jedis.zrangeByLex(bfoo, bLexMinusInf, bLexPlusInf));
}

@Test
public void zunion() {
jedis.zadd("foo", 1, "a");
jedis.zadd("foo", 2, "b");
jedis.zadd("bar", 2, "a");
jedis.zadd("bar", 2, "b");

ZParams params = new ZParams();
params.weights(2, 2.5);
params.aggregate(ZParams.Aggregate.SUM);
Set<String> expected = new LinkedHashSet<>();
expected.add("a");
expected.add("b");
assertEquals(expected, jedis.zunion(params, "foo", "bar"));

Set<Tuple> expectedTuple = new LinkedHashSet<>();
expectedTuple.add(new Tuple("b", new Double(9)));
expectedTuple.add(new Tuple("a", new Double(7)));
assertEquals(expectedTuple, jedis.zunionWithScores(params, "foo", "bar"));

// Binary
jedis.zadd(bfoo, 1, ba);
jedis.zadd(bfoo, 2, bb);
jedis.zadd(bbar, 2, ba);
jedis.zadd(bbar, 2, bb);

ZParams bparams = new ZParams();
bparams.weights(2, 2.5);
bparams.aggregate(ZParams.Aggregate.SUM);
Set<byte[]> bexpected = new LinkedHashSet<>();
bexpected.add(bb);
bexpected.add(ba);
assertByteArraySetEquals(bexpected, jedis.zunion(params, bfoo, bbar));

Set<Tuple> bexpectedTuple = new LinkedHashSet<>();
bexpectedTuple.add(new Tuple(bb, new Double(9)));
bexpectedTuple.add(new Tuple(ba, new Double(7)));
assertEquals(bexpectedTuple, jedis.zunionWithScores(bparams, bfoo, bbar));
}

@Test
public void zunionstore() {
jedis.zadd("foo", 1, "a");
Expand Down