Add RedisSlot helper to calculate key slot
This commit is contained in:
@@ -12,6 +12,7 @@ import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisCluster;
|
||||
import redis.clients.jedis.Pipeline;
|
||||
import redis.clients.jedis.exceptions.JedisMovedDataException;
|
||||
import redis.clients.jedis.tests.utils.RedisSlot;
|
||||
|
||||
public class JedisClusterTest extends Assert {
|
||||
private Jedis node1;
|
||||
@@ -68,8 +69,22 @@ public class JedisClusterTest extends Assert {
|
||||
jc.get("foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ask() {
|
||||
//TODO: needs to implement
|
||||
// @Test
|
||||
// public void ask() {
|
||||
// JedisCluster jc = new JedisCluster(new HashSet<HostAndPort>(HostAndPortUtil.getClusterServers()));
|
||||
// jc.set("foo", "bar");
|
||||
// int keySlot = RedisSlot.getSlot("foo");
|
||||
// String node2Id = getNodeId(node2.clusterNodes());
|
||||
// node1.clusterSetSlotMigrating(keySlot, node2Id);
|
||||
// node1.get("foo");
|
||||
// }
|
||||
|
||||
private String getNodeId(String infoOutput) {
|
||||
for (String infoLine : infoOutput.split("\n")) {
|
||||
if (infoLine.contains("myself")) {
|
||||
return infoLine.split(" ")[0];
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
25
src/test/java/redis/clients/jedis/tests/utils/RedisSlot.java
Normal file
25
src/test/java/redis/clients/jedis/tests/utils/RedisSlot.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package redis.clients.jedis.tests.utils;
|
||||
|
||||
public class RedisSlot {
|
||||
public final static int polynomial = 0x1021; // Represents x^16+x^12+x^5+1
|
||||
static int crc;
|
||||
|
||||
public RedisSlot(){
|
||||
crc = 0x0000;
|
||||
}
|
||||
|
||||
public static int getSlot(String key) {
|
||||
for (byte b : key.getBytes()) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
boolean bit = ((b >> (7-i) & 1) == 1);
|
||||
boolean c15 = ((crc >> 15 & 1) == 1);
|
||||
crc <<= 1;
|
||||
// If coefficient of bit and remainder polynomial = 1 xor crc with polynomial
|
||||
if (c15 ^ bit) crc ^= polynomial;
|
||||
}
|
||||
}
|
||||
|
||||
return crc &= 0xffff % 16384;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user