1 |
|
-module(mongoose_component_mnesia). |
2 |
|
-behaviour(mongoose_component_backend). |
3 |
|
|
4 |
|
-export([init/1, |
5 |
|
node_cleanup/1, |
6 |
|
register_components/1, |
7 |
|
unregister_components/1, |
8 |
|
lookup_component/1, |
9 |
|
lookup_component/2, |
10 |
|
get_all_components/1]). |
11 |
|
|
12 |
|
-include("external_component.hrl"). |
13 |
|
|
14 |
|
init(_) -> |
15 |
104 |
update_tables(), |
16 |
|
%% add distributed service_component routes |
17 |
104 |
mongoose_mnesia:create_table(external_component, |
18 |
|
[{type, bag}, |
19 |
|
{ram_copies, [node()]}, |
20 |
|
{attributes, |
21 |
|
record_info(fields, external_component)}]). |
22 |
|
|
23 |
|
update_tables() -> |
24 |
|
%% delete old schema |
25 |
104 |
case catch mnesia:table_info(external_componenst, local_content) of |
26 |
|
true -> |
27 |
:-( |
mnesia:delete_table(external_component); |
28 |
|
_ -> |
29 |
104 |
ok |
30 |
|
end. |
31 |
|
|
32 |
|
node_cleanup(Node) -> |
33 |
8 |
Entries = mnesia:dirty_match_object(external_component, |
34 |
|
#external_component{node = Node, _ = '_'}), |
35 |
8 |
[mnesia:dirty_delete_object(external_component, Entry) || Entry <- Entries], |
36 |
8 |
ok. |
37 |
|
|
38 |
|
register_components(Components) -> |
39 |
28 |
F = fun() -> |
40 |
28 |
lists:foreach(fun mnesia:write/1, Components) |
41 |
|
end, |
42 |
28 |
case mnesia:transaction(F) of |
43 |
28 |
{atomic, ok} -> ok; |
44 |
:-( |
{aborted, Reason} -> error({mnesia_aborted_write, Reason}) |
45 |
|
end. |
46 |
|
|
47 |
|
unregister_components(Components) -> |
48 |
28 |
F = fun() -> |
49 |
29 |
lists:foreach(fun do_unregister_component/1, Components) |
50 |
|
end, |
51 |
28 |
{atomic, ok} = mnesia:transaction(F), |
52 |
28 |
ok. |
53 |
|
|
54 |
|
do_unregister_component(Component) -> |
55 |
30 |
ok = mnesia:delete_object(external_component, Component, write). |
56 |
|
|
57 |
|
lookup_component(Domain) -> |
58 |
395 |
mnesia:dirty_read(external_component, Domain). |
59 |
|
|
60 |
|
lookup_component(Domain, Node) -> |
61 |
359 |
mnesia:dirty_match_object(external_component, |
62 |
|
#external_component{domain = Domain, node = Node, _ = '_'}). |
63 |
|
|
64 |
|
get_all_components(all) -> |
65 |
46 |
mnesia:dirty_all_keys(external_component); |
66 |
|
get_all_components(only_public) -> |
67 |
40 |
MatchNonHidden = {#external_component{ domain = '$1', is_hidden = false, _ = '_' }, [], ['$1']}, |
68 |
40 |
mnesia:dirty_select(external_component, [MatchNonHidden]). |