25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-26 17:38:45 +01:00

add nodeidx as index on pubsub_state and pubsub_item

This commit is contained in:
Christophe Romain 2011-03-18 17:36:08 +01:00
parent f9fa168d84
commit 75d2cbcb14
3 changed files with 12 additions and 8 deletions

View File

@ -97,9 +97,11 @@ init(_Host, _ServerHost, _Options) ->
pubsub_subscription:init(), pubsub_subscription:init(),
mnesia:create_table(pubsub_state, mnesia:create_table(pubsub_state,
[{disc_copies, [node()]}, [{disc_copies, [node()]},
{index, [nodeidx]},
{attributes, record_info(fields, pubsub_state)}]), {attributes, record_info(fields, pubsub_state)}]),
mnesia:create_table(pubsub_item, mnesia:create_table(pubsub_item,
[{disc_only_copies, [node()]}, [{disc_only_copies, [node()]},
{index, [nodeidx]},
{attributes, record_info(fields, pubsub_item)}]), {attributes, record_info(fields, pubsub_item)}]),
ItemsFields = record_info(fields, pubsub_item), ItemsFields = record_info(fields, pubsub_item),
case mnesia:table_info(pubsub_item, attributes) of case mnesia:table_info(pubsub_item, attributes) of
@ -224,7 +226,7 @@ create_node_permission(Host, ServerHost, NodeId, _ParentNodeId, Owner, Access) -
%% @doc <p></p> %% @doc <p></p>
create_node(NodeIdx, Owner) -> create_node(NodeIdx, Owner) ->
OwnerKey = jlib:jid_tolower(jlib:jid_remove_resource(Owner)), OwnerKey = jlib:jid_tolower(jlib:jid_remove_resource(Owner)),
set_state(#pubsub_state{stateid = {OwnerKey, NodeIdx}, affiliation = owner}), set_state(#pubsub_state{stateid = {OwnerKey, NodeIdx}, nodeidx = NodeIdx, affiliation = owner}),
{result, {default, broadcast}}. {result, {default, broadcast}}.
%% @spec (Nodes) -> {result, {default, broadcast, Reply}} %% @spec (Nodes) -> {result, {default, broadcast, Reply}}
@ -507,6 +509,7 @@ publish_item(NodeIdx, Publisher, PublishModel, MaxItems, ItemId, Payload) ->
payload = Payload}; payload = Payload};
_ -> _ ->
#pubsub_item{itemid = {ItemId, NodeIdx}, #pubsub_item{itemid = {ItemId, NodeIdx},
nodeidx = NodeIdx,
creation = {Now, GenKey}, creation = {Now, GenKey},
modification = PubId, modification = PubId,
payload = Payload} payload = Payload}
@ -860,8 +863,7 @@ get_nodes_helper(NodeTree,
%% ```get_states(NodeIdx) -> %% ```get_states(NodeIdx) ->
%% node_default:get_states(NodeIdx).'''</p> %% node_default:get_states(NodeIdx).'''</p>
get_states(NodeIdx) -> get_states(NodeIdx) ->
States = case catch mnesia:match_object( States = case catch mnesia:index_read(pubsub_state, NodeIdx, #pubsub_state.nodeidx) of
#pubsub_state{stateid = {'_', NodeIdx}, _ = '_'}) of
List when is_list(List) -> List; List when is_list(List) -> List;
_ -> [] _ -> []
end, end,
@ -911,7 +913,7 @@ del_state(NodeIdx, JID) ->
%% ```get_items(NodeIdx, From) -> %% ```get_items(NodeIdx, From) ->
%% node_default:get_items(NodeIdx, From).'''</p> %% node_default:get_items(NodeIdx, From).'''</p>
get_items(NodeIdx, _From) -> get_items(NodeIdx, _From) ->
Items = mnesia:match_object(#pubsub_item{itemid = {'_', NodeIdx}, _ = '_'}), Items = mnesia:index_read(pubsub_item, NodeIdx, #pubsub_item.nodeidx),
{result, lists:reverse(lists:keysort(#pubsub_item.modification, Items))}. {result, lists:reverse(lists:keysort(#pubsub_item.modification, Items))}.
get_items(NodeIdx, JID, AccessModel, PresenceSubscription, RosterGroup, _SubId) -> get_items(NodeIdx, JID, AccessModel, PresenceSubscription, RosterGroup, _SubId) ->

View File

@ -146,6 +146,7 @@
-record(pubsub_state, -record(pubsub_state,
{ {
stateid, stateid,
nodeidx,
items = [], items = [],
affiliation = 'none', affiliation = 'none',
subscriptions = [] subscriptions = []
@ -161,6 +162,7 @@
-record(pubsub_item, -record(pubsub_item,
{ {
itemid, itemid,
nodeidx,
creation = {'unknown','unknown'}, creation = {'unknown','unknown'},
modification = {'unknown','unknown'}, modification = {'unknown','unknown'},
payload = [] payload = []

View File

@ -23,7 +23,7 @@ state(JID, NodeId) ->
[S] -> S; [S] -> S;
_ -> undefined _ -> undefined
end. end.
states(NodeId) -> mnesia:dirty_match_object(#pubsub_state{stateid={'_', NodeId}, _='_'}). states(NodeId) -> mnesia:dirty_index_read(pubsub_state, NodeId, #pubsub_state.nodeidx).
stateid(S) -> element(1, S#pubsub_state.stateid). stateid(S) -> element(1, S#pubsub_state.stateid).
stateids(NodeId) -> [stateid(S) || S <- states(NodeId)]. stateids(NodeId) -> [stateid(S) || S <- states(NodeId)].
states_by_jid(JID) -> mnesia:dirty_match_object(#pubsub_state{stateid={JID, '_'}, _='_'}). states_by_jid(JID) -> mnesia:dirty_match_object(#pubsub_state{stateid={JID, '_'}, _='_'}).
@ -33,7 +33,7 @@ item(ItemId, NodeId) ->
[I] -> I; [I] -> I;
_ -> undefined _ -> undefined
end. end.
items(NodeId) -> mnesia:dirty_match_object(#pubsub_item{itemid={'_', NodeId}, _='_'}). items(NodeId) -> mnesia:dirty_index_read(pubsub_item, NodeIdi, #pubsub_item.nodeidx).
itemid(I) -> element(1, I#pubsub_item.itemid). itemid(I) -> element(1, I#pubsub_item.itemid).
itemids(NodeId) -> [itemid(I) || I <- items(NodeId)]. itemids(NodeId) -> [itemid(I) || I <- items(NodeId)].
items_by_id(ItemId) -> mnesia:dirty_match_object(#pubsub_item{itemid={ItemId, '_'}, _='_'}). items_by_id(ItemId) -> mnesia:dirty_match_object(#pubsub_item{itemid={ItemId, '_'}, _='_'}).
@ -41,10 +41,10 @@ items_by_id(ItemId) -> mnesia:dirty_match_object(#pubsub_item{itemid={ItemId, '_
affiliated(NodeId) -> [stateid(S) || S <- states(NodeId), S#pubsub_state.affiliation=/=none]. affiliated(NodeId) -> [stateid(S) || S <- states(NodeId), S#pubsub_state.affiliation=/=none].
subscribed(NodeId) -> [stateid(S) || S <- states(NodeId), S#pubsub_state.subscriptions=/=[]]. subscribed(NodeId) -> [stateid(S) || S <- states(NodeId), S#pubsub_state.subscriptions=/=[]].
%subscribed(NodeId) -> [stateid(S) || S <- states(NodeId), S#pubsub_state.subscription=/=none]. %% old record %subscribed(NodeId) -> [stateid(S) || S <- states(NodeId), S#pubsub_state.subscription=/=none]. %% old record
owners(NodeId) -> [stateid(S) || S <- mnesia:dirty_match_object(#pubsub_state{stateid={'_', NodeId}, affiliation=owner, _='_'})]. owners(NodeId) -> [stateid(S) || S <- states(NodeId), S#pubsub_state.affiliation==owner].
orphan_items(NodeId) -> orphan_items(NodeId) ->
itemids(NodeId) -- lists:foldl(fun(S, A) -> A++S#pubsub_state.items end, [], mnesia:dirty_match_object(#pubsub_state{stateid={'_', NodeId}, _='_'})). itemids(NodeId) -- lists:foldl(fun(S, A) -> A++S#pubsub_state.items end, [], states(NodeId)).
newer_items(NodeId, Seconds) -> newer_items(NodeId, Seconds) ->
Now = calendar:universal_time(), Now = calendar:universal_time(),
Oldest = calendar:seconds_to_daystime(Seconds), Oldest = calendar:seconds_to_daystime(Seconds),