From c6722f5373ec436ec7d708fce6ee9fa5ee62fc96 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 17 Jun 2014 09:16:09 +0200 Subject: [PATCH] Support for sorted set commands ZLEXCOUNT, ZRANGEBYLEX, ZREMRANGEBYLEX --- README.md | 1 - .../redis/RedisAsyncConnectionImpl.java | 20 +++++++++++ .../redis/RedisCommandBuilder.java | 26 +++++++++++++++ .../redis/RedisSortedSetsAsyncConnection.java | 8 +++++ .../redis/RedisSortedSetsConnection.java | 8 +++++ .../redis/protocol/CommandType.java | 2 +- .../redis/SortedSetCommandTest.java | 33 +++++++++++++++++++ 7 files changed, 96 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5265be8e89..26784a1a9b 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,6 @@ Lettuce works with: Currently following commands are __not yet__ supported: -* Sorted Sets: ZLEXCOUNT, ZRANGEBYLEX, ZREMRANGEBYLEX * Server Commands: DEBUG SEGFAULT, MONITOR, PUBSUB I'm pursuing the client code as a fork since the latest changes weren't merged back into https://github/wg/lettuce diff --git a/lettuce/src/main/java/com/lambdaworks/redis/RedisAsyncConnectionImpl.java b/lettuce/src/main/java/com/lambdaworks/redis/RedisAsyncConnectionImpl.java index 89ef24825f..4e8e1ec82d 100644 --- a/lettuce/src/main/java/com/lambdaworks/redis/RedisAsyncConnectionImpl.java +++ b/lettuce/src/main/java/com/lambdaworks/redis/RedisAsyncConnectionImpl.java @@ -1471,6 +1471,26 @@ public RedisFuture> clusterSlaves(String nodeId) { return dispatch(commandBuilder.clusterSlaves(nodeId)); } + @Override + public RedisFuture zlexcount(K key, String min, String max) { + return dispatch(commandBuilder.zlexcount(key, min, max)); + } + + @Override + public RedisFuture zremrangebylex(K key, String min, String max) { + return dispatch(commandBuilder.zremrangebylex(key, min, max)); + } + + @Override + public RedisFuture> zrangebylex(K key, String min, String max) { + return dispatch(commandBuilder.zrangebylex(key, min, max)); + } + + @Override + public RedisFuture> zrangebylex(K key, String min, String max, long offset, long count) { + return dispatch(commandBuilder.zrangebylex(key, min, max, offset, count)); + } + public RedisCommand dispatch(CommandType type, CommandOutput output) { return dispatch(type, output, null); } diff --git a/lettuce/src/main/java/com/lambdaworks/redis/RedisCommandBuilder.java b/lettuce/src/main/java/com/lambdaworks/redis/RedisCommandBuilder.java index 38158f6b70..a77e9919ce 100644 --- a/lettuce/src/main/java/com/lambdaworks/redis/RedisCommandBuilder.java +++ b/lettuce/src/main/java/com/lambdaworks/redis/RedisCommandBuilder.java @@ -13,6 +13,7 @@ import com.lambdaworks.redis.protocol.Command; import com.lambdaworks.redis.protocol.CommandArgs; import com.lambdaworks.redis.protocol.CommandOutput; +import com.lambdaworks.redis.protocol.RedisCommand; import com.lambdaworks.redis.protocol.SetArgs; /** @@ -1087,6 +1088,30 @@ public Command zunionstore(K destination, ZStoreArgs storeArgs, K... return createCommand(ZUNIONSTORE, new IntegerOutput(codec), args); } + public RedisCommand zlexcount(K key, String min, String max) { + CommandArgs args = new CommandArgs(codec); + args.addKey(key).add(min).add(max); + return createCommand(ZLEXCOUNT, new IntegerOutput(codec), args); + } + + public RedisCommand zremrangebylex(K key, String min, String max) { + CommandArgs args = new CommandArgs(codec); + args.addKey(key).add(min).add(max); + return createCommand(ZREMRANGEBYLEX, new IntegerOutput(codec), args); + } + + public RedisCommand> zrangebylex(K key, String min, String max) { + CommandArgs args = new CommandArgs(codec); + args.addKey(key).add(min).add(max); + return createCommand(ZRANGEBYLEX, new ValueListOutput(codec), args); + } + + public RedisCommand> zrangebylex(K key, String min, String max, long offset, long count) { + CommandArgs args = new CommandArgs(codec); + args.addKey(key).add(min).add(max).add(LIMIT).add(offset).add(count); + return createCommand(ZRANGEBYLEX, new ValueListOutput(codec), args); + } + public Command> time() { CommandArgs args = new CommandArgs(codec); return createCommand(TIME, new ValueListOutput(codec), args); @@ -1403,4 +1428,5 @@ public Command clusterReset(boolean hard) { } return createCommand(CLUSTER, new StatusOutput(codec), args); } + } diff --git a/lettuce/src/main/java/com/lambdaworks/redis/RedisSortedSetsAsyncConnection.java b/lettuce/src/main/java/com/lambdaworks/redis/RedisSortedSetsAsyncConnection.java index 500570263a..25ab90729a 100644 --- a/lettuce/src/main/java/com/lambdaworks/redis/RedisSortedSetsAsyncConnection.java +++ b/lettuce/src/main/java/com/lambdaworks/redis/RedisSortedSetsAsyncConnection.java @@ -153,4 +153,12 @@ RedisFuture zrevrangebyscoreWithScores(ScoredValueStreamingChannel chan RedisFuture zscan(ScoredValueStreamingChannel channel, K key, ScanCursor scanCursor, ScanArgs scanArgs); RedisFuture zscan(ScoredValueStreamingChannel channel, K key, ScanCursor scanCursor); + + RedisFuture zlexcount(K key, String min, String max); + + RedisFuture zremrangebylex(K key, String min, String max); + + RedisFuture> zrangebylex(K key, String min, String max); + + RedisFuture> zrangebylex(K key, String min, String max, long offset, long count); } diff --git a/lettuce/src/main/java/com/lambdaworks/redis/RedisSortedSetsConnection.java b/lettuce/src/main/java/com/lambdaworks/redis/RedisSortedSetsConnection.java index de98978e89..c7e5005480 100644 --- a/lettuce/src/main/java/com/lambdaworks/redis/RedisSortedSetsConnection.java +++ b/lettuce/src/main/java/com/lambdaworks/redis/RedisSortedSetsConnection.java @@ -151,4 +151,12 @@ Long zrevrangebyscoreWithScores(ScoredValueStreamingChannel channel, K key, S StreamScanCursor zscan(ScoredValueStreamingChannel channel, K key, ScanCursor scanCursor, ScanArgs scanArgs); StreamScanCursor zscan(ScoredValueStreamingChannel channel, K key, ScanCursor scanCursor); + + Long zlexcount(K key, String min, String max); + + Long zremrangebylex(K key, String min, String max); + + List zrangebylex(K key, String min, String max); + + List zrangebylex(K key, String min, String max, long offset, long count); } diff --git a/lettuce/src/main/java/com/lambdaworks/redis/protocol/CommandType.java b/lettuce/src/main/java/com/lambdaworks/redis/protocol/CommandType.java index 4f90a69090..88403298f5 100644 --- a/lettuce/src/main/java/com/lambdaworks/redis/protocol/CommandType.java +++ b/lettuce/src/main/java/com/lambdaworks/redis/protocol/CommandType.java @@ -54,7 +54,7 @@ public enum CommandType { // Sorted Set - ZADD, ZCARD, ZCOUNT, ZINCRBY, ZINTERSTORE, ZRANGE, ZRANGEBYSCORE, ZRANK, ZREM, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZREVRANGE, ZREVRANGEBYSCORE, ZREVRANK, ZSCORE, ZUNIONSTORE, ZSCAN, + ZADD, ZCARD, ZCOUNT, ZINCRBY, ZINTERSTORE, ZRANGE, ZRANGEBYSCORE, ZRANK, ZREM, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZREVRANGE, ZREVRANGEBYSCORE, ZREVRANK, ZSCORE, ZUNIONSTORE, ZSCAN, ZLEXCOUNT, ZREMRANGEBYLEX, ZRANGEBYLEX, // Scripting diff --git a/lettuce/src/test/java/com/lambdaworks/redis/SortedSetCommandTest.java b/lettuce/src/test/java/com/lambdaworks/redis/SortedSetCommandTest.java index 0b0fc82f7b..97fde014d0 100644 --- a/lettuce/src/test/java/com/lambdaworks/redis/SortedSetCommandTest.java +++ b/lettuce/src/test/java/com/lambdaworks/redis/SortedSetCommandTest.java @@ -14,6 +14,7 @@ import static org.junit.Assert.assertTrue; import java.util.HashSet; +import java.util.List; import java.util.Set; import org.junit.Test; @@ -304,6 +305,38 @@ public void zscanMatch() throws Exception { assertEquals(100, cursor.getValues().size()); } + @Test + public void zlexcount() throws Exception { + setup100KeyValues(new HashSet()); + Long result = redis.zlexcount(key, "-", "+"); + + assertEquals(100, result.longValue()); + + Long resultFromTo = redis.zlexcount(key, "[value", "[zzz"); + assertEquals(100, resultFromTo.longValue()); + } + + @Test + public void zrangebylex() throws Exception { + setup100KeyValues(new HashSet()); + List result = redis.zrangebylex(key, "-", "+"); + + assertEquals(100, result.size()); + + List result2 = redis.zrangebylex(key, "-", "+", 10, 10); + + assertEquals(10, result2.size()); + } + + @Test + public void zremrangebylex() throws Exception { + setup100KeyValues(new HashSet()); + Long result = redis.zremrangebylex(key, "(aaa", "[zzz"); + + assertEquals(100, result.longValue()); + + } + protected void setup100KeyValues(Set expect) { for (int i = 0; i < 100; i++) { redis.zadd(key + 1, i, value + i);