Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Riak vcard #460

Merged
merged 25 commits into from
Oct 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7b0fdf1
add vcard search schema
michalwski Jun 25, 2015
35a6726
extract default search fileds to mod_vcard
michalwski Jun 29, 2015
79de368
move ejabberd_sm_redis:binary_join/2 to ejabberd_binary:join/2
michalwski Jun 29, 2015
8c67e5b
improve vcard search schema
michalwski Jun 29, 2015
2f4f6f1
add search helper function to mongoose_riak module
michalwski Jun 29, 2015
d0ed5a8
add riak backend for vcards
michalwski Jun 29, 2015
1056f47
create vcard schema, index and bucket type on travis
michalwski Jun 29, 2015
20cb1fa
return one element list with vcard
michalwski Jun 30, 2015
d262a94
set last write wins for vcard bucket type
michalwski Jun 30, 2015
1ee999a
fix mod_vcard reading for mnesia backend
michalwski Jun 30, 2015
1790e15
fix mod_vcard params reading for odbc backend
michalwski Jun 30, 2015
85b8c54
small fix to email field
michalwski Jul 1, 2015
4fa84ab
search only in current host
michalwski Jul 1, 2015
149e0b0
remove search_all_hosts option for mod_vcard mnesia backend
michalwski Jul 1, 2015
5ffcbca
remove allow_return_all optonion for mnesia and odbc vcard backends
michalwski Jul 1, 2015
e3d1a18
mod_vcard_odbc and _mnesia search refactoring
michalwski Jul 2, 2015
749611a
extract match parameter reading to mod_vcard module
michalwski Jul 2, 2015
7b680b5
apply limitations to vcard search
michalwski Jul 2, 2015
0bc7f8e
search only in given host in mod_vcard_odbc and _mnesia
michalwski Jul 2, 2015
7a71ca6
[tests] apply changes done in esl/ejabberd_tests#122
michalwski Oct 8, 2015
8110de9
fix dialyzer errors
michalwski Oct 8, 2015
3e0b101
add vcard riak backend to doc [skip ci]
michalwski Oct 8, 2015
0aa5974
fix node2_vars.config syntax error
michalwski Oct 8, 2015
c653c48
[tests] do not check mod_time start result
michalwski Oct 9, 2015
935fb2f
remove no longer supported options from doc [skip ci]
michalwski Oct 12, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions apps/ejabberd/src/ejabberd_binary.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-module(ejabberd_binary).

-export([string_to_binary/1]).
-export([join/2]).

-ifdef(no_binary_to_integer).

Expand Down Expand Up @@ -28,3 +29,16 @@ string_to_binary(S) when is_list(S) ->
end;
string_to_binary(B) when is_binary(B) ->
B.


join([], _) ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this could be more optimal:

join(Parts, Sep) when is_integer(Sep) -> join(Parts, <<Sep>>);
join(Parts, Sep) -> join(Parts, Sep, <<>>).

join([], _Sep, Acc) -> Acc;
join([Part], _Sep, Acc) -> <<Acc/binary, Part/binary>>;
join([Part | RParts], Sep, Acc) -> join2(RParts, Sep, <<Acc/binary, Part/binary, Sep/binary>>).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually some time ago I saw some micro-benchmarking done by @arcusfelis showing that io_list_to_binary is faster than incrementally built binary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, true, I think I saw it too. I wonder if it still holds with current OTP versions (AFAIR those were done with R16).

<<>>;
join([Part], _) ->
Part;
join(Parts, Sep) ->
iolist_to_binary(do_join(Parts, Sep, [])).

do_join([Part], _, Acc) ->
lists:reverse([Part | Acc]);
do_join([Part | Rest], Sep, Acc) ->
do_join(Rest, Sep, [Sep, Part | Acc]).
14 changes: 2 additions & 12 deletions apps/ejabberd/src/ejabberd_sm_redis.erl
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,11 @@ cleanup(Node) ->
lists:foreach(fun(H) ->
[_, U, S, R | SIDEncoded] = re:split(H, ":"),
%% Add possible removed ":" from encoded SID
SID = binary_to_term(binary_join(SIDEncoded, <<":">>)),
SID = binary_to_term(ejabberd_binary:join(SIDEncoded, <<":">>)),
delete_session(SID, U, S, R)
end, Hashes).

binary_join([], _) ->
<<>>;
binary_join([Part], _) ->
Part;
binary_join(Parts, Sep) ->
iolist_to_binary(do_binary_join(Parts, Sep, [])).

do_binary_join([Part], _, Acc) ->
lists:reverse([Part | Acc]);
do_binary_join([Part | Rest], Sep, Acc) ->
do_binary_join(Rest, Sep, [Sep, Part | Acc]).


