From e293316a1ffd485e0fc62bd59db4d6aff10b467e Mon Sep 17 00:00:00 2001 From: cabol Date: Thu, 30 Mar 2017 12:32:36 -0500 Subject: [PATCH] =?UTF-8?q?[#136]=20=E2=80=93=20Rename=20'log=5Fqueries'?= =?UTF-8?q?=20option=20to=20'verbose'=20and=20log=20all=20operations.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/adapters/sumo_store_mnesia.erl | 33 +++++++++++++++++++----------- test/test.config | 2 +- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/adapters/sumo_store_mnesia.erl b/src/adapters/sumo_store_mnesia.erl index c16d1a4..4fa435a 100644 --- a/src/adapters/sumo_store_mnesia.erl +++ b/src/adapters/sumo_store_mnesia.erl @@ -44,7 +44,7 @@ | snmp | storage_properties. -type state() :: #{ - log_queries => boolean(), + verbose => boolean(), default_options => [{option(), term()}] }. @@ -55,8 +55,8 @@ -spec init(term()) -> {ok, state()}. init(Options) -> DefaultOptions = parse(Options), - LogQueries = application:get_env(sumo_db, log_queries, false), - {ok, #{default_options => DefaultOptions, log_queries => LogQueries}}. + Verbose = application:get_env(sumo_db, verbose, false), + {ok, #{default_options => DefaultOptions, verbose => Verbose}}. -spec persist(Doc, State) -> Response when Doc :: sumo_internal:doc(), @@ -83,6 +83,7 @@ persist(Doc, State) -> {error, Reason, State}; {atomic, ok} -> NewDoc = sumo_internal:set_field(IdField, NewId, Doc), + _ = maybe_log(persist, [DocName, NewDoc], State), {ok, NewDoc, State} end. @@ -96,6 +97,7 @@ fetch(DocName, Id, State) -> [Result] = mnesia:dirty_read(DocName, Id), Schema = sumo_internal:get_schema(DocName), Fields = schema_field_names(Schema), + _ = maybe_log(fetch, [DocName, Id], State), {ok, wakeup(result_to_doc(Result, Fields)), State} catch _:_ -> {error, notfound, State} @@ -117,6 +119,7 @@ delete_by(DocName, Conditions, State) -> {aborted, Reason} -> {error, Reason, State}; {atomic, Result} -> + _ = maybe_log(delete_by, [DocName, Conditions], State), {ok, Result, State} end. @@ -128,6 +131,7 @@ delete_all(DocName, State) -> Count = mnesia:table_info(DocName, size), case mnesia:clear_table(DocName) of {atomic, ok} -> + _ = maybe_log(delete_all, [DocName], State), {ok, Count, State}; {aborted, Reason} -> {error, Reason, State} @@ -200,7 +204,7 @@ find_by(DocName, Conditions, [], Limit, Offset, State) -> Schema = sumo_internal:get_schema(DocName), Fields = schema_field_names(Schema), Docs = [wakeup(result_to_doc(Result, Fields)) || Result <- Results], - _ = maybe_log_query(DocName, Conditions, Limit, Offset, MatchSpec, State), + _ = maybe_log(find_by, [DocName, Conditions, Limit, Offset, MatchSpec], State), {ok, Docs, State} end; find_by(_DocName, _Conditions, _Sort, _Limit, _Offset, State) -> @@ -386,14 +390,6 @@ result_to_doc(Result, Fields) -> transform_conditions(DocName, Conditions) -> sumo_utils:transform_conditions(fun validate_date/1, DocName, Conditions, [date]). -%% @private -maybe_log_query(DocName, Conditions, Limit, Offset, MatchSpec, #{log_queries := true}) -> - Msg = "find_by(~p, ~p, [], ~p, ~p)~nMatchSpec: ~p", - Args = [DocName, Conditions, Limit, Offset, MatchSpec], - lager:debug(Msg, Args); -maybe_log_query(_, _, _, _, _, _) -> - ok. - %% @private validate_date({FieldType, _, FieldValue}) -> case {FieldType, sumo_utils:is_datetime(FieldValue)} of @@ -427,3 +423,16 @@ wakeup_fun(date, _, {Date, _} = _FieldValue, _) -> Date; wakeup_fun(_, _, FieldValue, _) -> FieldValue. + +%% @private +maybe_log(Fun, Args, #{verbose := true}) -> + lager:debug(log_format(Fun), Args); +maybe_log(_, _, _) -> + ok. + +%% @private +log_format(persist) -> "persist(~p, ~p)"; +log_format(fetch) -> "fetch(~p, ~p)"; +log_format(delete_by) -> "delete_by(~p, ~p)"; +log_format(delete_all) -> "delete_all(~p)"; +log_format(find_by) -> "find_by(~p, ~p, [], ~p, ~p)~nMatchSpec: ~p". diff --git a/test/test.config b/test/test.config index 7b32874..f079373 100644 --- a/test/test.config +++ b/test/test.config @@ -1,7 +1,7 @@ [ {sumo_db, [ {wpool_opts, [{overrun_warning, 100}]}, - {log_queries, true}, + {verbose, true}, {query_timeout, 30000}, {storage_backends, [ {sumo_test_backend_mnesia, sumo_backend_mnesia, []}