From 39d2677e1809fbb9dd9c5de6a9eddb640372d896 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 23 Sep 2022 14:22:43 +0200 Subject: [PATCH] Add support for ZRANGESTORE usage with Rank #2202 --- .../core/AbstractRedisAsyncCommands.java | 10 ++ .../core/AbstractRedisReactiveCommands.java | 10 ++ .../io/lettuce/core/RedisCommandBuilder.java | 16 +++ .../async/RedisSortedSetAsyncCommands.java | 36 ++++++- .../RedisSortedSetReactiveCommands.java | 36 ++++++- .../core/api/sync/RedisSortedSetCommands.java | 36 ++++++- .../NodeSelectionSortedSetAsyncCommands.java | 36 ++++++- .../sync/NodeSelectionSortedSetCommands.java | 36 ++++++- .../RedisSortedSetCoroutinesCommands.kt | 46 +++++++-- .../RedisSortedSetCoroutinesCommandsImpl.kt | 99 ++++++++++++++----- .../core/api/RedisSortedSetCommands.java | 34 ++++++- .../SortedSetCommandIntegrationTests.java | 17 ++++ 12 files changed, 359 insertions(+), 53 deletions(-) diff --git a/src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java b/src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java index cf7153e759..6763c469c0 100644 --- a/src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java +++ b/src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java @@ -2536,6 +2536,11 @@ public RedisFuture zrangebyscoreWithScores(ScoredValueStreamingChannel return dispatch(commandBuilder.zrangebyscoreWithScores(channel, key, range, limit)); } + @Override + public RedisFuture zrangestore(K dstKey, K srcKey, Range range) { + return dispatch(commandBuilder.zrangestore(dstKey, srcKey, range, false)); + } + @Override public RedisFuture zrangestorebylex(K dstKey, K srcKey, Range range, Limit limit) { return dispatch(commandBuilder.zrangestorebylex(dstKey, srcKey, range, limit, false)); @@ -2606,6 +2611,11 @@ public RedisFuture zrevrangeWithScores(ScoredValueStreamingChannel chan return dispatch(commandBuilder.zrevrangeWithScores(channel, key, start, stop)); } + @Override + public RedisFuture zrevrangestore(K dstKey, K srcKey, Range range) { + return dispatch(commandBuilder.zrangestore(dstKey, srcKey, range, true)); + } + @Override public RedisFuture zrevrangestorebylex(K dstKey, K srcKey, Range range, Limit limit) { return dispatch(commandBuilder.zrangestorebylex(dstKey, srcKey, range, limit, true)); diff --git a/src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java b/src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java index e698ee5b8c..9252193b57 100644 --- a/src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java +++ b/src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java @@ -2609,6 +2609,11 @@ public Mono zrangebyscoreWithScores(ScoredValueStreamingChannel channel return createMono(() -> commandBuilder.zrangebyscoreWithScores(channel, key, range, limit)); } + @Override + public Mono zrangestore(K dstKey, K srcKey, Range range) { + return createMono(() -> commandBuilder.zrangestore(dstKey, srcKey, range, false)); + } + @Override public Mono zrangestorebylex(K dstKey, K srcKey, Range range, Limit limit) { return createMono(() -> commandBuilder.zrangestorebylex(dstKey, srcKey, range, limit, false)); @@ -2814,6 +2819,11 @@ public Mono zrevrangebyscoreWithScores(ScoredValueStreamingChannel chan return createMono(() -> commandBuilder.zrevrangebyscoreWithScores(channel, key, range, limit)); } + @Override + public Mono zrevrangestore(K dstKey, K srcKey, Range range) { + return createMono(() -> commandBuilder.zrangestore(dstKey, srcKey, range, true)); + } + @Override public Mono zrevrangestorebylex(K dstKey, K srcKey, Range range, Limit limit) { return createMono(() -> commandBuilder.zrangestorebylex(dstKey, srcKey, range, limit, true)); diff --git a/src/main/java/io/lettuce/core/RedisCommandBuilder.java b/src/main/java/io/lettuce/core/RedisCommandBuilder.java index 543d70ae94..0e9109edcf 100644 --- a/src/main/java/io/lettuce/core/RedisCommandBuilder.java +++ b/src/main/java/io/lettuce/core/RedisCommandBuilder.java @@ -3620,6 +3620,22 @@ Command zrangebyscoreWithScores(ScoredValueStreamingChannel chann return createCommand(ZRANGEBYSCORE, new ScoredValueStreamingOutput<>(codec, channel), args); } + Command zrangestore(K dstKey, K srcKey, Range range, boolean rev) { + notNullKey(srcKey); + notNullKey(dstKey); + notNullRange(range); + + CommandArgs args = new CommandArgs<>(codec); + args.addKeys(dstKey, srcKey); + + args.add(min(range)).add(max(range)); + + if (rev) { + args.add(REV); + } + return createCommand(ZRANGESTORE, new IntegerOutput<>(codec), args); + } + Command zrangestorebylex(K dstKey, K srcKey, Range range, Limit limit, boolean rev) { notNullKey(srcKey); notNullKey(dstKey); diff --git a/src/main/java/io/lettuce/core/api/async/RedisSortedSetAsyncCommands.java b/src/main/java/io/lettuce/core/api/async/RedisSortedSetAsyncCommands.java index a8653c9361..a68af484d4 100644 --- a/src/main/java/io/lettuce/core/api/async/RedisSortedSetAsyncCommands.java +++ b/src/main/java/io/lettuce/core/api/async/RedisSortedSetAsyncCommands.java @@ -841,6 +841,18 @@ public interface RedisSortedSetAsyncCommands { */ RedisFuture zrangebyscoreWithScores(ScoredValueStreamingChannel channel, K key, Range range, Limit limit); + /** + * Get the specified range of elements in the sorted set stored at {@code srcKey} and stores the result in the + * {@code dstKey} destination key. + * + * @param dstKey the dst key. + * @param srcKey the src key. + * @param range the rank. + * @return the number of elements in the resulting sorted set. + * @since 6.2.1 + */ + RedisFuture zrangestore(K dstKey, K srcKey, Range range); + /** * Get the specified range of elements in the sorted set stored at {@code srcKey} and stores the result in the * {@code dstKey} destination key. @@ -848,7 +860,8 @@ public interface RedisSortedSetAsyncCommands { * @param dstKey the dst key. * @param srcKey the src key. * @param range the lexicographical range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ RedisFuture zrangestorebylex(K dstKey, K srcKey, Range range, Limit limit); @@ -860,7 +873,8 @@ public interface RedisSortedSetAsyncCommands { * @param dstKey the dst key. * @param srcKey the src key. * @param range the score range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ RedisFuture zrangestorebyscore(K dstKey, K srcKey, Range range, Limit limit); @@ -1318,6 +1332,18 @@ public interface RedisSortedSetAsyncCommands { */ RedisFuture zrevrangebyscoreWithScores(ScoredValueStreamingChannel channel, K key, Range range, Limit limit); + /** + * Get the specified range of elements ordered from high to low in the sorted set stored at {@code srcKey} and stores the + * result in the {@code dstKey} destination key. + * + * @param dstKey the dst key. + * @param srcKey the src key. + * @param range the rank. + * @return the number of elements in the resulting sorted set. + * @since 6.2.1 + */ + RedisFuture zrevrangestore(K dstKey, K srcKey, Range range); + /** * Get the lexicographical range ordered from high to low of elements in the sorted set stored at {@code srcKey} and stores * the result in the {@code dstKey} destination key. @@ -1325,7 +1351,8 @@ public interface RedisSortedSetAsyncCommands { * @param dstKey the src key. * @param srcKey the dst key. * @param range the lexicographical range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ RedisFuture zrevrangestorebylex(K dstKey, K srcKey, Range range, Limit limit); @@ -1338,7 +1365,8 @@ public interface RedisSortedSetAsyncCommands { * * @param srcKey the dst key. * @param range the score range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ RedisFuture zrevrangestorebyscore(K dstKey, K srcKey, Range range, Limit limit); diff --git a/src/main/java/io/lettuce/core/api/reactive/RedisSortedSetReactiveCommands.java b/src/main/java/io/lettuce/core/api/reactive/RedisSortedSetReactiveCommands.java index ea1a2c4635..f81391239e 100644 --- a/src/main/java/io/lettuce/core/api/reactive/RedisSortedSetReactiveCommands.java +++ b/src/main/java/io/lettuce/core/api/reactive/RedisSortedSetReactiveCommands.java @@ -863,6 +863,18 @@ public interface RedisSortedSetReactiveCommands { @Deprecated Mono zrangebyscoreWithScores(ScoredValueStreamingChannel channel, K key, Range range, Limit limit); + /** + * Get the specified range of elements in the sorted set stored at {@code srcKey} and stores the result in the + * {@code dstKey} destination key. + * + * @param dstKey the dst key. + * @param srcKey the src key. + * @param range the rank. + * @return the number of elements in the resulting sorted set. + * @since 6.2.1 + */ + Mono zrangestore(K dstKey, K srcKey, Range range); + /** * Get the specified range of elements in the sorted set stored at {@code srcKey} and stores the result in the * {@code dstKey} destination key. @@ -870,7 +882,8 @@ public interface RedisSortedSetReactiveCommands { * @param dstKey the dst key. * @param srcKey the src key. * @param range the lexicographical range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ Mono zrangestorebylex(K dstKey, K srcKey, Range range, Limit limit); @@ -882,7 +895,8 @@ public interface RedisSortedSetReactiveCommands { * @param dstKey the dst key. * @param srcKey the src key. * @param range the score range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ Mono zrangestorebyscore(K dstKey, K srcKey, Range range, Limit limit); @@ -1360,6 +1374,18 @@ public interface RedisSortedSetReactiveCommands { @Deprecated Mono zrevrangebyscoreWithScores(ScoredValueStreamingChannel channel, K key, Range range, Limit limit); + /** + * Get the specified range of elements ordered from high to low in the sorted set stored at {@code srcKey} and stores the + * result in the {@code dstKey} destination key. + * + * @param dstKey the dst key. + * @param srcKey the src key. + * @param range the rank. + * @return the number of elements in the resulting sorted set. + * @since 6.2.1 + */ + Mono zrevrangestore(K dstKey, K srcKey, Range range); + /** * Get the lexicographical range ordered from high to low of elements in the sorted set stored at {@code srcKey} and stores * the result in the {@code dstKey} destination key. @@ -1367,7 +1393,8 @@ public interface RedisSortedSetReactiveCommands { * @param dstKey the src key. * @param srcKey the dst key. * @param range the lexicographical range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ Mono zrevrangestorebylex(K dstKey, K srcKey, Range range, Limit limit); @@ -1380,7 +1407,8 @@ public interface RedisSortedSetReactiveCommands { * * @param srcKey the dst key. * @param range the score range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ Mono zrevrangestorebyscore(K dstKey, K srcKey, Range range, Limit limit); diff --git a/src/main/java/io/lettuce/core/api/sync/RedisSortedSetCommands.java b/src/main/java/io/lettuce/core/api/sync/RedisSortedSetCommands.java index 60e59303af..12e5f4056b 100644 --- a/src/main/java/io/lettuce/core/api/sync/RedisSortedSetCommands.java +++ b/src/main/java/io/lettuce/core/api/sync/RedisSortedSetCommands.java @@ -841,6 +841,18 @@ public interface RedisSortedSetCommands { */ Long zrangebyscoreWithScores(ScoredValueStreamingChannel channel, K key, Range range, Limit limit); + /** + * Get the specified range of elements in the sorted set stored at {@code srcKey} and stores the result in the + * {@code dstKey} destination key. + * + * @param dstKey the dst key. + * @param srcKey the src key. + * @param range the rank. + * @return the number of elements in the resulting sorted set. + * @since 6.2.1 + */ + Long zrangestore(K dstKey, K srcKey, Range range); + /** * Get the specified range of elements in the sorted set stored at {@code srcKey} and stores the result in the * {@code dstKey} destination key. @@ -848,7 +860,8 @@ public interface RedisSortedSetCommands { * @param dstKey the dst key. * @param srcKey the src key. * @param range the lexicographical range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ Long zrangestorebylex(K dstKey, K srcKey, Range range, Limit limit); @@ -860,7 +873,8 @@ public interface RedisSortedSetCommands { * @param dstKey the dst key. * @param srcKey the src key. * @param range the score range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ Long zrangestorebyscore(K dstKey, K srcKey, Range range, Limit limit); @@ -1318,6 +1332,18 @@ public interface RedisSortedSetCommands { */ Long zrevrangebyscoreWithScores(ScoredValueStreamingChannel channel, K key, Range range, Limit limit); + /** + * Get the specified range of elements ordered from high to low in the sorted set stored at {@code srcKey} and stores the + * result in the {@code dstKey} destination key. + * + * @param dstKey the dst key. + * @param srcKey the src key. + * @param range the rank. + * @return the number of elements in the resulting sorted set. + * @since 6.2.1 + */ + Long zrevrangestore(K dstKey, K srcKey, Range range); + /** * Get the lexicographical range ordered from high to low of elements in the sorted set stored at {@code srcKey} and stores * the result in the {@code dstKey} destination key. @@ -1325,7 +1351,8 @@ public interface RedisSortedSetCommands { * @param dstKey the src key. * @param srcKey the dst key. * @param range the lexicographical range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ Long zrevrangestorebylex(K dstKey, K srcKey, Range range, Limit limit); @@ -1338,7 +1365,8 @@ public interface RedisSortedSetCommands { * * @param srcKey the dst key. * @param range the score range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ Long zrevrangestorebyscore(K dstKey, K srcKey, Range range, Limit limit); diff --git a/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionSortedSetAsyncCommands.java b/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionSortedSetAsyncCommands.java index d552c3eab6..b2b5142bb1 100644 --- a/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionSortedSetAsyncCommands.java +++ b/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionSortedSetAsyncCommands.java @@ -841,6 +841,18 @@ public interface NodeSelectionSortedSetAsyncCommands { */ AsyncExecutions zrangebyscoreWithScores(ScoredValueStreamingChannel channel, K key, Range range, Limit limit); + /** + * Get the specified range of elements in the sorted set stored at {@code srcKey} and stores the result in the + * {@code dstKey} destination key. + * + * @param dstKey the dst key. + * @param srcKey the src key. + * @param range the rank. + * @return the number of elements in the resulting sorted set. + * @since 6.2.1 + */ + AsyncExecutions zrangestore(K dstKey, K srcKey, Range range); + /** * Get the specified range of elements in the sorted set stored at {@code srcKey} and stores the result in the * {@code dstKey} destination key. @@ -848,7 +860,8 @@ public interface NodeSelectionSortedSetAsyncCommands { * @param dstKey the dst key. * @param srcKey the src key. * @param range the lexicographical range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ AsyncExecutions zrangestorebylex(K dstKey, K srcKey, Range range, Limit limit); @@ -860,7 +873,8 @@ public interface NodeSelectionSortedSetAsyncCommands { * @param dstKey the dst key. * @param srcKey the src key. * @param range the score range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ AsyncExecutions zrangestorebyscore(K dstKey, K srcKey, Range range, Limit limit); @@ -1318,6 +1332,18 @@ public interface NodeSelectionSortedSetAsyncCommands { */ AsyncExecutions zrevrangebyscoreWithScores(ScoredValueStreamingChannel channel, K key, Range range, Limit limit); + /** + * Get the specified range of elements ordered from high to low in the sorted set stored at {@code srcKey} and stores the + * result in the {@code dstKey} destination key. + * + * @param dstKey the dst key. + * @param srcKey the src key. + * @param range the rank. + * @return the number of elements in the resulting sorted set. + * @since 6.2.1 + */ + AsyncExecutions zrevrangestore(K dstKey, K srcKey, Range range); + /** * Get the lexicographical range ordered from high to low of elements in the sorted set stored at {@code srcKey} and stores * the result in the {@code dstKey} destination key. @@ -1325,7 +1351,8 @@ public interface NodeSelectionSortedSetAsyncCommands { * @param dstKey the src key. * @param srcKey the dst key. * @param range the lexicographical range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ AsyncExecutions zrevrangestorebylex(K dstKey, K srcKey, Range range, Limit limit); @@ -1338,7 +1365,8 @@ public interface NodeSelectionSortedSetAsyncCommands { * * @param srcKey the dst key. * @param range the score range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ AsyncExecutions zrevrangestorebyscore(K dstKey, K srcKey, Range range, Limit limit); diff --git a/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionSortedSetCommands.java b/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionSortedSetCommands.java index 8bc783d716..102da6a908 100644 --- a/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionSortedSetCommands.java +++ b/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionSortedSetCommands.java @@ -841,6 +841,18 @@ public interface NodeSelectionSortedSetCommands { */ Executions zrangebyscoreWithScores(ScoredValueStreamingChannel channel, K key, Range range, Limit limit); + /** + * Get the specified range of elements in the sorted set stored at {@code srcKey} and stores the result in the + * {@code dstKey} destination key. + * + * @param dstKey the dst key. + * @param srcKey the src key. + * @param range the rank. + * @return the number of elements in the resulting sorted set. + * @since 6.2.1 + */ + Executions zrangestore(K dstKey, K srcKey, Range range); + /** * Get the specified range of elements in the sorted set stored at {@code srcKey} and stores the result in the * {@code dstKey} destination key. @@ -848,7 +860,8 @@ public interface NodeSelectionSortedSetCommands { * @param dstKey the dst key. * @param srcKey the src key. * @param range the lexicographical range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ Executions zrangestorebylex(K dstKey, K srcKey, Range range, Limit limit); @@ -860,7 +873,8 @@ public interface NodeSelectionSortedSetCommands { * @param dstKey the dst key. * @param srcKey the src key. * @param range the score range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ Executions zrangestorebyscore(K dstKey, K srcKey, Range range, Limit limit); @@ -1318,6 +1332,18 @@ public interface NodeSelectionSortedSetCommands { */ Executions zrevrangebyscoreWithScores(ScoredValueStreamingChannel channel, K key, Range range, Limit limit); + /** + * Get the specified range of elements ordered from high to low in the sorted set stored at {@code srcKey} and stores the + * result in the {@code dstKey} destination key. + * + * @param dstKey the dst key. + * @param srcKey the src key. + * @param range the rank. + * @return the number of elements in the resulting sorted set. + * @since 6.2.1 + */ + Executions zrevrangestore(K dstKey, K srcKey, Range range); + /** * Get the lexicographical range ordered from high to low of elements in the sorted set stored at {@code srcKey} and stores * the result in the {@code dstKey} destination key. @@ -1325,7 +1351,8 @@ public interface NodeSelectionSortedSetCommands { * @param dstKey the src key. * @param srcKey the dst key. * @param range the lexicographical range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ Executions zrevrangestorebylex(K dstKey, K srcKey, Range range, Limit limit); @@ -1338,7 +1365,8 @@ public interface NodeSelectionSortedSetCommands { * * @param srcKey the dst key. * @param range the score range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ Executions zrevrangestorebyscore(K dstKey, K srcKey, Range range, Limit limit); diff --git a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisSortedSetCoroutinesCommands.kt b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisSortedSetCoroutinesCommands.kt index 2db1c59b37..60cbb09659 100644 --- a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisSortedSetCoroutinesCommands.kt +++ b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisSortedSetCoroutinesCommands.kt @@ -493,7 +493,22 @@ interface RedisSortedSetCoroutinesCommands { * @return List> array-reply list of elements in the specified score range. * @since 4.3 */ - fun zrangebyscoreWithScores(key: K, range: Range, limit: Limit): Flow> + fun zrangebyscoreWithScores( + key: K, + range: Range, + limit: Limit + ): Flow> + + /** + * Get the specified range of elements in the sorted set stored at `srcKey` and stores the result in the `dstKey` destination key. + * + * @param dstKey the dst key. + * @param srcKey the src key. + * @param range the rank. + * @return the number of elements in the resulting sorted set. + * @since 6.2.1 + */ + suspend fun zrangestore(dstKey: K, srcKey: K, range: Range): Long? /** * Get the specified range of elements in the sorted set stored at `srcKey` and stores the result in the `dstKey` destination key. @@ -501,7 +516,8 @@ interface RedisSortedSetCoroutinesCommands { * @param dstKey the dst key. * @param srcKey the src key. * @param range the lexicographical range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ suspend fun zrangestorebylex(dstKey: K, srcKey: K, range: Range, limit: Limit): Long? @@ -512,7 +528,8 @@ interface RedisSortedSetCoroutinesCommands { * @param dstKey the dst key. * @param srcKey the src key. * @param range the score range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ suspend fun zrangestorebyscore(dstKey: K, srcKey: K, range: Range, limit: Limit): Long? @@ -649,7 +666,22 @@ interface RedisSortedSetCoroutinesCommands { * @return List array-reply list of elements in the specified score range. * @since 4.3 */ - fun zrevrangebyscoreWithScores(key: K, range: Range, limit: Limit): Flow> + fun zrevrangebyscoreWithScores( + key: K, + range: Range, + limit: Limit + ): Flow> + + /** + * Get the specified range of elements ordered from high to low in the sorted set stored at `srcKey` and stores the result in the `dstKey` destination key. + * + * @param dstKey the dst key. + * @param srcKey the src key. + * @param range the rank. + * @return the number of elements in the resulting sorted set. + * @since 6.2.1 + */ + suspend fun zrevrangestore(dstKey: K, srcKey: K, range: Range): Long? /** * Get the lexicographical range ordered from high to low of elements in the sorted set stored at `srcKey` and stores the result in the `dstKey` destination key. @@ -657,7 +689,8 @@ interface RedisSortedSetCoroutinesCommands { * @param dstKey the src key. * @param srcKey the dst key. * @param range the lexicographical range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ suspend fun zrevrangestorebylex(dstKey: K, srcKey: K, range: Range, limit: Limit): Long? @@ -668,7 +701,8 @@ interface RedisSortedSetCoroutinesCommands { * @param dstKey the src key. * @param srcKey the dst key. * @param range the score range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ suspend fun zrevrangestorebyscore(dstKey: K, srcKey: K, range: Range, limit: Limit): Long? diff --git a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisSortedSetCoroutinesCommandsImpl.kt b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisSortedSetCoroutinesCommandsImpl.kt index 40a185c90c..963703696d 100644 --- a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisSortedSetCoroutinesCommandsImpl.kt +++ b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisSortedSetCoroutinesCommandsImpl.kt @@ -158,21 +158,45 @@ internal class RedisSortedSetCoroutinesCommandsImpl(internal v override fun zrangebyscore(key: K, range: Range): Flow = ops.zrangebyscore(key, range).asFlow() - override fun zrangebyscore(key: K, range: Range, limit: Limit): Flow = ops.zrangebyscore(key, range, limit).asFlow() + override fun zrangebyscore(key: K, range: Range, limit: Limit): Flow = + ops.zrangebyscore(key, range, limit).asFlow() - override fun zrangebyscoreWithScores(key: K, range: Range): Flow> = ops.zrangebyscoreWithScores(key, range).asFlow() + override fun zrangebyscoreWithScores( + key: K, + range: Range + ): Flow> = ops.zrangebyscoreWithScores(key, range).asFlow() + + override fun zrangebyscoreWithScores( + key: K, + range: Range, + limit: Limit + ): Flow> = ops.zrangebyscoreWithScores(key, range, limit).asFlow() - override fun zrangebyscoreWithScores(key: K, range: Range, limit: Limit): Flow> = ops.zrangebyscoreWithScores(key, range, limit).asFlow() + override suspend fun zrangestore(dstKey: K, srcKey: K, range: Range): Long? = + ops.zrangestore(dstKey, srcKey, range).awaitFirstOrNull() - override suspend fun zrangestorebylex(dstKey: K, srcKey: K, range: Range, limit: Limit): Long? = ops.zrangestorebylex(dstKey, srcKey, range, limit).awaitFirstOrNull() + override suspend fun zrangestorebylex( + dstKey: K, + srcKey: K, + range: Range, + limit: Limit + ): Long? = ops.zrangestorebylex(dstKey, srcKey, range, limit).awaitFirstOrNull() - override suspend fun zrangestorebyscore(dstKey: K, srcKey: K, range: Range, limit: Limit): Long? = ops.zrangestorebyscore(dstKey, srcKey, range, limit).awaitFirstOrNull() + override suspend fun zrangestorebyscore( + dstKey: K, + srcKey: K, + range: Range, + limit: Limit + ): Long? = ops.zrangestorebyscore(dstKey, srcKey, range, limit).awaitFirstOrNull() - override suspend fun zrank(key: K, member: V): Long? = ops.zrank(key, member).awaitFirstOrNull() + override suspend fun zrank(key: K, member: V): Long? = + ops.zrank(key, member).awaitFirstOrNull() - override suspend fun zrem(key: K, vararg members: V): Long? = ops.zrem(key, *members).awaitFirstOrNull() + override suspend fun zrem(key: K, vararg members: V): Long? = + ops.zrem(key, *members).awaitFirstOrNull() - override suspend fun zremrangebylex(key: K, range: Range): Long? = ops.zremrangebylex(key, range).awaitFirstOrNull() + override suspend fun zremrangebylex(key: K, range: Range): Long? = + ops.zremrangebylex(key, range).awaitFirstOrNull() override suspend fun zremrangebyrank(key: K, start: Long, stop: Long): Long? = ops.zremrangebyrank(key, start, stop).awaitFirstOrNull() @@ -184,25 +208,54 @@ internal class RedisSortedSetCoroutinesCommandsImpl(internal v override fun zrevrangebylex(key: K, range: Range): Flow = ops.zrevrangebylex(key, range).asFlow() - override fun zrevrangebylex(key: K, range: Range, limit: Limit): Flow = ops.zrevrangebylex(key, range, limit).asFlow() - - override fun zrevrangebyscore(key: K, range: Range): Flow = ops.zrevrangebyscore(key, range).asFlow() - - override fun zrevrangebyscore(key: K, range: Range, limit: Limit): Flow = ops.zrevrangebyscore(key, range, limit).asFlow() - - override fun zrevrangebyscoreWithScores(key: K, range: Range): Flow> = ops.zrevrangebyscoreWithScores(key, range).asFlow() + override fun zrevrangebylex(key: K, range: Range, limit: Limit): Flow = + ops.zrevrangebylex(key, range, limit).asFlow() - override fun zrevrangebyscoreWithScores(key: K, range: Range, limit: Limit): Flow> = ops.zrevrangebyscoreWithScores(key, range, limit).asFlow() + override fun zrevrangebyscore(key: K, range: Range): Flow = + ops.zrevrangebyscore(key, range).asFlow() - override suspend fun zrevrangestorebylex(dstKey: K, srcKey: K, range: Range, limit: Limit): Long? = ops.zrevrangestorebylex(dstKey, srcKey, range, limit).awaitFirstOrNull() - - override suspend fun zrevrangestorebyscore(dstKey: K, srcKey: K, range: Range, limit: Limit): Long? = ops.zrevrangestorebyscore(dstKey, srcKey, range, limit).awaitFirstOrNull() - - override suspend fun zrevrank(key: K, member: V): Long? = ops.zrevrank(key, member).awaitFirstOrNull() + override fun zrevrangebyscore( + key: K, + range: Range, + limit: Limit + ): Flow = ops.zrevrangebyscore(key, range, limit).asFlow() - override suspend fun zscan(key: K): ScoredValueScanCursor? = ops.zscan(key).awaitFirstOrNull() + override fun zrevrangebyscoreWithScores( + key: K, + range: Range + ): Flow> = ops.zrevrangebyscoreWithScores(key, range).asFlow() - override suspend fun zscan(key: K, scanArgs: ScanArgs): ScoredValueScanCursor? = ops.zscan(key, scanArgs).awaitFirstOrNull() + override fun zrevrangebyscoreWithScores( + key: K, + range: Range, + limit: Limit + ): Flow> = ops.zrevrangebyscoreWithScores(key, range, limit).asFlow() + + override suspend fun zrevrangestore(dstKey: K, srcKey: K, range: Range): Long? = + ops.zrevrangestore(dstKey, srcKey, range).awaitFirstOrNull() + + override suspend fun zrevrangestorebylex( + dstKey: K, + srcKey: K, + range: Range, + limit: Limit + ): Long? = ops.zrevrangestorebylex(dstKey, srcKey, range, limit).awaitFirstOrNull() + + override suspend fun zrevrangestorebyscore( + dstKey: K, + srcKey: K, + range: Range, + limit: Limit + ): Long? = ops.zrevrangestorebyscore(dstKey, srcKey, range, limit).awaitFirstOrNull() + + override suspend fun zrevrank(key: K, member: V): Long? = + ops.zrevrank(key, member).awaitFirstOrNull() + + override suspend fun zscan(key: K): ScoredValueScanCursor? = + ops.zscan(key).awaitFirstOrNull() + + override suspend fun zscan(key: K, scanArgs: ScanArgs): ScoredValueScanCursor? = + ops.zscan(key, scanArgs).awaitFirstOrNull() override suspend fun zscan(key: K, scanCursor: ScanCursor, scanArgs: ScanArgs): ScoredValueScanCursor? = ops.zscan(key, scanCursor, scanArgs).awaitFirstOrNull() diff --git a/src/main/templates/io/lettuce/core/api/RedisSortedSetCommands.java b/src/main/templates/io/lettuce/core/api/RedisSortedSetCommands.java index 5eefd5b599..103e207a94 100644 --- a/src/main/templates/io/lettuce/core/api/RedisSortedSetCommands.java +++ b/src/main/templates/io/lettuce/core/api/RedisSortedSetCommands.java @@ -840,13 +840,25 @@ Long zrangebyscoreWithScores(ScoredValueStreamingChannel channel, K key, Stri */ Long zrangebyscoreWithScores(ScoredValueStreamingChannel channel, K key, Range range, Limit limit); + /** + * Get the specified range of elements in the sorted set stored at {@code srcKey} and stores the result in the {@code dstKey} destination key. + * + * @param dstKey the dst key. + * @param srcKey the src key. + * @param range the rank. + * @return the number of elements in the resulting sorted set. + * @since 6.2.1 + */ + Long zrangestore(K dstKey, K srcKey, Range range); + /** * Get the specified range of elements in the sorted set stored at {@code srcKey} and stores the result in the {@code dstKey} destination key. * * @param dstKey the dst key. * @param srcKey the src key. * @param range the lexicographical range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ Long zrangestorebylex(K dstKey, K srcKey, Range range, Limit limit); @@ -857,7 +869,8 @@ Long zrangebyscoreWithScores(ScoredValueStreamingChannel channel, K key, Stri * @param dstKey the dst key. * @param srcKey the src key. * @param range the score range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ Long zrangestorebyscore(K dstKey, K srcKey, Range range, Limit limit); @@ -1317,13 +1330,25 @@ Long zrevrangebyscoreWithScores(ScoredValueStreamingChannel channel, K key, S */ Long zrevrangebyscoreWithScores(ScoredValueStreamingChannel channel, K key, Range range, Limit limit); + /** + * Get the specified range of elements ordered from high to low in the sorted set stored at {@code srcKey} and stores the result in the {@code dstKey} destination key. + * + * @param dstKey the dst key. + * @param srcKey the src key. + * @param range the rank. + * @return the number of elements in the resulting sorted set. + * @since 6.2.1 + */ + Long zrevrangestore(K dstKey, K srcKey, Range range); + /** * Get the lexicographical range ordered from high to low of elements in the sorted set stored at {@code srcKey} and stores the result in the {@code dstKey} destination key. * * @param dstKey the src key. * @param srcKey the dst key. * @param range the lexicographical range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ Long zrevrangestorebylex(K dstKey, K srcKey, Range range, Limit limit); @@ -1334,7 +1359,8 @@ Long zrevrangebyscoreWithScores(ScoredValueStreamingChannel channel, K key, S * @param dstKey the src key. * @param srcKey the dst key. * @param range the score range. - * @return The number of elements in the resulting sorted set. + * @param limit the limit to apply. + * @return the number of elements in the resulting sorted set. * @since 6.1 */ Long zrevrangestorebyscore(K dstKey, K srcKey, Range range, Limit limit); diff --git a/src/test/java/io/lettuce/core/commands/SortedSetCommandIntegrationTests.java b/src/test/java/io/lettuce/core/commands/SortedSetCommandIntegrationTests.java index b41370012d..0efdfadd0f 100644 --- a/src/test/java/io/lettuce/core/commands/SortedSetCommandIntegrationTests.java +++ b/src/test/java/io/lettuce/core/commands/SortedSetCommandIntegrationTests.java @@ -473,6 +473,14 @@ void zrangebyscoreWithScoresStreaming() { } + @Test + @EnabledOnCommand("ZRANGESTORE") // Redis 6.2 + void zrangestore() { + redis.zadd(key, 1.0, "a", 2.0, "b", 3.0, "c", 4.0, "d"); + assertThat(redis.zrangestore("key1", key, Range.create(2L, 3L))).isEqualTo(2); + assertThat(redis.zrange("key1", 0, 2)).isEqualTo(list("c", "d")); + } + @Test @EnabledOnCommand("ZRANGESTORE") // Redis 6.2 void zrangestorebylex() { @@ -542,6 +550,7 @@ void zremrangebyrank() { void zrevrange() { setup(); assertThat(redis.zrevrange(key, 0, -1)).isEqualTo(list("c", "b", "a")); + assertThat(redis.zrevrange(key, 1, 2)).isEqualTo(list("b", "a")); } @Test @@ -686,6 +695,14 @@ void zrevrank() { assertThat(redis.zrevrank(key, "a")).isEqualTo(2); } + @Test + @EnabledOnCommand("ZRANGESTORE") // Redis 6.2 + void zrevrangestore() { + redis.zadd(key, 1.0, "a", 2.0, "b", 3.0, "c", 4.0, "d"); + assertThat(redis.zrevrangestore("key1", key, Range.create(2L, 3L))).isEqualTo(2); + assertThat(redis.zrange("key1", 0, 2)).isEqualTo(list("a", "b")); + } + @Test @EnabledOnCommand("ZRANGESTORE") // Redis 6.2 void zrevrangestorebylex() {