murmur2 implements the same hashing algorithm used by the Apache Kafka Java client's DefaultPartitioner (org.apache.kafka.common.utils.Utils.murmur2). It is a variant of MurmurHash2 with a fixed seed of 0x9747b28c and little-endian byte ordering. Reference: - https://github.com/apache/kafka/blob/4.
(b []byte)
| 11 | // - https://github.com/apache/kafka/blob/4.3.0/clients/src/main/java/org/apache/kafka/common/utils/Utils.java#L496-L541 |
| 12 | // - https://github.com/aappleby/smhasher/blob/master/src/MurmurHash2.cpp |
| 13 | func murmur2(b []byte) uint32 { |
| 14 | const ( |
| 15 | seed uint32 = 0x9747b28c |
| 16 | m uint32 = 0x5bd1e995 |
| 17 | r = 24 |
| 18 | ) |
| 19 | h := seed ^ uint32(len(b)) |
| 20 | for len(b) >= 4 { |
| 21 | k := binary.LittleEndian.Uint32(b) |
| 22 | b = b[4:] |
| 23 | k *= m |
| 24 | k ^= k >> r |
| 25 | k *= m |
| 26 | h *= m |
| 27 | h ^= k |
| 28 | } |
| 29 | switch len(b) { |
| 30 | case 3: |
| 31 | h ^= uint32(b[2]) << 16 |
| 32 | fallthrough |
| 33 | case 2: |
| 34 | h ^= uint32(b[1]) << 8 |
| 35 | fallthrough |
| 36 | case 1: |
| 37 | h ^= uint32(b[0]) |
| 38 | h *= m |
| 39 | } |
| 40 | h ^= h >> 13 |
| 41 | h *= m |
| 42 | h ^= h >> 15 |
| 43 | return h |
| 44 | } |
no outgoing calls