Merge branch 'master' into srp

This commit is contained in:
Costin Leau
2012-06-21 20:05:41 +03:00
24 changed files with 720 additions and 561 deletions

View File

@@ -26,7 +26,7 @@ import org.w3c.dom.Element;
*
* @author Costin Leau
*/
public class RedisCollectionParser extends AbstractSimpleBeanDefinitionParser {
class RedisCollectionParser extends AbstractSimpleBeanDefinitionParser {
protected Class<?> getBeanClass(Element element) {

View File

@@ -372,8 +372,8 @@ public class DefaultStringRedisConnection implements StringRedisConnection {
return delegate.sDiff(keys);
}
public void sDiffStore(byte[] destKey, byte[]... keys) {
delegate.sDiffStore(destKey, keys);
public Long sDiffStore(byte[] destKey, byte[]... keys) {
return delegate.sDiffStore(destKey, keys);
}
public void select(int dbIndex) {
@@ -412,8 +412,8 @@ public class DefaultStringRedisConnection implements StringRedisConnection {
return delegate.sInter(keys);
}
public void sInterStore(byte[] destKey, byte[]... keys) {
delegate.sInterStore(destKey, keys);
public Long sInterStore(byte[] destKey, byte[]... keys) {
return delegate.sInterStore(destKey, keys);
}
public Boolean sIsMember(byte[] key, byte[] value) {
@@ -460,8 +460,8 @@ public class DefaultStringRedisConnection implements StringRedisConnection {
return delegate.sUnion(keys);
}
public void sUnionStore(byte[] destKey, byte[]... keys) {
delegate.sUnionStore(destKey, keys);
public Long sUnionStore(byte[] destKey, byte[]... keys) {
return delegate.sUnionStore(destKey, keys);
}
public Long ttl(byte[] key) {
@@ -1152,4 +1152,17 @@ public class DefaultStringRedisConnection implements StringRedisConnection {
public void openPipeline() {
delegate.openPipeline();
}
public Object execute(String command) {
return execute(command, (byte[][]) null);
}
public Object execute(String command, byte[]... args) {
return delegate.execute(command, args);
}
public Object execute(String command, String... args) {
return execute(command, serializeMulti(args));
}
}

View File

@@ -25,4 +25,16 @@ package org.springframework.data.redis.connection;
public interface RedisCommands extends RedisKeyCommands, RedisStringCommands, RedisListCommands, RedisSetCommands,
RedisZSetCommands, RedisHashCommands, RedisTxCommands, RedisPubSubCommands, RedisConnectionCommands,
RedisServerCommands {
/**
* 'Native' or 'raw' execution of the given command along-side the given arguments.
* The command is executed as is, with as little 'interpretation' as possible - it is up to the caller
* to take care of any processing of arguments or the result.
*
* @param command Command to execute
* @param args Possible command arguments (may be null)
* @return execution result.
*/
Object execute(String command, byte[]... args);
}

View File

@@ -39,15 +39,15 @@ public interface RedisSetCommands {
Set<byte[]> sInter(byte[]... keys);
void sInterStore(byte[] destKey, byte[]... keys);
Long sInterStore(byte[] destKey, byte[]... keys);
Set<byte[]> sUnion(byte[]... keys);
void sUnionStore(byte[] destKey, byte[]... keys);
Long sUnionStore(byte[] destKey, byte[]... keys);
Set<byte[]> sDiff(byte[]... keys);
void sDiffStore(byte[] destKey, byte[]... keys);
Long sDiffStore(byte[] destKey, byte[]... keys);
Set<byte[]> sMembers(byte[] key);

View File

@@ -42,6 +42,10 @@ public interface StringRedisConnection extends RedisConnection {
String getValueAsString();
}
Object execute(String command, String... args);
Object execute(String command);
Boolean exists(String key);
Long del(String... keys);

View File

@@ -58,8 +58,8 @@ public abstract class JedisUtils {
private static final String OK_CODE = "OK";
private static final String OK_MULTI_CODE = "+OK";
private static final byte[] ONE = new byte[] { 1 };
private static final byte[] ZERO = new byte[] { 0 };
private static final byte[] ONE = new byte[] { '1' };
private static final byte[] ZERO = new byte[] { '0' };
/**
* Converts the given, native Jedis exception to Spring's DAO hierarchy.

View File

@@ -53,23 +53,32 @@ abstract class AbstractOperations<K, V> {
protected abstract byte[] inRedis(byte[] rawKey, RedisConnection connection);
}
RedisSerializer keySerializer = null;
RedisSerializer valueSerializer = null;
RedisSerializer hashKeySerializer = null;
RedisSerializer hashValueSerializer = null;
RedisSerializer stringSerializer = null;
RedisTemplate<K, V> template;
AbstractOperations(RedisTemplate<K, V> template) {
keySerializer = template.getKeySerializer();
valueSerializer = template.getValueSerializer();
hashKeySerializer = template.getHashKeySerializer();
hashValueSerializer = template.getHashValueSerializer();
stringSerializer = template.getStringSerializer();
this.template = template;
}
RedisSerializer keySerializer() {
return template.getKeySerializer();
}
RedisSerializer valueSerializer() {
return template.getValueSerializer();
}
RedisSerializer hashKeySerializer() {
return template.getHashKeySerializer();
}
RedisSerializer hashValueSerializer() {
return template.getHashValueSerializer();
}
RedisSerializer stringSerializer() {
return template.getStringSerializer();
}
<T> T execute(RedisCallback<T> callback, boolean b) {
return template.execute(callback, b);
@@ -82,27 +91,27 @@ abstract class AbstractOperations<K, V> {
@SuppressWarnings("unchecked")
byte[] rawKey(Object key) {
Assert.notNull(key, "non null key required");
return keySerializer.serialize(key);
return keySerializer().serialize(key);
}
byte[] rawString(String key) {
return stringSerializer.serialize(key);
return stringSerializer().serialize(key);
}
@SuppressWarnings("unchecked")
byte[] rawValue(Object value) {
return valueSerializer.serialize(value);
return valueSerializer().serialize(value);
}
@SuppressWarnings("unchecked")
<HK> byte[] rawHashKey(HK hashKey) {
Assert.notNull(hashKey, "non null hash key required");
return hashKeySerializer.serialize(hashKey);
return hashKeySerializer().serialize(hashKey);
}
@SuppressWarnings("unchecked")
<HV> byte[] rawHashValue(HV value) {
return hashValueSerializer.serialize(value);
return hashValueSerializer().serialize(value);
}
byte[][] rawKeys(K key, K otherKey) {
@@ -136,31 +145,31 @@ abstract class AbstractOperations<K, V> {
@SuppressWarnings("unchecked")
Set<V> deserializeValues(Set<byte[]> rawValues) {
return SerializationUtils.deserialize(rawValues, valueSerializer);
return SerializationUtils.deserialize(rawValues, valueSerializer());
}
@SuppressWarnings("unchecked")
Set<TypedTuple<V>> deserializeTupleValues(Set<Tuple> rawValues) {
Set<TypedTuple<V>> set = new LinkedHashSet<TypedTuple<V>>(rawValues.size());
for (Tuple rawValue : rawValues) {
set.add(new DefaultTypedTuple(valueSerializer.deserialize(rawValue.getValue()), rawValue.getScore()));
set.add(new DefaultTypedTuple(valueSerializer().deserialize(rawValue.getValue()), rawValue.getScore()));
}
return set;
}
@SuppressWarnings("unchecked")
List<V> deserializeValues(List<byte[]> rawValues) {
return SerializationUtils.deserialize(rawValues, valueSerializer);
return SerializationUtils.deserialize(rawValues, valueSerializer());
}
@SuppressWarnings("unchecked")
<T> Set<T> deserializeHashKeys(Set<byte[]> rawKeys) {
return SerializationUtils.deserialize(rawKeys, hashKeySerializer);
return SerializationUtils.deserialize(rawKeys, hashKeySerializer());
}
@SuppressWarnings("unchecked")
<T> List<T> deserializeHashValues(List<byte[]> rawValues) {
return SerializationUtils.deserialize(rawValues, hashValueSerializer);
return SerializationUtils.deserialize(rawValues, hashValueSerializer());
}
@SuppressWarnings("unchecked")
@@ -181,25 +190,25 @@ abstract class AbstractOperations<K, V> {
@SuppressWarnings("unchecked")
K deserializeKey(byte[] value) {
return (K) keySerializer.deserialize(value);
return (K) keySerializer().deserialize(value);
}
@SuppressWarnings("unchecked")
V deserializeValue(byte[] value) {
return (V) valueSerializer.deserialize(value);
return (V) valueSerializer().deserialize(value);
}
String deserializeString(byte[] value) {
return (String) stringSerializer.deserialize(value);
return (String) stringSerializer().deserialize(value);
}
@SuppressWarnings( { "unchecked" })
<HK> HK deserializeHashKey(byte[] value) {
return (HK) hashKeySerializer.deserialize(value);
return (HK) hashKeySerializer().deserialize(value);
}
@SuppressWarnings("unchecked")
<HV> HV deserializeHashValue(byte[] value) {
return (HV) hashValueSerializer.deserialize(value);
return (HV) hashValueSerializer().deserialize(value);
}
}

View File

@@ -58,7 +58,7 @@ class DefaultBoundZSetOperations<K, V> extends DefaultBoundKeyOperations<K> impl
}
public void intersectAndStore(K destKey, K otherKey) {
public void intersectAndStore(K otherKey, K destKey) {
ops.intersectAndStore(getKey(), otherKey, destKey);
}

View File

@@ -49,8 +49,6 @@ class DefaultSetOperations<K, V> extends AbstractOperations<K, V> implements Set
return difference(key, Collections.singleton(otherKey));
}
@SuppressWarnings("unchecked")
public Set<V> difference(final K key, final Collection<K> otherKeys) {
final byte[][] rawKeys = rawKeys(key, otherKeys);
Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
@@ -64,19 +62,18 @@ class DefaultSetOperations<K, V> extends AbstractOperations<K, V> implements Set
}
public void differenceAndStore(K key, K otherKey, K destKey) {
differenceAndStore(key, Collections.singleton(otherKey), destKey);
public Long differenceAndStore(K key, K otherKey, K destKey) {
return differenceAndStore(key, Collections.singleton(otherKey), destKey);
}
public void differenceAndStore(final K key, final Collection<K> otherKeys, K destKey) {
public Long differenceAndStore(final K key, final Collection<K> otherKeys, K destKey) {
final byte[][] rawKeys = rawKeys(key, otherKeys);
final byte[] rawDestKey = rawKey(destKey);
execute(new RedisCallback<Object>() {
return execute(new RedisCallback<Long>() {
public Object doInRedis(RedisConnection connection) {
connection.sDiffStore(rawDestKey, rawKeys);
return null;
public Long doInRedis(RedisConnection connection) {
return connection.sDiffStore(rawDestKey, rawKeys);
}
}, true);
}
@@ -86,8 +83,6 @@ class DefaultSetOperations<K, V> extends AbstractOperations<K, V> implements Set
return intersect(key, Collections.singleton(otherKey));
}
@SuppressWarnings("unchecked")
public Set<V> intersect(K key, Collection<K> otherKeys) {
final byte[][] rawKeys = rawKeys(key, otherKeys);
Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
@@ -101,17 +96,17 @@ class DefaultSetOperations<K, V> extends AbstractOperations<K, V> implements Set
}
public void intersectAndStore(K key, K otherKey, K destKey) {
intersectAndStore(key, Collections.singleton(otherKey), destKey);
public Long intersectAndStore(K key, K otherKey, K destKey) {
return intersectAndStore(key, Collections.singleton(otherKey), destKey);
}
public void intersectAndStore(K key, Collection<K> otherKeys, K destKey) {
public Long intersectAndStore(K key, Collection<K> otherKeys, K destKey) {
final byte[][] rawKeys = rawKeys(key, otherKeys);
final byte[] rawDestKey = rawKey(destKey);
execute(new RedisCallback<Object>() {
return execute(new RedisCallback<Long>() {
public Object doInRedis(RedisConnection connection) {
public Long doInRedis(RedisConnection connection) {
connection.sInterStore(rawDestKey, rawKeys);
return null;
}
@@ -130,8 +125,6 @@ class DefaultSetOperations<K, V> extends AbstractOperations<K, V> implements Set
}, true);
}
@SuppressWarnings("unchecked")
public Set<V> members(K key) {
final byte[] rawKey = rawKey(key);
Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
@@ -222,19 +215,18 @@ class DefaultSetOperations<K, V> extends AbstractOperations<K, V> implements Set
}
public void unionAndStore(K key, K otherKey, K destKey) {
unionAndStore(key, Collections.singleton(otherKey), destKey);
public Long unionAndStore(K key, K otherKey, K destKey) {
return unionAndStore(key, Collections.singleton(otherKey), destKey);
}
public void unionAndStore(K key, Collection<K> otherKeys, K destKey) {
public Long unionAndStore(K key, Collection<K> otherKeys, K destKey) {
final byte[][] rawKeys = rawKeys(key, otherKeys);
final byte[] rawDestKey = rawKey(destKey);
execute(new RedisCallback<Object>() {
return execute(new RedisCallback<Long>() {
public Object doInRedis(RedisConnection connection) {
connection.sUnionStore(rawDestKey, rawKeys);
return null;
public Long doInRedis(RedisConnection connection) {
return connection.sUnionStore(rawDestKey, rawKeys);
}
}, true);
}

View File

@@ -73,10 +73,6 @@ class DefaultValueOperations<K, V> extends AbstractOperations<K, V> implements V
return connection.decr(rawKey);
}
if (delta < 0) {
return connection.decrBy(rawKey, delta);
}
return connection.incrBy(rawKey, delta);
}
}, true);
@@ -109,8 +105,6 @@ class DefaultValueOperations<K, V> extends AbstractOperations<K, V> implements V
return deserializeString(rawReturn);
}
@SuppressWarnings("unchecked")
public List<V> multiGet(Collection<K> keys) {
if (keys.isEmpty()) {
return Collections.emptyList();

View File

@@ -60,19 +60,18 @@ class DefaultZSetOperations<K, V> extends AbstractOperations<K, V> implements ZS
}
public void intersectAndStore(K key, K otherKey, K destKey) {
intersectAndStore(key, Collections.singleton(otherKey), destKey);
public Long intersectAndStore(K key, K otherKey, K destKey) {
return intersectAndStore(key, Collections.singleton(otherKey), destKey);
}
public void intersectAndStore(K key, Collection<K> otherKeys, K destKey) {
public Long intersectAndStore(K key, Collection<K> otherKeys, K destKey) {
final byte[][] rawKeys = rawKeys(key, otherKeys);
final byte[] rawDestKey = rawKey(destKey);
execute(new RedisCallback<Object>() {
return execute(new RedisCallback<Long>() {
public Object doInRedis(RedisConnection connection) {
connection.zInterStore(rawDestKey, rawKeys);
return null;
public Long doInRedis(RedisConnection connection) {
return connection.zInterStore(rawDestKey, rawKeys);
}
}, true);
}
@@ -233,25 +232,23 @@ class DefaultZSetOperations<K, V> extends AbstractOperations<K, V> implements ZS
}
public void removeRange(K key, final long start, final long end) {
public Long removeRange(K key, final long start, final long end) {
final byte[] rawKey = rawKey(key);
execute(new RedisCallback<Object>() {
return execute(new RedisCallback<Long>() {
public Object doInRedis(RedisConnection connection) {
connection.zRemRange(rawKey, start, end);
return null;
public Long doInRedis(RedisConnection connection) {
return connection.zRemRange(rawKey, start, end);
}
}, true);
}
public void removeRangeByScore(K key, final double min, final double max) {
public Long removeRangeByScore(K key, final double min, final double max) {
final byte[] rawKey = rawKey(key);
execute(new RedisCallback<Object>() {
return execute(new RedisCallback<Long>() {
public Object doInRedis(RedisConnection connection) {
connection.zRemRangeByScore(rawKey, min, max);
return null;
public Long doInRedis(RedisConnection connection) {
return connection.zRemRangeByScore(rawKey, min, max);
}
}, true);
}
@@ -294,19 +291,18 @@ class DefaultZSetOperations<K, V> extends AbstractOperations<K, V> implements ZS
}
public void unionAndStore(K key, K otherKey, K destKey) {
unionAndStore(key, Collections.singleton(otherKey), destKey);
public Long unionAndStore(K key, K otherKey, K destKey) {
return unionAndStore(key, Collections.singleton(otherKey), destKey);
}
public void unionAndStore(K key, Collection<K> otherKeys, K destKey) {
public Long unionAndStore(K key, Collection<K> otherKeys, K destKey) {
final byte[][] rawKeys = rawKeys(key, otherKeys);
final byte[] rawDestKey = rawKey(destKey);
execute(new RedisCallback<Object>() {
return execute(new RedisCallback<Long>() {
public Object doInRedis(RedisConnection connection) {
connection.zUnionStore(rawDestKey, rawKeys);
return null;
public Long doInRedis(RedisConnection connection) {
return connection.zUnionStore(rawDestKey, rawKeys);
}
}, true);
}

View File

@@ -81,14 +81,8 @@ public abstract class RedisConnectionUtils {
RedisConnection conn = factory.getConnection();
boolean synchronizationActive = TransactionSynchronizationManager.isSynchronizationActive();
if (bind || synchronizationActive) {
if (bind) {
connHolder = new RedisConnectionHolder(conn);
if (synchronizationActive) {
TransactionSynchronizationManager.registerSynchronization(new RedisConnectionSynchronization(
connHolder, factory, true));
}
TransactionSynchronizationManager.bindResource(factory, connHolder);
return connHolder.getConnection();
}

View File

@@ -160,16 +160,16 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
try {
RedisConnection connToExpose = (exposeConnection ? conn : createRedisConnectionProxy(conn));
T result = action.doInRedis(connToExpose);
// close pipeline
if (pipeline && !pipelineStatus) {
conn.closePipeline();
}
// TODO: any other connection processing?
return postProcessResult(result, conn, existingConnection);
} finally {
try {
if (pipeline && !pipelineStatus) {
conn.closePipeline();
}
} finally {
RedisConnectionUtils.releaseConnection(conn, factory);
}
RedisConnectionUtils.releaseConnection(conn, factory);
}
}

View File

@@ -30,25 +30,25 @@ public interface SetOperations<K, V> {
Set<V> difference(K key, Collection<K> otherKeys);
void differenceAndStore(K key, K otherKey, K destKey);
Long differenceAndStore(K key, K otherKey, K destKey);
void differenceAndStore(K key, Collection<K> otherKeys, K destKey);
Long differenceAndStore(K key, Collection<K> otherKeys, K destKey);
Set<V> intersect(K key, K otherKey);
Set<V> intersect(K key, Collection<K> otherKeys);
void intersectAndStore(K key, K otherKey, K destKey);
Long intersectAndStore(K key, K otherKey, K destKey);
void intersectAndStore(K key, Collection<K> otherKeys, K destKey);
Long intersectAndStore(K key, Collection<K> otherKeys, K destKey);
Set<V> union(K key, K otherKey);
Set<V> union(K key, Collection<K> otherKeys);
void unionAndStore(K key, K otherKey, K destKey);
Long unionAndStore(K key, K otherKey, K destKey);
void unionAndStore(K key, Collection<K> otherKeys, K destKey);
Long unionAndStore(K key, Collection<K> otherKeys, K destKey);
Boolean add(K key, V value);

View File

@@ -35,13 +35,13 @@ public interface ZSetOperations<K, V> {
Double getScore();
}
void intersectAndStore(K key, K otherKey, K destKey);
Long intersectAndStore(K key, K otherKey, K destKey);
void intersectAndStore(K key, Collection<K> otherKeys, K destKey);
Long intersectAndStore(K key, Collection<K> otherKeys, K destKey);
void unionAndStore(K key, K otherKey, K destKey);
Long unionAndStore(K key, K otherKey, K destKey);
void unionAndStore(K key, Collection<K> otherKeys, K destKey);
Long unionAndStore(K key, Collection<K> otherKeys, K destKey);
Set<V> range(K key, long start, long end);
@@ -71,9 +71,9 @@ public interface ZSetOperations<K, V> {
Boolean remove(K key, Object o);
void removeRange(K key, long start, long end);
Long removeRange(K key, long start, long end);
void removeRangeByScore(K key, double min, double max);
Long removeRangeByScore(K key, double min, double max);
Long count(K key, double min, double max);