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

Impl zpopmin command #1988

Merged
merged 11 commits into from Oct 16, 2019
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -191,6 +191,7 @@
</execution>
</executions>
</plugin>

gkorland marked this conversation as resolved.
Show resolved Hide resolved
</plugins>
</build>
</project>
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 zpopmin(final byte[] key) {
sendCommand(ZPOPMIN, key);
}

public void zpopmin(final byte[] key, final long count) {
sendCommand(ZPOPMIN, key, toByteArray(count));
}

public void multi() {
sendCommand(MULTI);
isInMulti = true;
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryJedis.java
Expand Up @@ -1841,6 +1841,22 @@ public Double zscore(final byte[] key, final byte[] member) {
return (score != null ? new Double(score) : null);
}



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

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

public Transaction multi() {
client.multi();
client.getOne(); // expected OK
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryJedisCluster.java
Expand Up @@ -906,6 +906,26 @@ public Double execute(Jedis connection) {
}.runBinary(key);
}

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

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

@Override
public List<byte[]> sort(final byte[] key) {
return new JedisClusterCommand<List<byte[]>>(connectionHandler, maxAttempts) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryShardedJedis.java
Expand Up @@ -534,6 +534,18 @@ public Double zscore(final byte[] key, final byte[] member) {
return j.zscore(key, member);
}

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

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

@Override
public List<byte[]> sort(final byte[] key) {
Jedis j = getShard(key);
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 zpopmin(final String key) {
zpopmin(SafeEncoder.encode(key));
}

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

@Override
public void watch(final String... keys) {
watch(SafeEncoder.encodeMany(keys));
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/redis/clients/jedis/Jedis.java
Expand Up @@ -1673,6 +1673,21 @@ public Double zscore(final String key, final String member) {
return BuilderFactory.DOUBLE.build(client.getOne());
}


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

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

@Override
public String watch(final String... keys) {
client.watch(keys);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/redis/clients/jedis/PipelineBase.java
Expand Up @@ -1299,6 +1299,18 @@ public Response<Double> zscore(final byte[] key, final byte[] member) {
return getResponse(BuilderFactory.DOUBLE);
}

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

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

@Override
public Response<Long> zlexcount(final byte[] key, final byte[] min, final byte[] max) {
getClient(key).zlexcount(key, min, max);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/redis/clients/jedis/Protocol.java
Expand Up @@ -249,7 +249,7 @@ 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, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP,
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,
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/redis/clients/jedis/ShardedJedis.java
Expand Up @@ -572,6 +572,18 @@ public Double zscore(final String key, final String member) {
return j.zscore(key, member);
}

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

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

@Override
public List<String> sort(final String key) {
Jedis j = getShard(key);
Expand Down
Expand Up @@ -179,6 +179,10 @@ public interface BinaryJedisClusterCommands {

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

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

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

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

List<byte[]> sort(byte[] key, SortingParams sortingParameters);
Expand Down
Expand Up @@ -184,6 +184,10 @@ public interface BinaryJedisCommands {

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

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

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

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

List<byte[]> sort(byte[] key, SortingParams sortingParameters);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/redis/clients/jedis/commands/Commands.java
Expand Up @@ -197,6 +197,10 @@ public interface Commands {

void zscore(String key, String member);

void zpopmin(String key);

void zpopmin(String key, long count);

void watch(String... keys);

void sort(String key);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/redis/clients/jedis/commands/JedisCommands.java
Expand Up @@ -187,6 +187,10 @@ public interface JedisCommands {

Double zscore(String key, String member);

Set<Tuple> zpopmin(String key);

Set<Tuple> zpopmin(String key, long count);

List<String> sort(String key);

List<String> sort(String key, SortingParams sortingParameters);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/redis/clients/jedis/commands/RedisPipeline.java
Expand Up @@ -228,6 +228,10 @@ Response<Set<Tuple>> zrevrangeByScoreWithScores(String key, String max, String m

Response<Double> zscore(String key, String member);

Response<Set<Tuple>> zpopmin(String key);

Response<Set<Tuple>> zpopmin(String key, long count);

Response<Long> zlexcount(String key, String min, String max);

Response<Set<String>> zrangeByLex(String key, String min, String max);
Expand Down
Expand Up @@ -544,6 +544,55 @@ public void zscore() {
}

@Test
public void zpopmin() {

jedis.zadd("foo", 1d, "a",ZAddParams.zAddParams().nx());
jedis.zadd("foo", 10d, "b",ZAddParams.zAddParams().nx());
jedis.zadd("foo", 0.1d, "c",ZAddParams.zAddParams().nx());
jedis.zadd("foo", 2d, "a",ZAddParams.zAddParams().nx());

Set<Tuple> range = jedis.zpopmin("foo", 2);

Set<Tuple> expected = new LinkedHashSet<Tuple>();
expected.add(new Tuple("c", 0.1d));
expected.add(new Tuple("a", 1d));

assertEquals(expected, range);


range = jedis.zpopmin("foo");

expected = new LinkedHashSet<Tuple>();
expected.add(new Tuple("b", 10d));

assertEquals(expected, range);



// Binary

jedis.zadd(bfoo, 1d, ba);
jedis.zadd(bfoo, 10d, bb);
jedis.zadd(bfoo, 0.1d, bc);
jedis.zadd(bfoo, 2d, ba);

Set<Tuple> brange = jedis.zpopmin(bfoo, 2);

Set<Tuple> bexpected = new LinkedHashSet<Tuple>();
bexpected.add(new Tuple(bc, 0.1d));
bexpected.add(new Tuple(ba, 2d));

assertEquals(bexpected, brange);

brange = jedis.zpopmin(bfoo);

bexpected = new LinkedHashSet<Tuple>();
bexpected.add(new Tuple(bb, 10d));

assertEquals(bexpected, brange);
}

@Test
public void zcount() {
jedis.zadd("foo", 1d, "a");
jedis.zadd("foo", 10d, "b");
Expand Down