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
8 changes: 4 additions & 4 deletions src/main/java/io/split/client/utils/MurmurHash3.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static final long getLongLittleEndian(byte[] buf, int offset) {
/**
* Returns the MurmurHash3_x86_32 hash.
*/
public static int murmurhash3_x86_32(byte[] data, int offset, int len, int seed) {
public static long murmurhash3_x86_32(byte[] data, int offset, int len, int seed) {

final int c1 = 0xcc9e2d51;
final int c2 = 0x1b873593;
Expand Down Expand Up @@ -111,7 +111,7 @@ public static int murmurhash3_x86_32(byte[] data, int offset, int len, int seed)
h1 *= 0xc2b2ae35;
h1 ^= h1 >>> 16;

return h1;
return h1 & 0xFFFFFFFFL;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend dropping a comment here explaining why the ANDing is happening

}


Expand All @@ -120,7 +120,7 @@ public static int murmurhash3_x86_32(byte[] data, int offset, int len, int seed)
* the string to a temporary buffer. This is more than 2x faster than hashing the result
* of String.getBytes().
*/
public static int murmurhash3_x86_32(CharSequence data, int offset, int len, int seed) {
public static long murmurhash3_x86_32(CharSequence data, int offset, int len, int seed) {

final int c1 = 0xcc9e2d51;
final int c2 = 0x1b873593;
Expand Down Expand Up @@ -236,7 +236,7 @@ public static int murmurhash3_x86_32(CharSequence data, int offset, int len, int
h1 *= 0xc2b2ae35;
h1 ^= h1 >>> 16;

return h1;
return h1 & 0xFFFFFFFFL;
}


Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/split/engine/splitter/Splitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static String getTreatment(String key, int seed, List<Partition> partitio
}

/*package private*/
static int hash(String key, int seed) {
static long hash(String key, int seed) {
return MurmurHash3.murmurhash3_x86_32(key, 0, key.length(), seed);
}

Expand Down Expand Up @@ -63,8 +63,8 @@ private static String getTreatment(int bucket, List<Partition> partitions) {
}

/*package private*/
static int bucket(int hash) {
return Math.abs(hash % 100) + 1;
static int bucket(long hash) {
return (int) (Math.abs(hash % 100) + 1);
}


Expand Down
10 changes: 5 additions & 5 deletions src/test/java/io/split/engine/splitter/HashConsistencyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public void testLegacyHashNonAlphaNum() throws IOException {

@Test
public void testMurmur3HashAlphaNum() throws IOException {
File file = new File("src/test/resources", "murmur3-sample-data.csv");
File file = new File("src/test/resources", "murmur3-sample-data-v2.csv");
validateFileMurmur3Hash(file);
}

@Test
public void testMurmur3HashNonAlphaNum() throws IOException {
File file = new File("src/test/resources", "murmur3-sample-data-non-alpha-numeric.csv");
File file = new File("src/test/resources", "murmur3-sample-data-non-alpha-numeric-v2.csv");
validateFileMurmur3Hash(file);
}

Expand Down Expand Up @@ -77,10 +77,10 @@ private void validateFileMurmur3Hash(File file) throws IOException {
String[] parts = line.split(",");
Integer seed = Integer.parseInt(parts[0]);
String key = parts[1];
int expected_hash = Integer.parseInt(parts[2]);
long expected_hash = Long.parseLong(parts[2]);
int expected_bucket = Integer.parseInt(parts[3]);

int hash = Splitter.hash(key, seed);
long hash = Splitter.hash(key, seed);
int bucket = Splitter.bucket(hash);

Assert.assertEquals(expected_hash, hash);
Expand All @@ -98,7 +98,7 @@ private void validateFileGuavaMurmur3Hash(File file) throws IOException {
String[] parts = line.split(",");
Integer seed = Integer.parseInt(parts[0]);
String key = parts[1];
int expected_hash = Integer.parseInt(parts[2]);
long expected_hash = Long.parseLong(parts[2]);
int expected_bucket = Integer.parseInt(parts[3]);

int hash = Hashing.murmur3_32(seed).hashString(key, Charset.forName("UTF-8")).asInt();
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/io/split/engine/splitter/HashingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ private void collisionTest(int seed, MyHash hash, List<String> keys) {
int collisions = 0;
long durationSum = 0;

Set<Integer> hashes = Sets.newHashSet();
Set<Long> hashes = Sets.newHashSet();

for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
long start = System.nanoTime();
int keyHash = hash.hash(seed, key);
long keyHash = hash.hash(seed, key);
durationSum += (System.nanoTime() - start);

if (!hashes.add(keyHash)) {
Expand All @@ -136,7 +136,7 @@ private void bucketTest(int seed, MyHash hash, List<String> keys) {

for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
int keyHash = hash.hash(seed, key);
long keyHash = hash.hash(seed, key);
int bucket = Splitter.bucket(keyHash);
buckets.add(bucket);
ranges[(bucket - 1) / 10]++;
Expand Down
17 changes: 10 additions & 7 deletions src/test/java/io/split/engine/splitter/MyHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
*/
public interface MyHash {

int hash(int seed, String key);
long hash(int seed, String key);

public static class Murmur32Hash implements MyHash {
@Override
public int hash(int seed, String key) {
public long hash(int seed, String key) {
return MurmurHash3.murmurhash3_x86_32(key, 0, key.length(), seed);
}

Expand All @@ -26,9 +26,12 @@ public String toString() {
}

public static class GuavaMurmur32Hash implements MyHash {

private final Charset UTF_8 = Charset.forName("UTF-8");

@Override
public int hash(int seed, String key) {
return Hashing.murmur3_32(seed).hashString(key, Charset.forName("UTF-8")).asInt();
public long hash(int seed, String key) {
return Hashing.murmur3_32(seed).hashString(key, UTF_8).asInt();
}

@Override
Expand All @@ -41,7 +44,7 @@ public String toString() {
public static class SeededNaturalHash implements MyHash {

@Override
public int hash(int seed, String key) {
public long hash(int seed, String key) {
int h = seed;
for (int i = 0; i < key.length(); i++) {
h = 31 * h + key.charAt(i);
Expand All @@ -57,7 +60,7 @@ public String toString() {
public static class XorNaturalHash implements MyHash {

@Override
public int hash(int seed, String key) {
public long hash(int seed, String key) {
int h = 0;
for (int i = 0; i < key.length(); i++) {
h = 31 * h + key.charAt(i);
Expand All @@ -73,7 +76,7 @@ public String toString() {
public static class LoseLoseHash implements MyHash {

@Override
public int hash(int seed, String key) {
public long hash(int seed, String key) {
//char[] val = key.toCharArray();
int h = seed;
for (int i = 0; i < key.length(); i++) {
Expand Down
44 changes: 42 additions & 2 deletions src/test/java/io/split/engine/splitter/SplitterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
import org.junit.Ignore;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Random;

Expand All @@ -33,7 +39,7 @@ public void generateData() {
for (int i = 0; i < 1000; i++) {
int keyLength = minKeyLength + r.nextInt(13);
String key = RandomStringUtils.randomAlphanumeric(keyLength);
int hash = Splitter.hash(key, seed);
long hash = Splitter.hash(key, seed);
int bucket = Splitter.bucket(hash);
System.out.println(Joiner.on(',').join(Lists.newArrayList(seed, key, hash, bucket)));
}
Expand All @@ -52,14 +58,48 @@ public void generateNonAlphaNumericData() {
for (int i = 0; i < 1000; i++) {
int keyLength = minKeyLength + r.nextInt(13);
String key = RandomStringUtils.random(keyLength);
int hash = Splitter.hash(key, seed);
long hash = Splitter.hash(key, seed);
int bucket = Splitter.bucket(hash);
System.out.println(Joiner.on(',').join(Lists.newArrayList(seed, key, hash, bucket)));
}
}

}

/**
* Use this utily method when algos changes are required and you need to
* generate another sample file using existing seed and key input from
* another file
*
* @throws IOException
*/
@Ignore
@Test
public void generateDataFromExistingInput() throws IOException {
File file = new File("src/test/resources", "murmur3-sample-data-non-alpha-numeric.csv");
BufferedReader reader = new BufferedReader(new FileReader(file));
reader.readLine(); // Header

File target = new File("src/test/resources", "murmur3-sample-data-non-alpha-numeric-v2.csv");
BufferedWriter writer = new BufferedWriter(new FileWriter(target));

// Writer header.
writer.append("# seed, key, hash, bucket");
writer.newLine();

String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
Integer seed = Integer.parseInt(parts[0]);
String key = parts[1];
long hash = Splitter.hash(key, seed);
int bucket = Splitter.bucket(hash);
writer.append(Joiner.on(',').join(Lists.newArrayList(seed, key, hash, bucket)));
writer.newLine();
}
writer.close();
}

@Test
public void works() {
List<Partition> partitions = Lists.newArrayList();
Expand Down
Loading