Skip to content

Commit

Permalink
move tree building function to pubsub backend module
Browse files Browse the repository at this point in the history
  • Loading branch information
michalwski committed Nov 27, 2018
1 parent de09141 commit 437e6ef
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 46 deletions.
6 changes: 5 additions & 1 deletion src/pubsub/mod_pubsub.erl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,11 @@ init_backend(Opts) ->
TrackedDBFuns = [set_state, del_state, get_state, get_states,
get_states_by_lus, get_states_by_bare,
get_states_by_full, get_own_nodes_states,
get_items, get_item, set_item, del_item, del_items],
get_items, get_item, set_item, del_item, del_items,
set_node, find_node_by_id, find_nodes_by_key,
find_node, delete_node, get_subnodes, get_parentnodes,
get_subnodes_tree, get_parentnodes_tree
],
gen_mod:start_backend_module(mod_pubsub_db, Opts, TrackedDBFuns),
mod_pubsub_db_backend:start(),

Expand Down
11 changes: 6 additions & 5 deletions src/pubsub/mod_pubsub_db.erl
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@
-callback find_nodes_by_key(Key :: mod_pubsub:hostPubsub() | jid:ljid()) ->
[mod_pubsub:pubsubNode()].

-callback find_nodes_by_id_and_pred(Key :: mod_pubsub:hostPubsub() | jid:ljid(),
Nodes :: [mod_pubsub:nodeId()],
Pred :: fun((mod_pubsub:nodeId(), mod_pubsub:pubsubNode()) -> boolean())) ->
[mod_pubsub:pubsubNode()].

-callback delete_node(Key :: mod_pubsub:hostPubsub() | jid:ljid(), Node :: mod_pubsub:nodeId()) -> ok.

-callback get_subnodes(Key :: mod_pubsub:hostPubsub() | jid:ljid(), Node :: mod_pubsub:nodeId() | <<>>) ->
Expand All @@ -94,6 +89,12 @@
-callback get_parentnodes(Key :: mod_pubsub:hostPubsub() | jid:ljid(), Node :: mod_pubsub:nodeId()) ->
[mod_pubsub:pubsubNode()].

-callback get_parentnodes_tree(Key :: mod_pubsub:hostPubsub() | jid:ljid(), Node :: mod_pubsub:nodeId()) ->
[{Depth::non_neg_integer(), Nodes::[mod_pubsub:pubsubNode(), ...]}].

-callback get_subnodes_tree(Key :: mod_pubsub:hostPubsub() | jid:ljid(), Node :: mod_pubsub:nodeId()) ->
[{Depth::non_neg_integer(), Nodes::[mod_pubsub:pubsubNode(), ...]}].

%% ----------------------- Affiliations ------------------------

-callback set_affiliation(Nidx :: mod_pubsub:nodeIdx(),
Expand Down
42 changes: 41 additions & 1 deletion src/pubsub/mod_pubsub_db_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
find_node/2,
delete_node/2,
get_subnodes/2,
get_parentnodes/2
get_parentnodes/2,
get_parentnodes_tree/2,
get_subnodes_tree/2
]).
% Affiliations
-export([
Expand Down Expand Up @@ -270,6 +272,44 @@ get_parentnodes(Key, Node) ->
Parent <- Parents, Key == NHost, Parent == NNode]),
qlc:e(Q)
end.

