Merge branch 'master' of github.com:SpringSource/spring-data-keyvalue

This commit is contained in:
J. Brisbin
2010-11-29 13:38:32 -06:00
7 changed files with 559 additions and 32 deletions

View File

@@ -27,7 +27,6 @@ import java.util.Set;
class DefaultBoundHashOperations<H, HK, HV> extends DefaultKeyBound<H> implements BoundHashOperations<H, HK, HV> {
private final HashOperations<H, HK, HV> ops;
private RedisOperations<H, ?> template;
/**
* Constructs a new <code>DefaultBoundHashOperations</code> instance.
@@ -52,7 +51,7 @@ class DefaultBoundHashOperations<H, HK, HV> extends DefaultKeyBound<H> implement
@Override
public RedisOperations<H, ?> getOperations() {
return template;
return ops.getOperations();
}
@Override

View File

@@ -43,4 +43,6 @@ public interface HashOperations<H, HK, HV> {
void set(H key, HK hashKey, HV value);
Collection<HV> values(H key);
RedisOperations<H, ?> getOperations();
}

View File

@@ -56,7 +56,8 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
private boolean exposeConnection = false;
private RedisSerializer keySerializer = new StringRedisSerializer();
private RedisSerializer valueSerializer = new SimpleRedisSerializer();
private RedisSerializer defaultSerializer = new SimpleRedisSerializer();
private RedisSerializer hashKeySerializer = new SimpleRedisSerializer();
private RedisSerializer hashValueSerializer = new SimpleRedisSerializer();
public RedisTemplate() {
}
@@ -82,7 +83,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
public <T> T execute(RedisCallback<T> action, boolean exposeConnection) {
return execute(action, isExposeConnection(), defaultSerializer);
return execute(action, exposeConnection, valueSerializer);
}
public <T> T execute(RedisCallback<T> action, boolean exposeConnection, RedisSerializer returnSerializer) {
@@ -133,18 +134,43 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
this.exposeConnection = exposeConnection;
}
/**
* Sets the key serializer to be used by this template. Defaults to {@link SimpleRedisSerializer}.
*
* @param serializer
*/
public void setKeySerializer(RedisSerializer serializer) {
this.keySerializer = serializer;
}
/**
* Sets the value serializer to be used by this template. Defaults to {@link SimpleRedisSerializer}.
*
* @param serializer
*/
public void setValueSerializer(RedisSerializer serializer) {
this.valueSerializer = serializer;
}
public void setDefaultSerializer(RedisSerializer serializer) {
this.defaultSerializer = serializer;
/**
* Sets the hash key (or field) serializer to be used by this template. Defaults to {@link SimpleRedisSerializer}.
*
* @param hashKeySerializer The hashKeySerializer to set.
*/
public void setHashKeySerializer(RedisSerializer hashKeySerializer) {
this.hashKeySerializer = hashKeySerializer;
}
/**
* Sets the hash value serializer to be used by this template. Defaults to {@link SimpleRedisSerializer}.
*
* @param hashValueSerializer The hashValueSerializer to set.
*/
public void setHashValueSerializer(RedisSerializer hashValueSerializer) {
this.hashValueSerializer = hashValueSerializer;
}
/**
* Invocation handler that suppresses close calls on JDO PersistenceManagers.
* Also prepares returned Query objects.
@@ -202,37 +228,76 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
return rawKeys;
}
private <HK> byte[] rawHashKey(HK value) {
return (value != null ? hashKeySerializer.serialize(value) : null);
}
private <HV> byte[] rawHashValue(HV value) {
return (value != null ? hashValueSerializer.serialize(value) : null);
}
@SuppressWarnings("unchecked")
private <T extends Collection<V>> T values(Collection<byte[]> rawValues, Class<? extends Collection> type) {
Collection<V> values = (List.class.isAssignableFrom(type) ? new ArrayList<V>(rawValues.size())
: new LinkedHashSet<V>(rawValues.size()));
for (byte[] bs : rawValues) {
values.add((V) valueSerializer.deserialize(bs));
if (bs != null) {
values.add((V) valueSerializer.deserialize(bs));
}
}
return (T) values;
}
@SuppressWarnings("unchecked")
private <H> Collection<H> arbitraryValues(Collection<byte[]> rawValues, Class<? extends Collection> type) {
private <H> Collection<H> hashValues(Collection<byte[]> rawValues, Class<? extends Collection> type) {
Collection<H> values = (List.class.isAssignableFrom(type) ? new ArrayList<H>(rawValues.size())
: new LinkedHashSet<H>(rawValues.size()));
for (byte[] bs : rawValues) {
values.add((H) valueSerializer.deserialize(bs));
if (bs != null) {
values.add((H) hashValueSerializer.deserialize(bs));
}
}
return values;
}
@SuppressWarnings("unchecked")
private K deserializeKey(byte[] value) {
return (K) deserialize(value, keySerializer);
}
@SuppressWarnings("unchecked")
private V deserializeValue(byte[] value) {
return (V) deserialize(value, valueSerializer);
}
@SuppressWarnings("unchecked")
private <HK> HK deserializeHashKey(byte[] value) {
return (HK) deserialize(value, hashKeySerializer);
}
@SuppressWarnings("unchecked")
private <HV> HV deserializeHashValue(byte[] value) {
return (HV) deserialize(value, hashValueSerializer);
}
private <T> T deserialize(byte[] value, RedisSerializer<T> serializer) {
if (isEmpty(value)) {
return null;
}
return (T) serializer.deserialize(value);
}
private static boolean isEmpty(byte[] data) {
return (data == null || data.length == 0);
}
// utility methods for the template internal methods
private abstract class ValueDeserializingRedisCallback implements RedisCallback<V> {
private K key;
public ValueDeserializingRedisCallback() {
this(null);
}
public ValueDeserializingRedisCallback(K key) {
this.key = key;
}
@@ -241,10 +306,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
@Override
public final V doInRedis(RedisConnection connection) {
byte[] result = inRedis(rawKey(key), connection);
if (result != null) {
return (V) valueSerializer.deserialize(result);
}
return null;
return deserializeValue(result);
}
protected abstract byte[] inRedis(byte[] rawKey, RedisConnection connection);
@@ -558,7 +620,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
public void diffAndStore(final K key, K destKey, final K... keys) {
final byte[][] rawKeys = rawKeys(aggregateKeys(key, keys));
final byte[] rawDestKey = rawKey(destKey);
Object rawValues = execute(new RedisCallback<Object>() {
execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) {
connection.sDiffStore(rawDestKey, rawKeys);
@@ -881,10 +943,15 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
private class DefaultHashOperations<HK, HV> implements HashOperations<K, HK, HV> {
@Override
public RedisOperations<K, ?> getOperations() {
return RedisTemplate.this;
}
@Override
public HV get(K key, Object hashKey) {
final byte[] rawKey = rawKey(key);
final byte[] rawHashKey = rawValue(hashKey);
final byte[] rawHashKey = rawHashKey(hashKey);
byte[] rawHashValue = execute(new RedisCallback<byte[]>() {
@Override
@@ -893,13 +960,13 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
}, true);
return (HV) valueSerializer.deserialize(rawHashValue);
return (HV) deserializeHashValue(rawHashValue);
}
@Override
public Boolean hasKey(K key, Object hashKey) {
final byte[] rawKey = rawKey(key);
final byte[] rawHashKey = rawValue(hashKey);
final byte[] rawHashKey = rawHashKey(hashKey);
return execute(new RedisCallback<Boolean>() {
@Override
@@ -912,7 +979,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
@Override
public Integer increment(K key, HK hashKey, final int delta) {
final byte[] rawKey = rawKey(key);
final byte[] rawHashKey = rawValue(hashKey);
final byte[] rawHashKey = rawHashKey(hashKey);
return execute(new RedisCallback<Integer>() {
@Override
@@ -934,7 +1001,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
}, true);
return (Set<HK>) arbitraryValues(rawValues, Set.class);
return (Set<HK>) hashValues(rawValues, Set.class);
}
@Override
@@ -951,12 +1018,16 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
@Override
public void multiSet(K key, Map<? extends HK, ? extends HV> m) {
if (m.isEmpty()) {
return;
}
final byte[] rawKey = rawKey(key);
final Map<byte[], byte[]> hashes = new LinkedHashMap<byte[], byte[]>(m.size());
for (Map.Entry<byte[], byte[]> entry : hashes.entrySet()) {
hashes.put(rawValue(entry.getKey()), rawValue(entry.getValue()));
for (Map.Entry<? extends HK, ? extends HV> entry : m.entrySet()) {
hashes.put(rawHashKey(entry.getKey()), rawHashValue(entry.getValue()));
}
execute(new RedisCallback<Object>() {
@@ -971,8 +1042,8 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
@Override
public void set(K key, HK hashKey, HV value) {
final byte[] rawKey = rawKey(key);
final byte[] rawHashKey = rawValue(hashKey);
final byte[] rawHashValue = rawValue(value);
final byte[] rawHashKey = rawHashKey(hashKey);
final byte[] rawHashValue = rawHashValue(value);
execute(new RedisCallback<Object>() {
@Override
@@ -994,13 +1065,13 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
}
}, true);
return (List<HV>) arbitraryValues(rawValues, List.class);
return (List<HV>) hashValues(rawValues, List.class);
}
@Override
public void delete(K key, Object hashKey) {
final byte[] rawKey = rawKey(key);
final byte[] rawHashKey = rawValue(hashKey);
final byte[] rawHashKey = rawHashKey(hashKey);
execute(new RedisCallback<Object>() {
@Override

View File

@@ -31,8 +31,8 @@ public class SimpleRedisSerializer implements RedisSerializer<Object> {
private Converter<Object, byte[]> serializer = new SerializingConverter();
private Converter<byte[], Object> deserializer = new DeserializingConverter();
private sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
private sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
// private sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
// private sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
@SuppressWarnings("unchecked")
@Override

View File

@@ -31,10 +31,21 @@ public class DefaultRedisMap<K, V> implements RedisMap<K, V> {
private final BoundHashOperations<String, K, V> hashOps;
/**
* Constructs a new <code>DefaultRedisMap</code> instance.
*
* @param key
* @param operations
*/
public DefaultRedisMap(String key, RedisOperations<String, ?> operations) {
this.hashOps = operations.forHash(key);
}
/**
* Constructs a new <code>DefaultRedisMap</code> instance.
*
* @param boundOps
*/
public DefaultRedisMap(BoundHashOperations<String, K, V> boundOps) {
this.hashOps = boundOps;
}
@@ -126,4 +137,31 @@ public class DefaultRedisMap<K, V> implements RedisMap<K, V> {
public Collection<V> values() {
return hashOps.values();
}
@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (o instanceof RedisMap) {
return o.hashCode() == hashCode();
}
return false;
}
@Override
public int hashCode() {
int result = 17 + getClass().hashCode();
result = result * 31 + getKey().hashCode();
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("RedisStore for key:");
sb.append(getKey());
return sb.toString();
}
}

View File

@@ -0,0 +1,351 @@
/*
* 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.data.keyvalue.redis.util;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.junit.matchers.JUnitMatchers.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.Map.Entry;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.keyvalue.redis.connection.RedisConnection;
import org.springframework.data.keyvalue.redis.connection.RedisConnectionFactory;
import org.springframework.data.keyvalue.redis.core.RedisCallback;
import org.springframework.data.keyvalue.redis.core.RedisOperations;
import org.springframework.data.keyvalue.redis.core.RedisTemplate;
/**
* Integration test for Redis Map.
*
* @author Costin Leau
*/
@RunWith(Parameterized.class)
public abstract class AbstractRedisMapTests<K, V> {
protected RedisMap<K, V> map;
protected ObjectFactory<K> keyFactory;
protected ObjectFactory<V> valueFactory;
protected RedisTemplate template;
private static Set<RedisConnectionFactory> connFactories = new LinkedHashSet<RedisConnectionFactory>();
abstract RedisMap<K, V> createMap();
@Before
public void setUp() throws Exception {
map = createMap();
}
public AbstractRedisMapTests(ObjectFactory<K> keyFactory, ObjectFactory<V> valueFactory, RedisTemplate template) {
this.keyFactory = keyFactory;
this.valueFactory = valueFactory;
this.template = template;
connFactories.add(template.getConnectionFactory());
}
@AfterClass
public static void cleanUp() {
if (connFactories != null) {
for (RedisConnectionFactory connectionFactory : connFactories) {
try {
((DisposableBean) connectionFactory).destroy();
System.out.println("Succesfully cleaned up factory " + connectionFactory);
} catch (Exception ex) {
System.err.println("Cannot clean factory " + connectionFactory + ex);
}
}
}
}
protected K getKey() {
return keyFactory.instance();
}
protected V getValue() {
return valueFactory.instance();
}
protected RedisStore<String> copyStore(RedisStore<String> store) {
return new DefaultRedisMap(store.getKey(), store.getOperations());
}
@After
public void tearDown() throws Exception {
// remove the collection entirely since clear() doesn't always work
map.getOperations().delete(map.getKey());
template.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) {
connection.flushDb();
return null;
}
});
}
@Test
public void testClear() {
map.clear();
assertEquals(0, map.size());
map.put(getKey(), getValue());
assertEquals(1, map.size());
map.clear();
assertEquals(0, map.size());
}
@Test
public void testContainsKey() {
K k1 = getKey();
K k2 = getKey();
assertFalse(map.containsKey(k1));
assertFalse(map.containsKey(k2));
map.put(k1, getValue());
assertTrue(map.containsKey(k1));
map.put(k2, getValue());
assertTrue(map.containsKey(k2));
}
@Test(expected = UnsupportedOperationException.class)
public void testContainsValue() {
V v1 = getValue();
V v2 = getValue();
assertFalse(map.containsValue(v1));
assertFalse(map.containsValue(v2));
map.put(getKey(), v1);
assertTrue(map.containsValue(v1));
map.put(getKey(), v2);
assertTrue(map.containsValue(v2));
}
public Set<Entry<K, V>> entrySet() {
return map.entrySet();
}
@Test
public void testEquals() {
RedisStore<String> clone = copyStore(map);
assertEquals(clone, map);
assertEquals(clone, clone);
assertEquals(map, map);
}
@Test
public void testNotEquals() {
RedisOperations<String, ?> ops = map.getOperations();
RedisStore<String> newInstance = new DefaultRedisMap<K, V>(ops.<K, V> forHash(map.getKey() + ":new"));
assertFalse(map.equals(newInstance));
assertFalse(newInstance.equals(map));
}
@Test
public void testGet() {
K k1 = getKey();
V v1 = getValue();
assertNull(map.get(UUID.randomUUID()));
assertNull(map.get(k1));
map.put(k1, v1);
assertEquals(v1, map.get(k1));
}
@Test
public void testGetKey() {
assertNotNull(map.getKey());
}
@Test
public void testGetOperations() {
assertEquals(template, map.getOperations());
}
@Test
public void testHashCode() {
assertThat(map.hashCode(), not(equalTo(map.getKey().hashCode())));
assertEquals(map.hashCode(), copyStore(map).hashCode());
}
@Test(expected = InvalidDataAccessApiUsageException.class)
public void testIncrement() {
K k1 = getKey();
V v1 = getValue();
map.put(k1, v1);
Integer value = map.increment(k1, 1);
System.out.println("Value is " + value);
}
@Test
public void testIsEmpty() {
map.clear();
assertTrue(map.isEmpty());
map.put(getKey(), getValue());
assertFalse(map.isEmpty());
map.clear();
assertTrue(map.isEmpty());
}
@Test
public void testKeySet() {
map.clear();
assertTrue(map.keySet().isEmpty());
K k1 = getKey();
K k2 = getKey();
K k3 = getKey();
map.put(k1, getValue());
map.put(k2, getValue());
map.put(k3, getValue());
Iterator<K> iterator = map.keySet().iterator();
assertEquals(k1, iterator.next());
assertEquals(k2, iterator.next());
assertEquals(k3, iterator.next());
assertFalse(iterator.hasNext());
}
@Test
public void testPut() {
K k1 = getKey();
K k2 = getKey();
V v1 = getValue();
V v2 = getValue();
map.put(k1, v1);
map.put(k2, v2);
assertEquals(v1, map.get(k1));
assertEquals(v2, map.get(k2));
}
@Test
public void testPutAll() {
Map<K, V> m = new LinkedHashMap<K, V>();
K k1 = getKey();
K k2 = getKey();
V v1 = getValue();
V v2 = getValue();
m.put(k1, v1);
m.put(k2, v2);
assertNull(map.get(k1));
assertNull(map.get(k2));
map.putAll(m);
assertEquals(v1, map.get(k1));
assertEquals(v2, map.get(k2));
}
@Test
public void testPutIfAbsent() {
K k1 = getKey();
K k2 = getKey();
V v1 = getValue();
V v2 = getValue();
assertNull(map.get(k1));
assertTrue(map.putIfAbsent(k1, v1));
assertFalse(map.putIfAbsent(k1, v2));
assertEquals(v1, map.get(k1));
assertTrue(map.putIfAbsent(k2, v2));
assertFalse(map.putIfAbsent(k2, v1));
assertEquals(v2, map.get(k2));
}
@Test
public void testRemove() {
K k1 = getKey();
K k2 = getKey();
V v1 = getValue();
V v2 = getValue();
assertNull(map.remove(k1));
assertNull(map.remove(k2));
map.put(k1, v1);
map.put(k2, v2);
assertEquals(v1, map.remove(k1));
assertNull(map.remove(k1));
assertNull(map.get(k1));
assertEquals(v2, map.remove(k2));
assertNull(map.remove(k2));
assertNull(map.get(k2));
}
@Test
public void testSize() {
assertEquals(0, map.size());
map.put(getKey(), getValue());
assertEquals(1, map.size());
K k = getKey();
map.put(k, getValue());
assertEquals(2, map.size());
map.remove(k);
assertEquals(1, map.size());
map.clear();
assertEquals(0, map.size());
}
@Test
public void testValues() {
V v1 = getValue();
V v2 = getValue();
V v3 = getValue();
map.put(getKey(), v1);
map.put(getKey(), v2);
Collection<V> values = map.values();
assertEquals(2, values.size());
assertThat(values, hasItems(v1, v2));
map.put(getKey(), v3);
values = map.values();
assertEquals(3, values.size());
assertThat(values, hasItems(v1, v2, v3));
}
@Test(expected = UnsupportedOperationException.class)
public void testEntrySet() {
map.entrySet();
}
}

