Skip to content
This repository has been archived by the owner on May 2, 2023. It is now read-only.

Commit

Permalink
use different opt config strategy (#218)
Browse files Browse the repository at this point in the history
* use different opt config strategy

* log different message when no adapter is used
  • Loading branch information
goncalotomas authored Jul 30, 2019
1 parent eed698c commit 838df8f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 26 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ bench-results:

compile:
${REBAR} as test compile
escript -s scripts/populate_fmke.escript

console: rel
./_build/default/rel/fmke/bin/env console
Expand Down
4 changes: 2 additions & 2 deletions config/fmke.config
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
%% (3): "8080 8081"
%% If you're connecting to multiple nodes on the same address but on different ports, you can list all of the different
%% ports here and leave a single entry in the database_addresses option (e.g. ["127.0.0.1"]).
{database_ports, [9042]}.
{database_ports, [8087]}.

%% Target back end data store. This is required in order for FMKe to load the
%% correct drivers to connect to your desired data store.
%% Currently FMKe supports the following data stores:
%% antidote, riak, redis
%% Please select one of the previous values in the form of an erlang atom
%% (e.g. riak) or string (e.g. "riak")
{target_database, cassandra}.
{target_database, antidote}.

%% Uses an optimized driver that implements the entire FMKe API (only if implemented).
%% Currently these are available for antidote, riak and redis. You're welcome to implement a new one and submit a
Expand Down
3 changes: 3 additions & 0 deletions src/fmke.erl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
start_link(Args) ->
gen_server:start_link({local, ?SERVER}, ?MODULE, Args, []).

init([none]) ->
lager:info("~p is booting in passthrough mode (no adapter)", [?MODULE]),
{ok, undefined};
init([Adapter]) ->
lager:info("~p will use the ~p adapter~n", [?MODULE, Adapter]),
{ok, Adapter}.
Expand Down
2 changes: 1 addition & 1 deletion src/fmke_driver_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ driver_adapter(Driver) ->
false ->
?KV_ADAPTER;
true ->
Driver
none
end.

db_from_driver(fmke_driver_ets) -> ets;
Expand Down
76 changes: 54 additions & 22 deletions src/fmke_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
%% Supervisor callbacks
-export([init/1]).

%% useful config funs
-import(fmke_driver_config, [
driver_adapter/1,
default_driver/1,
db_from_driver/1
]).

-define(SERVER, ?MODULE).

-define(KV_ADAPTER, fmke_kv_adapter).
Expand Down Expand Up @@ -118,40 +125,65 @@ config(Config) ->
Ports = get_option(database_ports, Config),
HttpPort = get_option(http_port, Config),
Model = get_option(data_model, Config),
config(driver, {Driver, Database}),
config(adapter, {driver, Driver}),
config(target_database, fmke_driver_config:db_from_driver(Driver)),
config(pool_size, PoolSize),
config(database_addresses, Addresses),
config(database_ports, Ports),
config(http_port, HttpPort),
config(data_model, Model).
set_opts([
{driver, Driver},
{target_database, Database},
{connection_pool_size, PoolSize},
{database_addresses, Addresses},
{database_ports, Ports},
{http_port, HttpPort},
{data_model, Model}
]).

set_opts([]) ->
ok;

set_opts([{_Opt, undefined} | Rest]) ->
set_opts(Rest);

set_opts([{Opt, Val} | Rest]) ->
config(Opt, Val),
set_opts(Rest).

config(data_model, Model) ->
maybe_config(data_model, Model);
config(http_port, HttpPort) ->
maybe_config(http_port, HttpPort);
config(target_database, Database) ->
maybe_config(target_database, Database);
maybe_config(target_database, Database),
Driver = default_driver(Database),
maybe_config(driver, Driver),
case driver_adapter(Driver) of
none ->
ok;
Adapter ->
maybe_config(adapter, Adapter)
end;
config(database_ports, Ports) ->
maybe_config(database_ports, Ports);
config(database_addresses, Addresses) ->
maybe_config(database_addresses, Addresses);
config(pool_size, Size) ->
config(connection_pool_size, Size) ->
maybe_config(connection_pool_size, Size);
config(adapter, {driver, Driver}) ->
maybe_config(adapter, fmke_driver_config:driver_adapter(Driver));
config(driver, {undefined, Database}) ->
maybe_config(driver, fmke_driver_config:default_driver(Database));
config(driver, {Driver, _Database}) ->
maybe_config(driver, Driver).

maybe_config(Key, undefined) ->
lager:info("Unable to set ~p (value undefined)~n", [Key]),
ok;
config(driver, Driver) ->
maybe_config(driver, Driver),
case driver_adapter(Driver) of
none ->
ok;
Adapter ->
maybe_config(adapter, Adapter)
end,
maybe_config(target_database, db_from_driver(Driver)).

maybe_config(Key, Val) ->
lager:info("Setting FMKe option ~p = ~p~n", [Key, Val]),
ok = application:set_env(?APP, Key, Val).
case application:get_env(?APP, Key) of
undefined ->
lager:info("Setting FMKe option ~p = ~p~n", [Key, Val]),
ok = application:set_env(?APP, Key, Val);
{ok, Predefined} ->
lager:info("Setting FMKe option ~p failed (already defined as ~p)~n", [Key, Predefined]),
already_defined
end.

get_option(Opt, Config) ->
{_Source, Val} = get_value(os:getenv(atom_to_list(Opt)),
Expand Down

0 comments on commit 838df8f

Please sign in to comment.