Skip to content

Commit c84039c

Browse files
committed
Merge pull request #541 from xetorthio/jedis_cluster_hashtag
Add support for redis cluster hashtags
2 parents adc9cb2 + 76814b5 commit c84039c

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/main/java/redis/clients/util/JedisClusterCRC16.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,20 @@
33
public class JedisClusterCRC16 {
44
public final static int polynomial = 0x1021; // Represents x^16+x^12+x^5+1
55
static int crc;
6+
67

78
public static int getSlot(String key) {
9+
int s = key.indexOf("{");
10+
if (s > -1) {
11+
int e = key.indexOf("}", s+1);
12+
if (e > -1 && e != s+1) {
13+
key = key.substring(s+1, e);
14+
}
15+
}
16+
return getCRC16(key) % 16384;
17+
}
18+
19+
private static int getCRC16(String key) {
820
crc = 0x0000;
921
for (byte b : key.getBytes()) {
1022
for (int i = 0; i < 8; i++) {
@@ -18,6 +30,6 @@ public static int getSlot(String key) {
1830
}
1931
}
2032

21-
return crc &= 0xffff % 16384;
33+
return crc &= 0xffff ;
2234
}
2335
}

src/test/java/redis/clients/jedis/tests/JedisClusterTest.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ public void testRedisClusterMaxRedirections() {
170170
node2.clusterSetSlotMigrating(slot51, getNodeId(node3.clusterNodes()));
171171
jc.set("51", "foo");
172172
}
173+
174+
@Test
175+
public void testRedisHashtag() {
176+
assertEquals(JedisClusterCRC16.getSlot("{bar"), JedisClusterCRC16.getSlot("foo{{bar}}zap"));
177+
assertEquals(JedisClusterCRC16.getSlot("{user1000}.following"), JedisClusterCRC16.getSlot("{user1000}.followers"));
178+
assertNotEquals(JedisClusterCRC16.getSlot("foo{}{bar}"), JedisClusterCRC16.getSlot("bar"));
179+
assertEquals(JedisClusterCRC16.getSlot("foo{bar}{zap}"), JedisClusterCRC16.getSlot("bar"));
180+
}
173181

174182
private String getNodeId(String infoOutput) {
175183
for (String infoLine : infoOutput.split("\n")) {
@@ -191,5 +199,5 @@ private void waitForClusterReady() throws InterruptedException {
191199
Thread.sleep(50);
192200
}
193201
}
194-
202+
195203
}

0 commit comments

Comments
 (0)