Skip to content

Commit

Permalink
Add support for COUNTKEYSINSLOT #107
Browse files Browse the repository at this point in the history
  • Loading branch information
mp911de committed Jul 30, 2015
1 parent a4bce12 commit 6079e58
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,11 @@ public RedisFuture<List<K>> clusterGetKeysInSlot(int slot, int count) {
return dispatch(commandBuilder.clusterGetKeysInSlot(slot, count));
}

@Override
public RedisFuture<Long> clusterCountKeysInSlot(int slot) {
return dispatch(commandBuilder.clusterCountKeysInSlot(slot));
}

@Override
public RedisFuture<List<Object>> clusterSlots() {
return dispatch(commandBuilder.clusterSlots());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1491,6 +1491,11 @@ public Observable<K> clusterGetKeysInSlot(int slot, int count) {
return createDissolvingObservable(() -> commandBuilder.clusterGetKeysInSlot(slot, count));
}

@Override
public Observable<Long> clusterCountKeysInSlot(int slot) {
return createObservable(() -> commandBuilder.clusterCountKeysInSlot(slot));
}

@Override
public Observable<Object> clusterSlots() {
return createDissolvingObservable(commandBuilder::clusterSlots);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public interface RedisClusterAsyncConnection<K, V> 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&lt;String&gt; array-reply list of slaves. The command returns data in the same format as {@link #clusterNodes()} but one line per slave.
*/
RedisFuture<List<String>> clusterSlaves(String nodeId);

Expand All @@ -139,6 +139,14 @@ public interface RedisClusterAsyncConnection<K, V> extends RedisHashesAsyncConne
*/
RedisFuture<List<K>> 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<Long> clusterCountKeysInSlot(int slot);

/**
* Get array of cluster slots to node mappings.
*
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/lambdaworks/redis/RedisClusterConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public interface RedisClusterConnection<K, V> extends RedisHashesConnection<K, V
* {@link com.lambdaworks.redis.cluster.models.partitions.ClusterPartitionParser#parse}
*
* @param nodeId node id of the master node
* @return List of slaves
* @return List&lt;String&gt; array-reply list of slaves. The command returns data in the same format as {@link #clusterNodes()} but one line per slave.
*/
List<String> clusterSlaves(String nodeId);

Expand All @@ -138,6 +138,14 @@ public interface RedisClusterConnection<K, V> extends RedisHashesConnection<K, V
*/
List<K> 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.
*
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/lambdaworks/redis/RedisCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,11 @@ public Command<K, V, List<K>> clusterGetKeysInSlot(int slot, int count) {
return createCommand(CLUSTER, new KeyListOutput<K, V>(codec), args);
}

public Command<K, V, Long> clusterCountKeysInSlot(int slot) {
CommandArgs<K, V> args = new CommandArgs<K, V>(codec).add(COUNTKEYSINSLOT).add(slot);
return createCommand(CLUSTER, new IntegerOutput<K, V>(codec), args);
}

public Command<K, V, List<Object>> clusterSlots() {
CommandArgs<K, V> args = new CommandArgs<K, V>(codec).add(SLOTS);
return createCommand(CLUSTER, new ArrayOutput<K, V>(codec), args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public interface RedisClusterAsyncCommands<K, V> 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&lt;String&gt; array-reply list of slaves. The command returns data in the same format as {@link #clusterNodes()} but one line per slave.
*/
RedisFuture<List<String>> clusterSlaves(String nodeId);

Expand All @@ -140,6 +140,14 @@ public interface RedisClusterAsyncCommands<K, V> extends RedisHashAsyncCommands<
*/
RedisFuture<List<K>> 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<Long> clusterCountKeysInSlot(int slot);

/**
* Get array of cluster slots to node mappings.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public interface RedisClusterReactiveCommands<K, V> 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&lt;String&gt; array-reply list of slaves. The command returns data in the same format as {@link #clusterNodes()} but one line per slave.
*/
Observable<String> clusterSlaves(String nodeId);

Expand All @@ -149,6 +149,14 @@ public interface RedisClusterReactiveCommands<K, V> extends RedisHashReactiveCom
*/
Observable<K> 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<Long> clusterCountKeysInSlot(int slot);

/**
* Get array of cluster slots to node mappings.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public interface RedisClusterCommands<K, V> extends RedisHashCommands<K, V>, Red
* {@link com.lambdaworks.redis.cluster.models.partitions.ClusterPartitionParser#parse}
*
* @param nodeId node id of the master node
* @return List of slaves
* @return List&lt;String&gt; array-reply list of slaves. The command returns data in the same format as {@link #clusterNodes()} but one line per slave.
*/
List<String> clusterSlaves(String nodeId);

Expand All @@ -145,6 +145,14 @@ public interface RedisClusterCommands<K, V> extends RedisHashCommands<K, V>, Red
*/
List<K> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public void before() throws Exception {

connection = client.connect(RedisURI.Builder.redis(host, port1).build());
rx = connection.reactive();

}

@After
Expand Down Expand Up @@ -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<String> 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);
}

}

0 comments on commit 6079e58

Please sign in to comment.