MCPcopy
hub / github.com/segmentio/kafka-go / murmur2

Function murmur2

balancer.go:311–353  ·  view source on GitHub ↗

Go port of the Java library's murmur2 function. https://github.com/apache/kafka/blob/1.0/clients/src/main/java/org/apache/kafka/common/utils/Utils.java#L353

(data []byte)

Source from the content-addressed store, hash-verified

309// Go port of the Java library's murmur2 function.
310// https://github.com/apache/kafka/blob/1.0/clients/src/main/java/org/apache/kafka/common/utils/Utils.java#L353
311func murmur2(data []byte) uint32 {
312 length := len(data)
313 const (
314 seed uint32 = 0x9747b28c
315 // 'm' and 'r' are mixing constants generated offline.
316 // They're not really 'magic', they just happen to work well.
317 m = 0x5bd1e995
318 r = 24
319 )
320
321 // Initialize the hash to a random value
322 h := seed ^ uint32(length)
323 length4 := length / 4
324
325 for i := 0; i < length4; i++ {
326 i4 := i * 4
327 k := (uint32(data[i4+0]) & 0xff) + ((uint32(data[i4+1]) & 0xff) << 8) + ((uint32(data[i4+2]) & 0xff) << 16) + ((uint32(data[i4+3]) & 0xff) << 24)
328 k *= m
329 k ^= k >> r
330 k *= m
331 h *= m
332 h ^= k
333 }
334
335 // Handle the last few bytes of the input array
336 extra := length % 4
337 if extra >= 3 {
338 h ^= (uint32(data[(length & ^3)+2]) & 0xff) << 16
339 }
340 if extra >= 2 {
341 h ^= (uint32(data[(length & ^3)+1]) & 0xff) << 8
342 }
343 if extra >= 1 {
344 h ^= uint32(data[length & ^3]) & 0xff
345 h *= m
346 }
347
348 h ^= h >> 13
349 h *= m
350 h ^= h >> 15
351
352 return h
353}

Callers 2

BalanceMethod · 0.85
TestMurmur2Function · 0.85

Calls

no outgoing calls

Tested by 1

TestMurmur2Function · 0.68