./ct_report/coverage/mod_roster_mnesia.COVER.html

1 %%%----------------------------------------------------------------------
2 %%% File : mod_roster_mnesia.erl
3 %%% Author : MichaƂ Piotrowski <michal.piotrowski@erlang-solutions.com>
4 %%% Purpose : mod_roster mnesia backend
5 %%%
6 %%%
7 %%% ejabberd, Copyright (C) 2002-2014 ProcessOne
8 %%% MongooseIM, Copyright (C) 2015 Erlang Solutions Ltd.
9 %%%
10 %%%----------------------------------------------------------------------
11 -module(mod_roster_mnesia).
12
13 -include("mod_roster.hrl").
14
15 -behaviour(mod_roster_backend).
16
17 %% API
18 -export([init/2,
19 transaction/2,
20 read_roster_version/3,
21 write_roster_version/5,
22 get_roster/3,
23 get_roster_entry/6,
24 get_subscription_lists/3,
25 roster_subscribe_t/2,
26 update_roster_t/2,
27 del_roster_t/4,
28 remove_user_t/3]).
29
30 -spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
31 init(_HostType, _Opts) ->
32 55 mongoose_mnesia:create_table(roster,
33 [{disc_copies, [node()]},
34 {attributes, record_info(fields, roster)}]),
35 55 mongoose_mnesia:create_table(roster_version,
36 [{disc_copies, [node()]},
37 {attributes, record_info(fields, roster_version)}]),
38 55 mnesia:add_table_index(roster, us),
39 55 mnesia:add_table_index(roster_version, us),
40 55 ok.
41
42 -spec transaction(mongooseim:host_type(), fun(() -> any())) ->
43 {aborted, any()} | {atomic, any()}.
44 transaction(_HostType, F) ->
45
:-(
mnesia:transaction(F).
46
47 -spec read_roster_version(mongooseim:host_type(), jid:luser(), jid:lserver()) ->
48 binary() | error.
49 read_roster_version(_HostType, LUser, LServer) ->
50
:-(
US = {LUser, LServer},
51
:-(
case mnesia:dirty_read(roster_version, US) of
52
:-(
[#roster_version{version = V}] -> V;
53
:-(
[] -> error
54 end.
55
56 -spec write_roster_version(mongooseim:host_type(), jid:luser(), jid:lserver(),
57 mod_roster:transaction_state(), mod_roster:version()) -> ok.
58 write_roster_version(_HostType, LUser, LServer, TransactionState, Ver) ->
59
:-(
write(#roster_version{us = {LUser, LServer}, version = Ver}, TransactionState).
60
61 -spec get_roster(mongooseim:host_type(), jid:luser(), jid:lserver()) -> [mod_roster:roster()].
62 get_roster(_HostType, LUser, LServer) ->
63
:-(
US = {LUser, LServer},
64
:-(
case catch mnesia:dirty_index_read(roster, US, #roster.us) of
65
:-(
Items when is_list(Items)-> Items;
66
:-(
_ -> []
67 end.
68
69 -spec get_roster_entry(mongooseim:host_type(), jid:luser(), jid:lserver(), mod_roster:contact(),
70 mod_roster:transaction_state(), mod_roster:entry_format()) ->
71 mod_roster:roster() | does_not_exist.
72 get_roster_entry(_HostType, LUser, LServer, LJID, TransactionState, _Format) ->
73
:-(
LowerJID = jid:to_lower(LJID),
74
:-(
case read({roster, {{LUser, LServer}, LowerJID}}, TransactionState) of
75 [] ->
76
:-(
does_not_exist;
77 [I] ->
78
:-(
I#roster{jid = LJID, xs = []}
79 end.
80
81 -spec get_subscription_lists(mongoose_acc:t(), jid:luser(), jid:lserver()) -> [mod_roster:roster()].
82 get_subscription_lists(_, LUser, LServer) ->
83 1 US = {LUser, LServer},
84 1 case catch mnesia:dirty_index_read(roster, US, #roster.us) of
85 1 Items when is_list(Items) -> Items;
86
:-(
_ -> []
87 end.
88
89 -spec roster_subscribe_t(mongooseim:host_type(), mod_roster:roster()) -> ok.
90 roster_subscribe_t(_HostType, Item) ->
91
:-(
mnesia:write(Item).
92
93 -spec update_roster_t(mongooseim:host_type(), mod_roster:roster()) -> ok.
94 update_roster_t(_HostType, Item) ->
95
:-(
mnesia:write(Item).
96
97 -spec del_roster_t(mongooseim:host_type(), jid:luser(), jid:lserver(), mod_roster:contact()) -> ok.
98 del_roster_t(_HostType, LUser, LServer, LJID) ->
99
:-(
mnesia:delete({roster, {{LUser, LServer}, LJID}}).
100
101 -spec remove_user_t(mongooseim:host_type(), jid:luser(), jid:lserver()) -> ok.
102 remove_user_t(_HostType, LUser, LServer) ->
103
:-(
US = {LUser, LServer},
104
:-(
lists:foreach(fun (R) -> mnesia:delete_object(R) end,
105 mnesia:index_read(roster, US, #roster.us)),
106
:-(
ok.
107
108 %% Helpers
109
110
:-(
write(Record, in_transaction) -> mnesia:write(Record);
111
:-(
write(Record, no_transaction) -> mnesia:dirty_write(Record).
112
113
:-(
read(TabKey, in_transaction) -> mnesia:read(TabKey);
114
:-(
read(TabKey, no_transaction) -> mnesia:dirty_read(TabKey).
Line Hits Source