1 |
|
-module(mod_caps_mnesia). |
2 |
|
-behaviour(mod_caps_backend). |
3 |
|
|
4 |
|
-export([init/2, |
5 |
|
read/2, |
6 |
|
write/3, |
7 |
|
delete_node/2]). |
8 |
|
|
9 |
|
-record(caps_features, |
10 |
|
{ |
11 |
|
node_pair = {<<>>, <<>>} :: mod_caps:node_pair(), |
12 |
|
features = [] :: mod_caps:maybe_pending_features() |
13 |
|
}). |
14 |
|
|
15 |
|
-spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok. |
16 |
|
init(_HostName, _Opts) -> |
17 |
2 |
case catch mnesia:table_info(caps_features, storage_type) of |
18 |
|
{'EXIT', _} -> |
19 |
1 |
ok; |
20 |
|
disc_only_copies -> |
21 |
1 |
ok; |
22 |
|
_ -> |
23 |
:-( |
mnesia:delete_table(caps_features) |
24 |
|
end, |
25 |
2 |
mongoose_mnesia:create_table(caps_features, |
26 |
|
[{disc_only_copies, [node()]}, |
27 |
|
{local_content, true}, |
28 |
|
{attributes, record_info(fields, caps_features)}]), |
29 |
2 |
ok. |
30 |
|
|
31 |
|
-spec read(mongooseim:host_type(), mod_caps:node_pair()) -> |
32 |
|
{ok, mod_caps:maybe_pending_features()} | error. |
33 |
|
read(_HostType, Node) -> |
34 |
:-( |
case mnesia:dirty_read({caps_features, Node}) of |
35 |
:-( |
[#caps_features{features = Features}] -> {ok, Features}; |
36 |
:-( |
_ -> error |
37 |
|
end. |
38 |
|
|
39 |
|
-spec write(mongooseim:host_type(), mod_caps:node_pair(), |
40 |
|
mod_caps:maybe_pending_features()) -> ok. |
41 |
|
write(_HostType, Node, Features) -> |
42 |
:-( |
mnesia:dirty_write(#caps_features{node_pair = Node, |
43 |
|
features = Features}), |
44 |
:-( |
ok. |
45 |
|
|
46 |
|
-spec delete_node(mongooseim:host_type(), mod_caps:node_pair()) -> ok. |
47 |
|
delete_node(_HostType, Node) -> |
48 |
:-( |
mnesia:dirty_delete(caps_features, Node), |
49 |
:-( |
ok. |