barrel_query_sub (barrel_docdb v1.1.1)

View Source

barrel_query_sub - Query-based subscription manager

Manages query-based subscriptions for document changes. Subscribers receive notifications when documents matching their queries are modified.

Uses path optimization: only evaluates the full query when a change affects paths referenced by the query.

Example:

   Query = #{where => [{path, [<<"type">>], <<"user">>}]},
   {ok, SubRef} = barrel_query_sub:subscribe(DbRef, Query, self()),
   receive
       {barrel_query_change, DbName, #{id := DocId}} -> ok
   end,
   ok = barrel_query_sub:unsubscribe(SubRef)

Subscriptions are ephemeral - tied to subscriber process lifetime.

Summary

Functions

Notify query subscribers about a document change Called by barrel_db_server after document modifications. Only evaluates queries whose path patterns match the changed paths.

Start the query subscription manager

Stop the query subscription manager

Subscribe to document changes matching a query Returns a subscription reference that can be used to unsubscribe. The query must be a valid barrel_query specification.

Unsubscribe using the subscription reference

Types

att_info/0

-type att_info() ::
          #{name := binary(),
            content_type := binary(),
            length := non_neg_integer(),
            digest := binary(),
            chunked => boolean(),
            chunk_size => pos_integer(),
            chunk_count => pos_integer()}.

change/0

-type change() :: map().

db_config/0

-type db_config() :: #{path => string(), store => module(), atom() => term()}.

db_name/0

-type db_name() :: binary().

db_ref/0

-type db_ref() :: pid() | atom().

doc/0

-type doc() :: #{binary() => term()}.

doc_info/0

-type doc_info() :: #{id := docid(), rev := revid(), deleted := boolean(), revtree := revtree()}.

docid/0

-type docid() :: binary().

endpoint/0

-type endpoint() :: db_name() | {node(), db_name()} | {module(), term()}.

rep_options/0

-type rep_options() ::
          #{continuous => boolean(),
            since => seq_string(),
            filter => fun((doc()) -> boolean()),
            atom() => term()}.

rev_info/0

-type rev_info() ::
          #{id := revid(),
            parent := revid() | undefined,
            deleted := boolean(),
            attachments => #{binary() => att_info()}}.

revid/0

-type revid() :: binary().

revtree/0

-type revtree() :: #{revid() => rev_info()}.

seq/0

-type seq() :: barrel_hlc:timestamp().

seq_string/0

-type seq_string() :: binary().

view_name/0

-type view_name() :: binary().

view_result/0

-type view_result() :: #{key := term(), value := term(), id := docid()}.

Functions

code_change(OldVsn, State, Extra)

handle_call(Request, From, State)

handle_cast(Msg, State)

handle_info(Info, State)

init(_)

notify_change(DbName, DocId, Rev, DocBody)

-spec notify_change(db_name(), docid(), binary(), map()) -> ok.

Notify query subscribers about a document change Called by barrel_db_server after document modifications. Only evaluates queries whose path patterns match the changed paths.

start_link()

-spec start_link() -> {ok, pid()} | {error, term()}.

Start the query subscription manager

stop()

-spec stop() -> ok.

Stop the query subscription manager

subscribe(DbName, QuerySpec, Pid)

-spec subscribe(db_name(), barrel_query:query_spec(), pid()) -> {ok, reference()} | {error, term()}.

Subscribe to document changes matching a query Returns a subscription reference that can be used to unsubscribe. The query must be a valid barrel_query specification.

terminate(Reason, State)

unsubscribe(SubRef)

-spec unsubscribe(reference()) -> ok.

Unsubscribe using the subscription reference