Skip to content

Commit

Permalink
added support for Double.NEGATIVE_INFINITY and Double.POSITIVE_INFINI…
Browse files Browse the repository at this point in the history
…TY as score range parameter

referring to #426. tests included too.
  • Loading branch information
jackygurui committed Mar 10, 2016
1 parent 9be5fe2 commit 2e679b2
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/main/java/org/redisson/RedissonScoredSortedSet.java
Expand Up @@ -165,12 +165,18 @@ public int removeRangeByScore(double startScore, boolean startScoreInclusive, do

@Override
public Future<Integer> 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;
}
Expand Down Expand Up @@ -406,8 +412,8 @@ public Collection<V> valueRange(double startScore, boolean startScoreInclusive,

@Override
public Future<Collection<V>> 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);
}

Expand All @@ -418,8 +424,8 @@ public Collection<ScoredEntry<V>> entryRange(double startScore, boolean startSco

@Override
public Future<Collection<ScoredEntry<V>>> 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");
}

Expand All @@ -430,8 +436,8 @@ public Collection<V> valueRange(double startScore, boolean startScoreInclusive,

@Override
public Future<Collection<V>> 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);
}

Expand All @@ -442,8 +448,8 @@ public Collection<ScoredEntry<V>> entryRange(double startScore, boolean startSco

@Override
public Future<Collection<ScoredEntry<V>>> 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);
}

Expand Down
96 changes: 96 additions & 0 deletions src/test/java/org/redisson/RedissonScoredSortedSetTest.java
Expand Up @@ -103,6 +103,36 @@ public void testRemoveRangeByScore() {
MatcherAssert.assertThat(set, Matchers.contains("a", "d", "e", "f", "g"));
}

@Test
public void testRemoveRangeByScoreNegativeInf() {
RScoredSortedSet<String> 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<String> 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<String> set = redisson.getScoredSortedSet("simple");
Expand Down Expand Up @@ -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<String> set = redisson.<String>getScoredSortedSet("simple");

set.add(0, "a");
set.add(1, "b");
set.add(2, "c");
set.add(3, "d");
set.add(4, "e");

Collection<String> 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<String> set = redisson.<String>getScoredSortedSet("simple");

set.add(0, "a");
set.add(1, "b");
set.add(2, "c");
set.add(3, "d");
set.add(4, "e");

Collection<String> 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() {
Expand All @@ -539,6 +599,42 @@ public void testScoredSortedSetEntryRange() {
Assert.assertEquals("c", a[0].getValue());
Assert.assertEquals("d", a[1].getValue());
}

@Test
public void testScoredSortedSetEntryRangeNegativeInf() {
RScoredSortedSet<String> set = redisson.<String>getScoredSortedSet("simple");

set.add(0, "a");
set.add(1, "b");
set.add(2, "c");
set.add(3, "d");
set.add(4, "e");

Collection<ScoredEntry<String>> r = set.entryRange(Double.NEGATIVE_INFINITY, true, 4, false, 1, 2);
ScoredEntry<String>[] 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<String> set = redisson.<String>getScoredSortedSet("simple");

set.add(0, "a");
set.add(1, "b");
set.add(2, "c");
set.add(3, "d");
set.add(4, "e");

Collection<ScoredEntry<String>> r = set.entryRange(1, true, Double.POSITIVE_INFINITY, false, 1, 2);
ScoredEntry<String>[] 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 {
Expand Down

0 comments on commit 2e679b2

Please sign in to comment.