Ankusa.Redis.Scan (ankusa_redis v0.7.0)

Copy Markdown View Source

Read a Redis hash a page at a time with HSCAN, for the stores that mirror a whole hash into memory (Ankusa.Routes.Store.Redis, Ankusa.SourceStore.Redis).

HGETALL is one command whose cost grows with the hash: at a million entries it holds Redis's single thread for the whole read and the connection for as long, so it either hits the command timeout or stalls every other client. A scan is many short commands (COUNT 1000 entries each), each bounded by its own timeout, with a deadline over the whole walk, and the caller handles every page as it arrives instead of building a million-entry list first.

HSCAN is not a snapshot: an entry is returned at least once if it was in the hash for the whole walk, possibly more than once, and an entry written or deleted meanwhile may or may not appear. A reducer must therefore be idempotent (a put by key), and a caller that needs a consistent read compares the hash's version before and after (both stores bump one in the same step as every write).

Summary

Functions

Fold fun over the field/value pairs of the hash at key, one HSCAN page at a time.

Types

reducer(acc)

@type reducer(acc) :: ([{String.t(), String.t()}], acc ->
                   {:ok, acc} | {:error, term()})

Functions

reduce(conn, key, acc, fun, opts)

@spec reduce(GenServer.server(), String.t(), acc, reducer(acc), keyword()) ::
  {:ok, acc} | {:error, term()}
when acc: term()

Fold fun over the field/value pairs of the hash at key, one HSCAN page at a time.

Options: :timeout (required, milliseconds for each command), :deadline (required, an absolute System.monotonic_time(:millisecond) the walk must be done by) and :count (the page-size hint, default 1000).

A command failure is {:error, {:redis_unavailable, reason}}, a missed deadline {:error, {:redis_unavailable, :load_timeout}}, and whatever fun returns as an error ends the walk with that error.