Implemented first List commands
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class Jedis extends Client {
|
||||
public Jedis(String host) {
|
||||
@@ -160,16 +160,15 @@ public class Jedis extends Client {
|
||||
|
||||
public String hmset(String key, Map<String, String> hash)
|
||||
throws JedisException {
|
||||
String[] params = new String[(hash.size() * 2) + 1];
|
||||
params[0] = key;
|
||||
Iterator<Entry<String, String>> iterator = hash.entrySet().iterator();
|
||||
int i = 1;
|
||||
while (iterator.hasNext()) {
|
||||
Entry<String, String> entry = iterator.next();
|
||||
params[i++] = entry.getKey();
|
||||
params[i++] = entry.getValue();
|
||||
List<String> params = new ArrayList<String>();
|
||||
params.add(key);
|
||||
|
||||
for (String field : hash.keySet()) {
|
||||
params.add(field);
|
||||
params.add(hash.get(field));
|
||||
}
|
||||
return sendCommand("HMSET", params).getStatusCodeReply();
|
||||
return sendCommand("HMSET", params.toArray(new String[params.size()]))
|
||||
.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public List<String> hmget(String key, String... fields)
|
||||
@@ -216,4 +215,22 @@ public class Jedis extends Client {
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
public int rpush(String key, String string) throws JedisException {
|
||||
return sendCommand("RPUSH", key, string).getIntegerReply();
|
||||
}
|
||||
|
||||
public int lpush(String key, String string) throws JedisException {
|
||||
return sendCommand("LPUSH", key, string).getIntegerReply();
|
||||
}
|
||||
|
||||
public int llen(String key) throws JedisException {
|
||||
return sendCommand("LLEN", key).getIntegerReply();
|
||||
}
|
||||
|
||||
public List<String> lrange(String key, int start, int end)
|
||||
throws JedisException {
|
||||
return sendCommand("LRANGE", key, String.valueOf(start),
|
||||
String.valueOf(end)).getMultiBulkReply();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package redis.clients.jedis.tests.commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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;
|
||||
|
||||
public class ListCommandsTest 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 rpush() throws JedisException {
|
||||
int size = jedis.rpush("foo", "bar");
|
||||
assertEquals(1, size);
|
||||
size = jedis.rpush("foo", "foo");
|
||||
assertEquals(2, size);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lpush() throws JedisException {
|
||||
int size = jedis.lpush("foo", "bar");
|
||||
assertEquals(1, size);
|
||||
size = jedis.lpush("foo", "foo");
|
||||
assertEquals(2, size);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void llen() throws JedisException {
|
||||
assertEquals(0, jedis.llen("foo"));
|
||||
jedis.lpush("foo", "bar");
|
||||
jedis.lpush("foo", "car");
|
||||
assertEquals(2, jedis.llen("foo"));
|
||||
}
|
||||
|
||||
@Test(expected = JedisException.class)
|
||||
public void llenNotOnList() throws JedisException {
|
||||
jedis.set("foo", "bar");
|
||||
jedis.llen("foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lrange() throws JedisException {
|
||||
jedis.rpush("foo", "a");
|
||||
jedis.rpush("foo", "b");
|
||||
jedis.rpush("foo", "c");
|
||||
|
||||
List<String> expected = new ArrayList<String>();
|
||||
expected.add("a");
|
||||
expected.add("b");
|
||||
expected.add("c");
|
||||
|
||||
List<String> range = jedis.lrange("foo", 0, 2);
|
||||
assertEquals(expected, range);
|
||||
|
||||
range = jedis.lrange("foo", 0, 20);
|
||||
assertEquals(expected, range);
|
||||
|
||||
expected = new ArrayList<String>();
|
||||
expected.add("b");
|
||||
expected.add("c");
|
||||
|
||||
range = jedis.lrange("foo", 1, 2);
|
||||
assertEquals(expected, range);
|
||||
|
||||
expected = new ArrayList<String>();
|
||||
range = jedis.lrange("foo", 2, 1);
|
||||
assertEquals(expected, range);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user