From 6079e581d616d28094f22339f16a4db623f13c20 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 30 Jul 2015 11:25:38 +0200 Subject: [PATCH] Add support for COUNTKEYSINSLOT #107 --- .../redis/AbstractRedisAsyncCommands.java | 5 +++++ .../redis/AbstractRedisReactiveCommands.java | 5 +++++ .../redis/RedisClusterAsyncConnection.java | 10 +++++++++- .../redis/RedisClusterConnection.java | 10 +++++++++- .../redis/RedisCommandBuilder.java | 5 +++++ .../api/async/RedisClusterAsyncCommands.java | 10 +++++++++- .../api/rx/RedisClusterReactiveCommands.java | 10 +++++++++- .../api/sync/RedisClusterCommands.java | 10 +++++++++- .../redis/protocol/CommandKeyword.java | 2 +- .../redis/cluster/RedisClusterClientTest.java | 11 ++++++++++ .../commands/rx/ClusterRxCommandTest.java | 20 ++++++++++++++++++- 11 files changed, 91 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/lambdaworks/redis/AbstractRedisAsyncCommands.java b/src/main/java/com/lambdaworks/redis/AbstractRedisAsyncCommands.java index 33e3b0d3d7..4ee201f7a2 100644 --- a/src/main/java/com/lambdaworks/redis/AbstractRedisAsyncCommands.java +++ b/src/main/java/com/lambdaworks/redis/AbstractRedisAsyncCommands.java @@ -1528,6 +1528,11 @@ public RedisFuture> clusterGetKeysInSlot(int slot, int count) { return dispatch(commandBuilder.clusterGetKeysInSlot(slot, count)); } + @Override + public RedisFuture clusterCountKeysInSlot(int slot) { + return dispatch(commandBuilder.clusterCountKeysInSlot(slot)); + } + @Override public RedisFuture> clusterSlots() { return dispatch(commandBuilder.clusterSlots()); diff --git a/src/main/java/com/lambdaworks/redis/AbstractRedisReactiveCommands.java b/src/main/java/com/lambdaworks/redis/AbstractRedisReactiveCommands.java index 3f7ba587bb..9d2040de6f 100644 --- a/src/main/java/com/lambdaworks/redis/AbstractRedisReactiveCommands.java +++ b/src/main/java/com/lambdaworks/redis/AbstractRedisReactiveCommands.java @@ -1491,6 +1491,11 @@ public Observable clusterGetKeysInSlot(int slot, int count) { return createDissolvingObservable(() -> commandBuilder.clusterGetKeysInSlot(slot, count)); } + @Override + public Observable clusterCountKeysInSlot(int slot) { + return createObservable(() -> commandBuilder.clusterCountKeysInSlot(slot)); + } + @Override public Observable clusterSlots() { return createDissolvingObservable(commandBuilder::clusterSlots); diff --git a/src/main/java/com/lambdaworks/redis/RedisClusterAsyncConnection.java b/src/main/java/com/lambdaworks/redis/RedisClusterAsyncConnection.java index 09baab9935..c83d5f24b9 100644 --- a/src/main/java/com/lambdaworks/redis/RedisClusterAsyncConnection.java +++ b/src/main/java/com/lambdaworks/redis/RedisClusterAsyncConnection.java @@ -126,7 +126,7 @@ public interface RedisClusterAsyncConnection extends RedisHashesAsyncConne * {@link com.lambdaworks.redis.cluster.models.partitions.ClusterPartitionParser#parse} * * @param nodeId node id of the master node - * @return List of slaves + * @return List<String> array-reply list of slaves. The command returns data in the same format as {@link #clusterNodes()} but one line per slave. */ RedisFuture> clusterSlaves(String nodeId); @@ -139,6 +139,14 @@ public interface RedisClusterAsyncConnection extends RedisHashesAsyncConne */ RedisFuture> clusterGetKeysInSlot(int slot, int count); + /** + * Returns the number of keys in the specified Redis Cluster hash {@code slot}. + * + * @param slot the slot + * @return Integer reply: The number of keys in the specified hash slot, or an error if the hash slot is invalid. + */ + RedisFuture clusterCountKeysInSlot(int slot); + /** * Get array of cluster slots to node mappings. * diff --git a/src/main/java/com/lambdaworks/redis/RedisClusterConnection.java b/src/main/java/com/lambdaworks/redis/RedisClusterConnection.java index 9058ecb989..156f86b8d4 100644 --- a/src/main/java/com/lambdaworks/redis/RedisClusterConnection.java +++ b/src/main/java/com/lambdaworks/redis/RedisClusterConnection.java @@ -125,7 +125,7 @@ public interface RedisClusterConnection extends RedisHashesConnection clusterSlaves(String nodeId); @@ -138,6 +138,14 @@ public interface RedisClusterConnection extends RedisHashesConnection clusterGetKeysInSlot(int slot, int count); + /** + * Returns the number of keys in the specified Redis Cluster hash {@code slot}. + * + * @param slot the slot + * @return Integer reply: The number of keys in the specified hash slot, or an error if the hash slot is invalid. + */ + Long clusterCountKeysInSlot(int slot); + /** * Get array of cluster slots to node mappings. * diff --git a/src/main/java/com/lambdaworks/redis/RedisCommandBuilder.java b/src/main/java/com/lambdaworks/redis/RedisCommandBuilder.java index 257ccbe47a..8a2103e56a 100644 --- a/src/main/java/com/lambdaworks/redis/RedisCommandBuilder.java +++ b/src/main/java/com/lambdaworks/redis/RedisCommandBuilder.java @@ -1659,6 +1659,11 @@ public Command> clusterGetKeysInSlot(int slot, int count) { return createCommand(CLUSTER, new KeyListOutput(codec), args); } + public Command clusterCountKeysInSlot(int slot) { + CommandArgs args = new CommandArgs(codec).add(COUNTKEYSINSLOT).add(slot); + return createCommand(CLUSTER, new IntegerOutput(codec), args); + } + public Command> clusterSlots() { CommandArgs args = new CommandArgs(codec).add(SLOTS); return createCommand(CLUSTER, new ArrayOutput(codec), args); diff --git a/src/main/java/com/lambdaworks/redis/cluster/api/async/RedisClusterAsyncCommands.java b/src/main/java/com/lambdaworks/redis/cluster/api/async/RedisClusterAsyncCommands.java index 2614fd218e..b442b1a471 100644 --- a/src/main/java/com/lambdaworks/redis/cluster/api/async/RedisClusterAsyncCommands.java +++ b/src/main/java/com/lambdaworks/redis/cluster/api/async/RedisClusterAsyncCommands.java @@ -127,7 +127,7 @@ public interface RedisClusterAsyncCommands extends RedisHashAsyncCommands< * {@link com.lambdaworks.redis.cluster.models.partitions.ClusterPartitionParser#parse} * * @param nodeId node id of the master node - * @return List of slaves + * @return List<String> array-reply list of slaves. The command returns data in the same format as {@link #clusterNodes()} but one line per slave. */ RedisFuture> clusterSlaves(String nodeId); @@ -140,6 +140,14 @@ public interface RedisClusterAsyncCommands extends RedisHashAsyncCommands< */ RedisFuture> clusterGetKeysInSlot(int slot, int count); + /** + * Returns the number of keys in the specified Redis Cluster hash {@code slot}. + * + * @param slot the slot + * @return Integer reply: The number of keys in the specified hash slot, or an error if the hash slot is invalid. + */ + RedisFuture clusterCountKeysInSlot(int slot); + /** * Get array of cluster slots to node mappings. * diff --git a/src/main/java/com/lambdaworks/redis/cluster/api/rx/RedisClusterReactiveCommands.java b/src/main/java/com/lambdaworks/redis/cluster/api/rx/RedisClusterReactiveCommands.java index 1c304ee7bb..1e3cf3a064 100644 --- a/src/main/java/com/lambdaworks/redis/cluster/api/rx/RedisClusterReactiveCommands.java +++ b/src/main/java/com/lambdaworks/redis/cluster/api/rx/RedisClusterReactiveCommands.java @@ -136,7 +136,7 @@ public interface RedisClusterReactiveCommands extends RedisHashReactiveCom * {@link com.lambdaworks.redis.cluster.models.partitions.ClusterPartitionParser#parse} * * @param nodeId node id of the master node - * @return List of slaves + * @return List<String> array-reply list of slaves. The command returns data in the same format as {@link #clusterNodes()} but one line per slave. */ Observable clusterSlaves(String nodeId); @@ -149,6 +149,14 @@ public interface RedisClusterReactiveCommands extends RedisHashReactiveCom */ Observable clusterGetKeysInSlot(int slot, int count); + /** + * Returns the number of keys in the specified Redis Cluster hash {@code slot}. + * + * @param slot the slot + * @return Integer reply: The number of keys in the specified hash slot, or an error if the hash slot is invalid. + */ + Observable clusterCountKeysInSlot(int slot); + /** * Get array of cluster slots to node mappings. * diff --git a/src/main/java/com/lambdaworks/redis/cluster/api/sync/RedisClusterCommands.java b/src/main/java/com/lambdaworks/redis/cluster/api/sync/RedisClusterCommands.java index 0b28929cd8..32d3c212d7 100644 --- a/src/main/java/com/lambdaworks/redis/cluster/api/sync/RedisClusterCommands.java +++ b/src/main/java/com/lambdaworks/redis/cluster/api/sync/RedisClusterCommands.java @@ -132,7 +132,7 @@ public interface RedisClusterCommands extends RedisHashCommands, Red * {@link com.lambdaworks.redis.cluster.models.partitions.ClusterPartitionParser#parse} * * @param nodeId node id of the master node - * @return List of slaves + * @return List<String> array-reply list of slaves. The command returns data in the same format as {@link #clusterNodes()} but one line per slave. */ List clusterSlaves(String nodeId); @@ -145,6 +145,14 @@ public interface RedisClusterCommands extends RedisHashCommands, Red */ List clusterGetKeysInSlot(int slot, int count); + /** + * Returns the number of keys in the specified Redis Cluster hash {@code slot}. + * + * @param slot the slot + * @return Integer reply: The number of keys in the specified hash slot, or an error if the hash slot is invalid. + */ + Long clusterCountKeysInSlot(int slot); + /** * Get array of cluster slots to node mappings. * diff --git a/src/main/java/com/lambdaworks/redis/protocol/CommandKeyword.java b/src/main/java/com/lambdaworks/redis/protocol/CommandKeyword.java index 81c0489263..0530d678e3 100644 --- a/src/main/java/com/lambdaworks/redis/protocol/CommandKeyword.java +++ b/src/main/java/com/lambdaworks/redis/protocol/CommandKeyword.java @@ -8,7 +8,7 @@ * @author Will Glozer */ public enum CommandKeyword implements ProtocolKeyword { - ADDR, ADDSLOTS, AFTER, AGGREGATE, ALPHA, AND, ASK, ASC, BEFORE, BY, CHANNELS, COUNT, DELSLOTS, DESC, SOFT, HARD, ENCODING, + ADDR, ADDSLOTS, AFTER, AGGREGATE, ALPHA, AND, ASK, ASC, BEFORE, BY, CHANNELS, COUNT, COUNTKEYSINSLOT, DELSLOTS, DESC, SOFT, HARD, ENCODING, FAILOVER, FORGET, FLUSH, FORCE, FLUSHSLOTS, GETNAME, GETKEYSINSLOT, HTSTATS, ID, IDLETIME, KILL, LEN, LIMIT, LIST, LOAD, MATCH, diff --git a/src/test/java/com/lambdaworks/redis/cluster/RedisClusterClientTest.java b/src/test/java/com/lambdaworks/redis/cluster/RedisClusterClientTest.java index 0096b86d1c..0001f2880b 100644 --- a/src/test/java/com/lambdaworks/redis/cluster/RedisClusterClientTest.java +++ b/src/test/java/com/lambdaworks/redis/cluster/RedisClusterClientTest.java @@ -383,4 +383,15 @@ public void getKeysInSlot() throws Exception { assertThat(keys).isEqualTo(ImmutableList.of("b")); } + @Test + public void countKeysInSlot() throws Exception { + + redissync1.set("b", value); + Long result = redissync1.clusterCountKeysInSlot(SlotHash.getSlot("b".getBytes())); + assertThat(result).isEqualTo(1L); + + result = redissync1.clusterCountKeysInSlot(SlotHash.getSlot("ZZZ".getBytes())); + assertThat(result).isEqualTo(0L); + } + } diff --git a/src/test/java/com/lambdaworks/redis/cluster/commands/rx/ClusterRxCommandTest.java b/src/test/java/com/lambdaworks/redis/cluster/commands/rx/ClusterRxCommandTest.java index f5d694c4aa..26ca27094d 100644 --- a/src/test/java/com/lambdaworks/redis/cluster/commands/rx/ClusterRxCommandTest.java +++ b/src/test/java/com/lambdaworks/redis/cluster/commands/rx/ClusterRxCommandTest.java @@ -67,7 +67,6 @@ public void before() throws Exception { connection = client.connect(RedisURI.Builder.redis(host, port1).build()); rx = connection.reactive(); - } @After @@ -135,4 +134,23 @@ public void clusterSlaves() throws Exception { assertThat(result.size()).isGreaterThan(0); } + @Test + public void getKeysInSlot() throws Exception { + + connection.sync().set("b", value); + List keys = rx.clusterGetKeysInSlot(SlotHash.getSlot("b".getBytes()), 10).toList().toBlocking().first(); + assertThat(keys).isEqualTo(ImmutableList.of("b")); + } + + @Test + public void countKeysInSlot() throws Exception { + + connection.sync().set("b", value); + Long result = rx.clusterCountKeysInSlot(SlotHash.getSlot("b".getBytes())).toBlocking().first(); + assertThat(result).isEqualTo(1L); + + result = rx.clusterCountKeysInSlot(SlotHash.getSlot("ZZZ".getBytes())).toBlocking().first(); + assertThat(result).isEqualTo(0L); + } + }