added support for Double.NEGATIVE_INFINITY and Double.POSITIVE_INFINITY as score range parameter

referring to #426. tests included too.
pull/427/head
jackygurui 9 years ago
parent 9be5fe2abd
commit 2e679b222e

@ -165,12 +165,18 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
@Override @Override
public Future<Integer> removeRangeByScoreAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive) { public Future<Integer> removeRangeByScoreAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive) {
String startValue = value(BigDecimal.valueOf(startScore).toPlainString(), startScoreInclusive); String startValue = value(startScore, startScoreInclusive);
String endValue = value(BigDecimal.valueOf(endScore).toPlainString(), endScoreInclusive); String endValue = value(endScore, endScoreInclusive);
return commandExecutor.writeAsync(getName(), codec, RedisCommands.ZREMRANGEBYSCORE, getName(), startValue, endValue); 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) { if (!inclusive) {
element = "(" + element; element = "(" + element;
} }
@ -406,8 +412,8 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
@Override @Override
public Future<Collection<V>> valueRangeAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive) { public Future<Collection<V>> valueRangeAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive) {
String startValue = value(BigDecimal.valueOf(startScore).toPlainString(), startScoreInclusive); String startValue = value(startScore, startScoreInclusive);
String endValue = value(BigDecimal.valueOf(endScore).toPlainString(), endScoreInclusive); String endValue = value(endScore, endScoreInclusive);
return commandExecutor.readAsync(getName(), codec, RedisCommands.ZRANGEBYSCORE, getName(), startValue, endValue); return commandExecutor.readAsync(getName(), codec, RedisCommands.ZRANGEBYSCORE, getName(), startValue, endValue);
} }
@ -418,8 +424,8 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
@Override @Override
public Future<Collection<ScoredEntry<V>>> entryRangeAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive) { public Future<Collection<ScoredEntry<V>>> entryRangeAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive) {
String startValue = value(BigDecimal.valueOf(startScore).toPlainString(), startScoreInclusive); String startValue = value(startScore, startScoreInclusive);
String endValue = value(BigDecimal.valueOf(endScore).toPlainString(), endScoreInclusive); String endValue = value(endScore, endScoreInclusive);
return commandExecutor.readAsync(getName(), codec, RedisCommands.ZRANGEBYSCORE_ENTRY, getName(), startValue, endValue, "WITHSCORES"); return commandExecutor.readAsync(getName(), codec, RedisCommands.ZRANGEBYSCORE_ENTRY, getName(), startValue, endValue, "WITHSCORES");
} }
@ -430,8 +436,8 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
@Override @Override
public Future<Collection<V>> valueRangeAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive, int offset, int count) { 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 startValue = value(startScore, startScoreInclusive);
String endValue = value(BigDecimal.valueOf(endScore).toPlainString(), endScoreInclusive); String endValue = value(endScore, endScoreInclusive);
return commandExecutor.readAsync(getName(), codec, RedisCommands.ZRANGEBYSCORE, getName(), startValue, endValue, "LIMIT", offset, count); return commandExecutor.readAsync(getName(), codec, RedisCommands.ZRANGEBYSCORE, getName(), startValue, endValue, "LIMIT", offset, count);
} }
@ -442,8 +448,8 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
@Override @Override
public Future<Collection<ScoredEntry<V>>> entryRangeAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive, int offset, int count) { 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 startValue = value(startScore, startScoreInclusive);
String endValue = value(BigDecimal.valueOf(endScore).toPlainString(), endScoreInclusive); String endValue = value(endScore, endScoreInclusive);
return commandExecutor.readAsync(getName(), codec, RedisCommands.ZRANGEBYSCORE_ENTRY, getName(), startValue, endValue, "WITHSCORES", "LIMIT", offset, count); return commandExecutor.readAsync(getName(), codec, RedisCommands.ZRANGEBYSCORE_ENTRY, getName(), startValue, endValue, "WITHSCORES", "LIMIT", offset, count);
} }

@ -103,6 +103,36 @@ public class RedissonScoredSortedSetTest extends BaseTest {
MatcherAssert.assertThat(set, Matchers.contains("a", "d", "e", "f", "g")); 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 @Test
public void testRemoveRangeByRank() { public void testRemoveRangeByRank() {
RScoredSortedSet<String> set = redisson.getScoredSortedSet("simple"); RScoredSortedSet<String> set = redisson.getScoredSortedSet("simple");
@ -521,6 +551,36 @@ public class RedissonScoredSortedSetTest extends BaseTest {
String[] a = r.toArray(new String[0]); String[] a = r.toArray(new String[0]);
Assert.assertArrayEquals(new String[]{"c", "d"}, a); 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 @Test
public void testScoredSortedSetEntryRange() { public void testScoredSortedSetEntryRange() {
@ -539,6 +599,42 @@ public class RedissonScoredSortedSetTest extends BaseTest {
Assert.assertEquals("c", a[0].getValue()); Assert.assertEquals("c", a[0].getValue());
Assert.assertEquals("d", a[1].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 @Test
public void testAddAndGet() throws InterruptedException { public void testAddAndGet() throws InterruptedException {

Loading…
Cancel
Save