From fdf40d8fda9a539a2f75457db13da9d214c22123 Mon Sep 17 00:00:00 2001 From: Jonathan Leibiusky Date: Sun, 13 Jun 2010 02:11:24 -0300 Subject: [PATCH] Added hash commands --- README.md | 2 +- pom.xml | 2 +- src/main/java/redis/clients/jedis/Jedis.java | 77 ++++++++ .../tests/commands/HashesCommandsTest.java | 179 ++++++++++++++++++ 4 files changed, 258 insertions(+), 2 deletions(-) create mode 100644 src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java diff --git a/README.md b/README.md index d213087..eae5229 100644 --- a/README.md +++ b/README.md @@ -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? diff --git a/pom.xml b/pom.xml index 5022c1d..540b1ec 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 redis.clients jedis - 0.0.3 + 0.0.4 junit diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index c4619fa..8fbc570 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -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 hash) + throws JedisException { + String[] params = new String[(hash.size() * 2) + 1]; + params[0] = key; + Iterator> iterator = hash.entrySet().iterator(); + int i = 1; + while (iterator.hasNext()) { + Entry entry = iterator.next(); + params[i++] = entry.getKey(); + params[i++] = entry.getValue(); + } + return sendCommand("HMSET", params).getStatusCodeReply(); + } + + public List 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 hkeys(String key) throws JedisException { + return sendCommand("HKEYS", key).getMultiBulkReply(); + } + + public List hvals(String key) throws JedisException { + return sendCommand("HVALS", key).getMultiBulkReply(); + } + + public Map hgetAll(String key) throws JedisException { + List flatHash = sendCommand("HGETALL", key).getMultiBulkReply(); + Map hash = new HashMap(); + Iterator iterator = flatHash.iterator(); + while (iterator.hasNext()) { + hash.put(iterator.next(), iterator.next()); + } + + return hash; + } } diff --git a/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java new file mode 100644 index 0000000..2e1db6c --- /dev/null +++ b/src/test/java/redis/clients/jedis/tests/commands/HashesCommandsTest.java @@ -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 hash = new HashMap(); + 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 hash = new HashMap(); + hash.put("bar", "car"); + hash.put("car", "bar"); + jedis.hmset("foo", hash); + + List values = jedis.hmget("foo", "bar", "car", "foo"); + List expected = new ArrayList(); + 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 hash = new HashMap(); + 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 hash = new HashMap(); + 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 hash = new HashMap(); + 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 hash = new LinkedHashMap(); + hash.put("bar", "car"); + hash.put("car", "bar"); + jedis.hmset("foo", hash); + + List keys = jedis.hkeys("foo"); + List expected = new ArrayList(); + expected.add("bar"); + expected.add("car"); + assertEquals(expected, keys); + } + + @Test + public void hvals() throws JedisException { + Map hash = new LinkedHashMap(); + hash.put("bar", "car"); + hash.put("car", "bar"); + jedis.hmset("foo", hash); + + List vals = jedis.hvals("foo"); + List expected = new ArrayList(); + expected.add("car"); + expected.add("bar"); + assertEquals(expected, vals); + } + + @Test + public void hgetAll() throws JedisException { + Map h = new HashMap(); + h.put("bar", "car"); + h.put("car", "bar"); + jedis.hmset("foo", h); + + Map hash = jedis.hgetAll("foo"); + Map expected = new HashMap(); + expected.put("bar", "car"); + expected.put("car", "bar"); + assertEquals(expected, hash); + } + +}