View Source Talan.Counter (talan v0.1.2)
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
@type t() :: %Talan.Counter{ atomics_ref: reference(), filter_length: non_neg_integer(), hash_function: function() }
Link to this section Functions
@spec cardinality(t()) :: non_neg_integer()
Returns the estimated cardinality for the given
%Talan.Counter{}
struct.
examples
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
@spec 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
Options
hash_function
- defaults toMurmur.hash_x64_128/1
examples
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
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
Examples
iex> c = Talan.Counter.new(10_000)
iex> c |> Talan.Counter.put(["you", :can, Hash, {"any", "elixir", "term"}])
:ok