./ct_report/coverage/mod_smart_markers_rdbms_async.COVER.html

1 %%% @doc
2 %%% RDBMS backend for mod_smart_markers
3 %%% @end
4 %%% @copyright (C) 2022, Erlang Solutions Ltd.
5 -module(mod_smart_markers_rdbms_async).
6 -behavior(mod_smart_markers_backend).
7 -behaviour(mongoose_aggregator_worker).
8
9 -include("jlib.hrl").
10
11 -export([init/2, stop/1, update_chat_marker/2, get_chat_markers/4, get_conv_chat_marker/6]).
12 -export([remove_domain/2, remove_user/2, remove_to/2, remove_to_for_user/3]).
13
14 %% Worker callbacks
15 -export([request/2, aggregate/3, verify/3]).
16
17 -ifdef(gen_server_request_id).
18 -type request_id() :: gen_server:request_id().
19 -else.
20 -type request_id() :: term().
21 -endif.
22
23 %%--------------------------------------------------------------------
24 %% API
25 %%--------------------------------------------------------------------
26 -spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
27 init(HostType, Opts) ->
28 3 AsyncOpts = prepare_pool_opts(Opts),
29 3 mod_smart_markers_rdbms:init(HostType, Opts),
30 3 start_pool(HostType, AsyncOpts),
31 3 ok.
32
33 stop(HostType) ->
34 3 mongoose_async_pools:stop_pool(HostType, smart_markers).
35
36 prepare_pool_opts(#{async_writer := AsyncOpts}) ->
37 3 AsyncOpts#{pool_type => aggregate,
38 request_callback => fun ?MODULE:request/2,
39 aggregate_callback => fun ?MODULE:aggregate/3,
40 verify_callback => fun ?MODULE:verify/3}.
41
42 -spec start_pool(mongooseim:host_type(), mongoose_async_pools:pool_opts()) -> term().
43 start_pool(HostType, Opts) ->
44 3 mongoose_async_pools:start_pool(HostType, smart_markers, Opts).
45
46 %%% @doc
47 %%% 'from', 'to', 'thread' and 'type' keys of the ChatMarker map serve
48 %%% as a composite database key. If key is not available in the database,
49 %%% then chat marker must be added. Otherwise this function must update
50 %%% chat marker record for that composite key.
51 %%% @end
52 -spec update_chat_marker(mongooseim:host_type(),
53 mod_smart_markers:chat_marker()) -> ok.
54 update_chat_marker(HostType, #{from := #jid{luser = LU, lserver = LS},
55 to := To, thread := Thread, type := Type} = Marker) ->
56 19 Key = {LU, LS, To, Thread, Type},
57 19 mongoose_async_pools:put_task(HostType, smart_markers, Key, Marker).
58
59 %% Synchronous calls
60 -spec get_conv_chat_marker(HostType :: mongooseim:host_type(),
61 From :: jid:jid(),
62 To :: jid:jid(),
63 Thread :: mod_smart_markers:maybe_thread(),
64 Timestamp :: integer(),
65 Private :: boolean()) -> [mod_smart_markers:chat_marker()].
66 get_conv_chat_marker(HostType, From, To, Thread, TS, Private) ->
67 12 mod_smart_markers_rdbms:get_conv_chat_marker(HostType, From, To, Thread, TS, Private).
68
69 -spec get_chat_markers(HostType :: mongooseim:host_type(),
70 To :: jid:jid(),
71 Thread :: mod_smart_markers:maybe_thread(),
72 Timestamp :: integer()) -> [mod_smart_markers:chat_marker()].
73 get_chat_markers(HostType, To, Thread, TS) ->
74 10 mod_smart_markers_rdbms:get_chat_markers(HostType, To, Thread, TS).
75
76 -spec remove_domain(mongooseim:host_type(), jid:lserver()) -> mongoose_rdbms:query_result().
77 remove_domain(HostType, Domain) ->
78
:-(
mod_smart_markers_rdbms:remove_domain(HostType, Domain).
79
80 -spec remove_user(mongooseim:host_type(), jid:jid()) -> mongoose_rdbms:query_result().
81 remove_user(HostType, User) ->
82 33 mod_smart_markers_rdbms:remove_user(HostType, User).
83
84 -spec remove_to(mongooseim:host_type(), jid:jid()) -> mongoose_rdbms:query_result().
85 remove_to(HostType, To) ->
86 2 mod_smart_markers_rdbms:remove_to(HostType, To).
87
88 -spec remove_to_for_user(mongooseim:host_type(), From :: jid:jid(), To :: jid:jid()) ->
89 mongoose_rdbms:query_result().
90 remove_to_for_user(HostType, From, To) ->
91 1 mod_smart_markers_rdbms:remove_to_for_user(HostType, From, To).
92
93 %% callbacks
94 -spec aggregate(mod_smart_markers:chat_marker(),
95 mod_smart_markers:chat_marker(),
96 mongoose_async_pools:pool_extra()) -> {ok, mod_smart_markers:chat_marker()}.
97 aggregate(_, NewTask, _Extra) ->
98
:-(
{ok, NewTask}.
99
100 -spec request(mod_smart_markers:chat_marker(),
101 mongoose_async_pools:pool_extra()) -> request_id().
102 request(#{from := #jid{luser = LU, lserver = LS}, to := To, thread := Thread,
103 type := Type, timestamp := TS, id := Id}, #{host_type := HostType}) ->
104 19 ToEncoded = mod_smart_markers_rdbms:encode_jid(To),
105 19 ThreadEncoded = mod_smart_markers_rdbms:encode_thread(Thread),
106 19 TypeEncoded = mod_smart_markers_rdbms:encode_type(Type),
107 19 KeyValues = [LS, LU, ToEncoded, ThreadEncoded, TypeEncoded],
108 19 UpdateValues = [Id, TS],
109 19 InsertValues = KeyValues ++ UpdateValues,
110 19 rdbms_queries:request_upsert(HostType, smart_markers_upsert,
111 InsertValues, UpdateValues, KeyValues).
112
113 -spec verify(term(), mod_smart_markers:chat_marker(), mongoose_async_pools:pool_extra()) -> ok.
114 verify(Answer, Marker, _Extra) ->
115 19 mod_smart_markers_rdbms:verify(Answer, Marker).
Line Hits Source