pulsar-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mme...@apache.org
Subject [incubator-pulsar] 01/03: Fix C++ MurmurHash3 algorithm (#1795)
Date Fri, 01 Jun 2018 06:25:57 GMT
This is an automated email from the ASF dual-hosted git repository.

mmerli pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/incubator-pulsar.git

commit 2f82811efaacb6f627f102d27dca1b4a88d6a309
Author: massakam <massakam@yahoo-corp.jp>
AuthorDate: Fri May 18 01:07:15 2018 +0900

    Fix C++ MurmurHash3 algorithm (#1795)
    
    * Fix C++ MurmurHash3 algorithm
    
    * Add the same test cases to HashTest.java
---
 pulsar-client-cpp/lib/Murmur3_32Hash.cc                        |  7 ++++---
 pulsar-client-cpp/tests/HashTest.cc                            | 10 +++++++++-
 .../src/test/java/org/apache/pulsar/client/impl/HashTest.java  |  4 ++++
 3 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/pulsar-client-cpp/lib/Murmur3_32Hash.cc b/pulsar-client-cpp/lib/Murmur3_32Hash.cc
index ef88590..d45169c 100644
--- a/pulsar-client-cpp/lib/Murmur3_32Hash.cc
+++ b/pulsar-client-cpp/lib/Murmur3_32Hash.cc
@@ -70,14 +70,15 @@ uint32_t Murmur3_32Hash::makeHash(const void *key, const int64_t len)
{
         h1 = mixH1(h1, k1);
     }
 
+    const uint8_t *tail = reinterpret_cast<const uint8_t *>(data + nblocks * MACRO_CHUNK_SIZE);
     uint32_t k1 = 0;
     switch (len - nblocks * MACRO_CHUNK_SIZE) {
         case 3:
-            k1 ^= static_cast<uint32_t>(blocks[2]) << 16;
+            k1 ^= static_cast<uint32_t>(tail[2]) << 16;
         case 2:
-            k1 ^= static_cast<uint32_t>(blocks[1]) << 8;
+            k1 ^= static_cast<uint32_t>(tail[1]) << 8;
         case 1:
-            k1 ^= static_cast<uint32_t>(blocks[0]);
+            k1 ^= static_cast<uint32_t>(tail[0]);
     };
 
     h1 ^= mixK1(k1);
diff --git a/pulsar-client-cpp/tests/HashTest.cc b/pulsar-client-cpp/tests/HashTest.cc
index 97dcb90..bd6de09 100644
--- a/pulsar-client-cpp/tests/HashTest.cc
+++ b/pulsar-client-cpp/tests/HashTest.cc
@@ -58,10 +58,18 @@ TEST(HashTest, testJavaStringHash) {
 
 TEST(HashTest, testMurmur3_32Hash) {
     Murmur3_32Hash hash;
+    std::string k1 = "k1";
+    std::string k2 = "k2";
     std::string key1 = "key1";
     std::string key2 = "key2";
+    std::string key01 = "key01";
+    std::string key02 = "key02";
 
     // Same value as Java client
+    ASSERT_EQ(2110152746, hash.makeHash(k1));
+    ASSERT_EQ(1479966664, hash.makeHash(k2));
     ASSERT_EQ(462881061, hash.makeHash(key1));
     ASSERT_EQ(1936800180, hash.makeHash(key2));
-}
\ No newline at end of file
+    ASSERT_EQ(39696932, hash.makeHash(key01));
+    ASSERT_EQ(751761803, hash.makeHash(key02));
+}
diff --git a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/HashTest.java b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/HashTest.java
index f53205d..c10c8d0 100644
--- a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/HashTest.java
+++ b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/HashTest.java
@@ -43,7 +43,11 @@ public class HashTest {
         Hash h = Murmur3_32Hash.getInstance();
 
         // Same value as C++ client
+        assertEquals(2110152746, h.makeHash("k1"));
+        assertEquals(1479966664, h.makeHash("k2"));
         assertEquals(462881061, h.makeHash("key1"));
         assertEquals(1936800180, h.makeHash("key2"));
+        assertEquals(39696932, h.makeHash("key01"));
+        assertEquals(751761803, h.makeHash("key02"));
     }
 }

-- 
To stop receiving notification emails like this one, please contact
mmerli@apache.org.

Mime
View raw message