Merge pull request #541 from xetorthio/jedis_cluster_hashtag
Add support for redis cluster hashtags
This commit is contained in:
@@ -4,7 +4,19 @@ public class JedisClusterCRC16 {
|
|||||||
public final static int polynomial = 0x1021; // Represents x^16+x^12+x^5+1
|
public final static int polynomial = 0x1021; // Represents x^16+x^12+x^5+1
|
||||||
static int crc;
|
static int crc;
|
||||||
|
|
||||||
|
|
||||||
public static int getSlot(String key) {
|
public static int getSlot(String key) {
|
||||||
|
int s = key.indexOf("{");
|
||||||
|
if (s > -1) {
|
||||||
|
int e = key.indexOf("}", s+1);
|
||||||
|
if (e > -1 && e != s+1) {
|
||||||
|
key = key.substring(s+1, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return getCRC16(key) % 16384;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int getCRC16(String key) {
|
||||||
crc = 0x0000;
|
crc = 0x0000;
|
||||||
for (byte b : key.getBytes()) {
|
for (byte b : key.getBytes()) {
|
||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
@@ -18,6 +30,6 @@ public class JedisClusterCRC16 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return crc &= 0xffff % 16384;
|
return crc &= 0xffff ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -171,6 +171,14 @@ public class JedisClusterTest extends Assert {
|
|||||||
jc.set("51", "foo");
|
jc.set("51", "foo");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRedisHashtag() {
|
||||||
|
assertEquals(JedisClusterCRC16.getSlot("{bar"), JedisClusterCRC16.getSlot("foo{{bar}}zap"));
|
||||||
|
assertEquals(JedisClusterCRC16.getSlot("{user1000}.following"), JedisClusterCRC16.getSlot("{user1000}.followers"));
|
||||||
|
assertNotEquals(JedisClusterCRC16.getSlot("foo{}{bar}"), JedisClusterCRC16.getSlot("bar"));
|
||||||
|
assertEquals(JedisClusterCRC16.getSlot("foo{bar}{zap}"), JedisClusterCRC16.getSlot("bar"));
|
||||||
|
}
|
||||||
|
|
||||||
private String getNodeId(String infoOutput) {
|
private String getNodeId(String infoOutput) {
|
||||||
for (String infoLine : infoOutput.split("\n")) {
|
for (String infoLine : infoOutput.split("\n")) {
|
||||||
if (infoLine.contains("myself")) {
|
if (infoLine.contains("myself")) {
|
||||||
|
|||||||
Reference in New Issue
Block a user