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 |
93 |
update_tables(), |
16 |
|
%% add distributed service_component routes |
17 |
93 |
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 |
93 |
case catch mnesia:table_info(external_componenst, local_content) of |
26 |
|
true -> |
27 |
:-( |
mnesia:delete_table(external_component); |
28 |
|
_ -> |
29 |
93 |
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 |
:-( |
F = fun() -> |
40 |
:-( |
lists:foreach(fun mnesia:write/1, Components) |
41 |
|
end, |
42 |
:-( |
case mnesia:transaction(F) of |
43 |
:-( |
{atomic, ok} -> ok; |
44 |
:-( |
{aborted, Reason} -> error({mnesia_aborted_write, Reason}) |
45 |
|
end. |
46 |
|
|
47 |
|
unregister_components(Components) -> |
48 |
:-( |
F = fun() -> |
49 |
:-( |
lists:foreach(fun do_unregister_component/1, Components) |
50 |
|
end, |
51 |
:-( |
{atomic, ok} = mnesia:transaction(F), |
52 |
:-( |
ok. |
53 |
|
|
54 |
|
do_unregister_component(Component) -> |
55 |
:-( |
ok = mnesia:delete_object(external_component, Component, write). |
56 |
|
|
57 |
|
lookup_component(Domain) -> |
58 |
178 |
mnesia:dirty_read(external_component, Domain). |
59 |
|
|
60 |
|
lookup_component(Domain, Node) -> |
61 |
197 |
mnesia:dirty_match_object(external_component, |
62 |
|
#external_component{domain = Domain, node = Node, _ = '_'}). |
63 |
|
|
64 |
|
get_all_components(all) -> |
65 |
41 |
mnesia:dirty_all_keys(external_component); |
66 |
|
get_all_components(only_public) -> |
67 |
25 |
MatchNonHidden = {#external_component{ domain = '$1', is_hidden = false, _ = '_' }, [], ['$1']}, |
68 |
25 |
mnesia:dirty_select(external_component, [MatchNonHidden]). |