-spec total_count() -> integer().
total_count() ->
Expand Down
31 changes: 31 additions & 0 deletions apps/ejabberd/src/mod_vcard.erl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
-export([process_local_iq/3,process_sm_iq/3,get_local_features/5,remove_user/2]).

-export([start_link/2]).
-export([default_search_fields/0]).
-export([get_results_limit/1]).

-export([config_change/4]).

Expand Down Expand Up @@ -96,6 +98,35 @@
Res :: list() when
VHost :: binary().

-spec default_search_fields() -> list().
default_search_fields() ->
[{<<"User">>, <<"user">>},
{<<"Full Name">>, <<"fn">>},
{<<"Given Name">>, <<"first">>},
{<<"Middle Name">>, <<"middle">>},
{<<"Family Name">>, <<"last">>},
{<<"Nickname">>, <<"nick">>},
{<<"Birthday">>, <<"bday">>},
{<<"Country">>, <<"ctry">>},
{<<"City">>, <<"locality">>},
{<<"Email">>, <<"email">>},
{<<"Organization Name">>, <<"orgname">>},
{<<"Organization Unit">>, <<"orgunit">>}].

-spec get_results_limit(ejabberd:lserver()) -> non_neg_integer() | inifinity.
get_results_limit(LServer) ->
case gen_mod:get_module_opt(LServer, mod_vcard, matches, ?JUD_MATCHES) of
infinity ->
infinity;
Val when is_integer(Val) and (Val > 0) ->
Val;
Val ->
?ERROR_MSG("Illegal option value ~p. "
"Default value ~p substituted.",
[{matches, Val}, ?JUD_MATCHES]),
?JUD_MATCHES
end.

%%--------------------------------------------------------------------
%% gen_mod callbacks
%%--------------------------------------------------------------------
Expand Down
67 changes: 21 additions & 46 deletions apps/ejabberd/src/mod_vcard_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,47 +55,30 @@ set_vcard(User, VHost, VCard, VCardSearch) ->

