./ct_report/coverage/mod_private_mnesia.COVER.html

1 %%%----------------------------------------------------------------------
2 %%% Old copyright notice from mod_private.erl
3 %%%
4 %%% Author : Alexey Shchepin <alexey@process-one.net>
5 %%% Created : 16 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
6 %%%
7 %%%
8 %%% ejabberd, Copyright (C) 2002-2011 ProcessOne
9 %%%
10 %%% This program is free software; you can redistribute it and/or
11 %%% modify it under the terms of the GNU General Public License as
12 %%% published by the Free Software Foundation; either version 2 of the
13 %%% License, or (at your option) any later version.
14 %%%
15 %%% This program is distributed in the hope that it will be useful,
16 %%% but WITHOUT ANY WARRANTY; without even the implied warranty of
17 %%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 %%% General Public License for more details.
19 %%%
20 %%% You should have received a copy of the GNU General Public License
21 %%% along with this program; if not, write to the Free Software
22 %%% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 %%%
24 %%%----------------------------------------------------------------------
25
26 %%% NS is namespace or key.
27 %%% XML is #xmlel{} or value.
28 -module(mod_private_mnesia).
29 -author('alexey@process-one.net').
30 -author('arcusfelis@gmail.com').
31 -behaviour(mod_private_backend).
32
33 -export([init/2,
34 multi_set_data/4,
35 multi_get_data/4,
36 get_all_nss/3,
37 remove_user/3,
38 remove_domain/2]).
39
40 -include("mongoose.hrl").
41 -include("jlib.hrl").
42
43 -record(private_storage, {usns, xml}).
44
45 init(_HostType, _Opts) ->
46 1 mnesia:create_table(private_storage,
47 [{disc_only_copies, [node()]},
48 {attributes, record_info(fields, private_storage)}]),
49 1 mnesia:add_table_copy(private_storage, node(), disc_only_copies),
50 1 ok.
51
52 multi_set_data(_HostType, LUser, LServer, NS2XML) ->
53 2 F = fun() -> multi_set_data_t(LUser, LServer, NS2XML) end,
54 2 case mnesia:transaction(F) of
55 2 {atomic, ok} -> ok;
56
:-(
{aborted, Reason} -> {aborted, Reason}
57 end.
58
59 multi_set_data_t(LUser, LServer, NS2XML) ->
60 2 [set_data_t(LUser, LServer, NS, XML) || {NS, XML} <- NS2XML],
61 2 ok.
62
63 set_data_t(LUser, LServer, NS, XML) ->
64 2 mnesia:write(#private_storage{usns = {LUser, LServer, NS}, xml = XML}).
65
66 multi_get_data(_HostType, LUser, LServer, NS2Def) ->
67 1 [get_data(LUser, LServer, NS, Default) || {NS, Default} <- NS2Def].
68
69 get_all_nss(_HostType, LUser, LServer) ->
70
:-(
F = fun() ->
71
:-(
select_namespaces_t(LUser, LServer)
72 end,
73
:-(
{atomic, NSs} = mnesia:transaction(F),
74
:-(
NSs.
75
76 %% @doc Return stored value or default.
77 get_data(LUser, LServer, NS, Default) ->
78 1 case mnesia:dirty_read(private_storage, {LUser, LServer, NS}) of
79 1 [#private_storage{xml=XML}] -> XML;
80
:-(
[] -> Default
81 end.
82
83 remove_user(_HostType, LUser, LServer) ->
84
:-(
F = fun() ->
85
:-(
NSs = select_namespaces_t(LUser, LServer),
86
:-(
[delete_record_t(LUser, LServer, NS) || NS <- NSs]
87 end,
88
:-(
mnesia:transaction(F).
89
90 %% There is no optimized way to remove a domain.
91 %% We expect, that domain removal process would call remove_user instead
92 %% for each user.
93 remove_domain(_HostType, _LServer) ->
94
:-(
ok.
95
96 select_namespaces_t(LUser, LServer) ->
97
:-(
Result = mnesia:select(
98 private_storage,
99 [{#private_storage{usns={LUser, LServer, '$1'}, _ = '_'},
100 [],
101 ['$$']}]),
102
:-(
[NS || [NS] <- Result].
103
104 delete_record_t(LUser, LServer, NS) ->
105
:-(
mnesia:delete({private_storage, {LUser, LServer, NS}}).
Line Hits Source