Skip to content

Commit

Permalink
Add mod_pubsub_cache_backend
Browse files Browse the repository at this point in the history
  • Loading branch information
gustawlippa committed Oct 26, 2021
1 parent ea8afc2 commit 5140b15
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 41 deletions.
1 change: 0 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
mod_keystore_backend,
mod_mam_cassandra_arch_params,
mod_mam_cassandra_prefs_params, mod_mam_muc_cassandra_arch_params,
mod_pubsub_cache_backend,
mod_pubsub_db_backend, mod_revproxy_dynamic,
mod_roster_backend, mod_routing_machine,
mod_shared_roster, mongoose_rdbms_type,
Expand Down
13 changes: 2 additions & 11 deletions src/pubsub/mod_pubsub.erl
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@
-export([config_metrics/1]).

-define(MOD_PUBSUB_DB_BACKEND, mod_pubsub_db_backend).
-define(MOD_PUBSUB_CACHE_BACKEND, mod_pubsub_cache_backend).
-ignore_xref([
{?MOD_PUBSUB_DB_BACKEND, transaction, 2},
{?MOD_PUBSUB_DB_BACKEND, get_user_nodes, 2},
Expand All @@ -120,10 +119,6 @@
{?MOD_PUBSUB_DB_BACKEND, start, 0},
{?MOD_PUBSUB_DB_BACKEND, set_subscription_opts, 4},
{?MOD_PUBSUB_DB_BACKEND, stop, 0},
{?MOD_PUBSUB_CACHE_BACKEND, get_last_item, 2},
{?MOD_PUBSUB_CACHE_BACKEND, upsert_last_item, 5},
{?MOD_PUBSUB_CACHE_BACKEND, start, 1},
{?MOD_PUBSUB_CACHE_BACKEND, delete_last_item, 2},
affiliation_to_string/1, caps_recognised/4, config/3, create_node/7, default_host/0,
delete_item/4, delete_node/3, disco_local_features/1, disco_sm_features/1,
disco_sm_identity/1, disco_sm_items/1, extended_error/3, get_cached_item/2,
Expand Down Expand Up @@ -4140,14 +4135,10 @@ maybe_start_cache_module(ServerHost, Opts) ->
case proplists:get_value(last_item_cache, Opts, false) of
false ->
ok;
Backend ->
start_cache_module(ServerHost, Backend)
_Backend ->
mod_pubsub_cache_backend:start(ServerHost, Opts)
end.

start_cache_module(ServerHost, Backend) ->
gen_mod:start_backend_module(mod_pubsub_cache, [{backend, Backend}],
[upsert_last_item, delete_last_item, get_last_item]),
mod_pubsub_cache_backend:start(ServerHost).

is_last_item_cache_enabled(Host) ->
case cache_backend(Host) of
Expand Down
27 changes: 0 additions & 27 deletions src/pubsub/mod_pubsub_cache.erl

This file was deleted.

67 changes: 67 additions & 0 deletions src/pubsub/mod_pubsub_cache_backend.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
-module(mod_pubsub_cache_backend).

-export([start/2,
stop/1,
upsert_last_item/5,
delete_last_item/2,
get_last_item/2]).

-ignore_xref([stop/1]).

-define(MAIN_MODULE, mod_pubsub_cache).

%%====================================================================
%% Behaviour callbacks
%%====================================================================

%% ------------------------ Backend start/stop ------------------------

-callback start(jid:lserver()) -> ok.

-callback stop() -> ok.

-callback upsert_last_item(ServerHost :: binary(),
Nidx :: mod_pubsub:nodeIdx(),
ItemID :: mod_pubsub:itemId(),
Publisher :: jid:jid(),
Payload :: mod_pubsub:payload()) -> ok | {error, Reason :: term()}.

-callback delete_last_item(ServerHost :: binary(),
Nidx :: mod_pubsub:nodeIdx()) -> ok | {error, Reason :: term()}.

-callback get_last_item(ServerHost :: binary(),
Nidx :: mod_pubsub:nodeIdx()) ->
{ok, LastItem :: mod_pubsub:pubsubLastItem()} | {error, Reason :: term()}.

-spec start(jid:lserver(), gen_mod:module_opts()) -> ok.
start(ServerHost, Opts) ->
TrackedFuns = [upsert_last_item, delete_last_item, get_last_item],
mongoose_backend:init_per_host_type(ServerHost, ?MAIN_MODULE, TrackedFuns, Opts),
Args = [ServerHost],
mongoose_backend:call(ServerHost, ?MAIN_MODULE, ?FUNCTION_NAME, Args).

-spec stop(jid:lserver()) -> ok.
stop(ServerHost) ->
mongoose_backend:call(ServerHost, ?MAIN_MODULE, ?FUNCTION_NAME, []).

-spec upsert_last_item(ServerHost :: binary(),
Nidx :: mod_pubsub:nodeIdx(),
ItemID :: mod_pubsub:itemId(),
Publisher :: jid:jid(),
Payload :: mod_pubsub:payload()) -> ok | {error, Reason :: term()}.
upsert_last_item(ServerHost, Nidx, ItemID, Publisher, Payload) ->
Args = [ServerHost, Nidx, ItemID, Publisher, Payload],
mongoose_backend:call_tracked(ServerHost, ?MAIN_MODULE, ?FUNCTION_NAME, Args).

-spec delete_last_item(ServerHost :: binary(),
Nidx :: mod_pubsub:nodeIdx()) -> ok | {error, Reason :: term()}.
delete_last_item(ServerHost, Nidx) ->
Args = [ServerHost, Nidx],
mongoose_backend:call_tracked(ServerHost, ?MAIN_MODULE, ?FUNCTION_NAME, Args).

-spec get_last_item(ServerHost :: binary(),
Nidx :: mod_pubsub:nodeIdx()) ->
{ok, LastItem :: mod_pubsub:pubsubLastItem()} | {error, Reason :: term()}.
get_last_item(ServerHost, Nidx) ->
Args = [ServerHost, Nidx],
mongoose_backend:call_tracked(ServerHost, ?MAIN_MODULE, ?FUNCTION_NAME, Args).
2 changes: 1 addition & 1 deletion src/pubsub/mod_pubsub_cache_mnesia.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-module(mod_pubsub_cache_mnesia).

-behaviour(mod_pubsub_cache).
-behaviour(mod_pubsub_cache_backend).

-include("pubsub.hrl").
-include("jlib.hrl").
Expand Down
2 changes: 1 addition & 1 deletion src/pubsub/mod_pubsub_cache_rdbms.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-module(mod_pubsub_cache_rdbms).

-behaviour(mod_pubsub_cache).
-behaviour(mod_pubsub_cache_backend).

-include("pubsub.hrl").
-include("jlib.hrl").
Expand Down

0 comments on commit 5140b15

Please sign in to comment.