From 663d6a217f8bcb53ffbd2d049b638eda62030232 Mon Sep 17 00:00:00 2001 From: Rui Gu Date: Mon, 24 Aug 2015 00:22:40 +0100 Subject: [PATCH 1/3] SentinelConnectionManager fails to instantiate When the redis master has no slave connected, SentinelConnectionManager fails to instantiate. --- .../connection/SentinelConnectionManager.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/redisson/connection/SentinelConnectionManager.java b/src/main/java/org/redisson/connection/SentinelConnectionManager.java index c05fb0b27..4682a7bd6 100644 --- a/src/main/java/org/redisson/connection/SentinelConnectionManager.java +++ b/src/main/java/org/redisson/connection/SentinelConnectionManager.java @@ -81,18 +81,20 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager { // TODO async List> sentinelSlaves = connection.sync(RedisCommands.SENTINEL_SLAVES, cfg.getMasterName()); for (Map map : sentinelSlaves) { - String ip = map.get("ip"); - String port = map.get("port"); - String flags = map.get("flags"); + if (!map.isEmpty()) { + String ip = map.get("ip"); + String port = map.get("port"); + String flags = map.get("flags"); - String host = ip + ":" + port; + String host = ip + ":" + port; - c.addSlaveAddress(host); - slaves.put(host, true); - log.info("slave: {} added, params: {}", host, map); + c.addSlaveAddress(host); + slaves.put(host, true); + log.info("slave: {} added, params: {}", host, map); - if (flags.contains("s_down") || flags.contains("disconnected")) { - disconnectedSlaves.add(host); + if (flags.contains("s_down") || flags.contains("disconnected")) { + disconnectedSlaves.add(host); + } } } break; From 74e75def39641e8fb672d4b06ad0da588438103f Mon Sep 17 00:00:00 2001 From: jackygurui Date: Sat, 14 Nov 2015 23:31:13 +0000 Subject: [PATCH 2/3] added lexRange methods with limit options --- .../org/redisson/RedissonLexSortedSet.java | 35 +++++++++++++++++++ .../java/org/redisson/core/RLexSortedSet.java | 6 ++++ .../org/redisson/core/RLexSortedSetAsync.java | 6 ++++ .../redisson/RedissonScoredSortedSetTest.java | 15 ++++++++ 4 files changed, 62 insertions(+) diff --git a/src/main/java/org/redisson/RedissonLexSortedSet.java b/src/main/java/org/redisson/RedissonLexSortedSet.java index 19c2428ac..38313f650 100644 --- a/src/main/java/org/redisson/RedissonLexSortedSet.java +++ b/src/main/java/org/redisson/RedissonLexSortedSet.java @@ -103,6 +103,41 @@ public class RedissonLexSortedSet extends RedissonScoredSortedSet implem return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), fromValue, toValue); } + @Override + public Collection lexRange(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive, int offset, int count) { + return get(lexRangeAsync(fromElement, fromInclusive, toElement, toInclusive, offset, count)); + } + + @Override + public Collection lexRangeHead(String toElement, boolean toInclusive, int offset, int count) { + return get(lexRangeHeadAsync(toElement, toInclusive, offset, count)); + } + + @Override + public Future> lexRangeHeadAsync(String toElement, boolean toInclusive, int offset, int count) { + String toValue = value(toElement, toInclusive); + return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), "-", toValue, "LIMIT", offset, count); + } + + @Override + public Collection lexRangeTail(String fromElement, boolean fromInclusive, int offset, int count) { + return get(lexRangeTailAsync(fromElement, fromInclusive, offset, count)); + } + + @Override + public Future> lexRangeTailAsync(String fromElement, boolean fromInclusive, int offset, int count) { + String fromValue = value(fromElement, fromInclusive); + return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), fromValue, "+", "LIMIT", offset, count); + } + + @Override + public Future> lexRangeAsync(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive, int offset, int count) { + String fromValue = value(fromElement, fromInclusive); + String toValue = value(toElement, toInclusive); + + return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), fromValue, toValue, "LIMIT", offset, count); + } + @Override public int lexCountTail(String fromElement, boolean fromInclusive) { return get(lexCountTailAsync(fromElement, fromInclusive)); diff --git a/src/main/java/org/redisson/core/RLexSortedSet.java b/src/main/java/org/redisson/core/RLexSortedSet.java index b79424042..1f68f6180 100644 --- a/src/main/java/org/redisson/core/RLexSortedSet.java +++ b/src/main/java/org/redisson/core/RLexSortedSet.java @@ -36,6 +36,12 @@ public interface RLexSortedSet extends RLexSortedSetAsync, Set, RExpirab Collection lexRange(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive); + Collection lexRangeTail(String fromElement, boolean fromInclusive, int offset, int count); + + Collection lexRangeHead(String toElement, boolean toInclusive, int offset, int count); + + Collection lexRange(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive, int offset, int count); + int lexCount(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive); int rank(String o); diff --git a/src/main/java/org/redisson/core/RLexSortedSetAsync.java b/src/main/java/org/redisson/core/RLexSortedSetAsync.java index e83d058cb..abdb18cb4 100644 --- a/src/main/java/org/redisson/core/RLexSortedSetAsync.java +++ b/src/main/java/org/redisson/core/RLexSortedSetAsync.java @@ -37,6 +37,12 @@ public interface RLexSortedSetAsync extends RCollectionAsync { Future> lexRangeAsync(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive); + Future> lexRangeTailAsync(String fromElement, boolean fromInclusive, int offset, int count); + + Future> lexRangeHeadAsync(String toElement, boolean toInclusive, int offset, int count); + + Future> lexRangeAsync(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive, int offset, int count); + Future lexCountAsync(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive); Future rankAsync(String o); diff --git a/src/test/java/org/redisson/RedissonScoredSortedSetTest.java b/src/test/java/org/redisson/RedissonScoredSortedSetTest.java index 9ee92e651..c7983f9ad 100644 --- a/src/test/java/org/redisson/RedissonScoredSortedSetTest.java +++ b/src/test/java/org/redisson/RedissonScoredSortedSetTest.java @@ -14,6 +14,7 @@ import org.hamcrest.Matchers; import org.junit.Assert; import org.junit.Test; import org.redisson.client.protocol.ScoredEntry; +import org.redisson.core.RLexSortedSet; import org.redisson.core.RScoredSortedSet; import org.redisson.core.RSortedSet; @@ -438,6 +439,20 @@ public class RedissonScoredSortedSetTest extends BaseTest { new ScoredEntry(50D, 5))); } + @Test + public void testLexSortedSet() { + RLexSortedSet set = redisson.getLexSortedSet("simple"); + + set.add("a"); + set.add("b"); + set.add("c"); + set.add("d"); + set.add("e"); + + Collection r = set.lexRange("b", true, "e", false, 1, 2); + String[] a = r.toArray(new String[0]); + Assert.assertArrayEquals(new String[]{"c", "d"}, a); + } @Test public void testAddAndGet() throws InterruptedException { From 6f029c8b899b6044f048210109fb8691414f88dc Mon Sep 17 00:00:00 2001 From: jackygurui Date: Sun, 15 Nov 2015 00:38:03 +0000 Subject: [PATCH 3/3] added zrangebyscore command support --- .../org/redisson/RedissonScoredSortedSet.java | 48 +++++++++++++++++++ .../client/protocol/RedisCommands.java | 2 + .../org/redisson/core/RScoredSortedSet.java | 8 ++++ .../redisson/core/RScoredSortedSetAsync.java | 8 ++++ .../redisson/RedissonScoredSortedSetTest.java | 33 +++++++++++++ 5 files changed, 99 insertions(+) diff --git a/src/main/java/org/redisson/RedissonScoredSortedSet.java b/src/main/java/org/redisson/RedissonScoredSortedSet.java index ca810aaf7..71d541e9d 100644 --- a/src/main/java/org/redisson/RedissonScoredSortedSet.java +++ b/src/main/java/org/redisson/RedissonScoredSortedSet.java @@ -321,4 +321,52 @@ public class RedissonScoredSortedSet extends RedissonExpirable implements RSc return commandExecutor.readAsync(getName(), codec, RedisCommands.ZRANGE_ENTRY, getName(), startIndex, endIndex, "WITHSCORES"); } + @Override + public Collection valueRange(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive) { + return get(valueRangeAsync(startScore, startScoreInclusive, endScore, endScoreInclusive)); + } + + @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); + return commandExecutor.readAsync(getName(), codec, RedisCommands.ZRANGEBYSCORE, getName(), startValue, endValue); + } + + @Override + public Collection> entryRange(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive) { + return get(entryRangeAsync(startScore, startScoreInclusive, endScore, endScoreInclusive)); + } + + @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); + return commandExecutor.readAsync(getName(), codec, RedisCommands.ZRANGEBYSCORE_ENTRY, getName(), startValue, endValue, "WITHSCORES"); + } + + @Override + public Collection valueRange(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive, int offset, int count) { + return get(valueRangeAsync(startScore, startScoreInclusive, endScore, endScoreInclusive, offset, count)); + } + + @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); + return commandExecutor.readAsync(getName(), codec, RedisCommands.ZRANGEBYSCORE, getName(), startValue, endValue, "LIMIT", offset, count); + } + + @Override + public Collection> entryRange(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive, int offset, int count) { + return get(entryRangeAsync(startScore, startScoreInclusive, endScore, endScoreInclusive, offset, count)); + } + + @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); + return commandExecutor.readAsync(getName(), codec, RedisCommands.ZRANGEBYSCORE_ENTRY, getName(), startValue, endValue, "WITHSCORES", "LIMIT", offset, count); + } + } diff --git a/src/main/java/org/redisson/client/protocol/RedisCommands.java b/src/main/java/org/redisson/client/protocol/RedisCommands.java index fa4064646..ab49a2d10 100644 --- a/src/main/java/org/redisson/client/protocol/RedisCommands.java +++ b/src/main/java/org/redisson/client/protocol/RedisCommands.java @@ -72,7 +72,9 @@ public interface RedisCommands { RedisStrictCommand ZREMRANGEBYSCORE = new RedisStrictCommand("ZREMRANGEBYSCORE", new IntegerReplayConvertor()); RedisStrictCommand ZREMRANGEBYLEX = new RedisStrictCommand("ZREMRANGEBYLEX", new IntegerReplayConvertor()); RedisCommand> ZRANGEBYLEX = new RedisCommand>("ZRANGEBYLEX", new ObjectListReplayDecoder()); + RedisCommand> ZRANGEBYSCORE = new RedisCommand>("ZRANGEBYSCORE", new ObjectListReplayDecoder()); RedisCommand>> ZRANGE_ENTRY = new RedisCommand>>("ZRANGE", new ScoredSortedSetReplayDecoder()); + RedisCommand>> ZRANGEBYSCORE_ENTRY = new RedisCommand>>("ZRANGEBYSCORE", new ScoredSortedSetReplayDecoder()); RedisCommand> ZSCAN = new RedisCommand>("ZSCAN", new NestedMultiDecoder(new ObjectListReplayDecoder(), new ScoredSortedSetScanReplayDecoder()), ValueType.OBJECT); RedisStrictCommand ZINCRBY = new RedisStrictCommand("ZINCRBY", new DoubleReplayConvertor()); diff --git a/src/main/java/org/redisson/core/RScoredSortedSet.java b/src/main/java/org/redisson/core/RScoredSortedSet.java index 3f51debde..8f215b328 100644 --- a/src/main/java/org/redisson/core/RScoredSortedSet.java +++ b/src/main/java/org/redisson/core/RScoredSortedSet.java @@ -61,4 +61,12 @@ public interface RScoredSortedSet extends RScoredSortedSetAsync, Iterable< Collection> entryRange(int startIndex, int endIndex); + Collection valueRange(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive); + + Collection> entryRange(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive); + + Collection valueRange(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive, int offset, int count); + + Collection> entryRange(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive, int offset, int count); + } diff --git a/src/main/java/org/redisson/core/RScoredSortedSetAsync.java b/src/main/java/org/redisson/core/RScoredSortedSetAsync.java index 067dc1dd3..0bf2f9990 100644 --- a/src/main/java/org/redisson/core/RScoredSortedSetAsync.java +++ b/src/main/java/org/redisson/core/RScoredSortedSetAsync.java @@ -53,4 +53,12 @@ public interface RScoredSortedSetAsync extends RExpirableAsync { Future>> entryRangeAsync(int startIndex, int endIndex); + Future> valueRangeAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive); + + Future>> entryRangeAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive); + + Future> valueRangeAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive, int offset, int count); + + Future>> entryRangeAsync(double startScore, boolean startScoreInclusive, double endScore, boolean endScoreInclusive, int offset, int count); + } diff --git a/src/test/java/org/redisson/RedissonScoredSortedSetTest.java b/src/test/java/org/redisson/RedissonScoredSortedSetTest.java index c7983f9ad..916b7d469 100644 --- a/src/test/java/org/redisson/RedissonScoredSortedSetTest.java +++ b/src/test/java/org/redisson/RedissonScoredSortedSetTest.java @@ -454,6 +454,39 @@ public class RedissonScoredSortedSetTest extends BaseTest { Assert.assertArrayEquals(new String[]{"c", "d"}, a); } + @Test + public void testScoredSortedSetValueRange() { + 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, 4, false, 1, 2); + String[] a = r.toArray(new String[0]); + Assert.assertArrayEquals(new String[]{"c", "d"}, a); + } + + @Test + public void testScoredSortedSetEntryRange() { + 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, 4, 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 { RScoredSortedSet set = redisson.getScoredSortedSet("simple");