Class MurmurHash3

java.lang.Object
com.loomcache.common.util.MurmurHash3

public final class MurmurHash3 extends Object
MurmurHash3 (32-bit) — fast, non-cryptographic hash for key distribution.

Why MurmurHash3? - Excellent distribution (passes all SMHasher tests) - Fast: ~3 GB/s on modern CPUs - No collision bias: critical for even partition distribution - Used by: Guava, Cassandra, Kafka, Redis Cluster

We use the 32-bit variant because our partition count fits in an int and 32-bit hashing is faster on most JVMs.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    hash(byte[] data)
    Hash a byte array using MurmurHash3 (32-bit, x86).
    static int
    hash(byte[] data, int offset, int length, int seed)
    Core MurmurHash3 implementation.
    static int
    hash(String key)
    Hash a string key to a positive integer.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MurmurHash3

      public MurmurHash3()
  • Method Details

    • hash

      public static int hash(String key)
      Hash a string key to a positive integer.
      Parameters:
      key - the string key to hash (must not be null)
      Returns:
      a positive hash value
    • hash

      public static int hash(byte[] data)
      Hash a byte array using MurmurHash3 (32-bit, x86).
      Parameters:
      data - the byte array to hash (must not be null)
      Returns:
      the hash value
    • hash

      public static int hash(byte[] data, int offset, int length, int seed)
      Core MurmurHash3 implementation.

      Reference: https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp

      Parameters:
      data - the byte array to hash (must not be null)
      offset - the offset to start hashing from
      length - the number of bytes to hash
      seed - the seed value
      Returns:
      the hash value