Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix for OneByteSimpleHash

  • Loading branch information...
commit 31b9b1a0c3b3715faa6cc552f31c991418856cb0 1 parent 34c95cc
Alex Baranau authored
View
13 src/main/java/com/sematext/hbase/wd/RowKeyDistributorByHashPrefix.java
@@ -75,10 +75,7 @@ public OneByteSimpleHash(int maxBuckets) {
@Override
public byte[] getHashPrefix(byte[] originalKey) {
- long hash = 0;
- for (byte b : originalKey) {
- hash = (hash << Byte.SIZE) + (b & 0xff);
- }
+ long hash = Math.abs(hashBytes(originalKey));
return new byte[] {(byte) (hash % mod)};
}
@@ -101,6 +98,14 @@ public String getParamsToStore() {
public void init(String storedParams) {
this.mod = Integer.valueOf(storedParams);
}
+
+ /** Compute hash for binary data. */
+ private static int hashBytes(byte[] bytes) {
+ int hash = 1;
+ for (int i = 0; i < bytes.length; i++)
+ hash = (31 * hash) + (int) bytes[i];
+ return hash;
+ }
}
@Override
View
6 src/test/java/com/sematext/hbase/wd/OneByteSimpleHashTest.java
@@ -72,6 +72,8 @@ public void testLimitedDistribution() {
@Test
public void testHashPrefixDistribution() {
testDistribution(32, 55);
+ testDistribution(37, 13);
+ testDistribution(255, 20);
testDistribution(256, 20);
testDistribution(256, 1);
testDistribution(1, 200);
@@ -91,7 +93,9 @@ private void testDistribution(int maxBuckets, int countForEachBucket) {
Assert.assertEquals(maxBuckets, allKeys.length);
for (int bucketCount : bucketCounts) {
- Assert.assertEquals(countForEachBucket, bucketCount);
+ // i.e. all buckets expected to have similar amount of values (+- 10%)
+ Assert.assertTrue("Unexpected values count in bucket: " + bucketCount + ", avg: " + countForEachBucket,
+ Math.abs((countForEachBucket - bucketCount) / countForEachBucket) < 0.10);
}
}
}
View
1  src/test/java/com/sematext/hbase/wd/RowKeyDistributorTestBase.java
@@ -79,7 +79,6 @@ public void testGet() throws IOException, InterruptedException {
byte[] distributedKey = keyDistributor.getDistributedKey(key);
byte[] value = Bytes.toBytes("some");
- // No need to adjust key here as hasher doesn't change it
hTable.put(new Put(distributedKey).add(CF, QUAL, value));
Result result = hTable.get(new Get(distributedKey));

0 comments on commit 31b9b1a

Please sign in to comment.
Something went wrong with that request. Please try again.