-module(bravo@dbag). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([new/3, insert/2, lookup/2, delete/1, delete_key/2, delete_all_objects/1, delete_object/2, tab2file/5, tab2list/1, file2tab/3]). -export_type([d_bag/1]). -opaque d_bag(GXN) :: {d_bag, gleam@erlang@atom:atom_(), integer()} | {gleam_phantom, GXN}. -spec new(binary(), integer(), bravo@etc:access()) -> {ok, d_bag(any())} | {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(GXT), list(GXT)) -> boolean(). insert(Dbag, Objects) -> bravo:try_insert(erlang:element(2, Dbag), erlang:element(3, Dbag), Objects). -spec lookup(d_bag(GXW), any()) -> list(GXW). lookup(Dbag, Key) -> bravo:try_lookup(erlang:element(2, Dbag), Key). -spec delete(d_bag(any())) -> boolean(). delete(Dbag) -> bravo:try_delete(erlang:element(2, Dbag)). -spec delete_key(d_bag(any()), any()) -> nil. delete_key(Dbag, Key) -> bravo:try_delete_key(erlang:element(2, Dbag), Key), nil. -spec delete_all_objects(d_bag(any())) -> nil. delete_all_objects(Dbag) -> bravo:try_delete_all_objects(erlang:element(2, Dbag)), nil. -spec delete_object(d_bag(GYH), GYH) -> nil. delete_object(Dbag, Object) -> bravo:try_delete_object(erlang:element(2, Dbag), Object), nil. -spec tab2file(d_bag(any()), binary(), boolean(), boolean(), boolean()) -> boolean(). tab2file(Dbag, Filename, Object_count, Md5sum, Sync) -> case bravo:try_tab2file( erlang:element(2, Dbag), gleam@string:to_utf_codepoints(Filename), Object_count, Md5sum, Sync ) of ok -> true; {error, _} -> false end. -spec tab2list(d_bag(GYR)) -> list(GYR). tab2list(Dbag) -> bravo:try_tab2list(erlang:element(2, Dbag)). -spec file2tab( binary(), boolean(), fun((gleam@dynamic:dynamic_()) -> {ok, GYL} | {error, any()}) ) -> gleam@option:option(d_bag(GYL)). file2tab(Filename, Verify, Decoder) -> case bravo:try_file2tab(gleam@string:to_utf_codepoints(Filename), Verify) of {error, _} -> none; {ok, Name} -> _assert_subject = gleam@dynamic:int( bravo:inform(Name, erlang:binary_to_atom(<<"keypos"/utf8>>)) ), {ok, Keypos} = case _assert_subject of {ok, _} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Assertion pattern match failed"/utf8>>, value => _assert_fail, module => <<"bravo/dbag"/utf8>>, function => <<"file2tab"/utf8>>, line => 153}) end, Table = {d_bag, Name, Keypos}, gleam@bool:guard( not (gleam@list:all( tab2list(Table), fun(Obj) -> case Decoder(gleam@dynamic:from(Obj)) of {ok, _} -> true; {error, _} -> false end end )), none, fun() -> {some, Table} end ) end.