Merge pull request #17 from aeternity/gh3407-expose-helpers

Expose low-level helpers, fix dialyzer warnings
This commit is contained in:
Ulf Wiger 2020-12-22 14:57:02 +01:00 committed by GitHub
commit 66e5a51634
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -30,6 +30,8 @@
-behaviour(mnesia_backend_type). -behaviour(mnesia_backend_type).
-behaviour(gen_server). -behaviour(gen_server).
-dialyzer(no_undefined_callbacks).
%% ---------------------------------------------------------------------------- %% ----------------------------------------------------------------------------
%% EXPORTS %% EXPORTS
%% ---------------------------------------------------------------------------- %% ----------------------------------------------------------------------------
@ -120,6 +122,12 @@
-export([ix_prefixes/3]). -export([ix_prefixes/3]).
%% Exposed low-level helpers
-export([get_ref/2, %% (Alias, Tab) -> {RocksDbHandle, TabType}
encode_key/1, %% (Key) -> EncodedKey
decode_key/1, %% (EncodedKey) -> Key
encode_val/1, %% (Value) -> EncodedValue
decode_val/1]). %% (EncodedValue) -> Value
%% ---------------------------------------------------------------------------- %% ----------------------------------------------------------------------------
%% DEFINES %% DEFINES
@ -191,6 +199,22 @@
, on_write_error_store = ?WRITE_ERR_STORE_DEFAULT :: on_write_error_store() , on_write_error_store = ?WRITE_ERR_STORE_DEFAULT :: on_write_error_store()
}). }).
-type data_tab() :: atom().
-type index_pos() :: integer() | {atom()}.
-type index_type() :: ordered.
-type index_info() :: {index_pos(), index_type()}.
-type retainer_name() :: any().
-type index_tab() :: {data_tab(), index, index_info()}.
-type retainer_tab() :: {data_tab(), retainer, retainer_name()}.
-type alias() :: atom().
-type table_type() :: set | ordered_set | bag.
-type table() :: data_tab() | index_tab() | retainer_tab().
-export_type([alias/0,
table/0,
table_type/0]).
%% ---------------------------------------------------------------------------- %% ----------------------------------------------------------------------------
%% CONVENIENCE API %% CONVENIENCE API
%% ---------------------------------------------------------------------------- %% ----------------------------------------------------------------------------
@ -445,11 +469,6 @@ close_table_(Alias, Tab) ->
[self(), Tab]), [self(), Tab]),
ok; ok;
{ok, _} -> {ok, _} ->
ok;
_Other ->
?dbg("~p: close_table_(~p) -> _Other = ~p~n",
[self(), Tab, _Other]),
mnesia_ext_sup:stop_proc(Tab),
ok ok
end. end.
@ -474,8 +493,6 @@ pp_calls(I, [{M,F,A,Pos} | T]) ->
[Pp(M,F,A,Pos)|[["\n",Spc,Pp(M1,F1,A1,P1)] || {M1,F1,A1,P1} <- T]]. [Pp(M,F,A,Pos)|[["\n",Spc,Pp(M1,F1,A1,P1)] || {M1,F1,A1,P1} <- T]].
pp_pos([]) -> ""; pp_pos([]) -> "";
pp_pos(L) when is_integer(L) ->
[" (", integer_to_list(L), ")"];
pp_pos([{file,_},{line,L}]) -> pp_pos([{file,_},{line,L}]) ->
[" (", integer_to_list(L), ")"]. [" (", integer_to_list(L), ")"].
-endif. -endif.
@ -1420,9 +1437,7 @@ read_info(Item, Default, Ets) ->
end. end.
tab_name(icache, Tab) -> tab_name(icache, Tab) ->
list_to_atom("mnesia_ext_icache_" ++ tabname(Tab)); list_to_atom("mnesia_ext_icache_" ++ tabname(Tab)).
tab_name(info, Tab) ->
list_to_atom("mnesia_ext_info_" ++ tabname(Tab)).
proc_name(_Alias, Tab) -> proc_name(_Alias, Tab) ->
list_to_atom("mnesia_ext_proc_" ++ tabname(Tab)). list_to_atom("mnesia_ext_proc_" ++ tabname(Tab)).
@ -1464,8 +1479,7 @@ needs_key_only([{HP,_,Body}]) ->
not(wild_in_body(BodyVars) orelse not(wild_in_body(BodyVars) orelse
case bound_in_headpat(HP) of case bound_in_headpat(HP) of
{all,V} -> lists:member(V, BodyVars); {all,V} -> lists:member(V, BodyVars);
none -> false; Vars when is_list(Vars) -> any_in_body(lists:keydelete(2,1,Vars), BodyVars)
Vars -> any_in_body(lists:keydelete(2,1,Vars), BodyVars)
end); end);
needs_key_only(_) -> needs_key_only(_) ->
%% don't know %% don't know
@ -1502,10 +1516,7 @@ bound_in_headpat(HP) when is_atom(HP) ->
{all, HP}; {all, HP};
bound_in_headpat(HP) when is_tuple(HP) -> bound_in_headpat(HP) when is_tuple(HP) ->
[_|T] = tuple_to_list(HP), [_|T] = tuple_to_list(HP),
map_vars(T, 2); map_vars(T, 2).
bound_in_headpat(_) ->
%% this is not the place to throw an exception
none.
map_vars([H|T], P) -> map_vars([H|T], P) ->
case extract_vars(H) of case extract_vars(H) of
@ -1687,9 +1698,11 @@ keypos({_, retainer, _}) ->
keypos(Tab) when is_atom(Tab) -> keypos(Tab) when is_atom(Tab) ->
2. 2.
-spec encode_key(any()) -> binary().
encode_key(Key) -> encode_key(Key) ->
sext:encode(Key). sext:encode(Key).
-spec decode_key(binary()) -> any().
decode_key(CodedKey) -> decode_key(CodedKey) ->
case sext:partial_decode(CodedKey) of case sext:partial_decode(CodedKey) of
{full, Result, _} -> {full, Result, _} ->
@ -1698,9 +1711,11 @@ decode_key(CodedKey) ->
error(badarg, CodedKey) error(badarg, CodedKey)
end. end.
-spec encode_val(any()) -> binary().
encode_val(Val) -> encode_val(Val) ->
term_to_binary(Val). term_to_binary(Val).
-spec decode_val(binary()) -> any().
decode_val(CodedVal) -> decode_val(CodedVal) ->
binary_to_term(CodedVal). binary_to_term(CodedVal).
@ -1794,6 +1809,7 @@ is_retainer_dir(F, TabS) ->
{true, Name} {true, Name}
end. end.
-spec get_ref(alias(), table()) -> {rocksdb:db_handle(), table_type()}.
get_ref(Alias, Tab) -> get_ref(Alias, Tab) ->
call(Alias, Tab, get_ref). call(Alias, Tab, get_ref).