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