Skip to content

Commit

Permalink
Merge pull request #438 from esl/simplify-mam-v1
Browse files Browse the repository at this point in the history
Simplify mam v1
  • Loading branch information
ppikula committed Aug 17, 2015
2 parents 4c9c418 + 2fcb9ca commit abbdb03
Show file tree
Hide file tree
Showing 19 changed files with 46 additions and 1,441 deletions.
23 changes: 0 additions & 23 deletions apps/ejabberd/priv/mssql2012.sql
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,6 @@ CREATE TABLE [dbo].[mam_server_user](
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[mam_user] Script Date: 9/17/2014 6:20:03 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[mam_user](
[id] [bigint] IDENTITY(11,1) NOT NULL,
[user_name] [varchar](250) NOT NULL,
CONSTRAINT [PK_mam_user_id] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [mam_user$uc_mam_user_name] UNIQUE NONCLUSTERED
(
[user_name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
Expand Down
8 changes: 0 additions & 8 deletions apps/ejabberd/priv/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,6 @@ CREATE TABLE mam_config(
);
CREATE INDEX i_mam_config USING HASH ON mam_config(user_id, remote_jid);

CREATE TABLE mam_user(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_name varchar(250) CHARACTER SET binary NOT NULL,
PRIMARY KEY(id) USING HASH,
CONSTRAINT uc_mam_user_name UNIQUE (user_name)
);
CREATE INDEX i_mam_user_name USING HASH ON mam_user(user_name);

CREATE TABLE mam_server_user(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
server varchar(250) CHARACTER SET binary NOT NULL,
Expand Down
9 changes: 0 additions & 9 deletions apps/ejabberd/priv/pg.sql
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,6 @@ CREATE INDEX i_mam_config
ON mam_config
(user_id, remote_jid);

CREATE TABLE mam_user(
id SERIAL UNIQUE PRIMARY KEY,
user_name varchar(250) NOT NULL
);
CREATE UNIQUE INDEX i_mam_user_name
ON mam_user
USING BTREE
(user_name);

CREATE TABLE mam_server_user(
id SERIAL UNIQUE PRIMARY KEY,
server varchar(250) NOT NULL,
Expand Down
1 change: 1 addition & 0 deletions apps/ejabberd/src/jlib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
parse_xdata_submit/1,
timestamp_to_xml/4,
timestamp_to_mam_xml/4,
timestamp_to_iso/2,
now_to_utc_binary/1,
datetime_binary_to_timestamp/1,
decode_base64/1,
Expand Down
99 changes: 3 additions & 96 deletions apps/ejabberd/src/mod_mam.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
%%%
%%% <ul>
%%% <li>Preference manager ({@link mod_mam_muc_odbc_prefs});</li>
%%% <li>Writer ({@link mod_mam_muc_odbc_arch} or {@link mod_mam_muc_odbc_async_writer});</li>
%%% <li>Writer ({@link mod_mam_muc_odbc_arch} or {@link mod_mam_muc_odbc_async_pool_writer});</li>
%%% <li>Archive manager ({@link mod_mam_muc_odbc_arch});</li>
%%% <li>User's ID generator ({@link mod_mam_muc_user}).</li>
%%% </ul>
Expand Down Expand Up @@ -47,9 +47,8 @@
remove_user/2,
filter_packet/1]).

%% Utils
-export([create_dump_file/2,
restore_dump_file/3]).
%%private
-export([archive_message/8]).

%% ----------------------------------------------------------------------
%% Imports
Expand Down Expand Up @@ -167,98 +166,6 @@ archive_id(Server, User)
Host = server_host(ArcJID),
archive_id_int(Host, ArcJID).


%% ----------------------------------------------------------------------
%% Utils API

-spec new_iterator(ejabberd:jid()) -> iterator_fun().
new_iterator(ArcJID=#jid{}) ->
Now = mod_mam_utils:now_to_microseconds(now()),
Host = server_host(ArcJID),
ArcID = archive_id_int(Host, ArcJID),
new_iterator(Host, ArcID, ArcJID, undefined, undefined,
undefined, undefined, Now, undefined, 50).


-spec new_iterator(ejabberd:server(), ArcID :: archive_id(),
ArcJID :: ejabberd:jid(), RSM :: 'undefined', Borders :: 'undefined',
Start :: 'undefined', End :: 'undefined', Now :: non_neg_integer(),
WithJID :: 'undefined', PageSize :: 50) -> iterator_fun().
new_iterator(Host, ArcID, ArcJID, RSM, Borders, Start, End, Now,
WithJID, PageSize) ->
fun() ->
{ok, {TotalCount, Offset, MessageRows}} =
lookup_messages(Host, ArcID, ArcJID, RSM, Borders,
Start, End, Now,
WithJID, PageSize, true, PageSize, false),
Data = [exml:to_iolist(message_row_to_dump_xml(M))
|| M <- MessageRows],
Cont = case is_last_page(TotalCount, Offset, PageSize) of
false ->
fun() -> {error, eof} end;
true ->
new_iterator(
Host, ArcID, ArcJID, after_rsm(MessageRows), Borders,
Start, End, Now, WithJID, PageSize)
end,
{ok, {Data, Cont}}
end.


-spec after_rsm(MRows :: [any(),...]) -> jlib:rsm_in().
after_rsm(MessageRows) ->
{MessID,_SrcJID,_Packet} = lists:last(MessageRows),
#rsm_in{direction = aft, id = MessID}.


-spec is_last_page(Total :: non_neg_integer(), Off :: non_neg_integer(),
PageSize :: 50) -> boolean().
is_last_page(TotalCount, Offset, PageSize) ->
Offset - PageSize >= TotalCount.


-spec create_dump_file(ArcJID :: ejabberd:jid(), file:name())
-> 'ok' | {'error',atom()}.
create_dump_file(ArcJID, OutFileName) ->
mod_mam_dump:create_dump_file(new_iterator(ArcJID), OutFileName).


-spec restore_dump_file(ArcJID :: ejabberd:simple_jid() | ejabberd:jid(),
InFilename :: file:filename(), Opts :: [restore_option()]) -> {'error',_}.
restore_dump_file(ArcJID, InFileName, Opts) ->
try
restore_dump_file_unsave(ArcJID, InFileName, Opts)
catch Type:Reason ->
Trace = erlang:get_stacktrace(),
lager:error("Error ~p:~p occured while restoring ~p from file ~ts.~nTrace: ~p",
[Type, Reason, jlib:jid_to_binary(ArcJID), InFileName, Trace]),
{error, Reason}
end.


-spec restore_dump_file_unsave(ArcJID :: ejabberd:simple_jid() | ejabberd:jid(),
InFilename :: file:filename(), Opts :: [restore_option()]) -> {'error',_}.
restore_dump_file_unsave(ArcJID, InFileName, Opts) ->
Host = server_host(ArcJID),
ArcID = archive_id_int(Host, ArcJID),
WriterF = fun(MessID, FromJID, ToJID, MessElem) ->
case ArcJID of
FromJID ->
archive_message(Host, MessID, ArcID,
ArcJID, ToJID, FromJID,
outgoing, MessElem);
ToJID ->
archive_message(Host, MessID, ArcID,
ArcJID, FromJID, FromJID,
incoming, MessElem);
_ ->
{error, no_local_jid}
end
end,
mod_mam_dump:restore_dump_file(WriterF, InFileName, Opts).


%% ----------------------------------------------------------------------
%% gen_mod callbacks
%% Starting and stopping functions for users' archives

Expand Down
Loading

0 comments on commit abbdb03

Please sign in to comment.