Skip to content

Commit

Permalink
Many changes to make boss_db compliant with current version of Erlang…
Browse files Browse the repository at this point in the history
… and to remove old unused code
  • Loading branch information
burbas committed Sep 29, 2023
1 parent 3fff899 commit a6525b2
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 71 deletions.
1 change: 1 addition & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

{erl_opts, [
debug_info,
warnings_as_errors,
warn_unused_vars,
warn_unused_import,
warn_exported_vars,
Expand Down
20 changes: 17 additions & 3 deletions src/boss_db.erl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ start(Options) ->
AdapterName = proplists:get_value(adapter, Options, mock),
Adapter = list_to_atom(lists:concat(["boss_db_adapter_", AdapterName])),
logger:info("Start Database Adapter ~p options ~p", [Adapter, Options]),
Adapter:start(Options),
%% We need to check if there's an adapter
case erlang:function_exported(Adapter, start, 1) of
true ->
Adapter:start(Options);
_ ->
ok
end,
lists:foldr(fun(ShardOptions, Acc) ->
case proplists:get_value(db_shard_models, ShardOptions, []) of
[] -> Acc;
Expand All @@ -84,7 +90,12 @@ start(Options) ->
undefined -> Adapter;
ShortName -> list_to_atom(lists:concat(["boss_db_adapter_", ShortName]))
end,
ShardAdapter:start(ShardOptions ++ Options),
case erlang:function_exported(ShardAdapter, start, 1) of
true ->
ShardAdapter:start(ShardOptions ++ Options);
_ ->
ok
end,
Acc
end
end, [], proplists:get_value(shards, Options, [])),
Expand All @@ -99,7 +110,10 @@ db_call(Msg) ->
db_call(Msg, Timeout) when is_integer(Timeout), Timeout > 0 ->
case erlang:get(boss_db_transaction_info) of
undefined ->
boss_pool:call(?POOLNAME, Msg, ?DEFAULT_TIMEOUT);
Worker = poolboy:checkout(?POOLNAME, true, Timeout),
Reply = gen_server:call(Worker, Msg, Timeout),
poolboy:checkin(?POOLNAME, Worker),
Reply;
State ->
{reply, Reply, NewState} =
boss_db_controller:handle_call(Msg, undefined, State),
Expand Down
6 changes: 5 additions & 1 deletion src/boss_db_adapter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

%% TODO: exact types
-callback start(_) -> ok.
-callback stop() -> ok.
-optional_callbacks([start/1]).

-callback init(_) -> any().
-callback terminate(_) -> any().
-callback find(_, _) -> any().
Expand All @@ -12,3 +13,6 @@
-callback counter(_, _) -> any().
-callback incr(_, _, _) -> any().
-callback save_record(_, _) -> any().

-callback dump(_) -> any().
-optional_callbacks([dump/1]).
51 changes: 29 additions & 22 deletions src/boss_db_controller.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@
-define(MAXDELAY, 10000).

-record(state, {
connection_state,
connection_delay,
connection_retry_timer,
options,
adapter,
read_connection,
write_connection,
shards = [],
model_dict = dict:new(),
cache_enable,
cache_ttl,
cache_prefix,
depth = 0}).
connection_state,
connection_delay,
connection_retry_timer,
options,
adapter,
read_connection,
write_connection,
shards = [],
model_dict = dict:new(),
cache_enable,
cache_ttl,
cache_prefix,
depth = 0,
supports_dump = false
}).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
start_link() ->
Expand Down Expand Up @@ -85,14 +87,17 @@ init(Options) ->
CacheTTL = proplists:get_value(cache_exp_time, Options, 60),
CachePrefix = proplists:get_value(cache_prefix, Options, db),
process_flag(trap_exit, true),
SupportsDump = erlang:function_exported(Adapter, dump, 1),
try_connection(self(), Options),
{ok, #state{connection_state = connecting,
connection_delay = 1,
options = Options,
adapter = Adapter,
cache_enable = CacheEnable,
cache_ttl = CacheTTL,
cache_prefix = CachePrefix }}.
connection_delay = 1,
options = Options,
adapter = Adapter,
cache_enable = CacheEnable,
cache_ttl = CacheTTL,
cache_prefix = CachePrefix,
supports_dump = SupportsDump
}}.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -176,10 +181,12 @@ handle_call(pop, _From, State) ->
handle_call(depth, _From, State) ->
{reply, State#state.depth, State};

handle_call(dump, _From, State) ->
Adapter = State#state.adapter,
Conn = State#state.read_connection,
handle_call(dump, _From, State = #state{adapter = Adapter, read_connection = Conn,
supports_dump = true}) ->
{reply, Adapter:dump(Conn), State};
handle_call(dump, _From, State = #state{adapter = Adapter, supports_dump = false}) ->
logger:error("Adapter ~s does not support the command 'dump'", [Adapter]),
{reply, {error, not_supported}, State};

handle_call({create_table, TableName, TableDefinition}, _From, State) ->
Adapter = State#state.adapter,
Expand Down
4 changes: 0 additions & 4 deletions src/db_adapters/boss_db_adapter_dynamodb.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
-behaviour(boss_db_adapter).
-export([
start/1,
stop/0,
init/1,
terminate/1,
find/2,
Expand All @@ -27,9 +26,6 @@
start(_Options) ->
application:start(ddb).

stop() ->
ok.

init(Options) ->
AccessKey = proplists:get_value(db_username, Options, os:getenv("AWS_ACCESS_KEY_ID")),
SecretKey = proplists:get_value(db_password, Options, os:getenv("AWS_SECRET_ACCESS_KEY")),
Expand Down
9 changes: 2 additions & 7 deletions src/db_adapters/boss_db_adapter_mnesia.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-module(boss_db_adapter_mnesia).
-behaviour(boss_db_adapter).
-export([init/1, terminate/1, start/1, stop/0, find/2, find/7]).
-export([init/1, terminate/1, start/1, find/2, find/7]).
-export([count/3, counter/2, incr/3, delete/2, save_record/2]).
-export([transaction/2]).
-export([table_exists/2, get_migrations_table/1, migration_done/3]).
Expand All @@ -10,17 +10,14 @@
start(_) ->
application:start(mnesia).

stop() ->
application:stop(mnesia).

% -----

init(_Options) ->
{ok, undefined}.

% -----
terminate(_) ->
ok.
application:stop(mnesia).

% -----
find(_, Id) when is_list(Id) ->
Expand Down Expand Up @@ -271,5 +268,3 @@ build_conditions1([{Key, 'eq', Value}|Rest], Pattern, Filter) ->
build_conditions1(Rest, lists:keystore(Key, 1, Pattern, {Key, Value}), Filter);
build_conditions1([First|Rest], Pattern, Filter) ->
build_conditions1(Rest, Pattern, [First|Filter]).


5 changes: 1 addition & 4 deletions src/db_adapters/boss_db_adapter_mock.erl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
% In-memory database for fast tests and easy setup
-module(boss_db_adapter_mock).
-behaviour(boss_db_adapter).
-export([init/1, terminate/1, start/1, stop/0]).
-export([init/1, terminate/1, start/1]).
-export([find/2, find/7, count/3, counter/2, incr/3, delete/2, save_record/2]).
-export([push/2, pop/2, dump/1, transaction/2]).

Expand All @@ -13,9 +13,6 @@ start(Options) ->
ok
end.

stop() ->
ok.

init(_Options) ->
{ok, undefined}.

Expand Down
17 changes: 1 addition & 16 deletions src/db_adapters/boss_db_adapter_mysql.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-module(boss_db_adapter_mysql).
-behaviour(boss_db_adapter).
-export([init/1, terminate/1, start/1, stop/0, find/2, find/7, find_by_sql/4]).
-export([init/1, terminate/1, find/2, find/7, find_by_sql/4]).
-export([count/3, counter/2, incr/3, delete/2, save_record/2]).
-export([push/2, pop/2, dump/1, execute/2, execute/3, transaction/2]).
-export([get_migrations_table/1, migration_done/3]).
Expand All @@ -9,12 +9,6 @@
-compile(export_all).
-endif.

start(_) ->
ok.

stop() ->
ok.

init(Options) ->
DBHost = proplists:get_value(db_host, Options, "localhost"),
DBPort = proplists:get_value(db_port, Options, 3306),
Expand Down Expand Up @@ -105,15 +99,6 @@ count(Pid, Type, Conditions) ->
{error, mysql:get_result_reason(MysqlRes)}
end.

table_exists(Pid, Type) ->
TableName = boss_record_lib:database_table(Type),
Res = fetch(Pid, ["SELECT 1 FROM ", TableName," LIMIT 1"]),
case Res of
{updated, _} ->
ok;
{error, MysqlRes} -> {error, mysql:get_result_reason(MysqlRes)}
end.

counter(Pid, Id) when is_list(Id) ->
Res = fetch(Pid, ["SELECT value FROM counters WHERE name = ", pack_value(Id)]),
case Res of
Expand Down
8 changes: 1 addition & 7 deletions src/db_adapters/boss_db_adapter_pgsql.erl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-module(boss_db_adapter_pgsql).
-behaviour(boss_db_adapter).

-export([init/1, terminate/1, start/1, stop/0, find/2, find/7, find_by_sql/4]).
-export([init/1, terminate/1, find/2, find/7, find_by_sql/4]).
-export([count/3, counter/2, incr/3, delete/2, save_record/2]).
-export([push/2, pop/2, dump/1, execute/2, execute/3, transaction/2, create_table/3, table_exists/2]).
-export([get_migrations_table/1, migration_done/3]).
Expand All @@ -15,12 +15,6 @@
-type sql_param_value() :: string()|number()|binary()|boolean().
-export_type([sql_param_value/0]).

start(_) ->
ok.

stop() ->
ok.

init(Options) ->
DBHost = proplists:get_value(db_host, Options, "localhost"),
DBPort = proplists:get_value(db_port, Options, 5432),
Expand Down
8 changes: 1 addition & 7 deletions src/db_adapters/boss_db_adapter_riak.erl
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
-module(boss_db_adapter_riak).
-behaviour(boss_db_adapter).
-export([init/1, terminate/1, start/1, stop/0, find/2, find/7]).
-export([init/1, terminate/1, find/2, find/7]).
-export([count/3, counter/2, incr/2, incr/3, delete/2, save_record/2]).
-export([push/2, pop/2]).

-define(LOG(Name, Value), logger:debug("DEBUG: ~s: ~p~n", [Name, Value])).

-define(HUGE_INT, 1000 * 1000 * 1000 * 1000).

start(_) ->
ok.

stop() ->
ok.

init(Options) ->
Host = proplists:get_value(db_host, Options, "localhost"),
Port = proplists:get_value(db_port, Options, 8087),
Expand Down

0 comments on commit a6525b2

Please sign in to comment.