Implemented ZRANGEBYSCORE

This commit is contained in:
Jonathan Leibiusky
2010-08-07 18:27:51 -03:00
parent 2279746489
commit 111eb11440
4 changed files with 124 additions and 18 deletions

View File

@@ -10,7 +10,7 @@ Jedis is a WORK IN PROGRESS.
- Sharding - Sharding
- Persistence control commands - Persistence control commands
- Remote server control commands - Remote server control commands
- The ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE commands - The ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE commands
But stay close because things are going fast and all this will be implemented soon! But stay close because things are going fast and all this will be implemented soon!
@@ -22,7 +22,7 @@ But stay close because things are going fast and all this will be implemented so
- Commands operating on hashes - Commands operating on hashes
- Commands operating on lists - Commands operating on lists
- Commands operating on sets - Commands operating on sets
- Commands operating on sorted sets (not ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE) - Commands operating on sorted sets (not ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE)
- Transactions - Transactions
- Pipelining - Pipelining
- Publish/Subscribe - Publish/Subscribe

View File

@@ -439,4 +439,28 @@ public class Client extends Connection {
public void zcount(String key, double min, double max) { public void zcount(String key, double min, double max) {
sendCommand("ZCOUNT", key, String.valueOf(min), String.valueOf(max)); sendCommand("ZCOUNT", key, String.valueOf(min), String.valueOf(max));
} }
public void zrangeByScore(String key, double min, double max) {
sendCommand("ZRANGEBYSCORE", key, String.valueOf(min), String
.valueOf(max));
}
public void zrangeByScore(String key, double min, double max, int offset,
int count) {
sendCommand("ZRANGEBYSCORE", key, String.valueOf(min), String
.valueOf(max), "LIMIT", String.valueOf(offset), String
.valueOf(count));
}
public void zrangeByScoreWithScores(String key, double min, double max) {
sendCommand("ZRANGEBYSCORE", key, String.valueOf(min), String
.valueOf(max), "WITHSCORES");
}
public void zrangeByScoreWithScores(String key, double min, double max,
int offset, int count) {
sendCommand("ZRANGEBYSCORE", key, String.valueOf(min), String
.valueOf(max), "LIMIT", String.valueOf(offset), String
.valueOf(count), "WITHSCORES");
}
} }

View File

@@ -411,27 +411,13 @@ public class Jedis {
public Set<Tuple> zrangeWithScores(String key, int start, int end) { public Set<Tuple> zrangeWithScores(String key, int start, int end) {
client.zrangeWithScores(key, start, end); client.zrangeWithScores(key, start, end);
List<String> membersWithScores = client.getMultiBulkReply(); Set<Tuple> set = getTupledSet();
Set<Tuple> set = new LinkedHashSet<Tuple>();
Iterator<String> iterator = membersWithScores.iterator();
while (iterator.hasNext()) {
set
.add(new Tuple(iterator.next(), Double.valueOf(iterator
.next())));
}
return set; return set;
} }
public Set<Tuple> zrevrangeWithScores(String key, int start, int end) { public Set<Tuple> zrevrangeWithScores(String key, int start, int end) {
client.zrevrangeWithScores(key, start, end); client.zrevrangeWithScores(key, start, end);
List<String> membersWithScores = client.getMultiBulkReply(); Set<Tuple> set = getTupledSet();
Set<Tuple> set = new LinkedHashSet<Tuple>();
Iterator<String> iterator = membersWithScores.iterator();
while (iterator.hasNext()) {
set
.add(new Tuple(iterator.next(), Double.valueOf(iterator
.next())));
}
return set; return set;
} }
@@ -550,4 +536,40 @@ public class Jedis {
client.zcount(key, min, max); client.zcount(key, min, max);
return client.getIntegerReply(); return client.getIntegerReply();
} }
public Set<String> zrangeByScore(String key, double min, double max) {
client.zrangeByScore(key, min, max);
return new LinkedHashSet<String>(client.getMultiBulkReply());
}
public Set<String> zrangeByScore(String key, double min, double max,
int offset, int count) {
client.zrangeByScore(key, min, max, offset, count);
return new LinkedHashSet<String>(client.getMultiBulkReply());
}
public Set<Tuple> zrangeByScoreWithScores(String key, double min, double max) {
client.zrangeByScoreWithScores(key, min, max);
Set<Tuple> set = getTupledSet();
return set;
}
public Set<Tuple> zrangeByScoreWithScores(String key, double min,
double max, int offset, int count) {
client.zrangeByScoreWithScores(key, min, max, offset, count);
Set<Tuple> set = getTupledSet();
return set;
}
private Set<Tuple> getTupledSet() {
List<String> membersWithScores = client.getMultiBulkReply();
Set<Tuple> set = new LinkedHashSet<Tuple>();
Iterator<String> iterator = membersWithScores.iterator();
while (iterator.hasNext()) {
set
.add(new Tuple(iterator.next(), Double.valueOf(iterator
.next())));
}
return set;
}
} }

View File

@@ -192,4 +192,64 @@ public class SortedSetCommandsTest extends JedisCommandTestBase {
assertEquals(2, result); assertEquals(2, result);
} }
@Test
public void zrangebyscore() {
jedis.zadd("foo", 1d, "a");
jedis.zadd("foo", 10d, "b");
jedis.zadd("foo", 0.1d, "c");
jedis.zadd("foo", 2d, "a");
Set<String> range = jedis.zrangeByScore("foo", 0d, 2d);
Set<String> expected = new LinkedHashSet<String>();
expected.add("c");
expected.add("a");
assertEquals(expected, range);
range = jedis.zrangeByScore("foo", 0d, 2d, 0, 1);
expected = new LinkedHashSet<String>();
expected.add("c");
assertEquals(expected, range);
range = jedis.zrangeByScore("foo", 0d, 2d, 1, 1);
expected = new LinkedHashSet<String>();
expected.add("a");
assertEquals(expected, range);
}
@Test
public void zrangebyscoreWithScores() {
jedis.zadd("foo", 1d, "a");
jedis.zadd("foo", 10d, "b");
jedis.zadd("foo", 0.1d, "c");
jedis.zadd("foo", 2d, "a");
Set<Tuple> range = jedis.zrangeByScoreWithScores("foo", 0d, 2d);
Set<Tuple> expected = new LinkedHashSet<Tuple>();
expected.add(new Tuple("c", 0.1d));
expected.add(new Tuple("a", 2d));
assertEquals(expected, range);
range = jedis.zrangeByScoreWithScores("foo", 0d, 2d, 0, 1);
expected = new LinkedHashSet<Tuple>();
expected.add(new Tuple("c", 0.1d));
assertEquals(expected, range);
range = jedis.zrangeByScoreWithScores("foo", 0d, 2d, 1, 1);
expected = new LinkedHashSet<Tuple>();
expected.add(new Tuple("a", 2d));
assertEquals(expected, range);
}
} }