diff --git a/src/main/java/org/springframework/data/redis/support/collections/AbstractRedisCollection.java b/src/main/java/org/springframework/data/redis/support/collections/AbstractRedisCollection.java index 4cc2e7397..1361da489 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/AbstractRedisCollection.java +++ b/src/main/java/org/springframework/data/redis/support/collections/AbstractRedisCollection.java @@ -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 extends AbstractCollection i */ public AbstractRedisCollection(String key, RedisOperations 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 extends AbstractCollection 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(); diff --git a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java index 0cca0c8a3..965887043 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java +++ b/src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java @@ -43,7 +43,7 @@ import org.springframework.data.redis.core.ZSetOperations.TypedTuple; public class DefaultRedisZSet extends AbstractRedisCollection implements RedisZSet { private final BoundZSetOperations boundZSetOps; - private double defaultScore = 1; + private final double defaultScore; private class DefaultRedisSortedSetIterator extends RedisIterator { diff --git a/src/main/java/org/springframework/data/redis/support/collections/RedisCollectionFactoryBean.java b/src/main/java/org/springframework/data/redis/support/collections/RedisCollectionFactoryBean.java index bb352dc85..bf3df4809 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/RedisCollectionFactoryBean.java +++ b/src/main/java/org/springframework/data/redis/support/collections/RedisCollectionFactoryBean.java @@ -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)) { diff --git a/src/main/java/org/springframework/data/redis/support/collections/RedisZSet.java b/src/main/java/org/springframework/data/redis/support/collections/RedisZSet.java index e3ccf919a..4750ab6bc 100644 --- a/src/main/java/org/springframework/data/redis/support/collections/RedisZSet.java +++ b/src/main/java/org/springframework/data/redis/support/collections/RedisZSet.java @@ -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 extends RedisCollection, Set { + /** + * 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 RedisZSet create(String key, RedisOperations 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 RedisZSet create(String key, RedisOperations 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 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 intersectAndStore(Collection> 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 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 unionAndStore(Collection> sets, String destKey); + /** + * Get elements between {@code start} and {@code end} from sorted set. + * + * @param start + * @param end + * @return + */ Set 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 reverseRange(long start, long end); /** @@ -105,29 +176,88 @@ public interface RedisZSet extends RedisCollection, Set { */ Set 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 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 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> 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> 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> 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> 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 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 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 removeByScore(double min, double max); /** diff --git a/src/test/java/org/springframework/data/redis/support/collections/RedisZSetIntegrationTests.java b/src/test/java/org/springframework/data/redis/support/collections/RedisZSetIntegrationTests.java index 86c87d8b1..b76406eee 100644 --- a/src/test/java/org/springframework/data/redis/support/collections/RedisZSetIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/support/collections/RedisZSetIntegrationTests.java @@ -36,7 +36,7 @@ public class RedisZSetIntegrationTests extends AbstractRedisZSetTestIntegration< } RedisStore copyStore(RedisStore store) { - return new DefaultRedisZSet(store.getKey().toString(), store.getOperations()); + return RedisZSet.create(store.getKey(), store.getOperations()); } AbstractRedisCollection createCollection() {