Nebulex v2.0.0-rc.0 Nebulex.Adapter.HashSlot behaviour View Source

This behaviour provides a callback to compute the hash slot for a specific key based on the number of slots/nodes.

The purpose of this module is to allow users to implement a custom hash function to distribute keys. It can be used to select the node/slot where a specific key is supposed to be.

It is highly recommended to use a Consistent Hashing algorithm.

Example

defmodule MyApp.HashSlot do
  @behaviour Nebulex.Adapter.HashSlot

  def keyslot(key, range) do
    key
    |> :erlang.phash2()
    |> :jchash.compute(range)
  end
end

This example uses Jumping Consistent Hash.

Link to this section Summary

Callbacks

Computes the hash for a specific key based on the number of slots given by range. Returns a slot within the range 0..range-1.

Link to this section Callbacks

Specs

keyslot(key :: any(), range :: pos_integer()) :: non_neg_integer()

Computes the hash for a specific key based on the number of slots given by range. Returns a slot within the range 0..range-1.

Example

iex> MyHash.keyslot("mykey", 10)
2