Skip to content

Commit

Permalink
feat: implement mria:match_delete/2 function
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeTupchiy committed Dec 18, 2023
1 parent a7e7c16 commit fe8abea
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
16 changes: 9 additions & 7 deletions src/mria.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
, sync_dirty/3
, sync_dirty/2
, clear_table/1
, match_delete/2

, dirty_write/2
, dirty_write/1
Expand Down Expand Up @@ -421,15 +422,16 @@ sync_dirty(Shard, Fun, Args) ->
sync_dirty(Shard, Fun) ->
sync_dirty(Shard, Fun, []).

%% `clear_table/1` and `match_delete/2` are implemented as transactions in Mnesia,
%% using call_backend_rw_dirty/3 is fine:
%% there is only one op, one table and one shard in these transactions
-spec clear_table(mria:table()) -> t_result(ok).
clear_table(Table) ->
Shard = mria_config:shard_rlookup(Table),
case is_upstream(Shard) of
true ->
maybe_middleman(mnesia, clear_table, [Table]);
false ->
rpc_to_core_node(Shard, mnesia, clear_table, [Table])
end.
call_backend_rw_dirty(?FUNCTION_NAME, Table, []).

-spec match_delete(mria:table(), ets:match_pattern()) -> t_result(ok).
match_delete(Table, Pattern) ->
call_backend_rw_dirty(?FUNCTION_NAME, Table, [Pattern]).

-spec dirty_write(tuple()) -> ok.
dirty_write(Record) ->
Expand Down
12 changes: 8 additions & 4 deletions src/mria_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
%% Hacks for manipulating Mnesia internal structures
-export([ set_where_to_read/2
, clear_table_int/1
, clear_table_int/2
, get_internals/0
]).

Expand Down Expand Up @@ -383,14 +384,17 @@ set_where_to_read(Node, Table) ->
false
end.

%% @doc Clear table without creating a new transaction.
-spec clear_table_int(mria:table()) -> ok.
clear_table_int(Tab) ->
clear_table_int(Tab, '_').

%% @doc Clear table without creating a new transaction.
-spec clear_table_int(mria:table(), ets:match_pattern()) -> ok.
clear_table_int(Tab, Pattern) ->
case get(mnesia_activity_state) of
{mnesia, Tid, Ts} ->
mnesia:clear_table(Tid, Ts, Tab, '_');
mnesia:clear_table(Tid, Ts, Tab, Pattern);
{Mod, Tid, Ts} ->
Mod:clear_table(Tid, Ts, Tab, '_');
Mod:clear_table(Tid, Ts, Tab, Pattern);
_ ->
error(no_transaction)
end.
Expand Down
6 changes: 6 additions & 0 deletions src/mria_replica_importer_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ import_op(Op, Acc) ->
Acc;
{clear_table, Tab} ->
mria_mnesia:clear_table_int(Tab),
Acc;
{clear_table, Tab, Pattern} ->
mria_mnesia:clear_table_int(Tab, Pattern),
Acc
end.

Expand All @@ -218,6 +221,9 @@ import_op_dirty(Op, Acc) ->
Acc;
{clear_table, Tab} ->
mnesia:clear_table(Tab),
Acc;
{clear_table, Tab, Pattern} ->
mnesia:match_delete(Tab, Pattern),
Acc
end.

Expand Down
4 changes: 3 additions & 1 deletion src/mria_rlog.erl
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ get_protocol_version() ->
%%
%% 0 -> 1: Add clear_table message to the batch message of the
%% boostrapper.
1.
%% 1 -> 2: Add `{clear_table, Tab, Pattern}` op to support
%% `mnesia:match_delete/2` API extension.
2.

intercept_trans(Tid, Commit) ->
?tp(mria_rlog_intercept_trans, Commit#{tid => Tid}),
Expand Down
5 changes: 4 additions & 1 deletion src/mria_rlog_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,10 @@ transform_op({{Tab, Key}, {_, _Incr}, update_counter}) ->
{delete, Tab, Key}
end;
transform_op({{Tab, '_'}, '_', clear_table}) ->
{clear_table, Tab}.
%% Keep it for backward compatibility
{clear_table, Tab};
transform_op({{Tab, '_'}, Pattern, clear_table}) ->
{clear_table, Tab, Pattern}.

%%================================================================================
%% Internal exports (gen_rpc)
Expand Down

0 comments on commit fe8abea

Please sign in to comment.