Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented zpopmax, zpopmax with count #1968

Merged
merged 5 commits into from Dec 9, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryClient.java
Expand Up @@ -503,6 +503,14 @@ public void zscore(final byte[] key, final byte[] member) {
sendCommand(ZSCORE, key, member);
}

public void zpopmax(final byte[] key) {
sendCommand(ZPOPMAX, key);
}

public void zpopmax(final byte[] key, final int count) {
sendCommand(ZPOPMAX, key, toByteArray(count));
}

public void zpopmin(final byte[] key) {
sendCommand(ZPOPMIN, key);
}
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/redis/clients/jedis/BinaryJedis.java
Expand Up @@ -1846,14 +1846,28 @@ public Double zscore(final byte[] key, final byte[] member) {
}

@Override
public Set<Tuple> zpopmin(final byte[] key) {
public Tuple zpopmax(final byte[] key) {
checkIsInMultiOrPipeline();
client.zpopmin(key);
client.zpopmax(key);
return BuilderFactory.TUPLE.build(client.getBinaryMultiBulkReply());
}

@Override
public Set<Tuple> zpopmax(final byte[] key, final int count) {
checkIsInMultiOrPipeline();
client.zpopmax(key, count);
return getTupledSet();
}

@Override
public Set<Tuple> zpopmin(final byte[] key, final long count) {
public Tuple zpopmin(final byte[] key) {
checkIsInMultiOrPipeline();
client.zpopmin(key);
return BuilderFactory.TUPLE.build(client.getBinaryMultiBulkReply());
}

@Override
public Set<Tuple> zpopmin(final byte[] key, final int count) {
checkIsInMultiOrPipeline();
client.zpopmin(key, count);
return getTupledSet();
Expand Down
28 changes: 24 additions & 4 deletions src/main/java/redis/clients/jedis/BinaryJedisCluster.java
Expand Up @@ -908,21 +908,41 @@ public Double execute(Jedis connection) {
}

@Override
public Set<Tuple> zpopmin(final byte[] key, final long count) {
public Tuple zpopmax(final byte[] key) {
return new JedisClusterCommand<Tuple>(connectionHandler, maxAttempts) {
@Override
public Tuple execute(Jedis connection) {
return connection.zpopmax(key);
}
}.runBinary(key);
}

@Override
public Set<Tuple> zpopmax(final byte[] key, final int count) {
return new JedisClusterCommand<Set<Tuple>>(connectionHandler, maxAttempts) {
@Override
public Set<Tuple> execute(Jedis connection) {
return connection.zpopmin(key, count);
return connection.zpopmax(key, count);
}
}.runBinary(key);
}

@Override
public Tuple zpopmin(final byte[] key) {
return new JedisClusterCommand<Tuple>(connectionHandler, maxAttempts) {
@Override
public Tuple execute(Jedis connection) {
return connection.zpopmin(key);
}
}.runBinary(key);
}

@Override
public Set<Tuple> zpopmin(final byte[] key) {
public Set<Tuple> zpopmin(final byte[] key, final int count) {
return new JedisClusterCommand<Set<Tuple>>(connectionHandler, maxAttempts) {
@Override
public Set<Tuple> execute(Jedis connection) {
return connection.zpopmin(key);
return connection.zpopmin(key, count);
}
}.runBinary(key);
}
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/redis/clients/jedis/BinaryShardedJedis.java
Expand Up @@ -539,13 +539,25 @@ public Double zscore(final byte[] key, final byte[] member) {
}

@Override
public Set<Tuple> zpopmin(final byte[] key) {
public Tuple zpopmax(final byte[] key) {
Jedis j = getShard(key);
return j.zpopmax(key);
}

@Override
public Set<Tuple> zpopmax(final byte[] key, final int count) {
Jedis j = getShard(key);
return j.zpopmax(key, count);
}

@Override
public Tuple zpopmin(final byte[] key) {
Jedis j = getShard(key);
return j.zpopmin(key);
}

@Override
public Set<Tuple> zpopmin(final byte[] key, final long count) {
public Set<Tuple> zpopmin(final byte[] key, final int count) {
Jedis j = getShard(key);
return j.zpopmin(key, count);
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/redis/clients/jedis/BuilderFactory.java
Expand Up @@ -285,6 +285,24 @@ public String toString() {

};

public static final Builder<Tuple> TUPLE = new Builder<Tuple>() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@z0mg can you please add a comment about the different between TUPLE_ZSET and TUPLE?
Or perhaps we better even rename this builder?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gkorland ZPOPMAX and ZPOPMIN without count returns only one Tuple. Returning a Tuple should be better instead of returning a Set of single Tuple. This builder helps on that purpose.

@Override
@SuppressWarnings("unchecked")
public Tuple build(Object data) {
List<byte[]> l = (List<byte[]>) data; // never null
if (l.isEmpty()) {
return null;
}
return new Tuple(l.get(0), DOUBLE.build(l.get(1)));
}

@Override
public String toString() {
return "Tuple";
}

};

public static final Builder<Object> EVAL_RESULT = new Builder<Object>() {

@Override
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/redis/clients/jedis/Client.java
Expand Up @@ -465,6 +465,16 @@ public void zscore(final String key, final String member) {
zscore(SafeEncoder.encode(key), SafeEncoder.encode(member));
}

@Override
public void zpopmax(final String key) {
zpopmax(SafeEncoder.encode(key));
}

@Override
public void zpopmax(final String key, final int count) {
zpopmax(SafeEncoder.encode(key), count);
}

@Override
public void zpopmin(final String key) {
zpopmin(SafeEncoder.encode(key));
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/redis/clients/jedis/Jedis.java
Expand Up @@ -1677,14 +1677,28 @@ public Double zscore(final String key, final String member) {
}

@Override
public Set<Tuple> zpopmin(final String key) {
public Tuple zpopmax(final String key) {
checkIsInMultiOrPipeline();
client.zpopmin(key);
client.zpopmax(key);
return BuilderFactory.TUPLE.build(client.getBinaryMultiBulkReply());
}

@Override
public Set<Tuple> zpopmax(final String key, final int count) {
checkIsInMultiOrPipeline();
client.zpopmax(key, count);
return getTupledSet();
}

@Override
public Set<Tuple> zpopmin(final String key, final long count) {
public Tuple zpopmin(final String key) {
checkIsInMultiOrPipeline();
client.zpopmin(key);
return BuilderFactory.TUPLE.build(client.getBinaryMultiBulkReply());
}

@Override
public Set<Tuple> zpopmin(final String key, final int count) {
checkIsInMultiOrPipeline();
client.zpopmin(key, count);
return getTupledSet();
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/redis/clients/jedis/JedisCluster.java
Expand Up @@ -945,6 +945,46 @@ public Double execute(Jedis connection) {
}.run(key);
}

@Override
public Tuple zpopmax(final String key) {
return new JedisClusterCommand<Tuple>(connectionHandler, maxAttempts) {
@Override
public Tuple execute(Jedis connection) {
return connection.zpopmax(key);
}
}.run(key);
}

@Override
public Set<Tuple> zpopmax(final String key, final int count) {
return new JedisClusterCommand<Set<Tuple>>(connectionHandler, maxAttempts) {
@Override
public Set<Tuple> execute(Jedis connection) {
return connection.zpopmax(key, count);
}
}.run(key);
}

@Override
public Tuple zpopmin(final String key) {
return new JedisClusterCommand<Tuple>(connectionHandler, maxAttempts) {
@Override
public Tuple execute(Jedis connection) {
return connection.zpopmin(key);
}
}.run(key);
}

@Override
public Set<Tuple> zpopmin(final String key, final int count) {
return new JedisClusterCommand<Set<Tuple>>(connectionHandler, maxAttempts) {
@Override
public Set<Tuple> execute(Jedis connection) {
return connection.zpopmin(key, count);
}
}.run(key);
}

@Override
public List<String> sort(final String key) {
return new JedisClusterCommand<List<String>>(connectionHandler, maxAttempts) {
Expand Down
40 changes: 38 additions & 2 deletions src/main/java/redis/clients/jedis/PipelineBase.java
Expand Up @@ -1301,13 +1301,49 @@ public Response<Double> zscore(final byte[] key, final byte[] member) {
}

@Override
public Response<Set<Tuple>> zpopmin(final String key) {
public Response<Tuple> zpopmax(final String key) {
getClient(key).zpopmax(key);
return getResponse(BuilderFactory.TUPLE);
}

@Override
public Response<Tuple> zpopmax(final byte[] key) {
getClient(key).zpopmax(key);
return getResponse(BuilderFactory.TUPLE);
}

@Override
public Response<Set<Tuple>> zpopmax(final String key, final int count) {
getClient(key).zpopmax(key, count);
return getResponse(BuilderFactory.TUPLE_ZSET);
}

@Override
public Response<Set<Tuple>> zpopmax(final byte[] key, final int count) {
getClient(key).zpopmax(key, count);
return getResponse(BuilderFactory.TUPLE_ZSET);
}

@Override
public Response<Tuple> zpopmin(final String key) {
getClient(key).zpopmin(key);
return getResponse(BuilderFactory.TUPLE);
}

@Override
public Response<Tuple> zpopmin(final byte[] key) {
getClient(key).zpopmin(key);
return getResponse(BuilderFactory.TUPLE);
}

@Override
public Response<Set<Tuple>> zpopmin(final byte[] key, final int count) {
getClient(key).zpopmin(key, count);
return getResponse(BuilderFactory.TUPLE_ZSET);
}

@Override
public Response<Set<Tuple>> zpopmin(final String key, final long count) {
public Response<Set<Tuple>> zpopmin(final String key, final int count) {
getClient(key).zpopmin(key, count);
return getResponse(BuilderFactory.TUPLE_ZSET);
}
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/redis/clients/jedis/Protocol.java
Expand Up @@ -249,13 +249,13 @@ public static enum Command implements ProtocolCommand {
HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM, LINDEX,
LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER, SINTER,
SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY,
ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, 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, 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, DUMP, RESTORE, PEXPIRE, PEXPIREAT, PTTL, INCRBYFLOAT,
ZRANK, ZREVRANK, ZREVRANGE, 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, 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, DUMP, RESTORE, PEXPIRE, PEXPIREAT, PTTL, INCRBYFLOAT,
PSETEX, CLIENT, TIME, MIGRATE, HINCRBYFLOAT, SCAN, HSCAN, SSCAN, ZSCAN, WAIT, CLUSTER, ASKING,
PFADD, PFCOUNT, PFMERGE, READONLY, GEOADD, GEODIST, GEOHASH, GEOPOS, GEORADIUS, GEORADIUS_RO,
GEORADIUSBYMEMBER, GEORADIUSBYMEMBER_RO, MODULE, BITFIELD, HSTRLEN, TOUCH, SWAPDB, MEMORY,
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/redis/clients/jedis/ShardedJedis.java
Expand Up @@ -574,13 +574,25 @@ public Double zscore(final String key, final String member) {
}

@Override
public Set<Tuple> zpopmin(final String key) {
public Tuple zpopmax(final String key) {
Jedis j = getShard(key);
return j.zpopmax(key);
}

@Override
public Set<Tuple> zpopmax(final String key, final int count) {
Jedis j = getShard(key);
return j.zpopmax(key, count);
}

@Override
public Tuple zpopmin(final String key) {
Jedis j = getShard(key);
return j.zpopmin(key);
}

@Override
public Set<Tuple> zpopmin(final String key, final long count) {
public Set<Tuple> zpopmin(final String key, final int count) {
Jedis j = getShard(key);
return j.zpopmin(key, count);
}
Expand Down
Expand Up @@ -179,9 +179,13 @@ public interface BinaryJedisClusterCommands {

Double zscore(byte[] key, byte[] member);

Set<Tuple> zpopmin(byte[] key);
Tuple zpopmax(byte[] key);

Set<Tuple> zpopmin(byte[] key, long count);
Set<Tuple> zpopmax(byte[] key, int count);

Tuple zpopmin(byte[] key);

Set<Tuple> zpopmin(byte[] key, int count);

List<byte[]> sort(byte[] key);

Expand Down
Expand Up @@ -184,9 +184,13 @@ public interface BinaryJedisCommands {

Double zscore(byte[] key, byte[] member);

Set<Tuple> zpopmin(byte[] key);
Tuple zpopmax(byte[] key);

Set<Tuple> zpopmin(byte[] key, long count);
Set<Tuple> zpopmax(byte[] key, int count);

Tuple zpopmin(byte[] key);

Set<Tuple> zpopmin(byte[] key, int count);

List<byte[]> sort(byte[] key);

Expand Down
Expand Up @@ -233,6 +233,14 @@ Response<Set<Tuple>> zrevrangeByScoreWithScores(byte[] key, byte[] max, byte[] m

Response<Double> zscore(byte[] key, byte[] member);

Response<Tuple> zpopmax(byte[] key);

Response<Set<Tuple>> zpopmax(byte[] key, int count);

Response<Tuple> zpopmin(byte[] key);

Response<Set<Tuple>> zpopmin(byte[] key, int count);

Response<Long> zlexcount(byte[] key, byte[] min, byte[] max);

Response<Set<byte[]>> zrangeByLex(byte[] key, byte[] min, byte[] max);
Expand Down