Nostr. NIP45
(Nostr Lib v0.2.1)
(nip45)
View Source
NIP-45: COUNT HyperLogLog helpers.
Provides deterministic offset derivation, HyperLogLog register operations, and helper functions for aggregating COUNT payloads from multiple relays.
Summary
Functions
Adds an event pubkey into HLL registers.
Adds a hex-encoded pubkey to HLL registers.
Aggregates COUNT payloads and merges any valid HLL values.
Returns estimated cardinality from HLL registers.
Parses a hex-encoded HLL register payload.
Returns true when a filter can derive an HLL offset.
Derives the deterministic NIP-45 offset for a filter.
Merges two HLL values by taking max register values.
Creates a new empty HLL with the given offset.
Creates a new empty HLL using offset derived from a filter.
Encodes HLL registers to a hex string.
Types
@type aggregate_result() :: %{ fallback_sum: non_neg_integer(), estimate: non_neg_integer() | nil, hll: binary() | nil, used_hll_count: non_neg_integer() }
@type offset_error() :: :no_tag_filter | :multiple_tag_filters | :invalid_tag_values
Functions
@spec add_event(Nostr.NIP45.HLL.t(), Nostr.Event.t()) :: {:ok, Nostr.NIP45.HLL.t()} | {:error, :invalid_pubkey}
Adds an event pubkey into HLL registers.
@spec add_pubkey(Nostr.NIP45.HLL.t(), binary()) :: {:ok, Nostr.NIP45.HLL.t()} | {:error, :invalid_pubkey}
Adds a hex-encoded pubkey to HLL registers.
@spec aggregate_count_payloads(Nostr.Filter.t() | map(), [ Nostr.Message.count_payload() | map() ]) :: {:ok, aggregate_result()} | {:error, :invalid_payloads}
Aggregates COUNT payloads and merges any valid HLL values.
Returns exact summed counts as :fallback_sum and approximate merged estimate
as :estimate when HLL data is usable.
@spec estimate(Nostr.NIP45.HLL.t()) :: non_neg_integer()
Returns estimated cardinality from HLL registers.
@spec from_hex(binary(), 8..23) :: {:ok, Nostr.NIP45.HLL.t()} | {:error, :invalid_hll}
Parses a hex-encoded HLL register payload.
@spec hll_eligible?(Nostr.Filter.t() | map()) :: boolean()
Returns true when a filter can derive an HLL offset.
@spec hll_offset(Nostr.Filter.t() | map()) :: {:ok, 8..23} | {:error, offset_error()}
Derives the deterministic NIP-45 offset for a filter.
Currently only filters with exactly one #<letter> attribute are treated
as eligible.
@spec merge(Nostr.NIP45.HLL.t(), Nostr.NIP45.HLL.t()) :: {:ok, Nostr.NIP45.HLL.t()} | {:error, :offset_mismatch}
Merges two HLL values by taking max register values.
@spec new(8..23) :: Nostr.NIP45.HLL.t()
Creates a new empty HLL with the given offset.
@spec new_from_filter(Nostr.Filter.t() | map()) :: {:ok, Nostr.NIP45.HLL.t()} | {:error, offset_error()}
Creates a new empty HLL using offset derived from a filter.
@spec to_hex(Nostr.NIP45.HLL.t()) :: binary()
Encodes HLL registers to a hex string.