Add delete_set, a function needed to remove a series of records from a bucket.

main
absc 2024-09-15 20:18:38 +00:00
parent 5b73df8999
commit 140427853e
1 changed files with 48 additions and 2 deletions

View File

@ -53,7 +53,7 @@ file:
shrink/2]).
% Public record operation functions
-export([delete/2, list/2, read/2, write/3, write/4]).
-export([delete/2, delete_set/2, list/2, read/2, write/3, write/4]).
% Module callbacks
-export([code_change/3,
@ -462,6 +462,30 @@ Spec:
delete(Bucket, Key) ->
gen_server:call(?MODULE, {delete, Bucket, Key}).
-doc """
Delete a set of records based on the given metadata list.
Spec:
```
-spec delete_set(Bucket, Metadata) -> ok | {error, Reason} when
Bucket :: atom(),
Metadata :: [{Key, Value}],
Key :: term(),
Value :: term(),
Reason :: term().
```
""".
-spec delete_set(Bucket, Metadata) -> ok | {error, Reason} when
Bucket :: atom(),
Metadata :: [{Key, Value}],
Key :: term(),
Value :: term(),
Reason :: term().
delete_set(Bucket, Metadata) ->
gen_server:call(?MODULE, {delete_set, Bucket, Metadata}).
-doc """
Spec:
@ -704,6 +728,20 @@ handle_call({delete, Bucket, Key}, _From, State) ->
{aborted, Reason} -> {reply, {error, Reason}, State}
end;
handle_call({delete_set, Bucket, Metadata}, _From, State) ->
Q = qlc:q([X || X <- mnesia:table(Bucket), filter(Metadata, X#object.metadata)]),
F = fun() ->
Recs = qlc:e(Q),
delete_matching(Bucket, Recs)
end,
case mnesia:transaction(F) of
{atomic, ok} -> {reply, ok, State};
{aborted, Reason} -> {reply, {error, Reason}, State}
end;
handle_call({write, Bucket, Key, Term}, _From, State) ->
handle_call({write, Bucket, Key, Term, []}, _From, State);
@ -792,3 +830,11 @@ filter([H | T], M) ->
true -> false
end
end.
%
% Delete all the records in a table for every key in the list.
%
delete_matching(_Bucket, []) -> ok;
delete_matching(Bucket, [H | T]) ->
mnesia:delete(Bucket, H#object.key),
delete_matching(Bucket, T).