Skip to content

Commit

Permalink
Merge pull request #541 from xetorthio/jedis_cluster_hashtag
Browse files Browse the repository at this point in the history
Add support for redis cluster hashtags
  • Loading branch information
xetorthio committed Feb 12, 2014
2 parents adc9cb2 + 76814b5 commit c84039c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/main/java/redis/clients/util/JedisClusterCRC16.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@
public class JedisClusterCRC16 {
public final static int polynomial = 0x1021; // Represents x^16+x^12+x^5+1
static int crc;


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;
for (byte b : key.getBytes()) {
for (int i = 0; i < 8; i++) {
Expand All @@ -18,6 +30,6 @@ public static int getSlot(String key) {
}
}

return crc &= 0xffff % 16384;
return crc &= 0xffff ;
}
}
10 changes: 9 additions & 1 deletion src/test/java/redis/clients/jedis/tests/JedisClusterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ public void testRedisClusterMaxRedirections() {
node2.clusterSetSlotMigrating(slot51, getNodeId(node3.clusterNodes()));
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) {
for (String infoLine : infoOutput.split("\n")) {
Expand All @@ -191,5 +199,5 @@ private void waitForClusterReady() throws InterruptedException {
Thread.sleep(50);
}
}

}

0 comments on commit c84039c

Please sign in to comment.