From 77d244b96a17b3f9a193e857b1164c2ae70d5b2a Mon Sep 17 00:00:00 2001 From: Jonathan Leibiusky Date: Mon, 2 Dec 2013 14:04:37 -0500 Subject: [PATCH] Add CLUSTER INFO, CLUSTER SETSLOT and CLUSTER GETKEYSINSLOT commands --- src/main/java/redis/clients/jedis/Jedis.java | 31 +++++++++++++ .../java/redis/clients/jedis/Protocol.java | 6 +++ .../tests/commands/ClusterCommandsTest.java | 45 ++++++++++++++++++- 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index b4db590..6451f30 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -3100,4 +3100,35 @@ public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommand client.cluster(Protocol.CLUSTER_DELSLOTS, slots); return client.getStatusCodeReply(); } + + public String clusterInfo() { + checkIsInMulti(); + client.cluster(Protocol.CLUSTER_INFO); + return client.getStatusCodeReply(); + } + + public List clusterGetKeysInSlot(final int slot, final int count) { + checkIsInMulti(); + final int[] args = new int[]{ slot, count }; + client.cluster(Protocol.CLUSTER_GETKEYSINSLOT, args); + return client.getMultiBulkReply(); + } + + public String clusterSetSlotNode(final int slot, final String nodeId) { + checkIsInMulti(); + client.cluster(Protocol.CLUSTER_SETSLOT, String.valueOf(slot), Protocol.CLUSTER_SETSLOT_NODE, nodeId); + return client.getStatusCodeReply(); + } + + public String clusterSetSlotMigrating(final int slot, final String nodeId) { + checkIsInMulti(); + client.cluster(Protocol.CLUSTER_SETSLOT, String.valueOf(slot), Protocol.CLUSTER_SETSLOT_MIGRATING, nodeId); + return client.getStatusCodeReply(); + } + + public String clusterSetSlotImporting(final int slot, final String nodeId) { + checkIsInMulti(); + client.cluster(Protocol.CLUSTER_SETSLOT, String.valueOf(slot), Protocol.CLUSTER_SETSLOT_IMPORTING, nodeId); + return client.getStatusCodeReply(); + } } diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index 1bbdc06..a3eacd6 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -34,6 +34,12 @@ public final class Protocol { public static final String CLUSTER_MEET = "meet"; public static final String CLUSTER_ADDSLOTS = "addslots"; public static final String CLUSTER_DELSLOTS = "delslots"; + public static final String CLUSTER_INFO = "info"; + public static final String CLUSTER_GETKEYSINSLOT = "getkeysinslot"; + public static final String CLUSTER_SETSLOT = "setslot"; + public static final String CLUSTER_SETSLOT_NODE = "node"; + public static final String CLUSTER_SETSLOT_MIGRATING = "migrating"; + public static final String CLUSTER_SETSLOT_IMPORTING = "importing"; private Protocol() { // this prevent the class from instantiation diff --git a/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java index 6b11b09..f4abd11 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ClusterCommandsTest.java @@ -1,5 +1,7 @@ package redis.clients.jedis.tests.commands; +import java.util.List; + import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -45,17 +47,56 @@ public class ClusterCommandsTest extends JedisTestBase { String status = node1.clusterMeet("127.0.0.1", nodeInfo2.getPort()); assertEquals("OK", status); } - + @Test public void clusterAddSlots() { String status = node1.clusterAddSlots(1, 2, 3, 4, 5); assertEquals("OK", status); } - + @Test public void clusterDelSlots() { node1.clusterAddSlots(900); String status = node1.clusterDelSlots(900); assertEquals("OK", status); } + + @Test + public void clusterInfo() { + String info = node1.clusterInfo(); + assertNotNull(info); + } + + @Test + public void clusterGetKeysInSlot() { + node1.clusterAddSlots(500); + List keys = node1.clusterGetKeysInSlot(500, 1); + assertEquals(0, keys.size()); + } + + @Test + public void clusterSetSlotNode() { + String[] nodes = node1.clusterNodes().split("\n"); + String nodeId = nodes[0].split(" ")[0]; + String status = node1.clusterSetSlotNode(10000, nodeId); + assertEquals("OK", status); + } + + @Test + public void clusterSetSlotMigrating() { + node1.clusterAddSlots(5000); + String[] nodes = node1.clusterNodes().split("\n"); + String nodeId = nodes[0].split(" ")[0]; + String status = node1.clusterSetSlotMigrating(5000, nodeId); + assertEquals("OK", status); + } + + @Test + public void clusterSetSlotImporting() { + node2.clusterAddSlots(6000); + String[] nodes = node1.clusterNodes().split("\n"); + String nodeId = nodes[0].split(" ")[0]; + String status = node1.clusterSetSlotImporting(6000, nodeId); + assertEquals("OK", status); + } } \ No newline at end of file