From 8815dbb7c63d81a8e0a918f61170e8d82a505701 Mon Sep 17 00:00:00 2001 From: Jonathan Leibiusky Date: Tue, 22 Jun 2010 13:48:59 -0300 Subject: [PATCH] Implemented list commands --- README.md | 4 +- pom.xml | 2 +- src/main/java/redis/clients/jedis/Jedis.java | 32 +++++ .../tests/commands/ListCommandsTest.java | 134 ++++++++++++++++++ 4 files changed, 169 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e9d43fd..436cda8 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,6 @@ 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 lists - Commands operating on sets - Commands operating on sorted sets - Sorting @@ -15,7 +14,7 @@ Jedis is a WORK IN PROGRESS. - Publish/Subscribe - Persistence control commands - Remote server control commands -- And the AUTH command +- The AUTH, SORT, BLPOP and BRPOP 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 all the kind of values - Commands operating on string values - Commands operating on hashes +- Commands operating on lists (not SORT, BLPOP, BRPOP) ## How do I use it? diff --git a/pom.xml b/pom.xml index 540b1ec..1fa8da8 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 redis.clients jedis - 0.0.4 + 0.0.5 junit diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index 7d38af0..60d6ea0 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -233,4 +233,36 @@ public class Jedis extends Client { return sendCommand("LRANGE", key, String.valueOf(start), String.valueOf(end)).getMultiBulkReply(); } + + public String ltrim(String key, int start, int end) throws JedisException { + return sendCommand("LTRIM", key, String.valueOf(start), + String.valueOf(end)).getStatusCodeReply(); + } + + public String lindex(String key, int index) throws JedisException { + return sendCommand("LINDEX", key, String.valueOf(index)).getBulkReply(); + } + + public String lset(String key, int index, String value) + throws JedisException { + return sendCommand("LSET", key, String.valueOf(index), value) + .getStatusCodeReply(); + } + + public int lrem(String key, int count, String value) throws JedisException { + return sendCommand("LREM", key, String.valueOf(count), value) + .getIntegerReply(); + } + + public String lpop(String key) throws JedisException { + return sendCommand("LPOP", key).getBulkReply(); + } + + public String rpop(String key) throws JedisException { + return sendCommand("RPOP", key).getBulkReply(); + } + + public String rpoplpush(String srckey, String dstkey) throws JedisException { + return sendCommand("RPOPLPUSH", srckey, dstkey).getBulkReply(); + } } diff --git a/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java index b6f8fe9..d45dab0 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java @@ -86,4 +86,138 @@ public class ListCommandsTest extends Assert { assertEquals(expected, range); } + @Test + public void ltrim() throws JedisException { + jedis.lpush("foo", "1"); + jedis.lpush("foo", "2"); + jedis.lpush("foo", "3"); + String status = jedis.ltrim("foo", 0, 1); + + List expected = new ArrayList(); + expected.add("3"); + expected.add("2"); + + assertEquals("OK", status); + assertEquals(2, jedis.llen("foo")); + assertEquals(expected, jedis.lrange("foo", 0, 100)); + } + + @Test + public void lindex() throws JedisException { + jedis.lpush("foo", "1"); + jedis.lpush("foo", "2"); + jedis.lpush("foo", "3"); + + List expected = new ArrayList(); + expected.add("3"); + expected.add("bar"); + expected.add("1"); + + String status = jedis.lset("foo", 1, "bar"); + + assertEquals("OK", status); + assertEquals(expected, jedis.lrange("foo", 0, 100)); + } + + @Test + public void lset() throws JedisException { + jedis.lpush("foo", "1"); + jedis.lpush("foo", "2"); + jedis.lpush("foo", "3"); + + assertEquals("3", jedis.lindex("foo", 0)); + assertEquals(null, jedis.lindex("foo", 100)); + } + + @Test + public void lrem() throws JedisException { + jedis.lpush("foo", "hello"); + jedis.lpush("foo", "hello"); + jedis.lpush("foo", "x"); + jedis.lpush("foo", "hello"); + jedis.lpush("foo", "c"); + jedis.lpush("foo", "b"); + jedis.lpush("foo", "a"); + + int count = jedis.lrem("foo", -2, "hello"); + + List expected = new ArrayList(); + expected.add("a"); + expected.add("b"); + expected.add("c"); + expected.add("hello"); + expected.add("x"); + + assertEquals(2, count); + assertEquals(expected, jedis.lrange("foo", 0, 1000)); + assertEquals(0, jedis.lrem("bar", 100, "foo")); + } + + @Test + public void lpop() throws JedisException { + jedis.rpush("foo", "a"); + jedis.rpush("foo", "b"); + jedis.rpush("foo", "c"); + + String element = jedis.lpop("foo"); + assertEquals("a", element); + + List expected = new ArrayList(); + expected.add("b"); + expected.add("c"); + + assertEquals(expected, jedis.lrange("foo", 0, 1000)); + jedis.lpop("foo"); + jedis.lpop("foo"); + + element = jedis.lpop("foo"); + assertEquals(null, element); + } + + @Test + public void rpop() throws JedisException { + jedis.rpush("foo", "a"); + jedis.rpush("foo", "b"); + jedis.rpush("foo", "c"); + + String element = jedis.rpop("foo"); + assertEquals("c", element); + + List expected = new ArrayList(); + expected.add("a"); + expected.add("b"); + + assertEquals(expected, jedis.lrange("foo", 0, 1000)); + jedis.rpop("foo"); + jedis.rpop("foo"); + + element = jedis.rpop("foo"); + assertEquals(null, element); + } + + @Test + public void rpoplpush() throws JedisException { + jedis.rpush("foo", "a"); + jedis.rpush("foo", "b"); + jedis.rpush("foo", "c"); + + jedis.rpush("dst", "foo"); + jedis.rpush("dst", "bar"); + + String element = jedis.rpoplpush("foo", "dst"); + + assertEquals("c", element); + + List srcExpected = new ArrayList(); + srcExpected.add("a"); + srcExpected.add("b"); + + List dstExpected = new ArrayList(); + dstExpected.add("c"); + dstExpected.add("foo"); + dstExpected.add("bar"); + + assertEquals(srcExpected, jedis.lrange("foo", 0, 1000)); + assertEquals(dstExpected, jedis.lrange("dst", 0, 1000)); + } }