Implement all Jedis cluster commands and rename RedisSlot class

This commit is contained in:
Marcos Nils
2013-12-20 16:56:29 -03:00
parent 0bc27ac3a2
commit af72248c22
4 changed files with 726 additions and 321 deletions

View File

@@ -0,0 +1,29 @@
package redis.clients.jedis.tests.utils;
public class JedisClusterCRC16 {
public final static int polynomial = 0x1021; // Represents x^16+x^12+x^5+1
static int crc;
public JedisClusterCRC16(){
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;
}
public static void main(String[] args) {
System.out.println(getSlot("test"));
}
}