Implementation of ZREVRANGEBYSCORE command

This commit is contained in:
lmar
2011-03-08 19:03:24 +01:00
parent 121af74972
commit 3fc43e7dec
7 changed files with 166 additions and 6 deletions

View File

@@ -477,6 +477,10 @@ public class BinaryClient extends Connection {
final double max) { final double max) {
sendCommand(ZRANGEBYSCORE, key, toByteArray(min), toByteArray(max)); sendCommand(ZRANGEBYSCORE, key, toByteArray(min), toByteArray(max));
} }
public void zrevrangeByScore(final byte[] key, final double max,
final double min) {
sendCommand(ZREVRANGEBYSCORE, key, toByteArray(max), toByteArray(min));
}
public void zrangeByScore(final byte[] key, final byte[] min, public void zrangeByScore(final byte[] key, final byte[] min,
final byte[] max) { final byte[] max) {
@@ -488,12 +492,22 @@ public class BinaryClient extends Connection {
sendCommand(ZRANGEBYSCORE, key, toByteArray(min), toByteArray(max), sendCommand(ZRANGEBYSCORE, key, toByteArray(min), toByteArray(max),
LIMIT.raw, toByteArray(offset), toByteArray(count)); LIMIT.raw, toByteArray(offset), toByteArray(count));
} }
public void zrevrangeByScore(final byte[] key, final double max,
final double min, final int offset, int count) {
sendCommand(ZREVRANGEBYSCORE, key, toByteArray(max), toByteArray(min),
LIMIT.raw, toByteArray(offset), toByteArray(count));
}
public void zrangeByScoreWithScores(final byte[] key, final double min, public void zrangeByScoreWithScores(final byte[] key, final double min,
final double max) { final double max) {
sendCommand(ZRANGEBYSCORE, key, toByteArray(min), toByteArray(max), sendCommand(ZRANGEBYSCORE, key, toByteArray(min), toByteArray(max),
WITHSCORES.raw); WITHSCORES.raw);
} }
public void zrevrangeByScoreWithScores(final byte[] key, final double max,
final double min) {
sendCommand(ZREVRANGEBYSCORE, key, toByteArray(max), toByteArray(min),
WITHSCORES.raw);
}
public void zrangeByScoreWithScores(final byte[] key, final double min, public void zrangeByScoreWithScores(final byte[] key, final double min,
final double max, final int offset, final int count) { final double max, final int offset, final int count) {
@@ -501,6 +515,12 @@ public class BinaryClient extends Connection {
LIMIT.raw, toByteArray(offset), toByteArray(count), LIMIT.raw, toByteArray(offset), toByteArray(count),
WITHSCORES.raw); WITHSCORES.raw);
} }
public void zrevrangeByScoreWithScores(final byte[] key, final double max,
final double min, final int offset, final int count) {
sendCommand(ZREVRANGEBYSCORE, key, toByteArray(max), toByteArray(min),
LIMIT.raw, toByteArray(offset), toByteArray(count),
WITHSCORES.raw);
}
public void zremrangeByRank(final byte[] key, final int start, final int end) { public void zremrangeByRank(final byte[] key, final int start, final int end) {
sendCommand(ZREMRANGEBYRANK, key, toByteArray(start), toByteArray(end)); sendCommand(ZREMRANGEBYRANK, key, toByteArray(start), toByteArray(end));

View File

@@ -412,6 +412,10 @@ public class Client extends BinaryClient implements Commands {
final double max) { final double max) {
zrangeByScore(SafeEncoder.encode(key), min, max); zrangeByScore(SafeEncoder.encode(key), min, max);
} }
public void zrevrangeByScore(final String key, final double max,
final double min) {
zrevrangeByScore(SafeEncoder.encode(key), max, min);
}
public void zrangeByScore(final String key, final String min, public void zrangeByScore(final String key, final String min,
final String max) { final String max) {
@@ -423,17 +427,30 @@ public class Client extends BinaryClient implements Commands {
final double max, final int offset, int count) { final double max, final int offset, int count) {
zrangeByScore(SafeEncoder.encode(key), min, max, offset, count); zrangeByScore(SafeEncoder.encode(key), min, max, offset, count);
} }
public void zrevrangeByScore(final String key, final double max,
final double min, final int offset, int count) {
zrevrangeByScore(SafeEncoder.encode(key), max, min, offset, count);
}
public void zrangeByScoreWithScores(final String key, final double min, public void zrangeByScoreWithScores(final String key, final double min,
final double max) { final double max) {
zrangeByScoreWithScores(SafeEncoder.encode(key), min, max); zrangeByScoreWithScores(SafeEncoder.encode(key), min, max);
} }
public void zrevrangeByScoreWithScores(final String key, final double max,
final double min) {
zrevrangeByScoreWithScores(SafeEncoder.encode(key), max, min);
}
public void zrangeByScoreWithScores(final String key, final double min, public void zrangeByScoreWithScores(final String key, final double min,
final double max, final int offset, final int count) { final double max, final int offset, final int count) {
zrangeByScoreWithScores(SafeEncoder.encode(key), min, max, offset, zrangeByScoreWithScores(SafeEncoder.encode(key), min, max, offset,
count); count);
} }
public void zrevrangeByScoreWithScores(final String key, final double max,
final double min, final int offset, final int count) {
zrevrangeByScoreWithScores(SafeEncoder.encode(key), max, min, offset,
count);
}
public void zremrangeByRank(final String key, final int start, final int end) { public void zremrangeByRank(final String key, final int start, final int end) {
zremrangeByRank(SafeEncoder.encode(key), start, end); zremrangeByRank(SafeEncoder.encode(key), start, end);

View File

@@ -2085,6 +2085,12 @@ public class Jedis extends BinaryJedis implements JedisCommands {
client.zrangeByScore(key, min, max); client.zrangeByScore(key, min, max);
return new LinkedHashSet<String>(client.getMultiBulkReply()); return new LinkedHashSet<String>(client.getMultiBulkReply());
} }
public Set<String> zrevrangeByScore(final String key, final double max,
final double min) {
runChecks();
client.zrevrangeByScore(key, max, min);
return new LinkedHashSet<String>(client.getMultiBulkReply());
}
public Set<String> zrangeByScore(final String key, final String min, public Set<String> zrangeByScore(final String key, final String min,
final String max) { final String max) {
@@ -2155,6 +2161,12 @@ public class Jedis extends BinaryJedis implements JedisCommands {
client.zrangeByScore(key, min, max, offset, count); client.zrangeByScore(key, min, max, offset, count);
return new LinkedHashSet<String>(client.getMultiBulkReply()); return new LinkedHashSet<String>(client.getMultiBulkReply());
} }
public Set<String> zrevrangeByScore(final String key, final double max,
final double min, final int offset, final int count) {
runChecks();
client.zrevrangeByScore(key, max, min, offset, count);
return new LinkedHashSet<String>(client.getMultiBulkReply());
}
/** /**
* Return the all the elements in the sorted set at key with a score between * Return the all the elements in the sorted set at key with a score between
@@ -2219,6 +2231,13 @@ public class Jedis extends BinaryJedis implements JedisCommands {
Set<Tuple> set = getTupledSet(); Set<Tuple> set = getTupledSet();
return set; return set;
} }
public Set<Tuple> zrevrangeByScoreWithScores(final String key,
final double max, final double min) {
runChecks();
client.zrevrangeByScoreWithScores(key, max, min);
Set<Tuple> set = getTupledSet();
return set;
}
/** /**
* Return the all the elements in the sorted set at key with a score between * Return the all the elements in the sorted set at key with a score between
@@ -2284,6 +2303,14 @@ public class Jedis extends BinaryJedis implements JedisCommands {
Set<Tuple> set = getTupledSet(); Set<Tuple> set = getTupledSet();
return set; return set;
} }
public Set<Tuple> zrevrangeByScoreWithScores(final String key,
final double max, final double min, final int offset,
final int count) {
runChecks();
client.zrevrangeByScoreWithScores(key, max, min, offset, count);
Set<Tuple> set = getTupledSet();
return set;
}
private Set<Tuple> getTupledSet() { private Set<Tuple> getTupledSet() {
runChecks(); runChecks();

View File

@@ -136,14 +136,20 @@ public interface JedisCommands {
Long zcount(String key, double min, double max); Long zcount(String key, double min, double max);
Set<String> zrangeByScore(String key, double min, double max); Set<String> zrangeByScore(String key, double min, double max);
Set<String> zrevrangeByScore(String key, double max, double min);
Set<String> zrangeByScore(String key, double min, double max, int offset, Set<String> zrangeByScore(String key, double min, double max, int offset,
int count); int count);
Set<String> zrevrangeByScore(String key, double max, double min, int offset,
int count);
Set<Tuple> zrangeByScoreWithScores(String key, double min, double max); Set<Tuple> zrangeByScoreWithScores(String key, double min, double max);
Set<Tuple> zrevrangeByScoreWithScores(String key, double max, double min);
Set<Tuple> zrangeByScoreWithScores(String key, double min, double max, Set<Tuple> zrangeByScoreWithScores(String key, double min, double max,
int offset, int count); int offset, int count);
Set<Tuple> zrevrangeByScoreWithScores(String key, double max, double min,
int offset, int count);
Long zremrangeByRank(String key, int start, int end); Long zremrangeByRank(String key, int start, int end);

View File

@@ -135,7 +135,7 @@ public final class Protocol {
} }
public static enum Command { public static enum Command {
PING, SET, GET, QUIT, EXISTS, DEL, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET, HMGET, 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, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, ZCOUNT, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH, SETBIT, GETBIT, SETRANGE, GETRANGE; PING, SET, GET, QUIT, EXISTS, DEL, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET, HMGET, 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, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, ZCOUNT, ZRANGEBYSCORE, ZREVRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH, SETBIT, GETBIT, SETRANGE, GETRANGE;
public final byte[] raw; public final byte[] raw;

View File

@@ -353,23 +353,41 @@ public class ShardedJedis extends BinaryShardedJedis implements JedisCommands {
Jedis j = getShard(key); Jedis j = getShard(key);
return j.zrangeByScore(key, min, max); return j.zrangeByScore(key, min, max);
} }
public Set<String> zrevrangeByScore(String key, double max, double min) {
Jedis j = getShard(key);
return j.zrevrangeByScore(key, max, min);
}
public Set<String> zrangeByScore(String key, double min, double max, public Set<String> zrangeByScore(String key, double min, double max,
int offset, int count) { int offset, int count) {
Jedis j = getShard(key); Jedis j = getShard(key);
return j.zrangeByScore(key, min, max, offset, count); return j.zrangeByScore(key, min, max, offset, count);
} }
public Set<String> zrevrangeByScore(String key, double max, double min,
int offset, int count) {
Jedis j = getShard(key);
return j.zrevrangeByScore(key, max, min, offset, count);
}
public Set<Tuple> zrangeByScoreWithScores(String key, double min, double max) { public Set<Tuple> zrangeByScoreWithScores(String key, double min, double max) {
Jedis j = getShard(key); Jedis j = getShard(key);
return j.zrangeByScoreWithScores(key, min, max); return j.zrangeByScoreWithScores(key, min, max);
} }
public Set<Tuple> zrevrangeByScoreWithScores(String key, double max, double min) {
Jedis j = getShard(key);
return j.zrevrangeByScoreWithScores(key, max, min);
}
public Set<Tuple> zrangeByScoreWithScores(String key, double min, public Set<Tuple> zrangeByScoreWithScores(String key, double min,
double max, int offset, int count) { double max, int offset, int count) {
Jedis j = getShard(key); Jedis j = getShard(key);
return j.zrangeByScoreWithScores(key, min, max, offset, count); return j.zrangeByScoreWithScores(key, min, max, offset, count);
} }
public Set<Tuple> zrevrangeByScoreWithScores(String key, double max,
double min, int offset, int count) {
Jedis j = getShard(key);
return j.zrevrangeByScoreWithScores(key, max, min, offset, count);
}
public Long zremrangeByRank(String key, int start, int end) { public Long zremrangeByRank(String key, int start, int end) {
Jedis j = getShard(key); Jedis j = getShard(key);

View File

@@ -449,6 +449,42 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
} }
@Test
public void zrevrangebyscore() {
jedis.zadd("foo", 1.0d, "a");
jedis.zadd("foo", 2.0d, "b");
jedis.zadd("foo", 3.0d, "c");
jedis.zadd("foo", 4.0d, "d");
jedis.zadd("foo", 5.0d, "e");
Set<String> range = jedis.zrevrangeByScore("foo", 3d, Double.NEGATIVE_INFINITY, 0, 1);
Set<String> expected = new LinkedHashSet<String>();
expected.add("c");
assertEquals(expected, range);
range = jedis.zrevrangeByScore("foo", 3.5d, Double.NEGATIVE_INFINITY, 0, 2);
expected = new LinkedHashSet<String>();
expected.add("c");
expected.add("b");
assertEquals(expected, range);
range = jedis.zrevrangeByScore("foo", 3.5d, Double.NEGATIVE_INFINITY, 1, 1);
expected = new LinkedHashSet<String>();
expected.add("b");
assertEquals(expected, range);
range = jedis.zrevrangeByScore("foo", 4d, 2d);
expected = new LinkedHashSet<String>();
expected.add("d");
expected.add("c");
expected.add("b");
assertEquals(expected, range);
}
@Test @Test
public void zrangebyscoreWithScores() { public void zrangebyscoreWithScores() {
jedis.zadd("foo", 1d, "a"); jedis.zadd("foo", 1d, "a");
@@ -509,6 +545,42 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
} }
@Test
public void zrevrangebyscoreWithScores() {
jedis.zadd("foo", 1.0d, "a");
jedis.zadd("foo", 2.0d, "b");
jedis.zadd("foo", 3.0d, "c");
jedis.zadd("foo", 4.0d, "d");
jedis.zadd("foo", 5.0d, "e");
Set<Tuple> range = jedis.zrevrangeByScoreWithScores("foo", 3d, Double.NEGATIVE_INFINITY, 0, 1);
Set<Tuple> expected = new LinkedHashSet<Tuple>();
expected.add(new Tuple("c", 3.0d));
assertEquals(expected, range);
range = jedis.zrevrangeByScoreWithScores("foo", 3.5d, Double.NEGATIVE_INFINITY, 0, 2);
expected = new LinkedHashSet<Tuple>();
expected.add(new Tuple("c", 3.0d));
expected.add(new Tuple("b", 2.0d));
assertEquals(expected, range);
range = jedis.zrevrangeByScoreWithScores("foo", 3.5d, Double.NEGATIVE_INFINITY, 1, 1);
expected = new LinkedHashSet<Tuple>();
expected.add(new Tuple("b", 2.0d));
assertEquals(expected, range);
range = jedis.zrevrangeByScoreWithScores("foo", 4d, 2d);
expected = new LinkedHashSet<Tuple>();
expected.add(new Tuple("d", 4.0d));
expected.add(new Tuple("c", 3.0d));
expected.add(new Tuple("b", 2.0d));
assertEquals(expected, range);
}
@Test @Test
public void zremrangeByRank() { public void zremrangeByRank() {
jedis.zadd("foo", 1d, "a"); jedis.zadd("foo", 1d, "a");