Skip to content

Commit

Permalink
Ensure read transactionality for roster users and groups
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Jan 21, 2024
1 parent 625f73d commit c4c3acb
Showing 1 changed file with 41 additions and 28 deletions.
69 changes: 41 additions & 28 deletions src/roster/mod_roster_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,37 @@ write_roster_version(HostType, LUser, LServer, _TransactionState, Ver) ->
-spec get_roster(mongooseim:host_type(), jid:luser(), jid:lserver()) -> [mod_roster:roster()].
get_roster(HostType, LUser, LServer) ->
{selected, Rows} = execute_roster_get(HostType, LUser, LServer),
{selected, GroupRows} =
mongoose_rdbms:execute_successfully(HostType, roster_group_get, [LServer, LUser]),
{selected, GroupRows} = execute_roster_group_get(HostType, LUser, LServer),
decode_roster_rows(LServer, LUser, Rows, GroupRows).

Check warning on line 60 in src/roster/mod_roster_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/roster/mod_roster_rdbms.erl#L59-L60

Added lines #L59 - L60 were not covered by tests

-spec get_roster_entry(mongooseim:host_type(), jid:luser(), jid:lserver(), mod_roster:contact(),
mod_roster:transaction_state(), mod_roster:entry_format()) ->
mod_roster:roster() | does_not_exist.
get_roster_entry(HostType, LUser, LServer, LJID, _TransactionState, full) ->
case get_roster_entry(HostType, LUser, LServer, LJID) of
does_not_exist ->
get_roster_entry(HostType, LUser, LServer, LJID, no_transaction, full) ->
F = fun() -> get_roster_entry(HostType, LUser, LServer, LJID, in_transaction, full) end,
case transaction(HostType, F) of

Check warning on line 67 in src/roster/mod_roster_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/roster/mod_roster_rdbms.erl#L66-L67

Added lines #L66 - L67 were not covered by tests
{atomic, Result} ->
Result;

Check warning on line 69 in src/roster/mod_roster_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/roster/mod_roster_rdbms.erl#L69

Added line #L69 was not covered by tests
_ ->
does_not_exist

Check warning on line 71 in src/roster/mod_roster_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/roster/mod_roster_rdbms.erl#L71

Added line #L71 was not covered by tests
end;
get_roster_entry(HostType, LUser, LServer, LJID, _, full) ->
BinJID = jid:to_binary(LJID),
case execute_roster_get_by_jid(HostType, LUser, LServer, BinJID) of

Check warning on line 75 in src/roster/mod_roster_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/roster/mod_roster_rdbms.erl#L74-L75

