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

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