Skip to content

Commit

Permalink
implemented zpopmax, zpopmax with count (#1968)
Browse files Browse the repository at this point in the history
* implemented zpopmax, zpopmax with count

* implemented zpopmax, zpopmax with count -> used Tuple

* implemented zpopmax, zpopmax with count -> trimmed stuff

* implemented zpopmax, zpopmax with count -> beautified code and tests

* reorder/format due to changes in master
  • Loading branch information
z0mg authored and sazzad16 committed Dec 9, 2019
1 parent 7f6bfc7 commit 5089cbd
Show file tree
Hide file tree
Showing 19 changed files with 369 additions and 44 deletions.
8 changes: 8 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryClient.java
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,24 @@ public String toString() {

};

public static final Builder<Tuple> TUPLE = new Builder<Tuple>() {
@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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit 5089cbd

Please sign in to comment.