View File

@@ -0,0 +1,66 @@
/*
* 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.data.keyvalue.redis.util;
import java.util.Arrays;
import java.util.Collection;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.data.keyvalue.redis.Person;
import org.springframework.data.keyvalue.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.keyvalue.redis.core.RedisTemplate;
/**
* Integration test for RedisMap.
*
* @author Costin Leau
*/
public class RedisMapTests extends AbstractRedisMapTests<Object, Object> {
public RedisMapTests(ObjectFactory<Object> keyFactory, ObjectFactory<Object> valueFactory, RedisTemplate template) {
super(keyFactory, valueFactory, template);
}
@Override
RedisMap<Object, Object> createMap() {
String redisName = getClass().getName();
return new DefaultRedisMap<Object, Object>(redisName, template);
}
@Parameters
public static Collection<Object[]> testParams() {
// create Jedis Factory
ObjectFactory<String> stringFactory = new StringObjectFactory();
ObjectFactory<Person> personFactory = new PersonObjectFactory();
JedisConnectionFactory jedisConnFactory = new JedisConnectionFactory();
jedisConnFactory.setPooling(false);
jedisConnFactory.afterPropertiesSet();
RedisTemplate<String, String> stringTemplate = new RedisTemplate<String, String>(jedisConnFactory);
RedisTemplate<String, Person> personTemplate = new RedisTemplate<String, Person>(jedisConnFactory);
// JredisConnectionFactory jredisConnFactory = new JredisConnectionFactory();
// jredisConnFactory.setPooling(false);
// jredisConnFactory.afterPropertiesSet();
//
// RedisTemplate<String, String> stringTemplateJR = new RedisTemplate<String, String>(jredisConnFactory);
// RedisTemplate<String, Person> personTemplateJR = new RedisTemplate<String, Person>(jredisConnFactory);
return Arrays.asList(new Object[][] { { stringFactory, stringFactory, stringTemplate },
{ personFactory, personFactory, personTemplate } });
}
}