Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 0 additions & 173 deletions src/main/java/io/split/client/utils/MurmurHash3.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,61 +60,6 @@ public static final long getLongLittleEndian(byte[] buf, int offset) {
}


/**
* Returns the MurmurHash3_x86_32 hash.
*/
public static long murmurhash3_x86_32(byte[] data, int offset, int len, int seed) {

final int c1 = 0xcc9e2d51;
final int c2 = 0x1b873593;

int h1 = seed;
int roundedEnd = offset + (len & 0xfffffffc); // round down to 4 byte block

for (int i = offset; i < roundedEnd; i += 4) {
// little endian load order
int k1 = (data[i] & 0xff) | ((data[i + 1] & 0xff) << 8) | ((data[i + 2] & 0xff) << 16) | (data[i + 3] << 24);
k1 *= c1;
k1 = (k1 << 15) | (k1 >>> 17); // ROTL32(k1,15);
k1 *= c2;

h1 ^= k1;
h1 = (h1 << 13) | (h1 >>> 19); // ROTL32(h1,13);
h1 = h1 * 5 + 0xe6546b64;
}

// tail
int k1 = 0;

switch (len & 0x03) {
case 3:
k1 = (data[roundedEnd + 2] & 0xff) << 16;
// fallthrough
case 2:
k1 |= (data[roundedEnd + 1] & 0xff) << 8;
// fallthrough
case 1:
k1 |= (data[roundedEnd] & 0xff);
k1 *= c1;
k1 = (k1 << 15) | (k1 >>> 17); // ROTL32(k1,15);
k1 *= c2;
h1 ^= k1;
}

// finalization
h1 ^= len;

// fmix(h1);
h1 ^= h1 >>> 16;
h1 *= 0x85ebca6b;
h1 ^= h1 >>> 13;
h1 *= 0xc2b2ae35;
h1 ^= h1 >>> 16;

return h1 & 0xFFFFFFFFL;
}


/**
* Returns the MurmurHash3_x86_32 hash of the UTF-8 bytes of the String without actually encoding
* the string to a temporary buffer. This is more than 2x faster than hashing the result
Expand Down Expand Up @@ -142,29 +87,6 @@ public static long murmurhash3_x86_32(CharSequence data, int offset, int len, in
k2 = code;
bits = 8;

/***
// optimized ascii implementation (currently slower!!! code size?)
if (shift == 24) {
k1 = k1 | (code << 24);

k1 *= c1;
k1 = (k1 << 15) | (k1 >>> 17); // ROTL32(k1,15);
k1 *= c2;

h1 ^= k1;
h1 = (h1 << 13) | (h1 >>> 19); // ROTL32(h1,13);
h1 = h1*5+0xe6546b64;

shift = 0;
nBytes += 4;
k1 = 0;
} else {
k1 |= code << shift;
shift += 8;
}
continue;
***/

} else if (code < 0x800) {
k2 = (0xC0 | (code >> 6))
| ((0x80 | (code & 0x3F)) << 8);
Expand Down Expand Up @@ -240,99 +162,4 @@ public static long murmurhash3_x86_32(CharSequence data, int offset, int len, in
}


/**
* Returns the MurmurHash3_x64_128 hash, placing the result in "out".
*/
public static void murmurhash3_x64_128(byte[] key, int offset, int len, int seed, LongPair out) {
// The original algorithm does have a 32 bit unsigned seed.
// We have to mask to match the behavior of the unsigned types and prevent sign extension.
long h1 = seed & 0x00000000FFFFFFFFL;
long h2 = seed & 0x00000000FFFFFFFFL;

final long c1 = 0x87c37b91114253d5L;
final long c2 = 0x4cf5ad432745937fL;

int roundedEnd = offset + (len & 0xFFFFFFF0); // round down to 16 byte block
for (int i = offset; i < roundedEnd; i += 16) {
long k1 = getLongLittleEndian(key, i);
long k2 = getLongLittleEndian(key, i + 8);
k1 *= c1;
k1 = Long.rotateLeft(k1, 31);
k1 *= c2;
h1 ^= k1;
h1 = Long.rotateLeft(h1, 27);
h1 += h2;
h1 = h1 * 5 + 0x52dce729;
k2 *= c2;
k2 = Long.rotateLeft(k2, 33);
k2 *= c1;
h2 ^= k2;
h2 = Long.rotateLeft(h2, 31);
h2 += h1;
h2 = h2 * 5 + 0x38495ab5;
}

long k1 = 0;
long k2 = 0;

switch (len & 15) {
case 15:
k2 = (key[roundedEnd + 14] & 0xffL) << 48;
case 14:
k2 |= (key[roundedEnd + 13] & 0xffL) << 40;
case 13:
k2 |= (key[roundedEnd + 12] & 0xffL) << 32;
case 12:
k2 |= (key[roundedEnd + 11] & 0xffL) << 24;
case 11:
k2 |= (key[roundedEnd + 10] & 0xffL) << 16;
case 10:
k2 |= (key[roundedEnd + 9] & 0xffL) << 8;
case 9:
k2 |= (key[roundedEnd + 8] & 0xffL);
k2 *= c2;
k2 = Long.rotateLeft(k2, 33);
k2 *= c1;
h2 ^= k2;
case 8:
k1 = ((long) key[roundedEnd + 7]) << 56;
case 7:
k1 |= (key[roundedEnd + 6] & 0xffL) << 48;
case 6:
k1 |= (key[roundedEnd + 5] & 0xffL) << 40;
case 5:
k1 |= (key[roundedEnd + 4] & 0xffL) << 32;
case 4:
k1 |= (key[roundedEnd + 3] & 0xffL) << 24;
case 3:
k1 |= (key[roundedEnd + 2] & 0xffL) << 16;
case 2:
k1 |= (key[roundedEnd + 1] & 0xffL) << 8;
case 1:
k1 |= (key[roundedEnd] & 0xffL);
k1 *= c1;
k1 = Long.rotateLeft(k1, 31);
k1 *= c2;
h1 ^= k1;
}

//----------
// finalization

h1 ^= len;
h2 ^= len;

h1 += h2;
h2 += h1;

h1 = fmix64(h1);
h2 = fmix64(h2);

h1 += h2;
h2 += h1;

out.val1 = h1;
out.val2 = h2;
}

}
15 changes: 14 additions & 1 deletion src/test/java/io/split/engine/splitter/HashingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ public void bucketTestForSequential() {

}

@Test
public void bucketTestForRandomKeys() {

int seed = (int) System.currentTimeMillis();

List<String> randomKeys = randomUUIDs(100);
bucketTest(seed, new MyHash.Murmur32Hash(), randomKeys);
bucketTest(seed, new MyHash.GuavaMurmur32Hash(), randomKeys);
bucketTest(seed, new MyHash.SeededNaturalHash(), randomKeys);
bucketTest(seed, new MyHash.XorNaturalHash(), randomKeys);

}

@Test
public void collisionTestForRandom() {

Expand Down Expand Up @@ -92,7 +105,7 @@ private List<String> sequentialIds(int size) {

private List<String> mshIds() {
List<String> bldr = Lists.newArrayList();
for (int i = 28243; i <= 28267; i++) {
for (int i = 28243; i <= 28273; i++) {
bldr.add("" + i);
}
return bldr;
Expand Down