Skip to content

Commit

Permalink
Update Murmur 3 from Commons Codec
Browse files Browse the repository at this point in the history
  • Loading branch information
acogoluegnes committed May 27, 2024
1 parent e8d30a7 commit 4e4db7f
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/main/java/com/rabbitmq/stream/impl/HashUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// info@rabbitmq.com.
package com.rabbitmq.stream.impl;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.nio.charset.StandardCharsets;
import java.util.function.ToIntFunction;

Expand All @@ -25,6 +26,7 @@ private HashUtils() {}

// from
// https://github.com/apache/commons-codec/blob/rel/commons-codec-1.15/src/main/java/org/apache/commons/codec/digest/MurmurHash3.java
// hash32x86 method
static class Murmur3 implements ToIntFunction<String> {

private static final int DEFAULT_SEED = 104729;
Expand All @@ -37,10 +39,10 @@ static class Murmur3 implements ToIntFunction<String> {
private static final int N_32 = 0xe6546b64;

private static int getLittleEndianInt(final byte[] data, final int index) {
return ((data[index] & 0xff))
| ((data[index + 1] & 0xff) << 8)
| ((data[index + 2] & 0xff) << 16)
| ((data[index + 3] & 0xff) << 24);
return data[index] & 0xff
| (data[index + 1] & 0xff) << 8
| (data[index + 2] & 0xff) << 16
| (data[index + 3] & 0xff) << 24;
}

private static int mix32(int k, int hash) {
Expand Down Expand Up @@ -70,6 +72,7 @@ private static int fmix32(int hash) {
this.seed = seed;
}

@SuppressFBWarnings({"SF_SWITCH_FALLTHROUGH", "SF_SWITCH_NO_DEFAULT"})
@Override
public int applyAsInt(String value) {
byte[] data = value.getBytes(StandardCharsets.UTF_8);
Expand All @@ -94,7 +97,7 @@ public int applyAsInt(String value) {
case 2:
k1 ^= (data[index + 1] & 0xff) << 8;
case 1:
k1 ^= (data[index] & 0xff);
k1 ^= data[index] & 0xff;

// mix functions
k1 *= C1_32;
Expand Down

0 comments on commit 4e4db7f

Please sign in to comment.