From cc26791c69b7a2a480384506127a3f65de276eea Mon Sep 17 00:00:00 2001 From: Jonathan Leibiusky Date: Tue, 29 Jun 2010 23:03:56 -0300 Subject: [PATCH] Added commands operating on sorted sets --- README.md | 4 +- pom.xml | 2 +- src/main/java/redis/clients/jedis/Jedis.java | 78 +++++++ .../tests/commands/SortedSetCommandsTest.java | 203 ++++++++++++++++++ 4 files changed, 284 insertions(+), 3 deletions(-) create mode 100644 src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java diff --git a/README.md b/README.md index ef3b51d..bb2a7e0 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,12 @@ Jedis was conceived to be EASY to use and FULLY COMPATIBLE with the latest versi Jedis is a WORK IN PROGRESS. ## What's still missing? -- Commands operating on sorted sets - Sorting - Transactions - Publish/Subscribe - Persistence control commands - Remote server control commands -- The AUTH, SORT, BLPOP and BRPOP commands +- The AUTH, SORT, BLPOP, BRPOP, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE commands But stay close because things are going fast and all this will be implemented soon! @@ -24,6 +23,7 @@ But stay close because things are going fast and all this will be implemented so - Commands operating on hashes - Commands operating on lists (not SORT, BLPOP, BRPOP) - Commands operating on sets +- Commands operating on sorted sets (not SORT, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE) ## How do I use it? diff --git a/pom.xml b/pom.xml index 920405a..84b1b9f 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 redis.clients jedis - 0.0.6 + 0.0.7 junit diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index 849abab..82a2353 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -337,4 +337,82 @@ public class Jedis extends Client { public String srandmember(String key) throws JedisException { return sendCommand("SRANDMEMBER", key).getBulkReply(); } + + public int zadd(String key, double score, String member) + throws JedisException { + return sendCommand("ZADD", key, String.valueOf(score), member) + .getIntegerReply(); + } + + public Set zrange(String key, int start, int end) + throws JedisException { + List members = sendCommand("ZRANGE", key, + String.valueOf(start), String.valueOf(end)).getMultiBulkReply(); + return new LinkedHashSet(members); + } + + public int zrem(String key, String member) throws JedisException { + return sendCommand("ZREM", key, member).getIntegerReply(); + } + + public double zincrby(String key, double score, String member) + throws JedisException { + String newscore = sendCommand("ZINCRBY", key, String.valueOf(score), + member).getBulkReply(); + return Double.valueOf(newscore); + } + + public int zrank(String key, String member) throws JedisException { + return sendCommand("ZRANK", key, member).getIntegerReply(); + } + + public int zrevrank(String key, String member) throws JedisException { + return sendCommand("ZREVRANK", key, member).getIntegerReply(); + } + + public Set zrevrange(String key, int start, int end) + throws JedisException { + List members = sendCommand("ZREVRANGE", key, + String.valueOf(start), String.valueOf(end)).getMultiBulkReply(); + return new LinkedHashSet(members); + } + + public Set zrangeWithScores(String key, int start, int end) + throws JedisException { + List membersWithScores = sendCommand("ZRANGE", key, + String.valueOf(start), String.valueOf(end), "WITHSCORES") + .getMultiBulkReply(); + Set set = new LinkedHashSet(); + Iterator iterator = membersWithScores.iterator(); + while (iterator.hasNext()) { + set + .add(new Tuple(iterator.next(), Double.valueOf(iterator + .next()))); + } + return set; + } + + public Set zrevrangeWithScores(String key, int start, int end) + throws JedisException { + List membersWithScores = sendCommand("ZREVRANGE", key, + String.valueOf(start), String.valueOf(end), "WITHSCORES") + .getMultiBulkReply(); + Set set = new LinkedHashSet(); + Iterator iterator = membersWithScores.iterator(); + while (iterator.hasNext()) { + set + .add(new Tuple(iterator.next(), Double.valueOf(iterator + .next()))); + } + return set; + } + + public int zcard(String key) throws JedisException { + return sendCommand("ZCARD", key).getIntegerReply(); + } + + public double zscore(String key, String member) throws JedisException { + String score = sendCommand("ZSCORE", key, member).getBulkReply(); + return Double.valueOf(score); + } } diff --git a/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java new file mode 100644 index 0000000..cc924ba --- /dev/null +++ b/src/test/java/redis/clients/jedis/tests/commands/SortedSetCommandsTest.java @@ -0,0 +1,203 @@ +package redis.clients.jedis.tests.commands; + +import java.util.LinkedHashSet; +import java.util.Set; + +import junit.framework.Assert; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisException; +import redis.clients.jedis.Tuple; + +public class SortedSetCommandsTest extends Assert { + private Jedis jedis; + + @Before + public void setUp() throws Exception { + jedis = new Jedis("localhost"); + jedis.connect(); + } + + @After + public void tearDown() throws Exception { + jedis.flushDB(); + jedis.disconnect(); + } + + @Test + public void zadd() throws JedisException { + int status = jedis.zadd("foo", 1d, "a"); + assertEquals(1, status); + + status = jedis.zadd("foo", 10d, "b"); + assertEquals(1, status); + + status = jedis.zadd("foo", 0.1d, "c"); + assertEquals(1, status); + + status = jedis.zadd("foo", 2d, "a"); + assertEquals(0, status); + } + + @Test + public void zrange() throws JedisException { + jedis.zadd("foo", 1d, "a"); + jedis.zadd("foo", 10d, "b"); + jedis.zadd("foo", 0.1d, "c"); + jedis.zadd("foo", 2d, "a"); + + Set expected = new LinkedHashSet(); + expected.add("c"); + expected.add("a"); + + Set range = jedis.zrange("foo", 0, 1); + assertEquals(expected, range); + + expected.add("b"); + range = jedis.zrange("foo", 0, 100); + assertEquals(expected, range); + } + + @Test + public void zrevrange() throws JedisException { + jedis.zadd("foo", 1d, "a"); + jedis.zadd("foo", 10d, "b"); + jedis.zadd("foo", 0.1d, "c"); + jedis.zadd("foo", 2d, "a"); + + Set expected = new LinkedHashSet(); + expected.add("b"); + expected.add("a"); + + Set range = jedis.zrevrange("foo", 0, 1); + assertEquals(expected, range); + + expected.add("c"); + range = jedis.zrevrange("foo", 0, 100); + assertEquals(expected, range); + } + + @Test + public void zrem() throws JedisException { + jedis.zadd("foo", 1d, "a"); + jedis.zadd("foo", 2d, "b"); + + int status = jedis.zrem("foo", "a"); + + Set expected = new LinkedHashSet(); + expected.add("b"); + + assertEquals(1, status); + assertEquals(expected, jedis.zrange("foo", 0, 100)); + + status = jedis.zrem("foo", "bar"); + + assertEquals(0, status); + } + + @Test + public void zincrby() throws JedisException { + jedis.zadd("foo", 1d, "a"); + jedis.zadd("foo", 2d, "b"); + + double score = jedis.zincrby("foo", 2d, "a"); + + Set expected = new LinkedHashSet(); + expected.add("a"); + expected.add("b"); + + assertEquals(3d, score); + assertEquals(expected, jedis.zrange("foo", 0, 100)); + } + + @Test + public void zrank() throws JedisException { + jedis.zadd("foo", 1d, "a"); + jedis.zadd("foo", 2d, "b"); + + int rank = jedis.zrank("foo", "a"); + assertEquals(0, rank); + + rank = jedis.zrank("foo", "b"); + assertEquals(1, rank); + } + + @Test + public void zrevrank() throws JedisException { + jedis.zadd("foo", 1d, "a"); + jedis.zadd("foo", 2d, "b"); + + int rank = jedis.zrevrank("foo", "a"); + assertEquals(1, rank); + + rank = jedis.zrevrank("foo", "b"); + assertEquals(0, rank); + } + + @Test + public void zrangeWithScores() throws JedisException { + jedis.zadd("foo", 1d, "a"); + jedis.zadd("foo", 10d, "b"); + jedis.zadd("foo", 0.1d, "c"); + jedis.zadd("foo", 2d, "a"); + + Set expected = new LinkedHashSet(); + expected.add(new Tuple("c", 0.1d)); + expected.add(new Tuple("a", 2d)); + + Set range = jedis.zrangeWithScores("foo", 0, 1); + assertEquals(expected, range); + + expected.add(new Tuple("b", 10d)); + range = jedis.zrangeWithScores("foo", 0, 100); + assertEquals(expected, range); + } + + @Test + public void zrevrangeWithScores() throws JedisException { + jedis.zadd("foo", 1d, "a"); + jedis.zadd("foo", 10d, "b"); + jedis.zadd("foo", 0.1d, "c"); + jedis.zadd("foo", 2d, "a"); + + Set expected = new LinkedHashSet(); + expected.add(new Tuple("b", 10d)); + expected.add(new Tuple("a", 2d)); + + Set range = jedis.zrevrangeWithScores("foo", 0, 1); + assertEquals(expected, range); + + expected.add(new Tuple("c", 0.1d)); + range = jedis.zrevrangeWithScores("foo", 0, 100); + assertEquals(expected, range); + } + + @Test + public void zcard() throws JedisException { + jedis.zadd("foo", 1d, "a"); + jedis.zadd("foo", 10d, "b"); + jedis.zadd("foo", 0.1d, "c"); + jedis.zadd("foo", 2d, "a"); + + int size = jedis.zcard("foo"); + assertEquals(3, size); + } + + @Test + public void zscore() throws JedisException { + jedis.zadd("foo", 1d, "a"); + jedis.zadd("foo", 10d, "b"); + jedis.zadd("foo", 0.1d, "c"); + jedis.zadd("foo", 2d, "a"); + + double score = jedis.zscore("foo", "b"); + assertEquals(10d, score); + + score = jedis.zscore("foo", "c"); + assertEquals(0.1d, score); + } +} \ No newline at end of file