Skip to content

Commit

Permalink
mod_mam_meta clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
DenysGonchar committed May 30, 2019
1 parent d4ea450 commit 102abd5
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 56 deletions.
2 changes: 2 additions & 0 deletions big_tests/tests/gdpr_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,14 @@ mam_required_modules(retrieve_mam_pm, Backend) ->
mam_required_modules(CN, Backend) when CN =:= retrieve_mam_pm_and_muc_light_dont_interfere;
CN =:= retrieve_mam_muc_light ->
[{mod_mam_meta, [{backend, Backend},
{db_message_format, mam_message_eterm}, %% used only by riak & cassandra backends
{pm, [{archive_groupchats, false}]},
{muc, [{host, "muclight.@HOST@"}]}]},
{mod_muc_light, [{host, "muclight.@HOST@"}]}];
mam_required_modules(retrieve_mam_pm_and_muc_light_interfere, Backend) ->
[{mod_mam_meta, [{backend, Backend},
{rdbms_message_format, simple}, %% ignored for any other than rdbms backend
{db_message_format, mam_message_xml}, %% used only by riak & cassandra backends
{pm, [{archive_groupchats, true}]},
{muc, [{host, "muclight.@HOST@"}]}]},
{mod_muc_light, [{host, "muclight.@HOST@"}]}].
Expand Down
7 changes: 5 additions & 2 deletions big_tests/tests/mam_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ init_modules(rdbms, muc_all, Config) ->
init_module(host(), mod_mam_muc, [{host, muc_domain(Config)}]),
Config;
init_modules(rdbms_simple, muc_all, Config) ->
init_module(host(), mod_mam_muc_rdbms_arch, [muc, simple]),
init_module(host(), mod_mam_muc_rdbms_arch, [muc, rdbms_simple_opts()]),
init_module(host(), mod_mam_rdbms_prefs, [muc]),
init_module(host(), mod_mam_rdbms_user, [muc]),
init_module(host(), mod_mam_muc, [{host, muc_domain(Config)}]),
Expand Down Expand Up @@ -750,7 +750,7 @@ init_modules(rdbms, C, Config) ->
Config;
init_modules(rdbms_simple, C, Config) ->
init_module(host(), mod_mam, addin_mam_options(C, Config)),
init_module(host(), mod_mam_rdbms_arch, [pm, simple]),
init_module(host(), mod_mam_rdbms_arch, [pm, rdbms_simple_opts()]),
init_module(host(), mod_mam_rdbms_prefs, [pm]),
init_module(host(), mod_mam_rdbms_user, [pm]),
Config;
Expand Down Expand Up @@ -815,6 +815,9 @@ init_modules(rdbms_mnesia_cache, C, Config) ->
init_module(host(), mod_mam_cache_user, [pm]),
Config.

rdbms_simple_opts() ->
[{db_jid_format, mam_jid_rfc}, {db_message_format, mam_message_xml}].

init_modules_for_muc_light(BackendType, Config) ->
dynamic_modules:start(host(), mod_muc_light, [{host, binary_to_list(muc_light_host())}]),
Config1 = init_modules(BackendType, muc_all, [{muc_domain, "muclight.@HOST@"} | Config]),
Expand Down
2 changes: 1 addition & 1 deletion big_tests/tests/muc_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ init_per_group(G, Config) when G =:= http_auth_no_server;
init_per_group(hibernation, Config) ->
case mam_helper:backend() of
rdbms ->
dynamic_modules:start(domain(), mod_mam_muc_rdbms_arch, [muc, simple]),
dynamic_modules:start(domain(), mod_mam_muc_rdbms_arch, [muc]),
dynamic_modules:start(domain(), mod_mam_rdbms_prefs, [muc]),
dynamic_modules:start(domain(), mod_mam_rdbms_user, [muc]),
dynamic_modules:start(domain(), mod_mam_muc, [{host, "muc.@HOST@"}]);
Expand Down
2 changes: 1 addition & 1 deletion big_tests/tests/rest_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ to_list(V) when is_list(V) ->
V.

