-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07a168e
commit 4e259b1
Showing
14 changed files
with
208 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
-module(mod_caps_backend). | ||
-export([init/2, | ||
read/2, | ||
write/3, | ||
delete_node/2]). | ||
|
||
-define(MAIN_MODULE, mod_caps). | ||
|
||
%% ---------------------------------------------------------------------- | ||
%% Callbacks | ||
%% (exactly the same as specs in this module) | ||
|
||
-callback init(HostType, Opts) -> ok when | ||
HostType :: mongooseim:host_type(), | ||
Opts :: gen_mod:module_opts(). | ||
|
||
-callback read(mongooseim:host_type(), mod_caps:node_pair()) -> | ||
{ok, mod_caps:maybe_pending_features()} | error. | ||
|
||
-callback write(mongooseim:host_type(), mod_caps:node_pair(), | ||
mod_caps:maybe_pending_features()) -> ok. | ||
|
||
-callback delete_node(mongooseim:host_type(), mod_caps:node_pair()) -> ok. | ||
|
||
%% ---------------------------------------------------------------------- | ||
%% API Functions | ||
|
||
-spec init(HostType, Opts) -> ok when | ||
HostType :: mongooseim:host_type(), | ||
Opts :: gen_mod:module_opts(). | ||
init(HostType, Opts) -> | ||
TrackedFuns = [], | ||
mongoose_backend:init(HostType, ?MAIN_MODULE, TrackedFuns, Opts), | ||
Args = [HostType, Opts], | ||
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args). | ||
|
||
-spec read(mongooseim:host_type(), mod_caps:node_pair()) -> | ||
{ok, mod_caps:maybe_pending_features()} | error. | ||
read(HostType, Node) -> | ||
Args = [HostType, Node], | ||
mongoose_backend:call_tracked(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args). | ||
|
||
-spec write(mongooseim:host_type(), mod_caps:node_pair(), | ||
mod_caps:maybe_pending_features()) -> ok. | ||
write(HostType, Node, Features) -> | ||
Args = [HostType, Node, Features], | ||
mongoose_backend:call_tracked(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args). | ||
|
||
-spec delete_node(mongooseim:host_type(), mod_caps:node_pair()) -> ok. | ||
delete_node(HostType, Node) -> | ||
Args = [HostType, Node], | ||
mongoose_backend:call_tracked(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args). | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
-module(mod_caps_mnesia). | ||
-behaviour(mod_caps_backend). | ||
|
||
-export([init/2, | ||
read/2, | ||
write/3, | ||
delete_node/2]). | ||
|
||
-record(caps_features, | ||
{ | ||
node_pair = {<<>>, <<>>} :: mod_caps:node_pair(), | ||
features = [] :: mod_caps:maybe_pending_features() | ||
}). | ||
|
||
-spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok. | ||
init(_HostName, _Opts) -> | ||
case catch mnesia:table_info(caps_features, storage_type) of | ||
{'EXIT', _} -> | ||
ok; | ||
disc_only_copies -> | ||
ok; | ||
_ -> | ||
mnesia:delete_table(caps_features) | ||
end, | ||
mongoose_mnesia:create_table(caps_features, | ||
[{disc_only_copies, [node()]}, | ||
{local_content, true}, | ||
{attributes, record_info(fields, caps_features)}]), | ||
ok. | ||
|
||
-spec read(mongooseim:host_type(), mod_caps:node_pair()) -> | ||
{ok, mod_caps:maybe_pending_features()} | error. | ||
read(_HostType, Node) -> | ||
case mnesia:dirty_read({caps_features, Node}) of | ||
[#caps_features{features = Features}] -> {ok, Features}; | ||
_ -> error | ||
end. | ||
|
||
-spec write(mongooseim:host_type(), mod_caps:node_pair(), | ||
mod_caps:maybe_pending_features()) -> ok. | ||
write(_HostType, Node, Features) -> | ||
mnesia:dirty_write(#caps_features{node_pair = Node, | ||
features = Features}), | ||
ok. | ||
|
||
-spec delete_node(mongooseim:host_type(), mod_caps:node_pair()) -> ok. | ||
delete_node(_HostType, Node) -> | ||
mnesia:dirty_delete(caps_features, Node), | ||
ok. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
-module(mod_caps_rdbms). | ||
-behaviour(mod_caps_backend). | ||
|
||
-export([init/2, | ||
read/2, | ||
write/3, | ||
delete_node/2]). | ||
|
||
-spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok. | ||
init(HostType, _Opts) -> | ||
rdbms_queries:prepare_upsert(HostType, caps_upsert, caps, | ||
[<<"node">>, <<"sub_node">>, <<"features">>], | ||
[<<"features">>], | ||
[<<"node">>, <<"sub_node">>]), | ||
mongoose_rdbms:prepare(caps_delete, caps, | ||
[node, sub_node], | ||
<<"DELETE FROM caps WHERE node=? AND sub_node=?">>), | ||
mongoose_rdbms:prepare(caps_select, caps, | ||
[node, sub_node], | ||
<<"SELECT features FROM caps WHERE node=? AND sub_node=?">>), | ||
ok. | ||
|
||
-spec read(mongooseim:host_type(), mod_caps:node_pair()) -> | ||
{ok, mod_caps:maybe_pending_features()} | error. | ||
read(HostType, {Node, SubNode}) -> | ||
case mongoose_rdbms:execute(HostType, caps_select, [Node, SubNode]) of | ||
[{selected, [{Encoded}]}] -> {ok, jiffy:decode(Encoded)}; | ||
_ -> error | ||
end. | ||
|
||
-spec write(mongooseim:host_type(), mod_caps:node_pair(), | ||
mod_caps:maybe_pending_features()) -> ok. | ||
write(HostType, {Node, SubNode}, Features) -> | ||
Encoded = jiffy:encode(Features), | ||
InsertParams = [Node, SubNode, Encoded], | ||
UpdateParams = [Encoded], | ||
UniqueKeyValues = [Node, SubNode], | ||
rdbms_queries:execute_upsert(HostType, caps_upsert, | ||
InsertParams, UpdateParams, UniqueKeyValues), | ||
ok. | ||
|
||
-spec delete_node(mongooseim:host_type(), mod_caps:node_pair()) -> ok. | ||
delete_node(HostType, {Node, SubNode}) -> | ||
{updated, _} = mongoose_rdbms:execute(HostType, caps_delete, [Node, SubNode]), | ||
ok. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.