diff --git a/src/main/java/org/redisson/RedissonScoredSortedSet.java b/src/main/java/org/redisson/RedissonScoredSortedSet.java index 8f5a5902d01..8ce8f1d8065 100644 --- a/src/main/java/org/redisson/RedissonScoredSortedSet.java +++ b/src/main/java/org/redisson/RedissonScoredSortedSet.java @@ -165,12 +165,18 @@ public int removeRangeByScore(double startScore, boolean startScoreInclusive, do @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 Collection valueRange(double startScore, boolean startScoreInclusive, @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); } @@ -418,8 +424,8 @@ public Collection> entryRange(double startScore, boolean startSco @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"); } @@ -430,8 +436,8 @@ public Collection valueRange(double startScore, boolean startScoreInclusive, @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); } @@ -442,8 +448,8 @@ public Collection> entryRange(double startScore, boolean startSco @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 9996ebb01f1..731ad7daa88 100644 --- a/src/test/java/org/redisson/RedissonScoredSortedSetTest.java +++ b/src/test/java/org/redisson/RedissonScoredSortedSetTest.java @@ -103,6 +103,36 @@ public void testRemoveRangeByScore() { 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"); @@ -521,6 +551,36 @@ public void testScoredSortedSetValueRange() { String[] a = r.toArray(new String[0]); Assert.assertArrayEquals(new String[]{"c", "d"}, a); } + + @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() { @@ -539,6 +599,42 @@ public void testScoredSortedSetEntryRange() { 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 {