maybe_enable_mam(rdbms, Host, Config) ->
init_module(Host, mod_mam_rdbms_arch, [muc, pm, simple]),
init_module(Host, mod_mam_rdbms_arch, [muc, pm]),
init_module(Host, mod_mam_rdbms_prefs, [muc, pm]),
init_module(Host, mod_mam_rdbms_user, [muc, pm]),
init_module(Host, mod_mam, []),
Expand Down
11 changes: 1 addition & 10 deletions src/mam/mod_mam_cassandra_arch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -772,19 +772,10 @@ stored_binary_to_packet(Bin) ->
%% {db_message_format, module()}
%% ])
compile_params_module(Params) ->
CodeStr = params_helper(expand_simple_param(Params)),
CodeStr = params_helper(Params),
{Mod, Code} = dynamic_compile:from_string(CodeStr),
code:load_binary(Mod, "mod_mam_cassandra_arch_params.erl", Code).

expand_simple_param(Params) ->
lists:flatmap(fun(simple) -> simple_params();
({simple, true}) -> simple_params();
(Param) -> [Param]
end, Params).

simple_params() ->
[{db_message_format, mam_message_xml}].

params_helper(Params) ->
binary_to_list(iolist_to_binary(io_lib:format(
"-module(mod_mam_cassandra_arch_params).~n"
Expand Down
45 changes: 19 additions & 26 deletions src/mam/mod_mam_meta.erl
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,24 @@ handle_nested_opts(Key, RootOpts, Default, Deps) ->
-spec parse_opts(Type :: pm | muc, Opts :: proplists:proplist(), deps()) -> deps().
parse_opts(Type, Opts, Deps) ->
CoreMod = mam_type_to_core_mod(Type),

CoreModOpts =
lists:filtermap(
fun(Key) ->
case proplists:lookup(Key, Opts) of
none -> false;
Opt -> {true, Opt}
end
end, valid_core_mod_opts(CoreMod)),

CoreModOpts = filter_opts(Opts, valid_core_mod_opts(CoreMod)),
WithCoreDeps = add_dep(CoreMod, CoreModOpts, Deps),
Backend = proplists:get_value(backend, Opts, rdbms),

parse_backend_opts(Backend, Type, Opts, WithCoreDeps).

-spec mam_type_to_core_mod(atom()) -> module().
mam_type_to_core_mod(pm) -> mod_mam;
mam_type_to_core_mod(muc) -> mod_mam_muc.

filter_opts(Opts, ValidOpts) ->
lists:filtermap(
fun(Key) ->
case proplists:lookup(Key, Opts) of
none -> false;
Opt -> {true, Opt}
end
end, ValidOpts).

-spec valid_core_mod_opts(module()) -> [atom()].
valid_core_mod_opts(mod_mam) ->
[
Expand Down Expand Up @@ -152,7 +151,8 @@ parse_backend_opts(cassandra, Type, Opts, Deps0) ->
muc -> mod_mam_muc_cassandra_arch
end,

Deps = add_dep(ModArch, Deps0),
Opts1 = filter_opts(Opts, [db_message_format, pool_name]),
Deps = add_dep(ModArch, Opts1, Deps0),

case proplists:get_value(user_prefs_store, Opts, false) of
cassandra -> add_dep(mod_mam_cassandra_prefs, [Type], Deps);
Expand All @@ -161,7 +161,8 @@ parse_backend_opts(cassandra, Type, Opts, Deps0) ->
end;

parse_backend_opts(riak, Type, Opts, Deps0) ->
Deps = add_dep(mod_mam_riak_timed_arch_yz, [Type], Deps0),
Opts1 = filter_opts(Opts, [db_message_format]),
Deps = add_dep(mod_mam_riak_timed_arch_yz, [Type | Opts1], Deps0),

case proplists:get_value(user_prefs_store, Opts, false) of
mnesia -> add_dep(mod_mam_mnesia_prefs, [Type], Deps);
Expand All @@ -181,25 +182,17 @@ parse_backend_opts(rdbms, Type, Opts0, Deps0) ->
Deps = add_dep(mod_mam_rdbms_user, [Type], Deps1),

lists:foldl(
pa:bind(fun parse_backend_opt/5, Type, ModRDBMSArch, ModAsyncWriter),
pa:bind(fun parse_rdbms_opt/5, Type, ModRDBMSArch, ModAsyncWriter),
Deps, Opts);

parse_backend_opts(elasticsearch, Type, Opts, Deps0) ->
ExtraOpts =
case proplists:get_value(elasticsearch_index_name, Opts) of
IndexName when is_binary(IndexName) ->
[{index_name, IndexName}];
_ ->
[]
end,

ModArch =
case Type of
pm -> mod_mam_elasticsearch_arch;
muc -> mod_mam_muc_elasticsearch_arch
end,

Deps = add_dep(ModArch, ExtraOpts, Deps0),
Deps = add_dep(ModArch, Deps0),

case proplists:get_value(user_prefs_store, Opts, false) of
mnesia -> add_dep(mod_mam_mnesia_prefs, [Type], Deps);
Expand All @@ -219,7 +212,7 @@ add_dep(Dep, Deps) ->
-spec add_dep(Dep :: module(), Args :: proplists:proplist(), deps()) -> deps().
add_dep(Dep, Args, Deps) ->
PrevArgs = maps:get(Dep, Deps, []),
NewArgs = Args ++ PrevArgs,
NewArgs = lists:usort(Args ++ PrevArgs),
maps:put(Dep, NewArgs, Deps).


Expand All @@ -236,9 +229,9 @@ add_default_rdbms_opts(Opts) ->
[{cache_users, true}, {async_writer, true}]).


-spec parse_backend_opt(Type :: pm | muc, module(), module(),
-spec parse_rdbms_opt(Type :: pm | muc, module(), module(),
Option :: {module(), term()}, deps()) -> deps().
parse_backend_opt(Type, ModRDBMSArch, ModAsyncWriter, Option, Deps) ->
parse_rdbms_opt(Type, ModRDBMSArch, ModAsyncWriter, Option, Deps) ->
case Option of
{cache_users, true} ->
add_dep(mod_mam_cache_user, [Type], Deps);
Expand Down
11 changes: 1 addition & 10 deletions src/mam/mod_mam_muc_cassandra_arch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -887,19 +887,10 @@ stored_binary_to_packet(Bin) ->
%% {db_message_format, module()}
%% ])
compile_params_module(Params) ->
CodeStr = params_helper(expand_simple_param(Params)),
CodeStr = params_helper(Params),
{Mod, Code} = dynamic_compile:from_string(CodeStr),
code:load_binary(Mod, "mod_mam_muc_cassandra_arch_params.erl", Code).

expand_simple_param(Params) ->
lists:flatmap(fun(simple) -> simple_params();
({simple, true}) -> simple_params();
(Param) -> [Param]
end, Params).

simple_params() ->
[{db_message_format, mam_muc_message_xml}].

params_helper(Params) ->
binary_to_list(iolist_to_binary(io_lib:format(
"-module(mod_mam_muc_cassandra_arch_params).~n"
Expand Down
20 changes: 14 additions & 6 deletions test/mod_mam_meta_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,34 @@ produces_valid_configurations(_Config) ->


handles_riak_config(_Config) ->
Deps = deps([{backend, riak}, {pm, [{user_prefs_store, mnesia}]}, {muc, []}]),
Deps = deps([
{backend, riak},
{db_message_format, some_format},
{pm, [{user_prefs_store, mnesia}]},
{muc, []}
]),

?assert(lists:keymember(mod_mam, 1, Deps)),
?assert(lists:keymember(mod_mam_muc, 1, Deps)),
check_has_args(mod_mam_riak_timed_arch_yz, [pm, muc], Deps),
check_has_args(mod_mam_riak_timed_arch_yz, [{db_message_format, some_format}], Deps),
check_has_args(mod_mam_mnesia_prefs, [pm], Deps),
check_has_no_args(mod_mam_mnesia_prefs, [muc], Deps).


handles_cassandra_config(_Config) ->
Deps = deps([
{backend, cassandra},
{pm, [{user_prefs_store, cassandra}]},
{muc, [{user_prefs_store, mnesia}]}
{pm, [{user_prefs_store, cassandra}, {db_message_format, some_format}]},
{muc, [{user_prefs_store, mnesia}, {pool_name, some_poolname}]}
]),

?assert(lists:keymember(mod_mam_cassandra_arch, 1, Deps)),
?assert(lists:keymember(mod_mam_muc_cassandra_arch, 1, Deps)),
check_has_args(mod_mam_mnesia_prefs, [muc], Deps),
check_has_args(mod_mam_cassandra_prefs, [pm], Deps).
check_has_args(mod_mam_cassandra_prefs, [pm], Deps),
check_has_args(mod_mam_cassandra_arch, [{db_message_format, some_format}], Deps),
check_has_args(mod_mam_muc_cassandra_arch, [{pool_name, some_poolname}], Deps),
check_has_no_args(mod_mam_cassandra_arch, [{pool_name, some_poolname}], Deps),
check_has_no_args(mod_mam_muc_cassandra_arch, [{db_message_format, some_format}], Deps).


example_muc_only_no_pref_good_performance(_Config) ->
Expand Down

0 comments on commit 102abd5

Please sign in to comment.