Polish RedisZSet.

Improve Javadoc. Add static factory methods for easier RedisZSet construction.

See #2007
Original Pull Request: #2088
This commit is contained in:
Mark Paluch
2021-06-16 14:18:18 +02:00
committed by Christoph Strobl
parent bdf15de653
commit 755c8417a3
5 changed files with 139 additions and 5 deletions

View File

@@ -22,6 +22,7 @@ import java.util.concurrent.TimeUnit;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Base implementation for {@link RedisCollection}. Provides a skeletal implementation. Note that the collection support
@@ -47,6 +48,9 @@ public abstract class AbstractRedisCollection<E> extends AbstractCollection<E> i
*/
public AbstractRedisCollection(String key, RedisOperations<String, E> operations) {
Assert.hasText(key, "Key must not be empty!");
Assert.notNull(operations, "RedisOperations must not be null!");
this.key = key;
this.operations = operations;
}
@@ -217,7 +221,7 @@ public abstract class AbstractRedisCollection<E> extends AbstractCollection<E> i
StringBuilder sb = new StringBuilder();
sb.append("RedisStore for key:");
sb.append(String.format("%s for key:", getClass().getSimpleName()));
sb.append(getKey());
return sb.toString();

View File

@@ -43,7 +43,7 @@ import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
public class DefaultRedisZSet<E> extends AbstractRedisCollection<E> implements RedisZSet<E> {
private final BoundZSetOperations<String, E> boundZSetOps;
private double defaultScore = 1;
private final double defaultScore;
private class DefaultRedisSortedSetIterator extends RedisIterator<E> {

View File

@@ -112,7 +112,7 @@ public class RedisCollectionFactoryBean implements InitializingBean, BeanNameAwa
return new DefaultRedisSet(key, template);
case ZSET:
return new DefaultRedisZSet(key, template);
return RedisZSet.create(key, template);
case HASH:
if (CollectionType.PROPERTIES.equals(type)) {

View File

@@ -23,9 +23,11 @@ import java.util.Set;
import java.util.SortedSet;
import java.util.concurrent.TimeUnit;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.connection.RedisZSetCommands.Limit;
import org.springframework.data.redis.connection.RedisZSetCommands.Range;
import org.springframework.data.redis.core.BoundZSetOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
/**
@@ -41,16 +43,85 @@ import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
*/
public interface RedisZSet<E> extends RedisCollection<E>, Set<E> {
/**
* Constructs a new {@link RedisZSet} instance with a default score of {@literal 1}.
*
* @param key Redis key of this set.
* @param operations {@link RedisOperations} for the value type of this set.
* @since 2.6
*/
static <E> RedisZSet<E> create(String key, RedisOperations<String, E> operations) {
return new DefaultRedisZSet<>(key, operations, 1);
}
/**
* Constructs a new {@link RedisZSet} instance.
*
* @param key Redis key of this set.
* @param operations {@link RedisOperations} for the value type of this set.
* @param defaultScore
* @since 2.6
*/
static <E> RedisZSet<E> create(String key, RedisOperations<String, E> operations, double defaultScore) {
return new DefaultRedisZSet<>(key, operations, defaultScore);
}
/**
* Create a new {@link RedisZSet} by intersecting this sorted set and {@link RedisZSet} and store result in
* destination {@code destKey}.
*
* @param set must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @return a new {@link RedisZSet} pointing at {@code destKey}
*/
RedisZSet<E> intersectAndStore(RedisZSet<?> set, String destKey);
/**
* Create a new {@link RedisZSet} by intersecting this sorted set and the collection {@link RedisZSet} and store
* result in destination {@code destKey}.
*
* @param sets must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @return a new {@link RedisZSet} pointing at {@code destKey}
*/
RedisZSet<E> intersectAndStore(Collection<? extends RedisZSet<?>> sets, String destKey);
/**
* Create a new {@link RedisZSet} by union this sorted set and {@link RedisZSet} and store result in destination
* {@code destKey}.
*
* @param set must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @return a new {@link RedisZSet} pointing at {@code destKey}
*/
RedisZSet<E> unionAndStore(RedisZSet<?> set, String destKey);
/**
* Create a new {@link RedisZSet} by union this sorted set and the collection {@link RedisZSet} and store result in
* destination {@code destKey}.
*
* @param sets must not be {@literal null}.
* @param destKey must not be {@literal null}.
* @return a new {@link RedisZSet} pointing at {@code destKey}
*/
RedisZSet<E> unionAndStore(Collection<? extends RedisZSet<?>> sets, String destKey);
/**
* Get elements between {@code start} and {@code end} from sorted set.
*
* @param start
* @param end
* @return
*/
Set<E> range(long start, long end);
/**
* Get elements in range from {@code start} to {@code end} from sorted set ordered from high to low.
*
* @param start
* @param end
* @return
*/
Set<E> reverseRange(long start, long end);
/**
@@ -105,29 +176,88 @@ public interface RedisZSet<E> extends RedisCollection<E>, Set<E> {
*/
Set<E> reverseRangeByLex(Range range, Limit limit);
/**
* Get elements where score is between {@code min} and {@code max} from sorted set.
*
* @param min
* @param max
* @return
*/
Set<E> rangeByScore(double min, double max);
/**
* Get elements where score is between {@code min} and {@code max} from sorted set ordered from high to low.
*
* @param min
* @param max
* @return
*/
Set<E> reverseRangeByScore(double min, double max);
/**
* Get set of {@link RedisZSetCommands.Tuple}s between {@code start} and {@code end} from sorted set.
*
* @param start
* @param end
* @return
*/
Set<TypedTuple<E>> rangeWithScores(long start, long end);
/**
* Get set of {@link RedisZSetCommands.Tuple}s in range from {@code start} to {@code end} from sorted set ordered from
* high to low.
*
* @param start
* @param end
* @return
*/
Set<TypedTuple<E>> reverseRangeWithScores(long start, long end);
/**
* Get set of {@link RedisZSetCommands.Tuple}s where score is between {@code min} and {@code max} from sorted set.
*
* @param min
* @param max
* @return
*/
Set<TypedTuple<E>> rangeByScoreWithScores(double min, double max);
/**
* Get set of {@link RedisZSetCommands.Tuple}s where score is between {@code min} and {@code max} from sorted set
* ordered from high to low.
*
* @param min
* @param max
* @return
*/
Set<TypedTuple<E>> reverseRangeByScoreWithScores(double min, double max);
/**
* Remove elements in range between {@code start} and {@code end} from sorted set.
*
* @param start
* @param end
* @return {@code this} set.
*/
RedisZSet<E> remove(long start, long end);
/**
* Remove all elements in range.
*
* @param range must not be {@literal null}.
* @return never {@literal null}.
* @return {@code this} set.
* @since 2.5
*/
// TODO: Switch to RedisZSet
Set<E> removeByLex(Range range);
/**
* Remove elements with scores between {@code min} and {@code max} from sorted set with the bound key.
*
* @param min
* @param max
* @return {@code this} set.
*/
RedisZSet<E> removeByScore(double min, double max);
/**