barrel_ars (barrel_docdb v1.0.0)

View Source

Document path extraction for automatic indexing

Extracts all paths from a document for automatic path indexing. Based on barrel_ars_view.erl from barrel apps branch.

Path format: [field1, field2, ..., value] For arrays, index position is included: [field, 0, nested_field, value]

Example:

   Doc = #{<<"type">> => <<"user">>,
           <<"profile">> => #{<<"name">> => <<"Alice">>}},
   Paths = barrel_ars:analyze(Doc),
   %% [{[<<"type">>, <<"user">>], <<>>},
   %%  {[<<"profile">>, <<"name">>, <<"Alice">>], <<>>}]

Performance optimizations: - Paths built in reverse then flipped (O(n) vs O(n^2) for ++) - Uses maps for O(1) diff lookups instead of sets

Summary

Functions

Extract all paths from a document. Accepts both Erlang maps and indexed CBOR binaries. Returns a list of {Path, Value} tuples where Path is a list of field names/indices ending with the value.

Compute the difference between old and new paths. Returns {Added, Removed} where: Added = paths in New but not in Old Removed = paths in Old but not in New

Convert a single path to an MQTT-style topic string. Handles various value types: binaries, integers, atoms, etc.

Convert analyzed paths to MQTT-style topic strings. Each path [field1, field2, value] becomes "field1/field2/value".

Truncate a value for indexing. Binary values longer than 100 bytes are truncated.

Functions

analyze(Doc)

-spec analyze(map() | binary()) -> [{Path :: [term()], binary()}].

Extract all paths from a document. Accepts both Erlang maps and indexed CBOR binaries. Returns a list of {Path, Value} tuples where Path is a list of field names/indices ending with the value.

diff(Old, New)

-spec diff(Old :: [{term(), term()}], New :: [{term(), term()}]) ->
              {Added :: [{term(), term()}], Removed :: [{term(), term()}]}.

Compute the difference between old and new paths. Returns {Added, Removed} where: Added = paths in New but not in Old Removed = paths in Old but not in New

Uses maps for O(1) membership tests instead of sets.

path_to_topic(Path)

-spec path_to_topic([term()]) -> binary().

Convert a single path to an MQTT-style topic string. Handles various value types: binaries, integers, atoms, etc.

paths_to_topics(Paths)

-spec paths_to_topics([{[term()], binary()}]) -> [binary()].

Convert analyzed paths to MQTT-style topic strings. Each path [field1, field2, value] becomes "field1/field2/value".

This is used for subscription matching with barrel_sub.

short(S)

-spec short(term()) -> term().

Truncate a value for indexing. Binary values longer than 100 bytes are truncated.