Move Jedis CRC16 util as it's being used in the project.

Changed Makefile to cleanup redis cluster node configuration before running tests
Add cleanup to ClusterCommandTest.
This commit is contained in:
Marcos Nils
2014-01-14 15:57:53 -03:00
parent 33716e237c
commit 4ab8ea2ef7
5 changed files with 45 additions and 8 deletions

View File

@@ -3,7 +3,7 @@ package redis.clients.jedis;
import redis.clients.jedis.exceptions.JedisAskDataException;
import redis.clients.jedis.exceptions.JedisClusterException;
import redis.clients.jedis.exceptions.JedisMovedDataException;
import redis.clients.jedis.tests.utils.JedisClusterCRC16;
import redis.clients.util.JedisClusterCRC16;
public abstract class JedisClusterCommand<T> {

View File

@@ -0,0 +1,21 @@
package redis.clients.util;
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) {
crc = 0x0000;
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;
}
}