Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement hashing with murmur #44

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
<version>${version.log4j2}</version>
</dependency>

<dependency>
<groupId>com.sangupta</groupId>
<artifactId>murmur</artifactId>
<version>1.0.0</version>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/no/finn/unleash/strategy/StrategyUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package no.finn.unleash.strategy;

import com.sangupta.murmur.Murmur3;

public final class StrategyUtils {
private static final int ONE_HUNDRED = 100;

Expand All @@ -25,15 +27,20 @@ public static boolean isNumeric(final CharSequence cs) {
}

/**
* Takes to string inputs concat them, produce a hashCode and return a normalized value between 0 and 100;
* Takes to string inputs concat them, produce a hash and return a normalized value between 0 and 100;
*
* @param identifier
* @param groupId
* @return
*/
public static int getNormalizedNumber(String identifier, String groupId) {
int hashCode = Math.abs((groupId + ':' + identifier).hashCode());
return hashCode % ONE_HUNDRED + 1;
return getNormalizedNumber(identifier, groupId, ONE_HUNDRED);
}

public static int getNormalizedNumber(String identifier, String groupId, int normalizer) {
byte[] value = (groupId + ':' + identifier).getBytes();
long hashCode = Murmur3.hash_x86_32(value, value.length, 0);
return (int)(hashCode % normalizer) + 1;
}

/**
Expand Down