esdb_hash_nif (reckon_db v1.2.6)

View Source

Optimized hashing operations for reckon-db.

This module provides high-performance hash implementations:

  • xxHash64: Extremely fast 64-bit hash
  • xxHash3: Even faster, modern 64-bit hash with SIMD
  • Partition hash: For consistent stream/subscription routing
  • FNV-1a: Fast for small keys

The mode is automatically detected at startup based on whether the NIF library is available. Community edition users (hex.pm) will always use the Erlang fallbacks, which provide identical functionality.

Usage

  %% Fast hash for routing
  Partition = esdb_hash_nif:partition_hash(StreamId, 16).
 
  %% Stream-specific routing
  Partition = esdb_hash_nif:stream_partition(StoreId, StreamId, 16).
 
  %% Raw xxHash for checksums
  Hash = esdb_hash_nif:xxhash64(Data).
 
  %% Check which mode is active
  nif = esdb_hash_nif:implementation().  %% Enterprise
  erlang = esdb_hash_nif:implementation(). %% Community

Summary

Functions

Fast replacement for erlang:phash2/2.

Compute FNV-1a hash of binary data. Fast for small keys (under 32 bytes).

Get the current implementation mode.

Check if the NIF is loaded (Enterprise mode).

Hash data and map to a partition number. Used for consistent routing of streams/subscriptions to workers.

Hash multiple binaries and return their partition assignments.

Hash {StoreId, StreamId} tuple for stream routing.

Compute xxHash3 (64-bit) of binary data. xxHash3 is faster than xxHash64, especially for small inputs.

Compute xxHash64 of binary data.

Compute xxHash64 with a seed.

Functions

fast_phash(Data, Range)

-spec fast_phash(Data :: binary(), Range :: pos_integer()) -> non_neg_integer().

Fast replacement for erlang:phash2/2.

fnv1a(Data)

-spec fnv1a(Data :: binary()) -> non_neg_integer().

Compute FNV-1a hash of binary data. Fast for small keys (under 32 bytes).

implementation()

-spec implementation() -> nif | erlang.

Get the current implementation mode.

is_nif_loaded()

-spec is_nif_loaded() -> boolean().

Check if the NIF is loaded (Enterprise mode).

partition_hash(Data, Partitions)

-spec partition_hash(Data :: binary(), Partitions :: pos_integer()) -> non_neg_integer().

Hash data and map to a partition number. Used for consistent routing of streams/subscriptions to workers.

partition_hash_batch(Items, Partitions)

-spec partition_hash_batch(Items :: [binary()], Partitions :: pos_integer()) -> [non_neg_integer()].

Hash multiple binaries and return their partition assignments.

stream_partition(StoreId, StreamId, Partitions)

-spec stream_partition(StoreId :: binary(), StreamId :: binary(), Partitions :: pos_integer()) ->
                          non_neg_integer().

Hash {StoreId, StreamId} tuple for stream routing.

xxhash3(Data)

-spec xxhash3(Data :: binary()) -> non_neg_integer().

Compute xxHash3 (64-bit) of binary data. xxHash3 is faster than xxHash64, especially for small inputs.

xxhash64(Data)

-spec xxhash64(Data :: binary()) -> non_neg_integer().

Compute xxHash64 of binary data.

xxhash64(Data, Seed)

-spec xxhash64(Data :: binary(), Seed :: non_neg_integer()) -> non_neg_integer().

Compute xxHash64 with a seed.