Added hash commands

This commit is contained in:
Jonathan Leibiusky
2010-06-13 02:11:24 -03:00
parent 14dc0399f2
commit fdf40d8fda
4 changed files with 258 additions and 2 deletions

View File

@@ -10,7 +10,6 @@ Jedis is a WORK IN PROGRESS.
- Commands operating on lists
- Commands operating on sets
- Commands operating on sorted sets
- Commands operating on hashes
- Sorting
- Transactions
- Publish/Subscribe
@@ -24,6 +23,7 @@ But stay close because things are going fast and all this will be implemented so
- Connection handling (not AUTH)
- Commands operating on all the kind of values
- Commands operating on string values
- Commands operating on hashes
## 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.3</version>
<version>0.0.4</version>
<dependencies>
<dependency>
<groupId>junit</groupId>

View File

@@ -1,6 +1,10 @@
package redis.clients.jedis;
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) {
@@ -139,4 +143,77 @@ public class Jedis extends Client {
return sendCommand("SUBSTR", key, String.valueOf(start),
String.valueOf(end)).getBulkReply();
}
public int hset(String key, String field, String value)
throws JedisException {
return sendCommand("HSET", key, field, value).getIntegerReply();
}
public String hget(String key, String field) throws JedisException {
return sendCommand("HGET", key, field).getBulkReply();
}
public int hsetnx(String key, String field, String value)
throws JedisException {
return sendCommand("HSETNX", key, field, value).getIntegerReply();
}
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();
}
return sendCommand("HMSET", params).getStatusCodeReply();
}
public List<String> hmget(String key, String... fields)
throws JedisException {
String[] params = new String[fields.length + 1];
params[0] = key;
System.arraycopy(fields, 0, params, 1, fields.length);
return sendCommand("HMGET", params).getMultiBulkReply();
}
public int hincrBy(String key, String field, int value)
throws JedisException {
return sendCommand("HINCRBY", key, field, String.valueOf(value))
.getIntegerReply();
}
public int hexists(String key, String field) throws JedisException {
return sendCommand("HEXISTS", key, field).getIntegerReply();
}
public int hdel(String key, String field) throws JedisException {
return sendCommand("HDEL", key, field).getIntegerReply();
}
public int hlen(String key) throws JedisException {
return sendCommand("HLEN", key).getIntegerReply();
}
public List<String> hkeys(String key) throws JedisException {
return sendCommand("HKEYS", key).getMultiBulkReply();
}
public List<String> hvals(String key) throws JedisException {
return sendCommand("HVALS", key).getMultiBulkReply();
}
public Map<String, String> hgetAll(String key) throws JedisException {
List<String> flatHash = sendCommand("HGETALL", key).getMultiBulkReply();
Map<String, String> hash = new HashMap<String, String>();
Iterator<String> iterator = flatHash.iterator();
while (iterator.hasNext()) {
hash.put(iterator.next(), iterator.next());
}
return hash;
}
}

View File

@@ -0,0 +1,179 @@
package redis.clients.jedis.tests.commands;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
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 HashesCommandsTest 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 hset() throws JedisException {
int status = jedis.hset("foo", "bar", "car");
assertEquals(1, status);
status = jedis.hset("foo", "bar", "foo");
assertEquals(0, status);
}
@Test
public void hget() throws JedisException {
jedis.hset("foo", "bar", "car");
assertEquals(null, jedis.hget("bar", "foo"));
assertEquals(null, jedis.hget("foo", "car"));
assertEquals("car", jedis.hget("foo", "bar"));
}
@Test
public void hsetnx() throws JedisException {
int status = jedis.hsetnx("foo", "bar", "car");
assertEquals(1, status);
assertEquals("car", jedis.hget("foo", "bar"));
status = jedis.hsetnx("foo", "bar", "foo");
assertEquals(0, status);
assertEquals("car", jedis.hget("foo", "bar"));
status = jedis.hsetnx("foo", "car", "bar");
assertEquals(1, status);
assertEquals("bar", jedis.hget("foo", "car"));
}
@Test
public void hmset() throws JedisException {
Map<String, String> hash = new HashMap<String, String>();
hash.put("bar", "car");
hash.put("car", "bar");
String status = jedis.hmset("foo", hash);
assertEquals("OK", status);
assertEquals("car", jedis.hget("foo", "bar"));
assertEquals("bar", jedis.hget("foo", "car"));
}
@Test
public void hmget() throws JedisException {
Map<String, String> hash = new HashMap<String, String>();
hash.put("bar", "car");
hash.put("car", "bar");
jedis.hmset("foo", hash);
List<String> values = jedis.hmget("foo", "bar", "car", "foo");
List<String> expected = new ArrayList<String>();
expected.add("car");
expected.add("bar");
expected.add(null);
assertEquals(expected, values);
}
@Test
public void hincrBy() throws JedisException {
int value = jedis.hincrBy("foo", "bar", 1);
assertEquals(1, value);
value = jedis.hincrBy("foo", "bar", -1);
assertEquals(0, value);
value = jedis.hincrBy("foo", "bar", -10);
assertEquals(-10, value);
}
@Test
public void hexists() throws JedisException {
Map<String, String> hash = new HashMap<String, String>();
hash.put("bar", "car");
hash.put("car", "bar");
jedis.hmset("foo", hash);
assertEquals(0, jedis.hexists("bar", "foo"));
assertEquals(0, jedis.hexists("foo", "foo"));
assertEquals(1, jedis.hexists("foo", "bar"));
}
@Test
public void hdel() throws JedisException {
Map<String, String> hash = new HashMap<String, String>();
hash.put("bar", "car");
hash.put("car", "bar");
jedis.hmset("foo", hash);
assertEquals(0, jedis.hdel("bar", "foo"));
assertEquals(0, jedis.hdel("foo", "foo"));
assertEquals(1, jedis.hdel("foo", "bar"));
assertEquals(null, jedis.hget("foo", "bar"));
}
@Test
public void hlen() throws JedisException {
Map<String, String> hash = new HashMap<String, String>();
hash.put("bar", "car");
hash.put("car", "bar");
jedis.hmset("foo", hash);
assertEquals(0, jedis.hlen("bar"));
assertEquals(2, jedis.hlen("foo"));
}
@Test
public void hkeys() throws JedisException {
Map<String, String> hash = new LinkedHashMap<String, String>();
hash.put("bar", "car");
hash.put("car", "bar");
jedis.hmset("foo", hash);
List<String> keys = jedis.hkeys("foo");
List<String> expected = new ArrayList<String>();
expected.add("bar");
expected.add("car");
assertEquals(expected, keys);
}
@Test
public void hvals() throws JedisException {
Map<String, String> hash = new LinkedHashMap<String, String>();
hash.put("bar", "car");
hash.put("car", "bar");
jedis.hmset("foo", hash);
List<String> vals = jedis.hvals("foo");
List<String> expected = new ArrayList<String>();
expected.add("car");
expected.add("bar");
assertEquals(expected, vals);
}
@Test
public void hgetAll() throws JedisException {
Map<String, String> h = new HashMap<String, String>();
h.put("bar", "car");
h.put("car", "bar");
jedis.hmset("foo", h);
Map<String, String> hash = jedis.hgetAll("foo");
Map<String, String> expected = new HashMap<String, String>();
expected.put("bar", "car");
expected.put("car", "bar");
assertEquals(expected, hash);
}
}