merlimat closed pull request #1795: Fix C++ MurmurHash3 algorithm
URL: https://github.com/apache/incubator-pulsar/pull/1795
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pulsar-client-cpp/lib/Murmur3_32Hash.cc 
b/pulsar-client-cpp/lib/Murmur3_32Hash.cc
index ef88590560..d45169c3f8 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 97dcb90501..bd6de09ed6 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 f53205d47e..c10c8d0c7a 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 void murmur3_32HashTest() {
         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"));
     }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to