diff --git a/src/main/java/org/redisson/RedissonScoredSortedSet.java b/src/main/java/org/redisson/RedissonScoredSortedSet.java index 1588fb381..13d76e593 100644 --- a/src/main/java/org/redisson/RedissonScoredSortedSet.java +++ b/src/main/java/org/redisson/RedissonScoredSortedSet.java @@ -165,12 +165,18 @@ public class RedissonScoredSortedSet extends RedissonExpirable implements RSc @Override public Future removeRangeByScoreAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive) { - String startValue = value(BigDecimal.valueOf(startScore).toPlainString(), startScoreInclusive); - String endValue = value(BigDecimal.valueOf(endScore).toPlainString(), endScoreInclusive); + String startValue = value(startScore, startScoreInclusive); + String endValue = value(endScore, endScoreInclusive); return commandExecutor.writeAsync(getName(), codec, RedisCommands.ZREMRANGEBYSCORE, getName(), startValue, endValue); } - private String value(String element, boolean inclusive) { + private String value(double score, boolean inclusive) { + String element; + if (Double.isInfinite(score)) { + element = (score > 0 ? "+" : "-") + "inf"; + } else { + element = BigDecimal.valueOf(score).toPlainString(); + } if (!inclusive) { element = "(" + element; } @@ -406,8 +412,8 @@ public class RedissonScoredSortedSet extends RedissonExpirable implements RSc @Override public Future> valueRangeAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive) { - String startValue = value(BigDecimal.valueOf(startScore).toPlainString(), startScoreInclusive); - String endValue = value(BigDecimal.valueOf(endScore).toPlainString(), endScoreInclusive); + String startValue = value(startScore, startScoreInclusive); + String endValue = value(endScore, endScoreInclusive); return commandExecutor.readAsync(getName(), codec, RedisCommands.ZRANGEBYSCORE, getName(), startValue, endValue); } @@ -433,8 +439,8 @@ public class RedissonScoredSortedSet extends RedissonExpirable implements RSc @Override public Future>> entryRangeAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive) { - String startValue = value(BigDecimal.valueOf(startScore).toPlainString(), startScoreInclusive); - String endValue = value(BigDecimal.valueOf(endScore).toPlainString(), endScoreInclusive); + String startValue = value(startScore, startScoreInclusive); + String endValue = value(endScore, endScoreInclusive); return commandExecutor.readAsync(getName(), codec, RedisCommands.ZRANGEBYSCORE_ENTRY, getName(), startValue, endValue, "WITHSCORES"); } @@ -445,8 +451,8 @@ public class RedissonScoredSortedSet extends RedissonExpirable implements RSc @Override public Future> valueRangeAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive, int offset, int count) { - String startValue = value(BigDecimal.valueOf(startScore).toPlainString(), startScoreInclusive); - String endValue = value(BigDecimal.valueOf(endScore).toPlainString(), endScoreInclusive); + String startValue = value(startScore, startScoreInclusive); + String endValue = value(endScore, endScoreInclusive); return commandExecutor.readAsync(getName(), codec, RedisCommands.ZRANGEBYSCORE, getName(), startValue, endValue, "LIMIT", offset, count); } @@ -469,8 +475,8 @@ public class RedissonScoredSortedSet extends RedissonExpirable implements RSc @Override public Future>> entryRangeAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive, int offset, int count) { - String startValue = value(BigDecimal.valueOf(startScore).toPlainString(), startScoreInclusive); - String endValue = value(BigDecimal.valueOf(endScore).toPlainString(), endScoreInclusive); + String startValue = value(startScore, startScoreInclusive); + String endValue = value(endScore, endScoreInclusive); return commandExecutor.readAsync(getName(), codec, RedisCommands.ZRANGEBYSCORE_ENTRY, getName(), startValue, endValue, "WITHSCORES", "LIMIT", offset, count); } diff --git a/src/test/java/org/redisson/RedissonScoredSortedSetTest.java b/src/test/java/org/redisson/RedissonScoredSortedSetTest.java index e5290ac8e..8da3dacd0 100644 --- a/src/test/java/org/redisson/RedissonScoredSortedSetTest.java +++ b/src/test/java/org/redisson/RedissonScoredSortedSetTest.java @@ -103,6 +103,36 @@ public class RedissonScoredSortedSetTest extends BaseTest { MatcherAssert.assertThat(set, Matchers.contains("a", "d", "e", "f", "g")); } + @Test + public void testRemoveRangeByScoreNegativeInf() { + RScoredSortedSet set = redisson.getScoredSortedSet("simple"); + set.add(0.1, "a"); + set.add(0.2, "b"); + set.add(0.3, "c"); + set.add(0.4, "d"); + set.add(0.5, "e"); + set.add(0.6, "f"); + set.add(0.7, "g"); + + Assert.assertEquals(3, set.removeRangeByScore(Double.NEGATIVE_INFINITY, false, 0.3, true)); + MatcherAssert.assertThat(set, Matchers.contains("d", "e", "f", "g")); + } + + @Test + public void testRemoveRangeByScorePositiveInf() { + RScoredSortedSet set = redisson.getScoredSortedSet("simple"); + set.add(0.1, "a"); + set.add(0.2, "b"); + set.add(0.3, "c"); + set.add(0.4, "d"); + set.add(0.5, "e"); + set.add(0.6, "f"); + set.add(0.7, "g"); + + Assert.assertEquals(3, set.removeRangeByScore(0.4, false, Double.POSITIVE_INFINITY, true)); + MatcherAssert.assertThat(set, Matchers.contains("a", "b", "c", "d")); + } + @Test public void testRemoveRangeByRank() { RScoredSortedSet set = redisson.getScoredSortedSet("simple"); @@ -562,6 +592,36 @@ public class RedissonScoredSortedSetTest extends BaseTest { Collection r = set.valueRangeReversed(1, true, 4, false); assertThat(r).containsExactly("d", "c", "b"); } + + @Test + public void testScoredSortedSetValueRangeNegativeInf() { + RScoredSortedSet set = redisson.getScoredSortedSet("simple"); + + set.add(0, "a"); + set.add(1, "b"); + set.add(2, "c"); + set.add(3, "d"); + set.add(4, "e"); + + Collection r = set.valueRange(Double.NEGATIVE_INFINITY, true, 4, false, 1, 2); + String[] a = r.toArray(new String[0]); + Assert.assertArrayEquals(new String[]{"b", "c"}, a); + } + + @Test + public void testScoredSortedSetValueRangePositiveInf() { + RScoredSortedSet set = redisson.getScoredSortedSet("simple"); + + set.add(0, "a"); + set.add(1, "b"); + set.add(2, "c"); + set.add(3, "d"); + set.add(4, "e"); + + Collection r = set.valueRange(1, true, Double.POSITIVE_INFINITY, false, 1, 2); + String[] a = r.toArray(new String[0]); + Assert.assertArrayEquals(new String[]{"c", "d"}, a); + } @Test public void testScoredSortedSetEntryRange() { @@ -580,6 +640,42 @@ public class RedissonScoredSortedSetTest extends BaseTest { Assert.assertEquals("c", a[0].getValue()); Assert.assertEquals("d", a[1].getValue()); } + + @Test + public void testScoredSortedSetEntryRangeNegativeInf() { + RScoredSortedSet set = redisson.getScoredSortedSet("simple"); + + set.add(0, "a"); + set.add(1, "b"); + set.add(2, "c"); + set.add(3, "d"); + set.add(4, "e"); + + Collection> r = set.entryRange(Double.NEGATIVE_INFINITY, true, 4, false, 1, 2); + ScoredEntry[] a = r.toArray(new ScoredEntry[0]); + Assert.assertEquals(1d, a[0].getScore(), 0); + Assert.assertEquals(2d, a[1].getScore(), 0); + Assert.assertEquals("b", a[0].getValue()); + Assert.assertEquals("c", a[1].getValue()); + } + + @Test + public void testScoredSortedSetEntryRangePositiveInf() { + RScoredSortedSet set = redisson.getScoredSortedSet("simple"); + + set.add(0, "a"); + set.add(1, "b"); + set.add(2, "c"); + set.add(3, "d"); + set.add(4, "e"); + + Collection> r = set.entryRange(1, true, Double.POSITIVE_INFINITY, false, 1, 2); + ScoredEntry[] a = r.toArray(new ScoredEntry[0]); + Assert.assertEquals(2d, a[0].getScore(), 0); + Assert.assertEquals(3d, a[1].getScore(), 0); + Assert.assertEquals("c", a[0].getValue()); + Assert.assertEquals("d", a[1].getValue()); + } @Test public void testAddAndGet() throws InterruptedException {