+ solved serialization problem for jedis
+ improve integration tests to better cleanup in case of failure
This commit is contained in:
@@ -35,11 +35,9 @@ import org.springframework.datastore.redis.connection.RedisConnection;
|
||||
public class JredisConnection implements RedisConnection {
|
||||
|
||||
private final JRedis jredis;
|
||||
private final String encoding;
|
||||
|
||||
public JredisConnection(JRedis jredis, String encoding) {
|
||||
public JredisConnection(JRedis jredis) {
|
||||
this.jredis = jredis;
|
||||
this.encoding = encoding;
|
||||
}
|
||||
|
||||
protected DataAccessException convertJedisAccessException(Exception ex) {
|
||||
@@ -206,7 +204,7 @@ public class JredisConnection implements RedisConnection {
|
||||
@Override
|
||||
public String get(String key) {
|
||||
try {
|
||||
return JredisUtils.convertToString(jredis.get(key), encoding);
|
||||
return JredisUtils.convertToString(jredis.get(key));
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
@@ -224,7 +222,7 @@ public class JredisConnection implements RedisConnection {
|
||||
@Override
|
||||
public String getSet(String key, String value) {
|
||||
try {
|
||||
return JredisUtils.convertToString(jredis.getset(key, value), encoding);
|
||||
return JredisUtils.convertToString(jredis.getset(key, value));
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
@@ -243,7 +241,7 @@ public class JredisConnection implements RedisConnection {
|
||||
@Override
|
||||
public List<String> mGet(String... keys) {
|
||||
try {
|
||||
return JredisUtils.convertToStringCollection(jredis.mget(keys), encoding, List.class);
|
||||
return JredisUtils.convertToStringCollection(jredis.mget(keys), List.class);
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
@@ -284,7 +282,7 @@ public class JredisConnection implements RedisConnection {
|
||||
@Override
|
||||
public String substr(String key, int start, int end) {
|
||||
try {
|
||||
return JredisUtils.convertToString(jredis.substr(key, (long) start, (long) end), encoding);
|
||||
return JredisUtils.convertToString(jredis.substr(key, (long) start, (long) end));
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
@@ -343,7 +341,7 @@ public class JredisConnection implements RedisConnection {
|
||||
@Override
|
||||
public String lIndex(String key, int index) {
|
||||
try {
|
||||
return JredisUtils.convertToString(jredis.lindex(key, (long) index), encoding);
|
||||
return JredisUtils.convertToString(jredis.lindex(key, (long) index));
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
@@ -361,7 +359,7 @@ public class JredisConnection implements RedisConnection {
|
||||
@Override
|
||||
public String lPop(String key) {
|
||||
try {
|
||||
return JredisUtils.convertToString(jredis.lpop(key), encoding);
|
||||
return JredisUtils.convertToString(jredis.lpop(key));
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
@@ -382,7 +380,7 @@ public class JredisConnection implements RedisConnection {
|
||||
try {
|
||||
List<byte[]> lrange = jredis.lrange(key, start, end);
|
||||
|
||||
return JredisUtils.convertToStringCollection(lrange, encoding, List.class);
|
||||
return JredisUtils.convertToStringCollection(lrange, List.class);
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
@@ -419,7 +417,7 @@ public class JredisConnection implements RedisConnection {
|
||||
@Override
|
||||
public String rPop(String key) {
|
||||
try {
|
||||
return JredisUtils.convertToString(jredis.rpop(key), encoding);
|
||||
return JredisUtils.convertToString(jredis.rpop(key));
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
@@ -428,7 +426,7 @@ public class JredisConnection implements RedisConnection {
|
||||
@Override
|
||||
public String rPopLPush(String srcKey, String dstKey) {
|
||||
try {
|
||||
return JredisUtils.convertToString(jredis.rpoplpush(srcKey, dstKey), encoding);
|
||||
return JredisUtils.convertToString(jredis.rpoplpush(srcKey, dstKey));
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
@@ -473,7 +471,7 @@ public class JredisConnection implements RedisConnection {
|
||||
|
||||
try {
|
||||
List<byte[]> result = jredis.sdiff(set1, sets);
|
||||
return JredisUtils.convertToStringCollection(result, encoding, Set.class);
|
||||
return JredisUtils.convertToStringCollection(result, Set.class);
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
@@ -498,7 +496,7 @@ public class JredisConnection implements RedisConnection {
|
||||
|
||||
try {
|
||||
List<byte[]> result = jredis.sinter(set1, sets);
|
||||
return JredisUtils.convertToStringCollection(result, encoding, Set.class);
|
||||
return JredisUtils.convertToStringCollection(result, Set.class);
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
@@ -528,7 +526,7 @@ public class JredisConnection implements RedisConnection {
|
||||
@Override
|
||||
public Set<String> sMembers(String key) {
|
||||
try {
|
||||
return JredisUtils.convertToStringCollection(jredis.smembers(key), encoding, Set.class);
|
||||
return JredisUtils.convertToStringCollection(jredis.smembers(key), Set.class);
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
@@ -546,7 +544,7 @@ public class JredisConnection implements RedisConnection {
|
||||
@Override
|
||||
public String sPop(String key) {
|
||||
try {
|
||||
return JredisUtils.convertToString(jredis.spop(key), encoding);
|
||||
return JredisUtils.convertToString(jredis.spop(key));
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
@@ -555,7 +553,7 @@ public class JredisConnection implements RedisConnection {
|
||||
@Override
|
||||
public String sRandMember(String key) {
|
||||
try {
|
||||
return JredisUtils.convertToString(jredis.srandmember(key), encoding);
|
||||
return JredisUtils.convertToString(jredis.srandmember(key));
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
@@ -577,7 +575,7 @@ public class JredisConnection implements RedisConnection {
|
||||
|
||||
try {
|
||||
List<byte[]> result = jredis.sunion(set1, sets);
|
||||
return JredisUtils.convertToStringCollection(result, encoding, Set.class);
|
||||
return JredisUtils.convertToStringCollection(result, Set.class);
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
@@ -649,8 +647,7 @@ public class JredisConnection implements RedisConnection {
|
||||
@Override
|
||||
public Set<String> zRange(String key, int start, int end) {
|
||||
try {
|
||||
return JredisUtils.convertToStringCollection(jredis.zrange(key, (long) start, (long) end), encoding,
|
||||
Set.class);
|
||||
return JredisUtils.convertToStringCollection(jredis.zrange(key, (long) start, (long) end), Set.class);
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
@@ -665,7 +662,7 @@ public class JredisConnection implements RedisConnection {
|
||||
@Override
|
||||
public Set<String> zRangeByScore(String key, double min, double max) {
|
||||
try {
|
||||
return JredisUtils.convertToStringCollection(jredis.zrangebyscore(key, min, max), encoding, Set.class);
|
||||
return JredisUtils.convertToStringCollection(jredis.zrangebyscore(key, min, max), Set.class);
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
@@ -725,7 +722,7 @@ public class JredisConnection implements RedisConnection {
|
||||
@Override
|
||||
public Set<String> zRevRange(String key, int start, int end) {
|
||||
try {
|
||||
return JredisUtils.convertToStringCollection(jredis.zrevrange(key, start, end), encoding, Set.class);
|
||||
return JredisUtils.convertToStringCollection(jredis.zrevrange(key, start, end), Set.class);
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
@@ -790,7 +787,7 @@ public class JredisConnection implements RedisConnection {
|
||||
@Override
|
||||
public String hGet(String key, String field) {
|
||||
try {
|
||||
return JredisUtils.convertToString(jredis.hget(key, field), encoding);
|
||||
return JredisUtils.convertToString(jredis.hget(key, field));
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
@@ -799,7 +796,7 @@ public class JredisConnection implements RedisConnection {
|
||||
@Override
|
||||
public Set<Entry> hGetAll(String key) {
|
||||
try {
|
||||
return JredisUtils.convert(jredis.hgetall(key), encoding);
|
||||
return JredisUtils.convert(jredis.hgetall(key));
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
@@ -855,7 +852,7 @@ public class JredisConnection implements RedisConnection {
|
||||
@Override
|
||||
public List<String> hVals(String key) {
|
||||
try {
|
||||
return JredisUtils.convertToStringCollection(jredis.hvals(key), encoding, List.class);
|
||||
return JredisUtils.convertToStringCollection(jredis.hvals(key), List.class);
|
||||
} catch (RedisException ex) {
|
||||
throw JredisUtils.convertJredisAccessException(ex);
|
||||
}
|
||||
|
||||
@@ -36,7 +36,6 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
public class JredisConnectionFactory implements InitializingBean, DisposableBean, RedisConnectionFactory {
|
||||
|
||||
private String encoding = "UTF-8";
|
||||
private ConnectionSpec connectionSpec;
|
||||
|
||||
private String password;
|
||||
@@ -117,7 +116,7 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean
|
||||
|
||||
@Override
|
||||
public RedisConnection getConnection() {
|
||||
return new JredisConnection((usePool ? pool : new JRedisClient(connectionSpec)), getEncoding());
|
||||
return new JredisConnection((usePool ? pool : new JRedisClient(connectionSpec)));
|
||||
}
|
||||
|
||||
|
||||
@@ -126,22 +125,6 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the encoding.
|
||||
*
|
||||
* @return Returns the encoding
|
||||
*/
|
||||
public String getEncoding() {
|
||||
return encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param encoding The encoding to set.
|
||||
*/
|
||||
public void setEncoding(String encoding) {
|
||||
this.encoding = encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the password
|
||||
*/
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.datastore.redis.connection.jredis;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -28,7 +27,6 @@ import java.util.Set;
|
||||
import org.jredis.RedisException;
|
||||
import org.jredis.RedisType;
|
||||
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;
|
||||
@@ -45,27 +43,19 @@ public abstract class JredisUtils {
|
||||
return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
static String convertToString(byte[] bytes, String encoding) {
|
||||
try {
|
||||
return new String(bytes, encoding);
|
||||
} catch (UnsupportedEncodingException ex) {
|
||||
throw new DataRetrievalFailureException("Unsupported encoding " + encoding, ex);
|
||||
}
|
||||
static String convertToString(byte[] bytes) {
|
||||
return new String(bytes);
|
||||
}
|
||||
|
||||
static <T extends Collection<String>> T convertToStringCollection(List<byte[]> bytes, String encoding, Class<T> collectionType) {
|
||||
static <T extends Collection<String>> T convertToStringCollection(List<byte[]> bytes, Class<T> collectionType) {
|
||||
|
||||
Collection<String> col = (List.class.isAssignableFrom(collectionType) ? new ArrayList<String>(bytes.size())
|
||||
: new LinkedHashSet<String>(bytes.size()));
|
||||
|
||||
try {
|
||||
for (byte[] bs : bytes) {
|
||||
col.add(new String(bs, encoding));
|
||||
}
|
||||
return (T) col;
|
||||
} catch (UnsupportedEncodingException ex) {
|
||||
throw new DataRetrievalFailureException("Unsupported encoding " + encoding, ex);
|
||||
for (byte[] bs : bytes) {
|
||||
col.add(new String(bs));
|
||||
}
|
||||
return (T) col;
|
||||
}
|
||||
|
||||
static DataType convertDataType(RedisType type) {
|
||||
@@ -87,14 +77,10 @@ public abstract class JredisUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
static Set<Entry> convert(Map<String, byte[]> map, String encoding) {
|
||||
static Set<Entry> convert(Map<String, byte[]> map) {
|
||||
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);
|
||||
for (Map.Entry<String, byte[]> entry : map.entrySet()) {
|
||||
entries.add(new DefaultEntry(entry.getKey(), new String(entry.getValue())));
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
@@ -50,6 +50,11 @@ public abstract class AbstractRedisCollection<E> extends AbstractCollection<E> i
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisCommands getCommands() {
|
||||
return commands;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends E> c) {
|
||||
boolean modified = false;
|
||||
|
||||
@@ -88,6 +88,11 @@ public class DefaultRedisMap implements RedisMap {
|
||||
return redisKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisCommands getCommands() {
|
||||
return commands;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
throw new UnsupportedOperationException();
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.datastore.redis.util;
|
||||
|
||||
import org.springframework.datastore.redis.connection.RedisCommands;
|
||||
|
||||
|
||||
/**
|
||||
* Basic interface for Redis-based collections.
|
||||
@@ -29,4 +31,11 @@ public interface RedisStore {
|
||||
* @return Redis key
|
||||
*/
|
||||
String getKey();
|
||||
|
||||
/**
|
||||
* Returns the underlying Redis commands used by the backing implementation.
|
||||
*
|
||||
* @return commands
|
||||
*/
|
||||
RedisCommands getCommands();
|
||||
}
|
||||
|
||||
@@ -54,7 +54,9 @@ public abstract class AbstractRedisCollectionTest<T> {
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
collection.clear();
|
||||
// remove the collection entirely since clear() doesn't always work
|
||||
collection.getCommands().del(collection.getKey());
|
||||
//collection.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -62,7 +64,7 @@ public abstract class AbstractRedisCollectionTest<T> {
|
||||
T t1 = getT();
|
||||
assertThat(collection.add(t1), is(Boolean.TRUE));
|
||||
assertThat(collection, hasItem(t1));
|
||||
assertEquals(collection.size(), 1);
|
||||
assertEquals(1, collection.size());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -81,8 +83,13 @@ public abstract class AbstractRedisCollectionTest<T> {
|
||||
assertEquals(collection.size(), 3);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
@Test
|
||||
public void testClear() {
|
||||
T t1 = getT();
|
||||
collection.add(t1);
|
||||
assertEquals(1, collection.size());
|
||||
collection.clear();
|
||||
assertEquals(0, collection.size());
|
||||
}
|
||||
|
||||
public boolean contains(Object o) {
|
||||
|
||||
@@ -17,7 +17,8 @@ package org.springframework.datastore.redis.util;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.datastore.redis.connection.jredis.JredisConnectionFactory;
|
||||
import org.springframework.datastore.redis.connection.RedisCommands;
|
||||
import org.springframework.datastore.redis.connection.jedis.JedisConnectionFactory;
|
||||
|
||||
|
||||
/**
|
||||
@@ -30,9 +31,25 @@ public class StringRedisListTest extends AbstractRedisCollectionTest<String> {
|
||||
private DefaultRedisList<String> redisList;
|
||||
|
||||
public StringRedisListTest() {
|
||||
JredisConnectionFactory factory = new JredisConnectionFactory();
|
||||
JedisConnectionFactory factory = new JedisConnectionFactory();
|
||||
factory.afterPropertiesSet();
|
||||
redisList = new DefaultRedisList<String>(getClass().getName(), factory.getConnection());
|
||||
String redisName = getClass().getName();
|
||||
RedisCommands commands = factory.getConnection();
|
||||
redisList = new DefaultRedisList<String>(redisName, commands);
|
||||
|
||||
|
||||
// SimpleRedisSerializer serializer = new SimpleRedisSerializer();
|
||||
//
|
||||
// String t = getT();
|
||||
//
|
||||
// String data = serializer.serializeAsString(t);
|
||||
// String name = "some-list";
|
||||
// System.out.println(data);
|
||||
// commands.lPush(name, data);
|
||||
// List<String> readData = commands.lRange(name, 0, -1);
|
||||
// System.out.println(readData);
|
||||
// System.out.println(serializer.deserialize(readData.get(0)));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -45,3 +62,4 @@ public class StringRedisListTest extends AbstractRedisCollectionTest<String> {
|
||||
return UUID.randomUUID().toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user