DATAREDIS-668 - Polishing.

Update comments, fix/add missing Javadoc and simplify implementation.

Original pull request: #259.
This commit is contained in:
Christoph Strobl
2017-07-26 09:16:41 +02:00
committed by Mark Paluch
parent c87e0af0ef
commit 6ef4a6f8eb
9 changed files with 1949 additions and 1638 deletions

View File

@@ -74,7 +74,7 @@ public interface RedisSetCommands {
* Move {@code value} from {@code srcKey} to {@code destKey}
*
* @param srcKey must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @param value
* @return
* @see <a href="http://redis.io/commands/smove">Redis Documentation: SMOVE</a>
@@ -84,7 +84,7 @@ public interface RedisSetCommands {
/**
* Get size of set at {@code key}.
*
* @param key must not be {@literal null}.
* @param key must not be {@literal null}.
* @return
* @see <a href="http://redis.io/commands/scard">Redis Documentation: SCARD</a>
*/
@@ -103,7 +103,7 @@ public interface RedisSetCommands {
/**
* Returns the members intersecting all given sets at {@code keys}.
*
* @param keys must not be {@literal null}.
* @param keys must not be {@literal null}.
* @return
* @see <a href="http://redis.io/commands/sinter">Redis Documentation: SINTER</a>
*/
@@ -132,7 +132,7 @@ public interface RedisSetCommands {
* Union all sets at given {@code keys} and store result in {@code destKey}.
*
* @param destKey must not be {@literal null}.
* @param keys must not be {@literal null}.
* @param keys must not be {@literal null}.
* @return
* @see <a href="http://redis.io/commands/sunionstore">Redis Documentation: SUNIONSTORE</a>
*/
@@ -169,7 +169,7 @@ public interface RedisSetCommands {
/**
* Get random element from set at {@code key}.
*
* @param key must not be {@literal null}.
* @param key must not be {@literal null}.
* @return
* @see <a href="http://redis.io/commands/srandmember">Redis Documentation: SRANDMEMBER</a>
*/

View File

@@ -1289,7 +1289,7 @@ public interface StringRedisConnection extends RedisConnection {
* @see <a href="http://redis.io/commands/zrangebyscore">Redis Documentation: ZRANGEBYSCORE</a>
* @see RedisZSetCommands#zRangeByScore(byte[], String, String)
*/
Set<byte[]> zRangeByScore(String key, String min, String max);
Set<String> zRangeByScore(String key, String min, String max);
/**
* Get elements in range from {@code start} to {@code end} where score is between {@code min} and {@code max} from
@@ -1305,7 +1305,7 @@ public interface StringRedisConnection extends RedisConnection {
* @see <a href="http://redis.io/commands/zrangebyscore">Redis Documentation: ZRANGEBYSCORE</a>
* @see RedisZSetCommands#zRangeByScore(byte[], double, double, long, long)
*/
Set<byte[]> zRangeByScore(String key, String min, String max, long offset, long count);
Set<String> zRangeByScore(String key, String min, String max, long offset, long count);
/**
* Get all the elements in the sorted set at {@literal key} in lexicographical ordering.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2016 the original author or authors.
* Copyright 2011-2017 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.
@@ -21,8 +21,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.springframework.core.convert.converter.Converter;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
/**
@@ -37,115 +35,134 @@ class DefaultSetOperations<K, V> extends AbstractOperations<K, V> implements Set
super(template);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#add(java.lang.Object, java.lang.Object[])
*/
public Long add(K key, V... values) {
final byte[] rawKey = rawKey(key);
final byte[][] rawValues = rawValues(values);
return execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection) {
return connection.sAdd(rawKey, rawValues);
}
}, true);
return execute(connection -> connection.sAdd(rawKey, rawValues), true);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#difference(java.lang.Object, java.lang.Object)
*/
public Set<V> difference(K key, K otherKey) {
return difference(key, Collections.singleton(otherKey));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#difference(java.lang.Object, java.util.Collection)
*/
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[]>>() {
public Set<byte[]> doInRedis(RedisConnection connection) {
return connection.sDiff(rawKeys);
}
}, true);
final byte[][] rawKeys = rawKeys(key, otherKeys);
Set<byte[]> rawValues = execute(connection -> connection.sDiff(rawKeys), true);
return deserializeValues(rawValues);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#differenceAndStore(java.lang.Object, java.lang.Object, java.lang.Object)
*/
public Long differenceAndStore(K key, K otherKey, K destKey) {
return differenceAndStore(key, Collections.singleton(otherKey), destKey);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#differenceAndStore(java.lang.Object, java.util.Collection, java.lang.Object)
*/
public Long differenceAndStore(final K key, final Collection<K> otherKeys, K destKey) {
final byte[][] rawKeys = rawKeys(key, otherKeys);
final byte[] rawDestKey = rawKey(destKey);
return execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection) {
return connection.sDiffStore(rawDestKey, rawKeys);
}
}, true);
return execute(connection -> connection.sDiffStore(rawDestKey, rawKeys), true);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#intersect(java.lang.Object, java.lang.Object)
*/
public Set<V> intersect(K key, K otherKey) {
return intersect(key, Collections.singleton(otherKey));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#intersect(java.lang.Object, java.util.Collection)
*/
public Set<V> intersect(K key, Collection<K> otherKeys) {
final byte[][] rawKeys = rawKeys(key, otherKeys);
Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
public Set<byte[]> doInRedis(RedisConnection connection) {
return connection.sInter(rawKeys);
}
}, true);
final byte[][] rawKeys = rawKeys(key, otherKeys);
Set<byte[]> rawValues = execute(connection -> connection.sInter(rawKeys), true);
return deserializeValues(rawValues);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#intersectAndStore(java.lang.Object, java.lang.Object, java.lang.Object)
*/
public Long intersectAndStore(K key, K otherKey, K destKey) {
return intersectAndStore(key, Collections.singleton(otherKey), destKey);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#intersectAndStore(java.lang.Object, java.util.Collection, java.lang.Object)
*/
public Long intersectAndStore(K key, Collection<K> otherKeys, K destKey) {
final byte[][] rawKeys = rawKeys(key, otherKeys);
final byte[] rawDestKey = rawKey(destKey);
return execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection) {
return connection.sInterStore(rawDestKey, rawKeys);
}
}, true);
return execute(connection -> connection.sInterStore(rawDestKey, rawKeys), true);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#isMember(java.lang.Object, java.lang.Object)
*/
public Boolean isMember(K key, Object o) {
final byte[] rawKey = rawKey(key);
final byte[] rawValue = rawValue(o);
return execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection) {
return connection.sIsMember(rawKey, rawValue);
}
}, true);
return execute(connection -> connection.sIsMember(rawKey, rawValue), true);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#members(java.lang.Object)
*/
public Set<V> members(K key) {
final byte[] rawKey = rawKey(key);
Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
public Set<byte[]> doInRedis(RedisConnection connection) {
return connection.sMembers(rawKey);
}
}, true);
final byte[] rawKey = rawKey(key);
Set<byte[]> rawValues = execute(connection -> connection.sMembers(rawKey), true);
return deserializeValues(rawValues);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#move(java.lang.Object, java.lang.Object, java.lang.Object)
*/
public Boolean move(K key, V value, K destKey) {
final byte[] rawKey = rawKey(key);
final byte[] rawDestKey = rawKey(destKey);
final byte[] rawValue = rawValue(value);
return execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection) {
return connection.sMove(rawKey, rawDestKey, rawValue);
}
}, true);
return execute(connection -> connection.sMove(rawKey, rawDestKey, rawValue), true);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#randomMember(java.lang.Object)
*/
public V randomMember(K key) {
return execute(new ValueDeserializingRedisCallback(key) {
@@ -156,47 +173,56 @@ class DefaultSetOperations<K, V> extends AbstractOperations<K, V> implements Set
}, true);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#distinctRandomMembers(java.lang.Object, long)
*/
public Set<V> distinctRandomMembers(K key, final long count) {
if (count < 0) {
throw new IllegalArgumentException("Negative count not supported. "
+ "Use randomMembers to allow duplicate elements.");
throw new IllegalArgumentException(
"Negative count not supported. " + "Use randomMembers to allow duplicate elements.");
}
final byte[] rawKey = rawKey(key);
Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
public Set<byte[]> doInRedis(RedisConnection connection) {
return new HashSet<byte[]>(connection.sRandMember(rawKey, count));
}
}, true);
Set<byte[]> rawValues = execute(
(RedisCallback<Set<byte[]>>) connection -> new HashSet<>(connection.sRandMember(rawKey, count)), true);
return deserializeValues(rawValues);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#randomMembers(java.lang.Object, long)
*/
public List<V> randomMembers(K key, final long count) {
if (count < 0) {
throw new IllegalArgumentException("Use a positive number for count. "
+ "This method is already allowing duplicate elements.");
throw new IllegalArgumentException(
"Use a positive number for count. " + "This method is already allowing duplicate elements.");
}
final byte[] rawKey = rawKey(key);
List<byte[]> rawValues = execute(new RedisCallback<List<byte[]>>() {
public List<byte[]> doInRedis(RedisConnection connection) {
return connection.sRandMember(rawKey, -count);
}
}, true);
List<byte[]> rawValues = execute(connection -> connection.sRandMember(rawKey, -count), true);
return deserializeValues(rawValues);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#remove(java.lang.Object, java.lang.Object[])
*/
public Long remove(K key, Object... values) {
final byte[] rawKey = rawKey(key);
final byte[][] rawValues = rawValues(values);
return execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection) {
return connection.sRem(rawKey, rawValues);
}
}, true);
return execute(connection -> connection.sRem(rawKey, rawValues), true);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#pop(java.lang.Object)
*/
public V pop(K key) {
return execute(new ValueDeserializingRedisCallback(key) {
@@ -219,45 +245,47 @@ class DefaultSetOperations<K, V> extends AbstractOperations<K, V> implements Set
return deserializeValues(rawValues);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#size(java.lang.Object)
*/
public Long size(K key) {
final byte[] rawKey = rawKey(key);
return execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection) {
return connection.sCard(rawKey);
}
}, true);
return execute(connection -> connection.sCard(rawKey), true);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#union(java.lang.Object, java.lang.Object)
*/
public Set<V> union(K key, K otherKey) {
return union(key, Collections.singleton(otherKey));
}
public Set<V> union(K key, Collection<K> otherKeys) {
final byte[][] rawKeys = rawKeys(key, otherKeys);
Set<byte[]> rawValues = execute(new RedisCallback<Set<byte[]>>() {
public Set<byte[]> doInRedis(RedisConnection connection) {
return connection.sUnion(rawKeys);
}
}, true);
final byte[][] rawKeys = rawKeys(key, otherKeys);
Set<byte[]> rawValues = execute(connection -> connection.sUnion(rawKeys), true);
return deserializeValues(rawValues);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#union(java.lang.Object, java.lang.Object, java.lang.Object)
*/
public Long unionAndStore(K key, K otherKey, K destKey) {
return unionAndStore(key, Collections.singleton(otherKey), destKey);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.SetOperations#unionAndStore(java.lang.Object, java.util.Collection, java.lang.Object)
*/
public Long unionAndStore(K key, Collection<K> otherKeys, K destKey) {
final byte[][] rawKeys = rawKeys(key, otherKeys);
final byte[] rawDestKey = rawKey(destKey);
return execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection) {
return connection.sUnionStore(rawDestKey, rawKeys);
}
}, true);
return execute(connection -> connection.sUnionStore(rawDestKey, rawKeys), true);
}
/*
@@ -268,19 +296,9 @@ class DefaultSetOperations<K, V> extends AbstractOperations<K, V> implements Set
public Cursor<V> scan(K key, final ScanOptions options) {
final byte[] rawKey = rawKey(key);
return template.executeWithStickyConnection(new RedisCallback<Cursor<V>>() {
@Override
public Cursor<V> doInRedis(RedisConnection connection) throws DataAccessException {
return new ConvertingCursor<byte[], V>(connection.sScan(rawKey, options), new Converter<byte[], V>() {
@Override
public V convert(byte[] source) {
return deserializeValue(source);
}
});
}
});
return template.executeWithStickyConnection(
(RedisCallback<Cursor<V>>) connection -> new ConvertingCursor<>(connection.sScan(rawKey, options),
source -> deserializeValue(source)));
}
}

View File

@@ -246,8 +246,9 @@ public interface SetOperations<K, V> {
* Get {@code count} distinct random elements from set at {@code key}.
*
* @param key must not be {@literal null}.
* @param count
* @return
* @param count nr of members to return
* @return empty {@link Set} if {@code key} does not exist.
* @throws IllegalArgumentException if count is negative.
* @see <a href="http://redis.io/commands/srandmember">Redis Documentation: SRANDMEMBER</a>
*/
Set<V> distinctRandomMembers(K key, long count);
@@ -256,8 +257,9 @@ public interface SetOperations<K, V> {
* Get {@code count} random elements from set at {@code key}.
*
* @param key must not be {@literal null}.
* @param count
* @return
* @param count nr of members to return.
* @return empty {@link List} if {@code key} does not exist.
* @throws IllegalArgumentException if count is negative.
* @see <a href="http://redis.io/commands/srandmember">Redis Documentation: SRANDMEMBER</a>
*/
List<V> randomMembers(K key, long count);