1 |
|
%% Copyright (c) 2023 Peter Morgan <peter.james.morgan@gmail.com> |
2 |
|
%% |
3 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
|
%% you may not use this file except in compliance with the License. |
5 |
|
%% You may obtain a copy of the License at |
6 |
|
%% |
7 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
8 |
|
%% |
9 |
|
%% Unless required by applicable law or agreed to in writing, software |
10 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
11 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
|
%% See the License for the specific language governing permissions and |
13 |
|
%% limitations under the License. |
14 |
|
|
15 |
|
|
16 |
|
-module(pgec_storage_truncate). |
17 |
|
|
18 |
|
|
19 |
|
-export([callback_mode/0]). |
20 |
|
-export([handle_event/4]). |
21 |
|
-import(pgec_statem, [nei/1]). |
22 |
|
-include_lib("kernel/include/logger.hrl"). |
23 |
|
|
24 |
|
|
25 |
|
callback_mode() -> |
26 |
1 |
handle_event_function. |
27 |
|
|
28 |
|
|
29 |
|
handle_event({call, _}, _, {truncate, _}, _) -> |
30 |
1 |
{keep_state_and_data, postpone}; |
31 |
|
|
32 |
|
handle_event(internal, |
33 |
|
{truncate = Action, #{bucket := Bucket}}, |
34 |
|
{Action, Ref}, |
35 |
|
_) -> |
36 |
1 |
{keep_state_and_data, |
37 |
|
nei({keys, |
38 |
|
#{bucket => Bucket, |
39 |
|
folder => fun |
40 |
|
(_, Key, A) -> |
41 |
50 |
[Key | A] |
42 |
|
end, |
43 |
|
ref => Ref, |
44 |
|
accumulator => []}})}; |
45 |
|
|
46 |
|
handle_event(internal, |
47 |
|
{response, |
48 |
|
#{reply := {async, F}, |
49 |
|
label := #{bucket := Bucket, ref := Ref}}}, |
50 |
|
{truncate, Ref}, |
51 |
|
#{previous := {ready, _} = Previous} = Data) -> |
52 |
1 |
{next_state, |
53 |
|
Previous, |
54 |
|
maps:without([previous], Data), |
55 |
|
lists:foldl( |
56 |
|
fun |
57 |
|
(Key, A) -> |
58 |
50 |
[nei({delete, |
59 |
|
#{bucket => Bucket, |
60 |
|
key => Key}}) | A] |
61 |
|
end, |
62 |
|
[pop_callback_module], |
63 |
|
F())}; |
64 |
|
|
65 |
|
handle_event(EventType, EventContent, State, Data) -> |
66 |
13 |
pgec_storage_common:handle_event(EventType, |
67 |
|
EventContent, |
68 |
|
State, |
69 |
|
Data). |