File tree 2 files changed +22
-2
lines changed
main/java/redis/clients/util
test/java/redis/clients/jedis/tests
2 files changed +22
-2
lines changed Original file line number Diff line number Diff line change 3
3
public class JedisClusterCRC16 {
4
4
public final static int polynomial = 0x1021 ; // Represents x^16+x^12+x^5+1
5
5
static int crc ;
6
+
6
7
7
8
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 ) {
8
20
crc = 0x0000 ;
9
21
for (byte b : key .getBytes ()) {
10
22
for (int i = 0 ; i < 8 ; i ++) {
@@ -18,6 +30,6 @@ public static int getSlot(String key) {
18
30
}
19
31
}
20
32
21
- return crc &= 0xffff % 16384 ;
33
+ return crc &= 0xffff ;
22
34
}
23
35
}
Original file line number Diff line number Diff line change @@ -170,6 +170,14 @@ public void testRedisClusterMaxRedirections() {
170
170
node2 .clusterSetSlotMigrating (slot51 , getNodeId (node3 .clusterNodes ()));
171
171
jc .set ("51" , "foo" );
172
172
}
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
+ }
173
181
174
182
private String getNodeId (String infoOutput ) {
175
183
for (String infoLine : infoOutput .split ("\n " )) {
@@ -191,5 +199,5 @@ private void waitForClusterReady() throws InterruptedException {
191
199
Thread .sleep (50 );
192
200
}
193
201
}
194
-
202
+
195
203
}
You can’t perform that action at this time.
0 commit comments