+ trying to add serializer into place

+ added more integration tests
+ added generics for RedisList
- still having serialization/deserialization problems
This commit is contained in:
Costin Leau
2010-11-10 18:47:44 +02:00
parent 05ef1afffa
commit 3dbbbaad45
14 changed files with 463 additions and 66 deletions

View File

@@ -117,6 +117,7 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
int size = getPoolSize();
pool = new JedisPool(shardInfo);
pool.setResourcesNumber(size);
pool.init();
}
}

View File

@@ -46,7 +46,7 @@ import org.springframework.util.ClassUtils;
public class RedisTemplate extends RedisAccessor {
private boolean exposeConnection = false;
private RedisSerializer<Object> converter = new SimpleRedisSerializer<Object>();
private RedisSerializer converter = new SimpleRedisSerializer();
public RedisTemplate() {
}

View File

@@ -21,9 +21,13 @@ package org.springframework.datastore.redis.serializer;
* @author Mark Pollack
* @author Costin Leau
*/
public interface RedisSerializer<T> {
public interface RedisSerializer {
byte[] serialize(T object);
byte[] serialize(Object object);
T deserialize(byte[] bytes);
String serializeAsString(Object object);
<T> T deserialize(byte[] bytes);
<T> T deserialize(String bytes);
}

View File

@@ -18,6 +18,7 @@ package org.springframework.datastore.redis.serializer;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.datastore.redis.UncategorizedRedisException;
/**
* Simple Redis serializer delegating to the default serializer in Spring 3.
@@ -25,18 +26,42 @@ import org.springframework.core.serializer.support.SerializingConverter;
* @author Mark Pollack
* @author Costin Leau
*/
public class SimpleRedisSerializer<T> implements RedisSerializer<T> {
public class SimpleRedisSerializer implements RedisSerializer {
private Converter<Object, byte[]> serializer = new SerializingConverter();
private Converter<byte[], Object> deserializer = new DeserializingConverter();
@SuppressWarnings("unchecked")
@Override
public T deserialize(byte[] bytes) {
return (T) deserializer.convert(bytes);
public <T> T deserialize(byte[] bytes) {
try {
return (T) deserializer.convert(bytes);
} catch (Exception ex) {
throw new UncategorizedRedisException("Cannot deserialize", ex);
}
}
@Override
public byte[] serialize(T object) {
return serializer.convert(object);
public <T> T deserialize(String bytes) {
// try {
return deserialize(bytes.getBytes());
// } catch (UnsupportedEncodingException ex) {
// throw new DataRetrievalFailureException("Unsupported encoding " + encoding, ex);
// }
}
}
@Override
public byte[] serialize(Object object) {
try {
return serializer.convert(object);
} catch (Exception ex) {
throw new UncategorizedRedisException("Cannot serialize", ex);
}
}
@Override
public String serializeAsString(Object object) {
return new String(serialize(object));
}
}

View File

@@ -19,20 +19,30 @@ import java.util.AbstractCollection;
import java.util.Collection;
import org.springframework.datastore.redis.connection.RedisCommands;
import org.springframework.datastore.redis.serializer.RedisSerializer;
import org.springframework.datastore.redis.serializer.SimpleRedisSerializer;
/**
* Base implementation for Redis collections.
*
* @author Costin Leau
*/
public abstract class AbstractRedisCollection extends AbstractCollection<String> implements RedisStore {
public abstract class AbstractRedisCollection<E> extends AbstractCollection<E> implements RedisStore {
public static final String ENCODING = "UTF-8";
protected final String key;
protected final RedisCommands commands;
protected final RedisSerializer serializer;
public AbstractRedisCollection(String key, RedisCommands commands) {
this(key, commands, new SimpleRedisSerializer());
}
public AbstractRedisCollection(String key, RedisCommands commands, RedisSerializer serializer) {
this.key = key;
this.commands = commands;
this.serializer = serializer;
}
@Override
@@ -41,15 +51,15 @@ public abstract class AbstractRedisCollection extends AbstractCollection<String>
}
@Override
public boolean addAll(Collection<? extends String> c) {
public boolean addAll(Collection<? extends E> c) {
boolean modified = false;
for (String string : c) {
modified |= add(string);
for (E e : c) {
modified |= add(e);
}
return modified;
}
public abstract boolean add(String e);
public abstract boolean add(E e);
public abstract void clear();
@@ -77,5 +87,4 @@ public abstract class AbstractRedisCollection extends AbstractCollection<String>
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.datastore.redis.util;
import java.util.ArrayList;
import java.util.List;
import org.springframework.datastore.redis.serializer.RedisSerializer;
/**
* Utility class used mainly for type conversion by the default collection implementations.
*
* @author Costin Leau
*/
abstract class CollectionUtils {
static <E> List<E> deserializeAsList(List<String> input, RedisSerializer serializer) {
List<E> result = new ArrayList<E>(input.size());
for (String string : input) {
E item = serializer.deserialize(string);
result.add(item);
}
return result;
}
}

View File

@@ -28,16 +28,16 @@ import org.springframework.datastore.redis.connection.RedisCommands;
*
* @author Costin Leau
*/
public class DefaultRedisList extends AbstractRedisCollection implements RedisList {
public class DefaultRedisList<E> extends AbstractRedisCollection<E> implements RedisList<E> {
private class DefaultRedisListIterator extends RedisIterator {
private class DefaultRedisListIterator<E> extends RedisIterator<E> {
public DefaultRedisListIterator(Iterator<String> delegate) {
public DefaultRedisListIterator(Iterator<E> delegate) {
super(delegate);
}
@Override
protected void removeFromRedisStorage(String item) {
protected void removeFromRedisStorage(E item) {
DefaultRedisList.this.remove(item);
}
}
@@ -47,22 +47,22 @@ public class DefaultRedisList extends AbstractRedisCollection implements RedisLi
}
@Override
public List<String> range(int start, int end) {
return commands.lRange(key, start, end);
public List<E> range(int start, int end) {
return CollectionUtils.deserializeAsList(commands.lRange(key, start, end), serializer);
}
@Override
public RedisList trim(int start, int end) {
public RedisList<E> trim(int start, int end) {
commands.lTrim(key, start, end);
return this;
}
private List<String> content() {
return commands.lRange(key, 0, -1);
private List<E> content() {
return CollectionUtils.deserializeAsList(commands.lRange(key, 0, -1), serializer);
}
@Override
public Iterator<String> iterator() {
public Iterator<E> iterator() {
return content().iterator();
}
@@ -73,8 +73,8 @@ public class DefaultRedisList extends AbstractRedisCollection implements RedisLi
@Override
public boolean add(String value) {
commands.rPush(key, value);
public boolean add(E value) {
commands.rPush(key, serializer.serializeAsString(value));
return true;
}
@@ -90,29 +90,29 @@ public class DefaultRedisList extends AbstractRedisCollection implements RedisLi
}
@Override
public void add(int index, String element) {
public void add(int index, E element) {
if (index == 0) {
commands.lPush(key, element);
commands.lPush(key, serializer.serializeAsString(element));
}
else if (index == size()) {
commands.rPush(key, element);
commands.rPush(key, serializer.serializeAsString(element));
}
throw new IllegalArgumentException("Redis supports insertion only at the beginning or the end of the list");
}
@Override
public boolean addAll(int index, Collection<? extends String> c) {
for (String string : c) {
add(index, string);
public boolean addAll(int index, Collection<? extends E> c) {
for (E e : c) {
add(index, e);
}
return true;
}
@Override
public String get(int index) {
return commands.lIndex(key, index);
public E get(int index) {
return serializer.deserialize(commands.lIndex(key, index));
}
@Override
@@ -126,37 +126,37 @@ public class DefaultRedisList extends AbstractRedisCollection implements RedisLi
}
@Override
public ListIterator<String> listIterator() {
public ListIterator<E> listIterator() {
throw new UnsupportedOperationException();
}
@Override
public ListIterator<String> listIterator(int index) {
public ListIterator<E> listIterator(int index) {
throw new UnsupportedOperationException();
}
@Override
public String remove(int index) {
public E remove(int index) {
throw new UnsupportedOperationException();
}
@Override
public String set(int index, String element) {
String object = get(index);
commands.lSet(key, index, element);
public E set(int index, E e) {
E object = get(index);
commands.lSet(key, index, serializer.serializeAsString(e));
return object;
}
@Override
public List<String> subList(int fromIndex, int toIndex) {
public List<E> subList(int fromIndex, int toIndex) {
throw new UnsupportedOperationException();
}
@Override
public String element() {
String value = peek();
public E element() {
E value = peek();
if (value == null)
throw new NoSuchElementException();
@@ -165,27 +165,27 @@ public class DefaultRedisList extends AbstractRedisCollection implements RedisLi
@Override
public boolean offer(String e) {
commands.lPush(key, e);
public boolean offer(E e) {
commands.lPush(key, serializer.serializeAsString(e));
return true;
}
@Override
public String peek() {
return commands.lIndex(key, 0);
public E peek() {
return serializer.deserialize(commands.lIndex(key, 0));
}
@Override
public String poll() {
return commands.lPop(key);
public E poll() {
return serializer.deserialize(commands.lPop(key));
}
@Override
public String remove() {
String value = poll();
public E remove() {
E value = poll();
if (value == null)
throw new NoSuchElementException();

View File

@@ -25,9 +25,9 @@ import org.springframework.datastore.redis.connection.RedisCommands;
*
* @author Costin Leau
*/
public class DefaultRedisSet extends AbstractRedisCollection implements RedisSet {
public class DefaultRedisSet extends AbstractRedisCollection<String> implements RedisSet {
private class DefaultRedisSetIterator extends RedisIterator {
private class DefaultRedisSetIterator extends RedisIterator<String> {
public DefaultRedisSetIterator(Iterator<String> delegate) {
super(delegate);

View File

@@ -27,9 +27,9 @@ import org.springframework.datastore.redis.connection.RedisCommands;
*
* @author Costin Leau
*/
class DefaultRedisSortedSet extends AbstractRedisCollection implements RedisSortedSet {
class DefaultRedisSortedSet extends AbstractRedisCollection<String> implements RedisSortedSet {
private class DefaultRedisSortedSetIterator extends RedisIterator {
private class DefaultRedisSortedSetIterator extends RedisIterator<String> {
public DefaultRedisSortedSetIterator(Iterator<String> delegate) {
super(delegate);

View File

@@ -22,18 +22,18 @@ import java.util.Iterator;
*
* @author Costin Leau
*/
abstract class RedisIterator implements Iterator<String> {
abstract class RedisIterator<E> implements Iterator<E> {
private final Iterator<String> delegate;
private final Iterator<E> delegate;
private String item;
private E item;
/**
* Constructs a new <code>RedisIterator</code> instance.
*
* @param delegate
*/
RedisIterator(Iterator<String> delegate) {
RedisIterator(Iterator<E> delegate) {
this.delegate = delegate;
}
@@ -49,7 +49,7 @@ abstract class RedisIterator implements Iterator<String> {
* @return
* @see java.util.Iterator#next()
*/
public String next() {
public E next() {
item = delegate.next();
return item;
}
@@ -64,5 +64,5 @@ abstract class RedisIterator implements Iterator<String> {
item = null;
}
protected abstract void removeFromRedisStorage(String item);
protected abstract void removeFromRedisStorage(E item);
}

View File

@@ -24,9 +24,9 @@ import java.util.Queue;
*
* @author Costin Leau
*/
public interface RedisList extends RedisStore, List<String>, Queue<String> {
public interface RedisList<E> extends RedisStore, List<E>, Queue<E> {
List<String> range(int start, int end);
List<E> range(int start, int end);
RedisList trim(int start, int end);
RedisList<E> trim(int start, int end);
}

View File

@@ -0,0 +1,130 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.datastore.redis.serializer;
import static org.junit.Assert.*;
import java.io.Serializable;
import java.util.UUID;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class SimpleRedisSerializerTest {
private static class A implements Serializable {
private Integer value = Integer.valueOf(30);
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
A other = (A) obj;
if (value == null) {
if (other.value != null)
return false;
}
else if (!value.equals(other.value))
return false;
return true;
}
}
private static class B implements Serializable {
private String name = getClass().getName();
private A a = new A();
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((a == null) ? 0 : a.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
B other = (B) obj;
if (a == null) {
if (other.a != null)
return false;
}
else if (!a.equals(other.a))
return false;
if (name == null) {
if (other.name != null)
return false;
}
else if (!name.equals(other.name))
return false;
return true;
}
}
private RedisSerializer serializer;
@Before
public void setUp() {
serializer = new SimpleRedisSerializer();
}
@After
public void tearDown() {
serializer = null;
}
@Test
public void testBasicSerializationRoundtrip() throws Exception {
Integer integer = new Integer(300);
verifySerializedObjects(new Integer(300), new Double(200), new B());
}
private void verifySerializedObjects(Object... objects) {
for (Object object : objects) {
assertEquals("Incorrectly (de)serialized object " + object, object,
serializer.deserialize(serializer.serialize(object)));
}
}
@Test
public void testStringEncodedSerialization() {
String value = UUID.randomUUID().toString();
assertEquals(value, serializer.deserialize(serializer.serializeAsString(value)));
assertEquals(value, serializer.deserialize(serializer.serializeAsString(value)));
assertEquals(value, serializer.deserialize(serializer.serializeAsString(value)));
}
}

View File

@@ -0,0 +1,143 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.datastore.redis.util;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.junit.matchers.JUnitMatchers.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* Base test for Redis collections.
*
* @author Costin Leau
*/
public abstract class AbstractRedisCollectionTest<T> {
private AbstractRedisCollection<T> collection;
@Before
public void setUp() throws Exception {
collection = getCollection();
}
abstract AbstractRedisCollection<T> getCollection();
/**
* Return a new instance of T
* @return
*/
abstract T getT();
@After
public void tearDown() throws Exception {
collection.clear();
}
@Test
public void testAdd() {
T t1 = getT();
assertThat(collection.add(t1), is(Boolean.TRUE));
assertThat(collection, hasItem(t1));
assertEquals(collection.size(), 1);
}
@SuppressWarnings("unchecked")
@Test
public void testAddAll() {
T t1 = getT();
T t2 = getT();
T t3 = getT();
List<T> list = Arrays.asList(t1, t2, t3);
assertThat(collection.addAll(list), is(Boolean.TRUE));
assertThat(collection, hasItem(t1));
assertThat(collection, hasItem(t2));
assertThat(collection, hasItem(t3));
assertEquals(collection.size(), 3);
}
public void clear() {
collection.clear();
}
public boolean contains(Object o) {
return collection.contains(o);
}
public boolean containsAll(Collection<?> c) {
return collection.containsAll(c);
}
public boolean equals(Object obj) {
return collection.equals(obj);
}
public String getKey() {
return collection.getKey();
}
public int hashCode() {
return collection.hashCode();
}
public boolean isEmpty() {
return collection.isEmpty();
}
public Iterator<T> iterator() {
return collection.iterator();
}
public boolean remove(Object o) {
return collection.remove(o);
}
public boolean removeAll(Collection<?> c) {
return collection.removeAll(c);
}
public boolean retainAll(Collection<?> c) {
return collection.retainAll(c);
}
public int size() {
return collection.size();
}
public Object[] toArray() {
return collection.toArray();
}
public <T> T[] toArray(T[] a) {
return collection.toArray(a);
}
public String toString() {
return collection.toString();
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.datastore.redis.util;
import java.util.UUID;
import org.springframework.datastore.redis.connection.jredis.JredisConnectionFactory;
/**
* String-based Redis List test.
*
* @author Costin Leau
*/
public class StringRedisListTest extends AbstractRedisCollectionTest<String> {
private DefaultRedisList<String> redisList;
public StringRedisListTest() {
JredisConnectionFactory factory = new JredisConnectionFactory();
factory.afterPropertiesSet();
redisList = new DefaultRedisList<String>(getClass().getName(), factory.getConnection());
}
@Override
AbstractRedisCollection<String> getCollection() {
return redisList;
}
@Override
String getT() {
return UUID.randomUUID().toString();
}
}