From 4722679004bf1c58f2f0260d83dff2460094e99b Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Thu, 21 Jan 2021 09:31:54 +0300 Subject: [PATCH] Feature - add revRangeTo() method to RScoredSortedSet object #3362 --- .../org/redisson/RedissonScoredSortedSet.java | 34 +++++++++++++ .../org/redisson/api/RScoredSortedSet.java | 50 +++++++++++++++++++ .../redisson/api/RScoredSortedSetAsync.java | 48 ++++++++++++++++++ .../api/RScoredSortedSetReactive.java | 50 +++++++++++++++++++ .../org/redisson/api/RScoredSortedSetRx.java | 50 +++++++++++++++++++ .../redisson/RedissonScoredSortedSetTest.java | 15 +++++- 6 files changed, 246 insertions(+), 1 deletion(-) diff --git a/redisson/src/main/java/org/redisson/RedissonScoredSortedSet.java b/redisson/src/main/java/org/redisson/RedissonScoredSortedSet.java index 68e98222c..0bf888ef8 100644 --- a/redisson/src/main/java/org/redisson/RedissonScoredSortedSet.java +++ b/redisson/src/main/java/org/redisson/RedissonScoredSortedSet.java @@ -1319,6 +1319,40 @@ public class RedissonScoredSortedSet extends RedissonExpirable implements RSc return get(rangeToAsync(destName, startScore, startScoreInclusive, endScore, endScoreInclusive, offset, count)); } + @Override + public int revRangeTo(String destName, int startIndex, int endIndex) { + return get(revRangeToAsync(destName, startIndex, endIndex)); + } + + @Override + public int revRangeTo(String destName, double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive) { + return get(revRangeToAsync(destName, startScore, startScoreInclusive, endScore, endScoreInclusive)); + } + + @Override + public int revRangeTo(String destName, double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive, int offset, int count) { + return get(revRangeToAsync(destName, startScore, startScoreInclusive, endScore, endScoreInclusive, offset, count)); + } + + @Override + public RFuture revRangeToAsync(String destName, int startIndex, int endIndex) { + return commandExecutor.writeAsync(getName(), codec, RedisCommands.ZRANGESTORE, destName, getName(), startIndex, endIndex, "REV"); + } + + @Override + public RFuture revRangeToAsync(String destName, double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive) { + String startValue = value(startScore, startScoreInclusive); + String endValue = value(endScore, endScoreInclusive); + return commandExecutor.writeAsync(getName(), codec, RedisCommands.ZRANGESTORE, destName, getName(), startValue, endValue, "BYSCORE", "REV"); + } + + @Override + public RFuture revRangeToAsync(String destName, double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive, int offset, int count) { + String startValue = value(startScore, startScoreInclusive); + String endValue = value(endScore, endScoreInclusive); + return commandExecutor.writeAsync(getName(), codec, RedisCommands.ZRANGESTORE, destName, getName(), startValue, endValue, "BYSCORE", "REV", "LIMIT", offset, count); + } + @Override public RFuture rangeToAsync(String destName, int startIndex, int endIndex) { return commandExecutor.writeAsync(getName(), codec, RedisCommands.ZRANGESTORE, destName, getName(), startIndex, endIndex); diff --git a/redisson/src/main/java/org/redisson/api/RScoredSortedSet.java b/redisson/src/main/java/org/redisson/api/RScoredSortedSet.java index 6901054c6..1d574e5bd 100644 --- a/redisson/src/main/java/org/redisson/api/RScoredSortedSet.java +++ b/redisson/src/main/java/org/redisson/api/RScoredSortedSet.java @@ -583,6 +583,56 @@ public interface RScoredSortedSet extends RScoredSortedSetAsync, Iterable< */ int rangeTo(String destName, double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive, int offset, int count); + /** + * Stores to defined ScoredSortedSet values in reversed order by rank range. Indexes are zero based. + * -1 means the highest score, -2 means the second highest score. + *