-spec get_parentnodes_tree(Key :: mod_pubsub:hostPubsub() | jid:ljid(), Node :: mod_pubsub:nodeId()) ->
[{Depth::non_neg_integer(), Nodes::[mod_pubsub:pubsubNode(), ...]}].
get_parentnodes_tree(Key, Node) ->
Pred = fun (NID, #pubsub_node{nodeid = {_, NNode}}) ->
NID == NNode
end,
Tr = fun (#pubsub_node{parents = Parents}) -> Parents
end,
traversal_helper(Pred, Tr, Key, [Node]).

-spec get_subnodes_tree(Key :: mod_pubsub:hostPubsub() | jid:ljid(), Node :: mod_pubsub:nodeId()) ->
[{Depth::non_neg_integer(), Nodes::[mod_pubsub:pubsubNode(), ...]}].
get_subnodes_tree(Key, Node) ->
Pred = fun (NID, #pubsub_node{parents = Parents}) ->
lists:member(NID, Parents)
end,
Tr = fun (#pubsub_node{nodeid = {_, N}}) -> [N] end,
traversal_helper(Pred, Tr, 1, Key, [Node],
[{0, [find_node(Key, Node)]}]).


-spec traversal_helper(
Pred :: fun(),
Transform :: fun(),
Host :: mod_pubsub:hostPubsub(),
Nodes :: [mod_pubsub:nodeId(), ...])
-> [{Depth::non_neg_integer(), Nodes::[mod_pubsub:pubsubNode(), ...]}].
traversal_helper(Pred, Tr, Host, Nodes) ->
traversal_helper(Pred, Tr, 0, Host, Nodes, []).

traversal_helper(_Pred, _Tr, _Depth, _Host, [], Acc) ->
Acc;
traversal_helper(Pred, Tr, Depth, Host, Nodes, Acc) ->
NodeRecs = find_nodes_by_id_and_pred(Host, Nodes, Pred),
IDs = lists:flatmap(Tr, NodeRecs),
traversal_helper(Pred, Tr, Depth + 1, Host, IDs, [{Depth, NodeRecs} | Acc]).

%% ------------------------ Affiliations ------------------------

-spec set_affiliation(Nidx :: mod_pubsub:nodeIdx(),
Expand Down
23 changes: 14 additions & 9 deletions src/pubsub/mod_pubsub_db_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
set_node/1,
find_node_by_id/1,
find_nodes_by_key/1,
find_nodes_by_id_and_pred/3,
find_node/2,
delete_node/2,
get_subnodes/2,
get_parentnodes/2
get_parentnodes/2,
get_parentnodes_tree/2,
get_subnodes_tree/2
]).
% Affiliations
-export([
Expand Down Expand Up @@ -241,12 +242,6 @@ find_node(Key, Node) ->
find_nodes_by_key(Key) ->
mod_pubsub_db_mnesia:find_nodes_by_key(Key).

-spec find_nodes_by_id_and_pred(Key :: mod_pubsub:hostPubsub() | jid:ljid(),
Nodes :: [mod_pubsub:nodeId()],
Pred :: fun((mod_pubsub:nodeId(), mod_pubsub:pubsubNode()) -> boolean())) ->
[mod_pubsub:pubsubNode()].
find_nodes_by_id_and_pred(Key, Nodes, Pred) ->
mod_pubsub_db_mnesia:find_nodes_by_id_and_pred(Key, Nodes, Pred).

-spec delete_node(Key :: mod_pubsub:hostPubsub() | jid:ljid(), Node :: mod_pubsub:nodeId()) -> ok.
delete_node(Key, Node) ->
Expand All @@ -260,7 +255,17 @@ get_subnodes(Key, Node) ->
-spec get_parentnodes(Key :: mod_pubsub:hostPubsub() | jid:ljid(), Node :: mod_pubsub:nodeId()) ->
[mod_pubsub:pubsubNode()].
get_parentnodes(Key, Node) ->
mod_pubsbu_db_mnesia:get_parentnodes(Key, Node).
mod_pubsub_db_mnesia:get_parentnodes(Key, Node).

-spec get_parentnodes_tree(Key :: mod_pubsub:hostPubsub() | jid:ljid(), Node :: mod_pubsub:nodeId()) ->
[{Depth::non_neg_integer(), Nodes::[mod_pubsub:pubsubNode(), ...]}].
get_parentnodes_tree(Key, Node) ->
mod_pubsub_db_mnesia:get_parentnodes_tree(Key, Node).

-spec get_subnodes_tree(Key :: mod_pubsub:hostPubsub() | jid:ljid(), Node :: mod_pubsub:nodeId()) ->
[{Depth::non_neg_integer(), Nodes::[mod_pubsub:pubsubNode(), ...]}].
get_subnodes_tree(Key, Node) ->
mod_pubsub_db_mnesia:get_subnodes_tree(Key, Node).
% ------------------- Affiliations --------------------------------

-spec set_affiliation(Nidx :: mod_pubsub:nodeIdx(),
Expand Down
34 changes: 4 additions & 30 deletions src/pubsub/nodetree_dag.erl
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,8 @@ get_parentnodes(Key, Node, _From) ->
Result
end.

get_parentnodes_tree(Host, Node, _From) ->
Pred = fun (NID, #pubsub_node{nodeid = {_, NNode}}) ->
NID == NNode
end,
Tr = fun (#pubsub_node{parents = Parents}) -> Parents
end,
traversal_helper(Pred, Tr, Host, [Node]).
get_parentnodes_tree(Key, Node, _From) ->
mod_pubsub_db_backend:get_parentnodes_tree(Key, Node).

get_subnodes(Host, Node, _From) ->
get_subnodes(Host, Node).
Expand All @@ -132,13 +127,8 @@ get_subnodes(Host, Node) ->
_ -> mod_pubsub_db_backend:get_subnodes(Host, Node)
end.

get_subnodes_tree(Host, Node, From) ->
Pred = fun (NID, #pubsub_node{parents = Parents}) ->
lists:member(NID, Parents)
end,
Tr = fun (#pubsub_node{nodeid = {_, N}}) -> [N] end,
traversal_helper(Pred, Tr, 1, Host, [Node],
[{0, [get_node(Host, Node, From)]}]).
get_subnodes_tree(Host, Node, _From) ->
mod_pubsub_db_backend:get_subnodes_tree(Host, Node).

%%====================================================================
%% Internal functions
Expand All @@ -153,22 +143,6 @@ find_opt(Key, Default, Options) ->
_ -> Default
end.

-spec traversal_helper(
Pred :: fun(),
Transform :: fun(),
Host :: mod_pubsub:hostPubsub(),
Nodes :: [mod_pubsub:nodeId(), ...])
-> [{Depth::non_neg_integer(), Nodes::[mod_pubsub:pubsubNode(), ...]}].
traversal_helper(Pred, Tr, Host, Nodes) ->
traversal_helper(Pred, Tr, 0, Host, Nodes, []).

traversal_helper(_Pred, _Tr, _Depth, _Host, [], Acc) ->
Acc;
traversal_helper(Pred, Tr, Depth, Host, Nodes, Acc) ->
NodeRecs = mod_pubsub_db_backend:find_nodes_by_id_and_pred(Host, Nodes, Pred),
IDs = lists:flatmap(Tr, NodeRecs),
traversal_helper(Pred, Tr, Depth + 1, Host, IDs, [{Depth, NodeRecs} | Acc]).

remove_config_parent(Node, Options) ->
remove_config_parent(Node, Options, []).

Expand Down

0 comments on commit 437e6ef

Please sign in to comment.