+ several adjustments to the intersect/diff signatures
+ added overloaded methods to support single key operations w/o having to create a collection
This commit is contained in:
@@ -42,9 +42,9 @@ public interface RedisListCommands {
|
||||
|
||||
Long lLen(byte[] key);
|
||||
|
||||
List<byte[]> lRange(byte[] key, long start, long end);
|
||||
List<byte[]> lRange(byte[] key, long begin, long end);
|
||||
|
||||
void lTrim(byte[] key, long start, long end);
|
||||
void lTrim(byte[] key, long begin, long end);
|
||||
|
||||
byte[] lIndex(byte[] key, long index);
|
||||
|
||||
|
||||
@@ -52,9 +52,9 @@ public interface RedisStringCommands {
|
||||
|
||||
Long append(byte[] key, byte[] value);
|
||||
|
||||
byte[] getRange(byte[] key, int start, int end);
|
||||
byte[] getRange(byte[] key, int begin, int end);
|
||||
|
||||
void setRange(byte[] key, int start, int end);
|
||||
void setRange(byte[] key, int begin, int end);
|
||||
|
||||
Boolean getBit(byte[] key, long offset);
|
||||
|
||||
|
||||
@@ -52,13 +52,13 @@ public interface RedisZSetCommands {
|
||||
|
||||
Long zRevRank(byte[] key, byte[] value);
|
||||
|
||||
Set<byte[]> zRange(byte[] key, long start, long end);
|
||||
Set<byte[]> zRange(byte[] key, long begin, long end);
|
||||
|
||||
Set<Tuple> zRangeWithScore(byte[] key, long start, long end);
|
||||
Set<Tuple> zRangeWithScore(byte[] key, long begin, long end);
|
||||
|
||||
Set<byte[]> zRevRange(byte[] key, long start, long end);
|
||||
Set<byte[]> zRevRange(byte[] key, long begin, long end);
|
||||
|
||||
Set<Tuple> zRevRangeWithScore(byte[] key, long start, long end);
|
||||
Set<Tuple> zRevRangeWithScore(byte[] key, long begin, long end);
|
||||
|
||||
Set<byte[]> zRangeByScore(byte[] key, double min, double max);
|
||||
|
||||
@@ -74,7 +74,7 @@ public interface RedisZSetCommands {
|
||||
|
||||
Double zScore(byte[] key, byte[] value);
|
||||
|
||||
Long zRemRange(byte[] key, long start, long end);
|
||||
Long zRemRange(byte[] key, long begin, long end);
|
||||
|
||||
Long zRemRangeByScore(byte[] key, double min, double max);
|
||||
|
||||
|
||||
@@ -28,17 +28,29 @@ public interface BoundSetOperations<K, V> extends KeyBound<K> {
|
||||
|
||||
RedisOperations<K, V> getOperations();
|
||||
|
||||
Set<V> diff(K key);
|
||||
|
||||
Set<V> diff(Collection<K> keys);
|
||||
|
||||
void diffAndStore(K destKey, Collection<K> keys);
|
||||
void diffAndStore(K key, K destKey);
|
||||
|
||||
void diffAndStore(Collection<K> keys, K destKey);
|
||||
|
||||
Set<V> intersect(K key);
|
||||
|
||||
Set<V> intersect(Collection<K> keys);
|
||||
|
||||
void intersectAndStore(K destKey, Collection<K> keys);
|
||||
void intersectAndStore(K key, K destKey);
|
||||
|
||||
void intersectAndStore(Collection<K> keys, K destKey);
|
||||
|
||||
Set<V> union(K key);
|
||||
|
||||
Set<V> union(Collection<K> keys);
|
||||
|
||||
void unionAndStore(K destKey, Collection<K> keys);
|
||||
void unionAndStore(K key, K destKey);
|
||||
|
||||
void unionAndStore(Collection<K> keys, K destKey);
|
||||
|
||||
Boolean add(V value);
|
||||
|
||||
|
||||
@@ -29,7 +29,9 @@ public interface BoundZSetOperations<K, V> extends KeyBound<K> {
|
||||
|
||||
RedisOperations<K, V> getOperations();
|
||||
|
||||
void intersectAndStore(K destKey, Collection<K> keys);
|
||||
void intersectAndStore(K otherKey, K destKey);
|
||||
|
||||
void intersectAndStore(Collection<K> otherKeys, K destKey);
|
||||
|
||||
Set<V> range(long start, long end);
|
||||
|
||||
@@ -41,7 +43,9 @@ public interface BoundZSetOperations<K, V> extends KeyBound<K> {
|
||||
|
||||
void removeRangeByScore(double min, double max);
|
||||
|
||||
void unionAndStore(K destKey, Collection<K> keys);
|
||||
void unionAndStore(K otherKey, K destKey);
|
||||
|
||||
void unionAndStore(Collection<K> otherKeys, K destKey);
|
||||
|
||||
Boolean add(V value, double score);
|
||||
|
||||
|
||||
@@ -45,14 +45,25 @@ class DefaultBoundSetOperations<K, V> extends DefaultKeyBound<K> implements Boun
|
||||
return ops.add(getKey(), value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<V> diff(K key) {
|
||||
return ops.difference(getKey(), key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<V> diff(Collection<K> keys) {
|
||||
return ops.difference(getKey(), keys);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void diffAndStore(K destKey, Collection<K> keys) {
|
||||
ops.differenceAndStore(getKey(), destKey, keys);
|
||||
public void diffAndStore(K key, K destKey) {
|
||||
ops.differenceAndStore(getKey(), key, destKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void diffAndStore(Collection<K> keys, K destKey) {
|
||||
ops.differenceAndStore(getKey(), keys, destKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -60,14 +71,24 @@ class DefaultBoundSetOperations<K, V> extends DefaultKeyBound<K> implements Boun
|
||||
return ops.getOperations();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<V> intersect(K key) {
|
||||
return ops.intersect(getKey(), key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<V> intersect(Collection<K> keys) {
|
||||
return ops.intersect(getKey(), keys);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void intersectAndStore(K destKey, Collection<K> keys) {
|
||||
ops.intersectAndStore(getKey(), destKey, keys);
|
||||
public void intersectAndStore(K key, K destKey) {
|
||||
ops.intersectAndStore(getKey(), key, destKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void intersectAndStore(Collection<K> keys, K destKey) {
|
||||
ops.intersectAndStore(getKey(), keys, destKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -82,7 +103,7 @@ class DefaultBoundSetOperations<K, V> extends DefaultKeyBound<K> implements Boun
|
||||
|
||||
@Override
|
||||
public Boolean move(K destKey, V value) {
|
||||
return ops.move(getKey(), destKey, value);
|
||||
return ops.move(getKey(), value, destKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -105,13 +126,24 @@ class DefaultBoundSetOperations<K, V> extends DefaultKeyBound<K> implements Boun
|
||||
return ops.size(getKey());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Set<V> union(K key) {
|
||||
return ops.union(getKey(), key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<V> union(Collection<K> keys) {
|
||||
return ops.union(getKey(), keys);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unionAndStore(K destKey, Collection<K> keys) {
|
||||
ops.unionAndStore(getKey(), destKey, keys);
|
||||
public void unionAndStore(K key, K destKey) {
|
||||
ops.unionAndStore(getKey(), key, destKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unionAndStore(Collection<K> keys, K destKey) {
|
||||
ops.unionAndStore(getKey(), keys, destKey);
|
||||
}
|
||||
}
|
||||
@@ -55,8 +55,13 @@ class DefaultBoundZSetOperations<K, V> extends DefaultKeyBound<K> implements Bou
|
||||
}
|
||||
|
||||
@Override
|
||||
public void intersectAndStore(K destKey, Collection<K> keys) {
|
||||
ops.intersectAndStore(getKey(), destKey, keys);
|
||||
public void intersectAndStore(K destKey, K otherKey) {
|
||||
ops.intersectAndStore(getKey(), otherKey, destKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void intersectAndStore(Collection<K> otherKeys, K destKey) {
|
||||
ops.intersectAndStore(getKey(), otherKeys, destKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -115,7 +120,12 @@ class DefaultBoundZSetOperations<K, V> extends DefaultKeyBound<K> implements Bou
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unionAndStore(K destKey, Collection<K> keys) {
|
||||
ops.unionAndStore(getKey(), destKey, keys);
|
||||
public void unionAndStore(K otherKey, K destKey) {
|
||||
ops.unionAndStore(getKey(), otherKey, destKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unionAndStore(Collection<K> otherKeys, K destKey) {
|
||||
ops.unionAndStore(getKey(), otherKeys, destKey);
|
||||
}
|
||||
}
|
||||
@@ -426,6 +426,15 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
return rawKeys;
|
||||
}
|
||||
|
||||
private byte[][] rawKeys(K key, K otherKey) {
|
||||
final byte[][] rawKeys = new byte[2][];
|
||||
|
||||
|
||||
rawKeys[0] = rawKey(key);
|
||||
rawKeys[1] = rawKey(key);
|
||||
return rawKeys;
|
||||
}
|
||||
|
||||
private byte[][] rawKeys(K key, Collection<K> keys) {
|
||||
final byte[][] rawKeys = new byte[keys.size() + 1][];
|
||||
|
||||
@@ -1327,8 +1336,13 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<V> difference(final K key, final Collection<K> keys) {
|
||||
final byte[][] rawKeys = rawKeys(key, keys);
|
||||
public Set<V> difference(K key, K otherKey) {
|
||||
return difference(key, Collections.singleton(otherKey));
|
||||
}
|
||||
|
||||
@Override
|
||||
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[]>>() {
|
||||
@Override
|
||||
public Set<byte[]> doInRedis(RedisConnection connection) {
|
||||
@@ -1340,8 +1354,13 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
|
||||
@Override
|
||||
public void differenceAndStore(final K key, K destKey, final Collection<K> keys) {
|
||||
final byte[][] rawKeys = rawKeys(key, keys);
|
||||
public void differenceAndStore(K key, K otherKey, K destKey) {
|
||||
differenceAndStore(key, Collections.singleton(otherKey), destKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void 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>() {
|
||||
@Override
|
||||
@@ -1358,8 +1377,13 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<V> intersect(K key, Collection<K> keys) {
|
||||
final byte[][] rawKeys = rawKeys(key, keys);
|
||||
public Set<V> intersect(K key, K otherKey) {
|
||||
return intersect(key, Collections.singleton(otherKey));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<V> intersect(K key, Collection<K> otherKeys) {
|
||||
final byte[][] rawKeys = rawKeys(key, otherKeys);
|
||||
Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
|
||||
@Override
|
||||
public Set<byte[]> doInRedis(RedisConnection connection) {
|
||||
@@ -1371,8 +1395,13 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
|
||||
@Override
|
||||
public void intersectAndStore(K key, K destKey, Collection<K> keys) {
|
||||
final byte[][] rawKeys = rawKeys(key, keys);
|
||||
public void intersectAndStore(K key, K otherKey, K destKey) {
|
||||
intersectAndStore(key, Collections.singleton(otherKey), destKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void intersectAndStore(K key, Collection<K> otherKeys, K destKey) {
|
||||
final byte[][] rawKeys = rawKeys(key, otherKeys);
|
||||
final byte[] rawDestKey = rawKey(destKey);
|
||||
execute(new RedisCallback<Object>() {
|
||||
@Override
|
||||
@@ -1409,7 +1438,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean move(K key, K destKey, V value) {
|
||||
public Boolean move(K key, V value, K destKey) {
|
||||
final byte[] rawKey = rawKey(key);
|
||||
final byte[] rawDestKey = rawKey(destKey);
|
||||
final byte[] rawValue = rawValue(value);
|
||||
@@ -1467,8 +1496,13 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<V> union(K key, Collection<K> keys) {
|
||||
final byte[][] rawKeys = rawKeys(key, keys);
|
||||
public Set<V> union(K key, K otherKey) {
|
||||
return union(key, Collections.singleton(otherKey));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<V> union(K key, Collection<K> otherKeys) {
|
||||
final byte[][] rawKeys = rawKeys(key, otherKeys);
|
||||
Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
|
||||
@Override
|
||||
public Set<byte[]> doInRedis(RedisConnection connection) {
|
||||
@@ -1480,8 +1514,13 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unionAndStore(K key, K destKey, Collection<K> keys) {
|
||||
final byte[][] rawKeys = rawKeys(key, keys);
|
||||
public void unionAndStore(K key, K otherKey, K destKey) {
|
||||
unionAndStore(key, Collections.singleton(otherKey), destKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unionAndStore(K key, Collection<K> otherKeys, K destKey) {
|
||||
final byte[][] rawKeys = rawKeys(key, otherKeys);
|
||||
final byte[] rawDestKey = rawKey(destKey);
|
||||
execute(new RedisCallback<Object>() {
|
||||
@Override
|
||||
@@ -1540,9 +1579,15 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
return RedisTemplate.this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void intersectAndStore(K key, K destKey, Collection<K> keys) {
|
||||
final byte[][] rawKeys = rawKeys(key, keys);
|
||||
public void intersectAndStore(K key, K otherKey, K destKey) {
|
||||
intersectAndStore(key, Collections.singleton(otherKey), destKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void intersectAndStore(K key, Collection<K> otherKeys, K destKey) {
|
||||
final byte[][] rawKeys = rawKeys(key, otherKeys);
|
||||
final byte[] rawDestKey = rawKey(destKey);
|
||||
execute(new RedisCallback<Object>() {
|
||||
@Override
|
||||
@@ -1698,8 +1743,13 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unionAndStore(K key, K destKey, Collection<K> keys) {
|
||||
final byte[][] rawKeys = rawKeys(key, keys);
|
||||
public void unionAndStore(K key, K otherKey, K destKey) {
|
||||
unionAndStore(key, Collections.singleton(otherKey), destKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unionAndStore(K key, Collection<K> otherKeys, K destKey) {
|
||||
final byte[][] rawKeys = rawKeys(key, otherKeys);
|
||||
final byte[] rawDestKey = rawKey(destKey);
|
||||
execute(new RedisCallback<Object>() {
|
||||
@Override
|
||||
|
||||
@@ -26,17 +26,29 @@ import java.util.Set;
|
||||
*/
|
||||
public interface SetOperations<K, V> {
|
||||
|
||||
Set<V> difference(K key, Collection<K> keys);
|
||||
Set<V> difference(K key, K otherKey);
|
||||
|
||||
void differenceAndStore(K key, K destKey, Collection<K> keys);
|
||||
Set<V> difference(K key, Collection<K> otherKeys);
|
||||
|
||||
Set<V> intersect(K key, Collection<K> keys);
|
||||
void differenceAndStore(K key, K otherKey, K destKey);
|
||||
|
||||
void intersectAndStore(K key, K destKey, Collection<K> keys);
|
||||
void differenceAndStore(K key, Collection<K> otherKeys, K destKey);
|
||||
|
||||
Set<V> union(K key, Collection<K> keys);
|
||||
Set<V> intersect(K key, K otherKey);
|
||||
|
||||
void unionAndStore(K key, K destKey, Collection<K> keys);
|
||||
Set<V> intersect(K key, Collection<K> otherKeys);
|
||||
|
||||
void intersectAndStore(K key, K otherKey, K destKey);
|
||||
|
||||
void 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);
|
||||
|
||||
void unionAndStore(K key, Collection<K> otherKeys, K destKey);
|
||||
|
||||
Boolean add(K key, V value);
|
||||
|
||||
@@ -44,7 +56,7 @@ public interface SetOperations<K, V> {
|
||||
|
||||
Set<V> members(K key);
|
||||
|
||||
Boolean move(K key, K destKey, V value);
|
||||
Boolean move(K key, V value, K destKey);
|
||||
|
||||
V randomMember(K key);
|
||||
|
||||
|
||||
@@ -26,9 +26,13 @@ import java.util.Set;
|
||||
*/
|
||||
public interface ZSetOperations<K, V> {
|
||||
|
||||
void intersectAndStore(K key, K destKey, Collection<K> keys);
|
||||
void intersectAndStore(K key, K otherKey, K destKey);
|
||||
|
||||
void unionAndStore(K key, K destKey, Collection<K> keys);
|
||||
void intersectAndStore(K key, Collection<K> otherKeys, K destKey);
|
||||
|
||||
void unionAndStore(K key, K otherKey, K destKey);
|
||||
|
||||
void unionAndStore(K key, Collection<K> otherKeys, K destKey);
|
||||
|
||||
Set<V> range(K key, long start, long end);
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2011 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.serializer;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Simple toString() serializer for the core (lang) numberic JDK types.
|
||||
*
|
||||
* @see String#valueOf(Object)
|
||||
* @see Long#valueOf(String)
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class BasicNumberToStringSerializer<T extends Number> implements RedisSerializer<T> {
|
||||
|
||||
private final Charset charset;
|
||||
private final Constructor<T> ctor;
|
||||
|
||||
public BasicNumberToStringSerializer(Class<T> type) {
|
||||
this(type, Charset.forName("UTF8"));
|
||||
}
|
||||
|
||||
public BasicNumberToStringSerializer(Class<T> type, Charset charset) {
|
||||
Assert.notNull(type);
|
||||
this.charset = charset;
|
||||
|
||||
if (!(Byte.class.isAssignableFrom(type) || Short.class.isAssignableFrom(type)
|
||||
|| Long.class.isAssignableFrom(type) || Integer.class.isAssignableFrom(type)
|
||||
|| Float.class.isAssignableFrom(type) || Double.class.isAssignableFrom(type))) {
|
||||
throw new IllegalArgumentException("Type " + type + " not supported");
|
||||
}
|
||||
|
||||
try {
|
||||
ctor = type.getConstructor(String.class);
|
||||
} catch (Exception ex) {
|
||||
throw new IllegalArgumentException("Cannot find suitable constructor for " + type);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public T deserialize(byte[] bytes) {
|
||||
String string = new String(bytes, charset);
|
||||
return BeanUtils.instantiateClass(ctor, string);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] serialize(T object) {
|
||||
String string = String.valueOf(object);
|
||||
return string.getBytes(charset);
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,8 @@ import org.springframework.data.keyvalue.redis.core.RedisOperations;
|
||||
import org.springframework.data.keyvalue.redis.core.RedisTemplate;
|
||||
import org.springframework.data.keyvalue.redis.core.SessionCallback;
|
||||
import org.springframework.data.keyvalue.redis.core.ValueOperations;
|
||||
import org.springframework.data.keyvalue.redis.serializer.BasicNumberToStringSerializer;
|
||||
import org.springframework.data.keyvalue.redis.serializer.StringRedisSerializer;
|
||||
|
||||
/**
|
||||
* Atomic integer backed by Redis.
|
||||
@@ -47,6 +49,8 @@ public class RedisAtomicInteger extends Number implements Serializable, KeyBound
|
||||
*/
|
||||
public RedisAtomicInteger(String redisCounter, RedisConnectionFactory factory) {
|
||||
RedisTemplate<String, Integer> redisTemplate = new RedisTemplate<String, Integer>(factory);
|
||||
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||
redisTemplate.setValueSerializer(new BasicNumberToStringSerializer<Integer>(Integer.class));
|
||||
redisTemplate.setExposeConnection(true);
|
||||
this.key = redisCounter;
|
||||
this.generalOps = redisTemplate;
|
||||
|
||||
@@ -24,6 +24,8 @@ import org.springframework.data.keyvalue.redis.core.RedisOperations;
|
||||
import org.springframework.data.keyvalue.redis.core.RedisTemplate;
|
||||
import org.springframework.data.keyvalue.redis.core.SessionCallback;
|
||||
import org.springframework.data.keyvalue.redis.core.ValueOperations;
|
||||
import org.springframework.data.keyvalue.redis.serializer.BasicNumberToStringSerializer;
|
||||
import org.springframework.data.keyvalue.redis.serializer.StringRedisSerializer;
|
||||
|
||||
/**
|
||||
* Atomic long backed by Redis.
|
||||
@@ -47,6 +49,8 @@ public class RedisAtomicLong extends Number implements Serializable, KeyBound<St
|
||||
*/
|
||||
public RedisAtomicLong(String redisCounter, RedisConnectionFactory factory) {
|
||||
RedisTemplate<String, Long> redisTemplate = new RedisTemplate<String, Long>(factory);
|
||||
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||
redisTemplate.setValueSerializer(new BasicNumberToStringSerializer<Long>(Long.class));
|
||||
redisTemplate.setExposeConnection(true);
|
||||
this.key = redisCounter;
|
||||
this.generalOps = redisTemplate;
|
||||
|
||||
@@ -66,36 +66,71 @@ public class DefaultRedisSet<E> extends AbstractRedisCollection<E> implements Re
|
||||
this.boundSetOps = boundOps;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Set<E> diff(RedisSet<?> set) {
|
||||
return boundSetOps.diff(set.getKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<E> diff(Collection<? extends RedisSet<?>> sets) {
|
||||
return boundSetOps.diff(CollectionUtils.extractKeys(sets));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public RedisSet<E> diffAndStore(String destKey, Collection<? extends RedisSet<?>> sets) {
|
||||
boundSetOps.diffAndStore(destKey, CollectionUtils.extractKeys(sets));
|
||||
public RedisSet<E> diffAndStore(RedisSet<?> set, String destKey) {
|
||||
boundSetOps.diffAndStore(set.getKey(), destKey);
|
||||
return new DefaultRedisSet<E>(boundSetOps.getOperations().boundSetOps(destKey));
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisSet<E> diffAndStore(Collection<? extends RedisSet<?>> sets, String destKey) {
|
||||
boundSetOps.diffAndStore(CollectionUtils.extractKeys(sets), destKey);
|
||||
return new DefaultRedisSet<E>(boundSetOps.getOperations().boundSetOps(destKey));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<E> intersect(RedisSet<?> set) {
|
||||
return boundSetOps.intersect(set.getKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<E> intersect(Collection<? extends RedisSet<?>> sets) {
|
||||
return boundSetOps.intersect(CollectionUtils.extractKeys(sets));
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisSet<E> intersectAndStore(String destKey, Collection<? extends RedisSet<?>> sets) {
|
||||
boundSetOps.intersectAndStore(destKey, CollectionUtils.extractKeys(sets));
|
||||
public RedisSet<E> intersectAndStore(RedisSet<?> set, String destKey) {
|
||||
boundSetOps.intersectAndStore(set.getKey(), destKey);
|
||||
return new DefaultRedisSet<E>(boundSetOps.getOperations().boundSetOps(destKey));
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisSet<E> intersectAndStore(Collection<? extends RedisSet<?>> sets, String destKey) {
|
||||
boundSetOps.intersectAndStore(CollectionUtils.extractKeys(sets), destKey);
|
||||
return new DefaultRedisSet<E>(boundSetOps.getOperations().boundSetOps(destKey));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<E> union(RedisSet<?> set) {
|
||||
return boundSetOps.union(set.getKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<E> union(Collection<? extends RedisSet<?>> sets) {
|
||||
return boundSetOps.union(CollectionUtils.extractKeys(sets));
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisSet<E> unionAndStore(String destKey, Collection<? extends RedisSet<?>> sets) {
|
||||
boundSetOps.unionAndStore(destKey, CollectionUtils.extractKeys(sets));
|
||||
public RedisSet<E> unionAndStore(RedisSet<?> set, String destKey) {
|
||||
boundSetOps.unionAndStore(set.getKey(), destKey);
|
||||
return new DefaultRedisSet<E>(boundSetOps.getOperations().boundSetOps(destKey));
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisSet<E> unionAndStore(Collection<? extends RedisSet<?>> sets, String destKey) {
|
||||
boundSetOps.unionAndStore(CollectionUtils.extractKeys(sets), destKey);
|
||||
return new DefaultRedisSet<E>(boundSetOps.getOperations().boundSetOps(destKey));
|
||||
}
|
||||
|
||||
@@ -109,7 +144,7 @@ public class DefaultRedisSet<E> extends AbstractRedisCollection<E> implements Re
|
||||
// intersect the set with a non existing one
|
||||
// TODO: find a safer way to clean the set
|
||||
String randomKey = UUID.randomUUID().toString();
|
||||
boundSetOps.intersectAndStore(getKey(), Collections.singleton(randomKey));
|
||||
boundSetOps.intersectAndStore(Collections.singleton(randomKey), getKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -91,8 +91,14 @@ public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements R
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisZSet<E> intersectAndStore(String destKey, Collection<? extends RedisZSet<?>> sets) {
|
||||
boundZSetOps.intersectAndStore(destKey, CollectionUtils.extractKeys(sets));
|
||||
public RedisZSet<E> intersectAndStore(RedisZSet<?> set, String destKey) {
|
||||
boundZSetOps.intersectAndStore(set.getKey(), destKey);
|
||||
return new DefaultRedisZSet<E>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisZSet<E> intersectAndStore(Collection<? extends RedisZSet<?>> sets, String destKey) {
|
||||
boundZSetOps.intersectAndStore(CollectionUtils.extractKeys(sets), destKey);
|
||||
return new DefaultRedisZSet<E>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
|
||||
}
|
||||
|
||||
@@ -124,8 +130,14 @@ public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements R
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisZSet<E> unionAndStore(String destKey, Collection<? extends RedisZSet<?>> sets) {
|
||||
boundZSetOps.unionAndStore(destKey, CollectionUtils.extractKeys(sets));
|
||||
public RedisZSet<E> unionAndStore(RedisZSet<?> set, String destKey) {
|
||||
boundZSetOps.unionAndStore(set.getKey(), destKey);
|
||||
return new DefaultRedisZSet<E>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisZSet<E> unionAndStore(Collection<? extends RedisZSet<?>> sets, String destKey) {
|
||||
boundZSetOps.unionAndStore(CollectionUtils.extractKeys(sets), destKey);
|
||||
return new DefaultRedisZSet<E>(boundZSetOps.getOperations().boundZSetOps(destKey), getDefaultScore());
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ import java.util.concurrent.BlockingDeque;
|
||||
*/
|
||||
public interface RedisList<E> extends RedisCollection<E>, List<E>, BlockingDeque<E> {
|
||||
|
||||
List<E> range(long start, long end);
|
||||
List<E> range(long begin, long end);
|
||||
|
||||
RedisList<E> trim(int start, int end);
|
||||
RedisList<E> trim(int begin, int end);
|
||||
}
|
||||
|
||||
@@ -26,15 +26,27 @@ import java.util.Set;
|
||||
*/
|
||||
public interface RedisSet<E> extends RedisCollection<E>, Set<E> {
|
||||
|
||||
Set<E> intersect(RedisSet<?> set);
|
||||
|
||||
Set<E> intersect(Collection<? extends RedisSet<?>> sets);
|
||||
|
||||
Set<E> union(RedisSet<?> set);
|
||||
|
||||
Set<E> union(Collection<? extends RedisSet<?>> sets);
|
||||
|
||||
Set<E> diff(RedisSet<?> set);
|
||||
|
||||
Set<E> diff(Collection<? extends RedisSet<?>> sets);
|
||||
|
||||
RedisSet<E> intersectAndStore(String destKey, Collection<? extends RedisSet<?>> sets);
|
||||
RedisSet<E> intersectAndStore(RedisSet<?> set, String destKey);
|
||||
|
||||
RedisSet<E> unionAndStore(String destKey, Collection<? extends RedisSet<?>> sets);
|
||||
RedisSet<E> intersectAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
|
||||
|
||||
RedisSet<E> diffAndStore(String destKey, Collection<? extends RedisSet<?>> sets);
|
||||
RedisSet<E> unionAndStore(RedisSet<?> set, String destKey);
|
||||
|
||||
RedisSet<E> unionAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
|
||||
|
||||
RedisSet<E> diffAndStore(RedisSet<?> set, String destKey);
|
||||
|
||||
RedisSet<E> diffAndStore(Collection<? extends RedisSet<?>> sets, String destKey);
|
||||
}
|
||||
|
||||
@@ -30,9 +30,13 @@ import java.util.SortedSet;
|
||||
*/
|
||||
public interface RedisZSet<E> extends RedisCollection<E>, Set<E> {
|
||||
|
||||
RedisZSet<E> intersectAndStore(String destKey, Collection<? extends RedisZSet<?>> sets);
|
||||
RedisZSet<E> intersectAndStore(RedisZSet<?> set, String destKey);
|
||||
|
||||
RedisZSet<E> unionAndStore(String destKey, Collection<? extends RedisZSet<?>> sets);
|
||||
RedisZSet<E> intersectAndStore(Collection<? extends RedisZSet<?>> sets, String destKey);
|
||||
|
||||
RedisZSet<E> unionAndStore(RedisZSet<?> set, String destKey);
|
||||
|
||||
RedisZSet<E> unionAndStore(Collection<? extends RedisZSet<?>> sets, String destKey);
|
||||
|
||||
Set<E> range(long start, long end);
|
||||
|
||||
|
||||
@@ -102,7 +102,7 @@ public abstract class AbstractRedisSetTests<T> extends AbstractRedisCollectionTe
|
||||
diffSet2.add(t4);
|
||||
|
||||
String resultName = "test:set:diff:result:1";
|
||||
RedisSet<T> diff = set.diffAndStore(resultName, Arrays.asList(diffSet1, diffSet2));
|
||||
RedisSet<T> diff = set.diffAndStore(Arrays.asList(diffSet1, diffSet2), resultName);
|
||||
|
||||
assertEquals(1, diff.size());
|
||||
assertThat(diff, hasItem(t1));
|
||||
@@ -153,7 +153,7 @@ public abstract class AbstractRedisSetTests<T> extends AbstractRedisCollectionTe
|
||||
intSet2.add(t3);
|
||||
|
||||
String resultName = "test:set:intersect:result:1";
|
||||
RedisSet<T> inter = set.intersectAndStore(resultName, Arrays.asList(intSet1, intSet2));
|
||||
RedisSet<T> inter = set.intersectAndStore(Arrays.asList(intSet1, intSet2), resultName);
|
||||
assertEquals(1, inter.size());
|
||||
assertThat(inter, hasItem(t2));
|
||||
assertEquals(resultName, inter.getKey());
|
||||
@@ -199,7 +199,7 @@ public abstract class AbstractRedisSetTests<T> extends AbstractRedisCollectionTe
|
||||
unionSet2.add(t3);
|
||||
|
||||
String resultName = "test:set:union:result:1";
|
||||
RedisSet<T> union = set.unionAndStore(resultName, Arrays.asList(unionSet1, unionSet2));
|
||||
RedisSet<T> union = set.unionAndStore(Arrays.asList(unionSet1, unionSet2), resultName);
|
||||
assertEquals(4, union.size());
|
||||
assertThat(union, hasItems(t1, t2, t3, t4));
|
||||
assertEquals(resultName, union.getKey());
|
||||
|
||||
@@ -207,7 +207,7 @@ public abstract class AbstractRedisZSetTest<T> extends AbstractRedisCollectionTe
|
||||
interSet2.add(t3, 3);
|
||||
|
||||
String resultName = "test:zset:inter:result:1";
|
||||
RedisZSet<T> inter = zSet.intersectAndStore(resultName, Arrays.asList(interSet1, interSet2));
|
||||
RedisZSet<T> inter = zSet.intersectAndStore(Arrays.asList(interSet1, interSet2), resultName);
|
||||
|
||||
assertEquals(1, inter.size());
|
||||
assertThat(inter, hasItem(t2));
|
||||
@@ -327,7 +327,7 @@ public abstract class AbstractRedisZSetTest<T> extends AbstractRedisCollectionTe
|
||||
unionSet2.add(t3, 6);
|
||||
|
||||
String resultName = "test:zset:union:result:1";
|
||||
RedisZSet<T> union = zSet.unionAndStore(resultName, Arrays.asList(unionSet1, unionSet2));
|
||||
RedisZSet<T> union = zSet.unionAndStore(Arrays.asList(unionSet1, unionSet2), resultName);
|
||||
assertEquals(4, union.size());
|
||||
assertThat(union, hasItems(t1, t2, t3, t4));
|
||||
assertEquals(resultName, union.getKey());
|
||||
|
||||
Reference in New Issue
Block a user