25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-20 16:15:59 +01:00

Don't store xdata() in Mnesia table

This commit is contained in:
Evgeniy Khramtsov 2017-10-27 11:46:37 +03:00
parent 484e5de072
commit 24df1262bb
3 changed files with 39 additions and 20 deletions

View File

@ -21,4 +21,4 @@
timestamp = p1_time_compat:timestamp() :: erlang:timestamp(), timestamp = p1_time_compat:timestamp() :: erlang:timestamp(),
service = {<<"">>, <<"">>, <<"">>} :: ljid(), service = {<<"">>, <<"">>, <<"">>} :: ljid(),
node = <<"">> :: binary(), node = <<"">> :: binary(),
xdata = #xdata{} :: xdata()}). xml = undefined :: xmlel()}).

View File

@ -31,7 +31,7 @@
%% API %% API
-export([init/2, store_session/6, lookup_session/4, lookup_session/3, -export([init/2, store_session/6, lookup_session/4, lookup_session/3,
lookup_sessions/3, lookup_sessions/2, lookup_sessions/1, lookup_sessions/3, lookup_sessions/2, lookup_sessions/1,
delete_session/3, delete_old_sessions/2]). delete_session/3, delete_old_sessions/2, transform/1]).
-include_lib("stdlib/include/ms_transform.hrl"). -include_lib("stdlib/include/ms_transform.hrl").
-include("logger.hrl"). -include("logger.hrl").
@ -61,7 +61,7 @@ store_session(LUser, LServer, TS, PushJID, Node, XData) ->
timestamp = TS, timestamp = TS,
service = PushLJID, service = PushLJID,
node = Node, node = Node,
xdata = XData}) xml = encode_xdata(XData)})
end, end,
case mnesia:transaction(F) of case mnesia:transaction(F) of
{atomic, ok} -> {atomic, ok} ->
@ -83,8 +83,8 @@ lookup_session(LUser, LServer, PushJID, Node) ->
Rec Rec
end), end),
case mnesia:dirty_select(push_session, MatchSpec) of case mnesia:dirty_select(push_session, MatchSpec) of
[#push_session{timestamp = TS, xdata = XData}] -> [#push_session{timestamp = TS, xml = El}] ->
{ok, {TS, PushLJID, Node, XData}}; {ok, {TS, PushLJID, Node, decode_xdata(El)}};
[] -> [] ->
?DEBUG("No push session found for ~s@~s (~p, ~s)", ?DEBUG("No push session found for ~s@~s (~p, ~s)",
[LUser, LServer, PushJID, Node]), [LUser, LServer, PushJID, Node]),
@ -100,8 +100,8 @@ lookup_session(LUser, LServer, TS) ->
Rec Rec
end), end),
case mnesia:dirty_select(push_session, MatchSpec) of case mnesia:dirty_select(push_session, MatchSpec) of
[#push_session{service = PushLJID, node = Node, xdata = XData}] -> [#push_session{service = PushLJID, node = Node, xml = El}] ->
{ok, {TS, PushLJID, Node, XData}}; {ok, {TS, PushLJID, Node, decode_xdata(El)}};
[] -> [] ->
?DEBUG("No push session found for ~s@~s (~p)", ?DEBUG("No push session found for ~s@~s (~p)",
[LUser, LServer, TS]), [LUser, LServer, TS]),
@ -113,22 +113,18 @@ lookup_sessions(LUser, LServer, PushJID) ->
MatchSpec = ets:fun2ms( MatchSpec = ets:fun2ms(
fun(#push_session{us = {U, S}, service = P, fun(#push_session{us = {U, S}, service = P,
node = Node, timestamp = TS, node = Node, timestamp = TS,
xdata = XData} = Rec) xml = El} = Rec)
when U == LUser, when U == LUser,
S == LServer, S == LServer,
P == PushLJID -> P == PushLJID ->
{TS, PushLJID, Node, XData} Rec
end), end),
{ok, mnesia:dirty_select(push_session, MatchSpec)}. Records = mnesia:dirty_select(push_session, MatchSpec),
{ok, records_to_sessions(Records)}.
lookup_sessions(LUser, LServer) -> lookup_sessions(LUser, LServer) ->
Records = mnesia:dirty_read(push_session, {LUser, LServer}), Records = mnesia:dirty_read(push_session, {LUser, LServer}),
Clients = [{TS, PushLJID, Node, XData} {ok, records_to_sessions(Records)}.
|| #push_session{timestamp = TS,
service = PushLJID,
node = Node,
xdata = XData} <- Records],
{ok, Clients}.
lookup_sessions(LServer) -> lookup_sessions(LServer) ->
MatchSpec = ets:fun2ms( MatchSpec = ets:fun2ms(
@ -136,11 +132,12 @@ lookup_sessions(LServer) ->
timestamp = TS, timestamp = TS,
service = PushLJID, service = PushLJID,
node = Node, node = Node,
xdata = XData}) xml = El})
when S == LServer -> when S == LServer ->
{TS, PushLJID, Node, XData} {TS, PushLJID, Node, El}
end), end),
{ok, mnesia:dirty_select(push_session, MatchSpec)}. Records = mnesia:dirty_select(push_session, MatchSpec),
{ok, records_to_sessions(Records)}.
delete_session(LUser, LServer, TS) -> delete_session(LUser, LServer, TS) ->
MatchSpec = ets:fun2ms( MatchSpec = ets:fun2ms(
@ -180,6 +177,11 @@ delete_old_sessions(_LServer, Time) ->
{error, db_failure} {error, db_failure}
end. end.
transform({push_session, US, TS, Service, Node, XData}) ->
?INFO_MSG("Transforming push_session Mnesia table", []),
#push_session{us = US, timestamp = TS, service = Service,
node = Node, xml = encode_xdata(XData)}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Internal functions. %% Internal functions.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -198,3 +200,20 @@ enforce_max_sessions({U, S} = US, Max) ->
true -> true ->
ok ok
end. end.
decode_xdata(undefined) ->
undefined;
decode_xdata(El) ->
xmpp:decode(El).
encode_xdata(undefined) ->
undefined;
encode_xdata(XData) ->
xmpp:encode(XData).
records_to_sessions(Records) ->
[{TS, PushLJID, Node, decode_xdata(El)}
|| #push_session{timestamp = TS,
service = PushLJID,
node = Node,
xml = El} <- Records].

View File

@ -177,7 +177,7 @@ export(_Server) ->
timestamp = NowTS, timestamp = NowTS,
service = PushLJID, service = PushLJID,
node = Node, node = Node,
xdata = XData}) xml = XData})
when LServer == Host -> when LServer == Host ->
TS = misc:now_to_usec(NowTS), TS = misc:now_to_usec(NowTS),
Service = jid:encode(PushLJID), Service = jid:encode(PushLJID),