Implemented list commands

This commit is contained in:
Jonathan Leibiusky
2010-06-22 13:48:59 -03:00
parent 034315edf7
commit 8815dbb7c6
4 changed files with 169 additions and 3 deletions

View File

@@ -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?

View File

@@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>0.0.4</version>
<version>0.0.5</version>
<dependencies>
<dependency>
<groupId>junit</groupId>

View File

@@ -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();
}
}

View File

@@ -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<String> expected = new ArrayList<String>();
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<String> expected = new ArrayList<String>();
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<String> expected = new ArrayList<String>();
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<String> expected = new ArrayList<String>();
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<String> expected = new ArrayList<String>();
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<String> srcExpected = new ArrayList<String>();
srcExpected.add("a");
srcExpected.add("b");
List<String> dstExpected = new ArrayList<String>();
dstExpected.add("c");
dstExpected.add("foo");
dstExpected.add("bar");
assertEquals(srcExpected, jedis.lrange("foo", 0, 1000));
assertEquals(dstExpected, jedis.lrange("dst", 0, 1000));
}
}