Compare commits

...

3 Commits

2 changed files with 58 additions and 10 deletions

View File

@ -18,7 +18,7 @@
-include_lib("dudeswave_backend/include/defines.hrl"). -include_lib("dudeswave_backend/include/defines.hrl").
-include_lib("storage/include/storage.hrl"). -include_lib("storage/include/storage.hrl").
-export([details/2, new/4, update/5, delete/1]). -export([details/2, new/4, update/5, delete/2]).
-doc """ -doc """
Return user details. Return user details.
@ -136,17 +136,19 @@ update(User, Host, Name, Email, Desc) ->
Delete an existing user from the database. Delete an existing user from the database.
``` ```
-spec delete(User) -> ok | {error, Reason} when -spec delete(User, Host) -> ok | {error, Reason} when
User :: binary(), User :: binary(),
Host :: binary(),
Reason :: term(). Reason :: term().
``` ```
""". """.
-spec delete(User) -> ok | {error, Reason} when -spec delete(User, Host) -> ok | {error, Reason} when
User :: binary(), User :: binary(),
Host :: binary(),
Reason :: term(). Reason :: term().
delete(User) -> delete(User, Host) ->
% We are missing the cleanup of the cookies ComplUser = <<User/binary, "@", Host/binary>>,
% here. For that, we need to add at least another
% API to the storage layer. ok = storage:delete(?USERSBUCK, ComplUser),
storage:delete(?USERSBUCK, User). ok = storage:delete_set(?COOKIESBUCK, [{user, ComplUser}]).

View File

@ -53,7 +53,7 @@ file:
shrink/2]). shrink/2]).
% Public record operation functions % 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 % Module callbacks
-export([code_change/3, -export([code_change/3,
@ -462,6 +462,30 @@ Spec:
delete(Bucket, Key) -> delete(Bucket, Key) ->
gen_server:call(?MODULE, {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 """ -doc """
Spec: Spec:
@ -704,6 +728,20 @@ handle_call({delete, Bucket, Key}, _From, State) ->
{aborted, Reason} -> {reply, {error, Reason}, State} {aborted, Reason} -> {reply, {error, Reason}, State}
end; 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) ->
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 true -> false
end end
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).