Current section
Files
Jump to
Current section
Files
src/bravo@dbag.erl
-module(bravo@dbag).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new/3, insert/2, insert_obj/2, lookup/2, delete/1]).
-export_type([d_bag/0]).
-opaque d_bag() :: {d_bag, gleam@erlang@atom:atom_(), integer()}.
-spec new(binary(), integer(), bravo@etc:access()) -> {ok, d_bag()} |
{error, gleam@option:option(bravo@error:erlang_error())}.
new(Name, Keypos, Access) ->
Atom = erlang:binary_to_atom(Name),
gleam@bool:guard(
Keypos < 1,
{error, none},
fun() ->
gleam@result:'try'(
begin
_pipe = bravo:try_new(Atom, [duplicate_bag, case Access of
public ->
public;
protected ->
protected;
private ->
private
end, named_table, {keypos, Keypos}, {write_concurrency,
auto}, {read_concurrency, true}, {decentralized_counters,
true}]),
gleam@result:map_error(_pipe, fun(E) -> {some, E} end)
end,
fun(A) -> {ok, {d_bag, A, Keypos}} end
)
end
).
-spec insert(d_bag(), list(any())) -> boolean().
insert(Dbag, Objects) ->
bravo:try_insert(erlang:element(2, Dbag), erlang:element(3, Dbag), Objects).
-spec insert_obj(d_bag(), list(bravo@object:object(any()))) -> boolean().
insert_obj(Dbag, Objects) ->
_pipe = gleam@list:map(Objects, fun bravo@object:extract/1),
insert(Dbag, _pipe).
-spec lookup(d_bag(), any()) -> list(bravo@object:object(gleam@dynamic:dynamic_())).
lookup(Dbag, Key) ->
_pipe = bravo:try_lookup(erlang:element(2, Dbag), Key),
gleam@list:map(_pipe, fun(Raw) -> bravo@object:new(Raw) end).
-spec delete(d_bag()) -> boolean().
delete(Dbag) ->
bravo:try_delete(erlang:element(2, Dbag)).