kura_stream (kura v2.31.3)

View Source

Server-side cursor streaming for processing large result sets in batches. It needs a backend with cursors (DECLARE, FETCH), which PostgreSQL has and SQLite does not.

Q = kura_query:where(kura_query:from(my_user), {active, true}),
kura_stream:stream(my_repo, Q, fun(Batch) ->
    [process(Row) || Row <- Batch],
    ok
end, #{batch_size => 100}).

Summary

Functions

Stream query results in batches of 500, calling Fun for each batch.

Stream query results with options. Opts: #{batch_size => pos_integer()}.

Functions

stream(RepoMod, Query, Fun)

-spec stream(module(),
             #kura_query{from :: atom() | module() | binary() | undefined,
                         select :: [atom() | term()] | {exprs, [term()]},
                         wheres :: [term()],
                         joins :: [term()],
                         order_bys :: [term()],
                         group_bys :: [atom()],
                         havings :: [term()],
                         limit :: non_neg_integer() | undefined,
                         offset :: non_neg_integer() | undefined,
                         distinct :: boolean() | [atom()],
                         lock :: kura_query:lock() | binary() | undefined,
                         prefix :: binary() | undefined,
                         preloads :: [atom() | {atom(), list() | map()}],
                         ctes :: [{binary(), #kura_query{}}],
                         combinations :: [{union | union_all | intersect | except, #kura_query{}}],
                         include_deleted :: boolean()},
             fun(([map()]) -> ok)) ->
                ok | {error, term()}.

Stream query results in batches of 500, calling Fun for each batch.

stream(RepoMod, Query, Fun, Opts)

-spec stream(module(),
             #kura_query{from :: atom() | module() | binary() | undefined,
                         select :: [atom() | term()] | {exprs, [term()]},
                         wheres :: [term()],
                         joins :: [term()],
                         order_bys :: [term()],
                         group_bys :: [atom()],
                         havings :: [term()],
                         limit :: non_neg_integer() | undefined,
                         offset :: non_neg_integer() | undefined,
                         distinct :: boolean() | [atom()],
                         lock :: kura_query:lock() | binary() | undefined,
                         prefix :: binary() | undefined,
                         preloads :: [atom() | {atom(), list() | map()}],
                         ctes :: [{binary(), #kura_query{}}],
                         combinations :: [{union | union_all | intersect | except, #kura_query{}}],
                         include_deleted :: boolean()},
             fun(([map()]) -> ok),
             map()) ->
                ok | {error, term()}.

Stream query results with options. Opts: #{batch_size => pos_integer()}.

The query is scoped as kura_repo_worker:all/2 scopes it: soft-deleted rows are left out unless the query asks for them with kura_query:with_deleted/1, and a prefix tenant applies.

Raises {kura, {unsupported_capability, cursors, PoolModule}} on a backend that declares a capability model and no cursors (see kura_capabilities), before any statement runs.