Added lines #L74 - L75 were not covered by tests
{selected, []} ->
does_not_exist;
Rec ->
Groups = get_groups_by_jid(HostType, LUser, LServer, LJID),
record_with_groups(Rec, Groups)
{selected, [Row]} ->
Groups = get_groups_by_jid(HostType, LUser, LServer, BinJID),
row_to_record(LServer, LUser, Row, #{BinJID => Groups})

Check warning on line 80 in src/roster/mod_roster_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/roster/mod_roster_rdbms.erl#L79-L80

Added lines #L79 - L80 were not covered by tests
end;
get_roster_entry(HostType, LUser, LServer, LJID, _TransactionState, short) ->
get_roster_entry(HostType, LUser, LServer, LJID).
BinJID = jid:to_binary(LJID),
case execute_roster_get_by_jid(HostType, LUser, LServer, BinJID) of

Check warning on line 84 in src/roster/mod_roster_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/roster/mod_roster_rdbms.erl#L83-L84

Added lines #L83 - L84 were not covered by tests
{selected, []} ->
does_not_exist;

Check warning on line 86 in src/roster/mod_roster_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/roster/mod_roster_rdbms.erl#L86

Added line #L86 was not covered by tests
{selected, [Row]} ->
row_to_record(LServer, LUser, Row, #{})

Check warning on line 88 in src/roster/mod_roster_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/roster/mod_roster_rdbms.erl#L88

Added line #L88 was not covered by tests
end.

-spec get_subscription_lists(mongoose_acc:t(), jid:luser(), jid:lserver()) -> [mod_roster:roster()].
get_subscription_lists(Acc, LUser, LServer) ->
Expand Down Expand Up @@ -178,6 +192,21 @@ prepare_version_upsert(HostType) ->
execute_roster_get(HostType, LUser, LServer) ->
mongoose_rdbms:execute_successfully(HostType, roster_get, [LServer, LUser]).

-spec execute_roster_group_get(mongooseim:host_type(), jid:luser(), jid:lserver()) ->
mongoose_rdbms:query_result().
execute_roster_group_get(HostType, LUser, LServer) ->
mongoose_rdbms:execute_successfully(HostType, roster_group_get, [LServer, LUser]).

Check warning on line 198 in src/roster/mod_roster_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/roster/mod_roster_rdbms.erl#L198

Added line #L198 was not covered by tests

-spec execute_roster_get_by_jid(mongooseim:host_type(), jid:luser(), jid:lserver(), jid:literal_jid()) ->
mongoose_rdbms:query_result().
execute_roster_get_by_jid(HostType, LUser, LServer, BinJID) ->
mongoose_rdbms:execute_successfully(HostType, roster_get_by_jid, [LServer, LUser, BinJID]).

Check warning on line 203 in src/roster/mod_roster_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/roster/mod_roster_rdbms.erl#L203

Added line #L203 was not covered by tests

-spec execute_roster_get_groups_by_jid(mongooseim:host_type(), jid:luser(), jid:lserver(), jid:literal_jid()) ->
mongoose_rdbms:query_result().
execute_roster_get_groups_by_jid(HostType, LUser, LServer, BinJID) ->
mongoose_rdbms:execute_successfully(HostType, roster_group_get_by_jid, [LServer, LUser, BinJID]).

Check warning on line 208 in src/roster/mod_roster_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/roster/mod_roster_rdbms.erl#L208

Added line #L208 was not covered by tests

-spec roster_upsert(mongooseim:host_type(), list()) -> mongoose_rdbms:query_result().
roster_upsert(HostType, [LServer, LUser, BinJID | Rest] = RosterRow) ->
InsertParams = RosterRow,
Expand All @@ -195,23 +224,10 @@ version_upsert(HostType, LUser, LServer, Version) ->
{updated, _} = rdbms_queries:execute_upsert(HostType, roster_version_upsert,
InsertParams, UpdateParams, UniqueKeyValues).

-spec get_roster_entry(mongooseim:host_type(), jid:luser(), jid:lserver(), jid:simple_jid()) ->
mod_roster:roster() | does_not_exist.
get_roster_entry(HostType, LUser, LServer, LJID) ->
BinJID = jid:to_binary(LJID),
{selected, Rows} = mongoose_rdbms:execute_successfully(HostType, roster_get_by_jid,
[LServer, LUser, BinJID]),
case Rows of
[] -> does_not_exist;
[Row] -> row_to_record(LServer, LUser, Row, #{})
end.

-spec get_groups_by_jid(mongooseim:host_type(), jid:luser(), jid:lserver(), jid:simple_jid()) ->
-spec get_groups_by_jid(mongooseim:host_type(), jid:luser(), jid:lserver(), jid:literal_jid()) ->
[binary()].
get_groups_by_jid(HostType, LUser, LServer, LJID) ->
BinJID = jid:to_binary(LJID),
{selected, Rows} = mongoose_rdbms:execute_successfully(
HostType, roster_group_get_by_jid, [LServer, LUser, BinJID]),
get_groups_by_jid(HostType, LUser, LServer, BinJID) ->
{selected, Rows} = execute_roster_get_groups_by_jid(HostType, LUser, LServer, BinJID),

Check warning on line 230 in src/roster/mod_roster_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/roster/mod_roster_rdbms.erl#L230

Added line #L230 was not covered by tests
[Group || {Group} <- Rows].

%%==============================================================================
Expand Down Expand Up @@ -273,9 +289,6 @@ row_to_record(LServer, LUser,
#roster{usj = USJ, us = US, jid = LJID, name = Nick,
subscription = Subscription, ask = Ask, groups = Groups, askmessage = AskMessage}.

record_with_groups(Rec, Groups) ->
Rec#roster{groups = Groups}.

decode_roster_rows(LServer, LUser, Rows, JIDGroups) ->
GroupsPerJID = group_per_jid(JIDGroups),
[row_to_record(LServer, LUser, Row, GroupsPerJID) || Row <- Rows].

Check warning on line 294 in src/roster/mod_roster_rdbms.erl

View check run for this annotation

Codecov / codecov/patch

src/roster/mod_roster_rdbms.erl#L293-L294

Added lines #L293 - L294 were not covered by tests
Expand Down

0 comments on commit c4c3acb

Please sign in to comment.