+ add Jredis support for hash commands

+ minor adjustment to hash commands interface
This commit is contained in:
Costin Leau
2010-11-09 17:33:08 +02:00
parent 2b903fe850
commit 0ae7385f45
5 changed files with 110 additions and 11 deletions

View File

@@ -32,7 +32,7 @@ public interface RedisHashCommands {
public String getValue();
}
Integer hSet(String key, String field, String value);
Boolean hSet(String key, String field, String value);
String hGet(String key, String field);

View File

@@ -317,13 +317,13 @@ public class JedisConnection implements RedisConnection {
}
@Override
public Integer hSet(String key, String field, String value) {
public Boolean hSet(String key, String field, String value) {
try {
if (isQueueing()) {
transaction.hset(key, field, value);
return null;
}
return jedis.hset(key, field, value);
return JedisUtils.convertCodeReply(jedis.hset(key, field, value));
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}

View File

@@ -95,11 +95,11 @@ public abstract class JedisUtils {
}
static Map<String, String> convert(String[] fields, String[] values) {
Map<String, String> arg = new LinkedHashMap<String, String>(fields.length);
Map<String, String> result = new LinkedHashMap<String, String>(fields.length);
for (int i = 0; i < values.length; i++) {
arg.put(fields[i], values[i]);
result.put(fields[i], values[i]);
}
return arg;
return result;
}
}

View File

@@ -17,6 +17,7 @@ package org.springframework.datastore.redis.connection.jredis;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
@@ -694,6 +695,11 @@ public class JredisConnection implements RedisConnection {
}
}
//
// Hash commands
//
@Override
public Integer zUnionStore(String destKey, Aggregate aggregate, int[] weights, String... sets) {
throw new UnsupportedOperationException();
@@ -704,13 +710,90 @@ public class JredisConnection implements RedisConnection {
throw new UnsupportedOperationException();
}
//
// Hash commands
//
@Override
public Boolean hDel(String key, String field) {
try {
return jredis.hdel(key, field);
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
}
@Override
public Integer hSet(String key, String field, String value) {
public Boolean hExists(String key, String field) {
try {
return jredis.hexists(key, field);
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
}
@Override
public String hGet(String key, String field) {
try {
return JredisUtils.convertToString(jredis.hget(key, field), encoding);
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
}
@Override
public Set<Entry> hGetAll(String key) {
try {
return JredisUtils.convert(jredis.hgetall(key), encoding);
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
}
@Override
public Integer hIncrBy(String key, String field, int delta) {
throw new UnsupportedOperationException();
}
@Override
public Set<String> hKeys(String key) {
try {
return new LinkedHashSet<String>(jredis.hkeys(key));
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
}
@Override
public Integer hLen(String key) {
try {
return Integer.valueOf((int) jredis.hlen(key));
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
}
@Override
public List<String> hMGet(String key, String... fields) {
throw new UnsupportedOperationException();
}
@Override
public void hMSet(String key, String[] fields, String[] values) {
throw new UnsupportedOperationException();
}
@Override
public Boolean hSet(String key, String field, String value) {
try {
return jredis.hset(key, field, value);
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
}
@Override
public List<String> hVals(String key) {
try {
return JredisUtils.convertToStringCollection(jredis.hvals(key), encoding, List.class);
} catch (RedisException ex) {
throw JredisUtils.convertJredisAccessException(ex);
}
}
}

View File

@@ -21,6 +21,8 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jredis.RedisException;
import org.jredis.RedisType;
@@ -28,6 +30,8 @@ import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.datastore.redis.connection.DataType;
import org.springframework.datastore.redis.connection.DefaultEntry;
import org.springframework.datastore.redis.connection.RedisHashCommands.Entry;
/**
* Helper class featuring methods for JRedis connection handling, providing support for exception translation.
@@ -81,4 +85,16 @@ public abstract class JredisUtils {
return null;
}
static Set<Entry> convert(Map<String, byte[]> map, String encoding) {
Set<Entry> entries = new LinkedHashSet<Entry>(map.size());
try {
for (Map.Entry<String, byte[]> entry : map.entrySet()) {
entries.add(new DefaultEntry(entry.getKey(), new String(entry.getValue(), encoding)));
}
} catch (UnsupportedEncodingException ex) {
throw new DataRetrievalFailureException("Unsupported encoding " + encoding, ex);
}
return entries;
}
}