talan v0.1.1 Talan.Counter View Source

Linear probabilistic counter implementation with concurrent accessibility, powered by :atomics module for cardinality estimation.

Cardinality is the count of unique elements.

For more info about linear probabilistic counting: linear probabilistic counting

Link to this section Summary

Functions

Returns the estimated cardinality for the given %Talan.Counter{} struct.

Returns a new %Talan.Counter{} struct.

Hashes term and sets a bit to mark it has been seen.

Link to this section Types

Link to this type

t()

View Source
t() :: %Talan.Counter{
  atomics_ref: reference(),
  filter_length: non_neg_integer(),
  hash_function: function()
}

Link to this section Functions

Link to this function

cardinality(counter)

View Source
cardinality(t()) :: non_neg_integer()

Returns the estimated cardinality for the given %Talan.Counter{} struct.

Examples

iex> c = Talan.Counter.new(10_000)
iex> c |> Talan.Counter.put(["you", :can, Hash, {"any", "elixir", "term"}])
iex> c |> Talan.Counter.put(["you", :can, Hash, {"any", "elixir", "term"}])
iex> c |> Talan.Counter.cardinality()
1
iex> c |> Talan.Counter.put("more")
iex> c |> Talan.Counter.cardinality()
2
Link to this function

new(expected_cardinality, options \\ [])

View Source
new(non_neg_integer(), list()) :: t()

Returns a new %Talan.Counter{} struct.

expected_cardinality is the max number of uniq items the counter will handle with approx 1% of error rate.

Options

Examples

iex> c = Talan.Counter.new(10_000)
iex> c |> Talan.Counter.put(["you", :can, Hash, {"any", "elixir", "term"}])
iex> c |> Talan.Counter.put("more")
iex> c |> Talan.Counter.put("another")
iex> c |> Talan.Counter.cardinality()
3
Link to this function

put(counter, term)

View Source
put(t(), any()) :: :ok

Hashes term and sets a bit to mark it has been seen.

Doesn't store the term so it's space efficient. Uses :atomics so it's mutable & highly concurrent.

Returns :ok.

Examples

iex> c = Talan.Counter.new(10_000)
iex> c |> Talan.Counter.put(["you", :can, Hash, {"any", "elixir", "term"}])
:ok