+ * Requires Redis 6.2.0 and higher. + * + * @param startIndex - start index + * @param endIndex - end index + * @return elements + */ + int revRangeTo(String destName, int startIndex, int endIndex); + + /** + * Stores to defined ScoredSortedSet values in reversed order between startScore and endScore. + *

+ * Requires Redis 6.2.0 and higher. + * + * @param startScore - start score. + * Use Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY + * to define infinity numbers + * @param startScoreInclusive - start score inclusive + * @param endScore - end score + * Use Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY + * to define infinity numbers + * + * @param endScoreInclusive - end score inclusive + * @return values + */ + int revRangeTo(String destName, double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive); + + /** + * Stores to defined ScoredSortedSet values in reversed order between startScore and endScore. + *

+ * Requires Redis 6.2.0 and higher. + * + * @param startScore - start score. + * Use Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY + * to define infinity numbers + * @param startScoreInclusive - start score inclusive + * @param endScore - end score + * Use Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY + * to define infinity numbers + * + * @param endScoreInclusive - end score inclusive + * @param offset - offset of sorted data + * @param count - amount of sorted data + * @return values + */ + int revRangeTo(String destName, double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive, int offset, int count); + /** * Returns values by rank range. Indexes are zero based. * -1 means the highest score, -2 means the second highest score. diff --git a/redisson/src/main/java/org/redisson/api/RScoredSortedSetAsync.java b/redisson/src/main/java/org/redisson/api/RScoredSortedSetAsync.java index 15c952caa..283f6a1b0 100644 --- a/redisson/src/main/java/org/redisson/api/RScoredSortedSetAsync.java +++ b/redisson/src/main/java/org/redisson/api/RScoredSortedSetAsync.java @@ -448,6 +448,54 @@ public interface RScoredSortedSetAsync extends RExpirableAsync, RSortableAsyn */ RFuture rangeToAsync(String destName, double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive, int offset, int count); + /** + * Stores to defined ScoredSortedSet values in reversed order by rank range. Indexes are zero based. + * -1 means the highest score, -2 means the second highest score. + * + * @param startIndex - start index + * @param endIndex - end index + * @return elements + */ + RFuture revRangeToAsync(String destName, int startIndex, int endIndex); + + /** + * Stores to defined ScoredSortedSet values in reversed order between startScore and endScore. + *

+ * Requires Redis 6.2.0 and higher. + * + * @param startScore - start score. + * Use Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY + * to define infinity numbers + * @param startScoreInclusive - start score inclusive + * @param endScore - end score + * Use Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY + * to define infinity numbers + * + * @param endScoreInclusive - end score inclusive + * @return values + */ + RFuture revRangeToAsync(String destName, double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive); + + /** + * Stores to defined ScoredSortedSet values in reversed order between startScore and endScore. + *

+ * Requires Redis 6.2.0 and higher. + * + * @param startScore - start score. + * Use Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY + * to define infinity numbers + * @param startScoreInclusive - start score inclusive + * @param endScore - end score + * Use Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY + * to define infinity numbers + * + * @param endScoreInclusive - end score inclusive + * @param offset - offset of sorted data + * @param count - amount of sorted data + * @return values + */ + RFuture revRangeToAsync(String destName, double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive, int offset, int count); + /** * Returns values by rank range. Indexes are zero based. * -1 means the highest score, -2 means the second highest score. diff --git a/redisson/src/main/java/org/redisson/api/RScoredSortedSetReactive.java b/redisson/src/main/java/org/redisson/api/RScoredSortedSetReactive.java index 43674e24f..f6fff0a77 100644 --- a/redisson/src/main/java/org/redisson/api/RScoredSortedSetReactive.java +++ b/redisson/src/main/java/org/redisson/api/RScoredSortedSetReactive.java @@ -667,6 +667,56 @@ public interface RScoredSortedSetReactive extends RExpirableReactive, RSortab */ Mono rangeTo(String destName, double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive, int offset, int count); + /** + * Stores to defined ScoredSortedSet values in reversed order by rank range. Indexes are zero based. + * -1 means the highest score, -2 means the second highest score. + *

+ * Requires Redis 6.2.0 and higher. + * + * @param startIndex - start index + * @param endIndex - end index + * @return elements + */ + Mono revRangeTo(String destName, int startIndex, int endIndex); + + /** + * Stores to defined ScoredSortedSet values in reversed order between startScore and endScore. + *

+ * Requires Redis 6.2.0 and higher. + * + * @param startScore - start score. + * Use Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY + * to define infinity numbers + * @param startScoreInclusive - start score inclusive + * @param endScore - end score + * Use Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY + * to define infinity numbers + * + * @param endScoreInclusive - end score inclusive + * @return values + */ + Mono revRangeTo(String destName, double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive); + + /** + * Stores to defined ScoredSortedSet values in reversed order between startScore and endScore. + *

+ * Requires Redis 6.2.0 and higher. + * + * @param startScore - start score. + * Use Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY + * to define infinity numbers + * @param startScoreInclusive - start score inclusive + * @param endScore - end score + * Use Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY + * to define infinity numbers + * + * @param endScoreInclusive - end score inclusive + * @param offset - offset of sorted data + * @param count - amount of sorted data + * @return values + */ + Mono revRangeTo(String destName, double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive, int offset, int count); + /** * Intersect provided ScoredSortedSets * and store result to current ScoredSortedSet diff --git a/redisson/src/main/java/org/redisson/api/RScoredSortedSetRx.java b/redisson/src/main/java/org/redisson/api/RScoredSortedSetRx.java index 6ca5361a4..3196aca50 100644 --- a/redisson/src/main/java/org/redisson/api/RScoredSortedSetRx.java +++ b/redisson/src/main/java/org/redisson/api/RScoredSortedSetRx.java @@ -474,6 +474,56 @@ public interface RScoredSortedSetRx extends RExpirableRx, RSortableRx> */ Single rangeTo(String destName, double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive, int offset, int count); + /** + * Stores to defined ScoredSortedSet values in reversed order by rank range. Indexes are zero based. + * -1 means the highest score, -2 means the second highest score. + *

+ * Requires Redis 6.2.0 and higher. + * + * @param startIndex - start index + * @param endIndex - end index + * @return elements + */ + Single revRangeTo(String destName, int startIndex, int endIndex); + + /** + * Stores to defined ScoredSortedSet values in reversed order between startScore and endScore. + *

+ * Requires Redis 6.2.0 and higher. + * + * @param startScore - start score. + * Use Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY + * to define infinity numbers + * @param startScoreInclusive - start score inclusive + * @param endScore - end score + * Use Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY + * to define infinity numbers + * + * @param endScoreInclusive - end score inclusive + * @return values + */ + Single revRangeTo(String destName, double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive); + + /** + * Stores to defined ScoredSortedSet values in reversed order between startScore and endScore. + *

+ * Requires Redis 6.2.0 and higher. + * + * @param startScore - start score. + * Use Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY + * to define infinity numbers + * @param startScoreInclusive - start score inclusive + * @param endScore - end score + * Use Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY + * to define infinity numbers + * + * @param endScoreInclusive - end score inclusive + * @param offset - offset of sorted data + * @param count - amount of sorted data + * @return values + */ + Single revRangeTo(String destName, double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive, int offset, int count); + /** * Returns values by rank range. Indexes are zero based. * -1 means the highest score, -2 means the second highest score. diff --git a/redisson/src/test/java/org/redisson/RedissonScoredSortedSetTest.java b/redisson/src/test/java/org/redisson/RedissonScoredSortedSetTest.java index a08e72291..3e8dbf793 100644 --- a/redisson/src/test/java/org/redisson/RedissonScoredSortedSetTest.java +++ b/redisson/src/test/java/org/redisson/RedissonScoredSortedSetTest.java @@ -1390,7 +1390,20 @@ public class RedissonScoredSortedSetTest extends BaseTest { set1.rangeTo("simple2", 0, 3); RScoredSortedSet set2 = redisson.getScoredSortedSet("simple2"); - assertThat(set2.readAll()).containsOnly(0, 1, 2, 3, 4); + assertThat(set2.readAll()).containsOnly(0, 1, 2, 3); + } + + @Test + public void testRevRange() { + RScoredSortedSet set1 = redisson.getScoredSortedSet("simple1"); + for (int i = 0; i < 10; i++) { + set1.add(i, i); + + } + + set1.revRangeTo("simple2", 3, true, 0, false); + RScoredSortedSet set2 = redisson.getScoredSortedSet("simple2"); + assertThat(set2.readAll()).containsOnly(3, 2, 1); } @Test