Class MurmurHash

java.lang.Object
io.karma.kommons.hash.MurmurHash

@API(status=STABLE) public final class MurmurHash extends Object
Implementation of the MurmurHash3 32-bit and 128-bit hash functions. Taken straight from here. Modified to suit the needs of the Kommons library.

MurmurHash is a non-cryptographic hash function suitable for general hash-based lookup. The name comes from two basic operations, multiply (MU) and rotate (R), used in its inner loop. Unlike cryptographic hash functions, it is not specifically designed to be difficult to reverse by an adversary, making it unsuitable for cryptographic purposes.

This contains a Java port of the 32-bit hash function MurmurHash3_x86_32 and the 128-bit hash function MurmurHash3_x64_128 from Austin Applyby's original c++ code in SMHasher.

This is public domain code with no copyrights. From home page of SMHasher:

"All MurmurHash versions are public domain software, and the author disclaims all copyright to their code."

Original adaption from Apache Hive. That adaption contains a hash64 method that is not part of the original MurmurHash3 code. It is not recommended to use these methods. They will be removed in a future release. To obtain a 64-bit hash use half of the bits from the hash128x64 methods using the input data converted to bytes.

Since:
1.13
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    A default seed to use for the murmur hash algorithm.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    hash32(long data)
    Generates 32-bit hash from a long with a default seed value.
    static int
    hash32(long data, int seed)
    Generates 32-bit hash from a long with the given seed.
    static int
    hash32(long data1, long data2)
    Generates 32-bit hash from two longs with a default seed value.
    static int
    hash32(long data1, long data2, int seed)
    Generates 32-bit hash from two longs with the given seed.
    static int
    hash32(@Nullable String s)
     
    static int
    hash32x86(byte[] data)
    Generates 32-bit hash from the byte array with a seed of zero.
    static int
    hash32x86(byte[] data, int offset, int length, int seed)
    Generates 32-bit hash from the byte array with the given offset, length and seed.
    static void
     

    Methods inherited from class java.lang.Object

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

    • DEFAULT_SEED

      public static final int DEFAULT_SEED
      A default seed to use for the murmur hash algorithm. Has the value 104729.
      See Also:
  • Method Details

    • hash32

      public static int hash32(long data1, long data2)
      Generates 32-bit hash from two longs with a default seed value. This is a helper method that will produce the same result as:
       int offset = 0;
       int seed = 104729;
       int hash = MurmurHash.hash32x86(ByteBuffer.allocate(16)
                                                  .putLong(data1)
                                                  .putLong(data2)
                                                  .array(), offset, 16, seed);
       
      Parameters:
      data1 - The first long to hash
      data2 - The second long to hash
      Returns:
      The 32-bit hash
      See Also:
    • hash32

      public static int hash32(long data1, long data2, int seed)
      Generates 32-bit hash from two longs with the given seed. This is a helper method that will produce the same result as:
       int offset = 0;
       int hash = MurmurHash3.hash32x86(ByteBuffer.allocate(16)
                                                  .putLong(data1)
                                                  .putLong(data2)
                                                  .array(), offset, 16, seed);
       
      Parameters:
      data1 - The first long to hash
      data2 - The second long to hash
      seed - The initial seed value
      Returns:
      The 32-bit hash
      See Also:
    • hash32

      public static int hash32(long data)
      Generates 32-bit hash from a long with a default seed value. This is a helper method that will produce the same result as:
       int offset = 0;
       int seed = 104729;
       int hash = MurmurHash3.hash32x86(ByteBuffer.allocate(8)
                                                  .putLong(data)
                                                  .array(), offset, 8, seed);
       
      Parameters:
      data - The long to hash
      Returns:
      The 32-bit hash
      See Also:
    • hash32

      public static int hash32(long data, int seed)
      Generates 32-bit hash from a long with the given seed. This is a helper method that will produce the same result as:
       int offset = 0;
       int hash = MurmurHash3.hash32x86(ByteBuffer.allocate(8)
                                                  .putLong(data)
                                                  .array(), offset, 8, seed);
       
      Parameters:
      data - The long to hash
      seed - The initial seed value
      Returns:
      The 32-bit hash
      See Also:
    • hash32x86

      public static int hash32x86(byte[] data)
      Generates 32-bit hash from the byte array with a seed of zero. This is a helper method that will produce the same result as:
       int offset = 0;
       int seed = 0;
       int hash = MurmurHash3.hash32x86(data, offset, data.length, seed);
       
      Parameters:
      data - The input byte array
      Returns:
      The 32-bit hash
      Since:
      1.14
      See Also:
    • hash32x86

      public static int hash32x86(byte[] data, int offset, int length, int seed)
      Generates 32-bit hash from the byte array with the given offset, length and seed.

      This is an implementation of the 32-bit hash function MurmurHash3_x86_32 from Austin Applyby's original MurmurHash3 c++ code in SMHasher.

      Parameters:
      data - The input byte array
      offset - The offset of data
      length - The length of array
      seed - The initial seed value
      Returns:
      The 32-bit hash
      Since:
      1.14
    • hash32

      public static int hash32(@Nullable @Nullable String s)
    • initProviders

      public static void initProviders()