search(VHost, Data, _Lang, DefaultReportedFields) ->
MatchHead = make_matchhead(VHost, Data),
AllowReturnAll = gen_mod:get_module_opt(VHost, ?MODULE,
allow_return_all, false),
R=if
(MatchHead == #vcard_search{_ = '_'}) and (not AllowReturnAll) ->
[];
true ->
case catch mnesia:dirty_select(vcard_search,
[{MatchHead, [], ['$_']}]) of
{'EXIT', Reason} ->
?ERROR_MSG("~p", [Reason]),
[];
Rs ->
case gen_mod:get_module_opt(VHost, ?MODULE,
matches, ?JUD_MATCHES) of
infinity ->
Rs;
Val when is_integer(Val) and (Val > 0) ->
lists:sublist(Rs, Val);
Val ->
?ERROR_MSG("Illegal option value ~p. Default value ~p substituted.",
[{matches, Val}, ?JUD_MATCHES]),
lists:sublist(Rs, ?JUD_MATCHES)
end
end
end,
R = do_search(VHost, MatchHead),
Items = lists:map(fun record_to_item/1,R),
[DefaultReportedFields | Items].

do_search(_, #vcard_search{_ = '_'}) ->
[];
do_search(VHost, MatchHeadIn) ->
MatchHead = MatchHeadIn#vcard_search{us = {'_', VHost}},
case catch mnesia:dirty_select(vcard_search,
[{MatchHead, [], ['$_']}]) of
{'EXIT', Reason} ->
?ERROR_MSG("~p", [Reason]),
[];
Rs ->
case mod_vcard:get_results_limit(VHost) of
infinity ->
Rs;
Val ->
lists:sublist(Rs, Val)
end
end.

search_fields(_VHost) ->
[{<<"User">>, <<"user">>},
{<<"Full Name">>, <<"fn">>},
{<<"Given Name">>, <<"first">>},
{<<"Middle Name">>, <<"middle">>},
{<<"Family Name">>, <<"last">>},
{<<"Nickname">>, <<"nick">>},
{<<"Birthday">>, <<"bday">>},
{<<"Country">>, <<"ctry">>},
{<<"City">>, <<"locality">>},
{<<"Email">>, <<"email">>},
{<<"Organization Name">>, <<"orgname">>},
{<<"Organization Unit">>, <<"orgunit">>}].
mod_vcard:default_search_fields().
%%--------------------------------------------------------------------
%% internal
%%--------------------------------------------------------------------
Expand Down Expand Up @@ -287,15 +270,7 @@ filter_fields([{SVar, [Val]} | Ds], Match, VHost)
LVal = stringprep:tolower(Val),
NewMatch =
case SVar of
<<"user">> ->
case gen_mod:get_module_opt(VHost, ?MODULE,
search_all_hosts, true) of
true ->
Match#vcard_search{luser = make_val(LVal)};
false ->
Host = find_my_host(VHost),
Match#vcard_search{us = {make_val(LVal), Host}}
end;
<<"user">> -> Match#vcard_search{luser = make_val(LVal)};
<<"fn">> -> Match#vcard_search{lfn = make_val(LVal)};
<<"last">> -> Match#vcard_search{lfamily = make_val(LVal)};
<<"first">> -> Match#vcard_search{lgiven = make_val(LVal)};
Expand Down
68 changes: 23 additions & 45 deletions apps/ejabberd/src/mod_vcard_odbc.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%%%----------------------------------------------------------------------
%%% File : mod_vcard_odbc.erl
%%% File : mod_vcard.erl
%%% Author : Alexey Shchepin <alexey@process-one.net>
%%% Purpose : vCard support via ODBC
%%% Created : 2 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
Expand Down Expand Up @@ -109,61 +109,39 @@ set_vcard(User, VHost, VCard, VCardSearch) ->

search(LServer, Data, _Lang, DefaultReportedFields) ->
RestrictionSQL = make_restriction_sql(LServer, Data),
AllowReturnAll = gen_mod:get_module_opt(LServer, ?MODULE,
allow_return_all, false),
R=if
(RestrictionSQL == "") and (not AllowReturnAll) ->
[];
true ->
Limit = case gen_mod:get_module_opt(LServer, ?MODULE,
matches, ?JUD_MATCHES) of
infinity ->
infinity;
Val when is_integer(Val) and (Val > 0) ->
Val;
Val ->
?ERROR_MSG("Illegal option value ~p. "
"Default value ~p substituted.",
[{matches, Val}, ?JUD_MATCHES]),
?JUD_MATCHES
end,
case catch odbc_queries:search_vcard(LServer, RestrictionSQL, Limit) of
{selected, [<<"username">>, <<"server">>, <<"fn">>, <<"family">>, <<"given">>,
<<"middle">>, <<"nickname">>, <<"bday">>, <<"ctry">>, <<"locality">>,
<<"email">>, <<"orgname">>, <<"orgunit">>], Rs} when is_list(Rs) ->
Rs;
Error ->
?ERROR_MSG("~p", [Error]),
[]
end
end,
R = do_search(LServer, RestrictionSQL),
Items = lists:map(fun(I) -> record_to_item(LServer,I) end, R),
[DefaultReportedFields | Items].

do_search(_LServer, "") ->
[];
do_search(LServer, RestrictionSQL) ->
Limit = mod_vcard:get_results_limit(LServer),
case catch odbc_queries:search_vcard(LServer, RestrictionSQL, Limit) of
{selected, [<<"username">>, <<"server">>, <<"fn">>, <<"family">>, <<"given">>,
<<"middle">>, <<"nickname">>, <<"bday">>, <<"ctry">>, <<"locality">>,
<<"email">>, <<"orgname">>, <<"orgunit">>], Rs} when is_list(Rs) ->
Rs;
Error ->
?ERROR_MSG("~p", [Error]),
[]
end.

search_fields(_VHost) ->
[{<<"User">>, <<"user">>},
{<<"Full Name">>, <<"fn">>},
{<<"Given Name">>, <<"first">>},
{<<"Middle Name">>, <<"middle">>},
{<<"Family Name">>, <<"last">>},
{<<"Nickname">>, <<"nick">>},
{<<"Birthday">>, <<"bday">>},
{<<"Country">>, <<"ctry">>},
{<<"City">>, <<"locality">>},
{<<"Email">>, <<"email">>},
{<<"Organization Name">>, <<"orgname">>},
{<<"Organization Unit">>, <<"orgunit">>}].
mod_vcard:default_search_fields().

%%--------------------------------------------------------------------
%% internal
%%--------------------------------------------------------------------
make_restriction_sql(LServer, Data) ->
filter_fields(Data, "", LServer).

filter_fields([], RestrictionSQL, _LServer) ->
case RestrictionSQL of
"" -> "";
_ -> [" where ", RestrictionSQL]
filter_fields([], RestrictionSQLIn, LServer) ->
case RestrictionSQLIn of
"" ->
"";
_ ->
[" where ", [RestrictionSQLIn, " and ", ["server = '", ejabberd_odbc:escape(LServer),"'"]]]
end;
filter_fields([{SVar, [Val]} | Ds], RestrictionSQL, LServer)
when is_binary(Val) and (Val /= <<"">>) ->
Expand Down
Loading