From 75d2cbcb140989dca037a7d5bb7fcba9bfd96ae3 Mon Sep 17 00:00:00 2001 From: Christophe Romain Date: Fri, 18 Mar 2011 17:36:08 +0100 Subject: [PATCH] add nodeidx as index on pubsub_state and pubsub_item --- src/mod_pubsub/node_hometree.erl | 10 ++++++---- src/mod_pubsub/pubsub.hrl | 2 ++ src/mod_pubsub/pubsub_debug.erl | 8 ++++---- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/mod_pubsub/node_hometree.erl b/src/mod_pubsub/node_hometree.erl index e8e926e54..989868a42 100644 --- a/src/mod_pubsub/node_hometree.erl +++ b/src/mod_pubsub/node_hometree.erl @@ -97,9 +97,11 @@ init(_Host, _ServerHost, _Options) -> pubsub_subscription:init(), mnesia:create_table(pubsub_state, [{disc_copies, [node()]}, + {index, [nodeidx]}, {attributes, record_info(fields, pubsub_state)}]), mnesia:create_table(pubsub_item, [{disc_only_copies, [node()]}, + {index, [nodeidx]}, {attributes, record_info(fields, pubsub_item)}]), ItemsFields = record_info(fields, pubsub_item), case mnesia:table_info(pubsub_item, attributes) of @@ -224,7 +226,7 @@ create_node_permission(Host, ServerHost, NodeId, _ParentNodeId, Owner, Access) - %% @doc

create_node(NodeIdx, 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}}. %% @spec (Nodes) -> {result, {default, broadcast, Reply}} @@ -507,6 +509,7 @@ publish_item(NodeIdx, Publisher, PublishModel, MaxItems, ItemId, Payload) -> payload = Payload}; _ -> #pubsub_item{itemid = {ItemId, NodeIdx}, + nodeidx = NodeIdx, creation = {Now, GenKey}, modification = PubId, payload = Payload} @@ -860,8 +863,7 @@ get_nodes_helper(NodeTree, %% ```get_states(NodeIdx) -> %% node_default:get_states(NodeIdx).'''

get_states(NodeIdx) -> - States = case catch mnesia:match_object( - #pubsub_state{stateid = {'_', NodeIdx}, _ = '_'}) of + States = case catch mnesia:index_read(pubsub_state, NodeIdx, #pubsub_state.nodeidx) of List when is_list(List) -> List; _ -> [] end, @@ -911,7 +913,7 @@ del_state(NodeIdx, JID) -> %% ```get_items(NodeIdx, From) -> %% node_default: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))}. get_items(NodeIdx, JID, AccessModel, PresenceSubscription, RosterGroup, _SubId) -> diff --git a/src/mod_pubsub/pubsub.hrl b/src/mod_pubsub/pubsub.hrl index 8cc20c49e..88d2529f0 100644 --- a/src/mod_pubsub/pubsub.hrl +++ b/src/mod_pubsub/pubsub.hrl @@ -146,6 +146,7 @@ -record(pubsub_state, { stateid, + nodeidx, items = [], affiliation = 'none', subscriptions = [] @@ -161,6 +162,7 @@ -record(pubsub_item, { itemid, + nodeidx, creation = {'unknown','unknown'}, modification = {'unknown','unknown'}, payload = [] diff --git a/src/mod_pubsub/pubsub_debug.erl b/src/mod_pubsub/pubsub_debug.erl index 97ca8275f..397adf857 100644 --- a/src/mod_pubsub/pubsub_debug.erl +++ b/src/mod_pubsub/pubsub_debug.erl @@ -23,7 +23,7 @@ state(JID, NodeId) -> [S] -> S; _ -> undefined 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). stateids(NodeId) -> [stateid(S) || S <- states(NodeId)]. states_by_jid(JID) -> mnesia:dirty_match_object(#pubsub_state{stateid={JID, '_'}, _='_'}). @@ -33,7 +33,7 @@ item(ItemId, NodeId) -> [I] -> I; _ -> undefined 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). itemids(NodeId) -> [itemid(I) || I <- items(NodeId)]. 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]. 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 -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) -> - 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) -> Now = calendar:universal_time(), Oldest = calendar:seconds_to_daystime(Seconds),