fixes db crash; simplifies the ezic_db proxy module
The db crash happens when calling `ezic_db:get_all(X)` when `X` is not a valid table name. `ezic_db_ets` crashes, causing the `ezic_db` server to crash, tossing the database with it. The solution prevents ezic_db_ets:get_all/1 from crashing, and instead returns an error value. The simplification here eliminated some code through deduplication. The `handle_call` functions could be improved similarly.
This commit is contained in:
@@ -28,7 +28,7 @@
|
|||||||
]).
|
]).
|
||||||
|
|
||||||
|
|
||||||
-record(state, {zones, rules, get_all, flatzone, insert_all, wipe, implementation}).
|
-record(state, {mod}).
|
||||||
|
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
% PUBLIC API
|
% PUBLIC API
|
||||||
@@ -78,25 +78,12 @@ get_implementation() ->
|
|||||||
start_link(StartArgs) ->
|
start_link(StartArgs) ->
|
||||||
gen_server:start_link({local, ?MODULE}, ?MODULE, StartArgs, []).
|
gen_server:start_link({local, ?MODULE}, ?MODULE, StartArgs, []).
|
||||||
|
|
||||||
init(ezic_db_mnesia) ->
|
|
||||||
ezic_db_mnesia:init(),
|
%% TODO: create a db behavior
|
||||||
State = #state{zones = fun ezic_db_mnesia:zones/1,
|
init(DbModule) when DbModule =:= ezic_db_mnesia;
|
||||||
rules = fun ezic_db_mnesia:rules/1,
|
DbModule=:= ezic_db_ets ->
|
||||||
get_all = fun ezic_db_mnesia:get_all/1,
|
DbModule:init(),
|
||||||
flatzone = fun ezic_db_mnesia:flatzone/2,
|
State = #state{mod = DbModule},
|
||||||
insert_all = fun ezic_db_mnesia:insert_all/1,
|
|
||||||
wipe = fun ezic_db_mnesia:wipe/1,
|
|
||||||
implementation = "mnesia"},
|
|
||||||
{ok, State};
|
|
||||||
init(_) ->
|
|
||||||
ezic_db_ets:init(),
|
|
||||||
State = #state{zones = fun ezic_db_ets:zones/1,
|
|
||||||
rules = fun ezic_db_ets:rules/1,
|
|
||||||
get_all = fun ezic_db_ets:get_all/1,
|
|
||||||
flatzone = fun ezic_db_ets:flatzone/2,
|
|
||||||
insert_all = fun ezic_db_ets:insert_all/1,
|
|
||||||
wipe = fun ezic_db_ets:wipe/1,
|
|
||||||
implementation = "ets"},
|
|
||||||
{ok, State}.
|
{ok, State}.
|
||||||
|
|
||||||
%%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
%%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
@@ -105,39 +92,40 @@ init(_) ->
|
|||||||
|
|
||||||
|
|
||||||
handle_call({zones, Name}, _, State) ->
|
handle_call({zones, Name}, _, State) ->
|
||||||
ZoneFun= State#state.zones,
|
Mod= State#state.mod,
|
||||||
Matches= ZoneFun(Name),
|
Matches= Mod:zones(Name),
|
||||||
{reply, Matches, State};
|
{reply, Matches, State};
|
||||||
handle_call({rules, Name}, _, State) ->
|
handle_call({rules, Name}, _, State) ->
|
||||||
RuleFun= State#state.rules,
|
Mod= State#state.mod,
|
||||||
Matches= RuleFun(Name),
|
Matches= Mod:rules(Name),
|
||||||
{reply, Matches, State};
|
{reply, Matches, State};
|
||||||
handle_call({all, Tab}, _, State) ->
|
handle_call({all, Tab}, _, State) ->
|
||||||
GetAllFun= State#state.get_all,
|
Mod= State#state.mod,
|
||||||
Matches= GetAllFun(Tab),
|
Matches= Mod:get_all(Tab),
|
||||||
{reply, Matches, State};
|
{reply, Matches, State};
|
||||||
handle_call({flatzone, Date, Name}, _, State) ->
|
handle_call({flatzone, Date, Name}, _, State) ->
|
||||||
FlatzoneFun= State#state.flatzone,
|
Mod= State#state.mod,
|
||||||
Result= FlatzoneFun(Date, Name),
|
Result= Mod:flatzone(Date, Name),
|
||||||
{reply, Result, State};
|
{reply, Result, State};
|
||||||
handle_call({insert_all, Records}, _, State) ->
|
handle_call({insert_all, Records}, _, State) ->
|
||||||
InsertAllFun= State#state.insert_all,
|
Mod= State#state.mod,
|
||||||
InsertAllFun(Records),
|
Mod:insert_all(Records),
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
handle_call({wipe, Tab}, _, State) ->
|
handle_call({wipe, Tab}, _, State) ->
|
||||||
WipeFun= State#state.wipe,
|
Mod= State#state.mod,
|
||||||
Result= WipeFun(Tab),
|
Result= Mod:wipe(Tab),
|
||||||
{reply, Result, State};
|
{reply, Result, State};
|
||||||
handle_call({flatten}, _, State) ->
|
handle_call({flatten}, _, State) ->
|
||||||
GetAllFun= State#state.get_all,
|
Mod= State#state.mod,
|
||||||
Zones= GetAllFun(zone),
|
Zones= Mod:get_all(zone),
|
||||||
Rules= GetAllFun(rule),
|
Rules= Mod:get_all(rule),
|
||||||
FlatZone= ezic_flatten:flatten(Zones, Rules),
|
FlatZone= ezic_flatten:flatten(Zones, Rules),
|
||||||
InsertAllFun= State#state.insert_all,
|
Result= Mod:insert_all(FlatZone),
|
||||||
Result= InsertAllFun(FlatZone),
|
|
||||||
{reply, Result, State};
|
{reply, Result, State};
|
||||||
handle_call({implementation}, _, State) ->
|
handle_call({implementation}, _, State) ->
|
||||||
{reply, State#state.implementation, State};
|
Mod= State#state.mod,
|
||||||
|
Impl= Mod:implementation(),
|
||||||
|
{reply, Impl, State};
|
||||||
handle_call(_, _, State) ->
|
handle_call(_, _, State) ->
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
||||||
|
|||||||
@@ -13,13 +13,17 @@
|
|||||||
, get_all/1
|
, get_all/1
|
||||||
, insert_all/1
|
, insert_all/1
|
||||||
, wipe/1
|
, wipe/1
|
||||||
|
, implementation/0
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
% PUBLIC API
|
% PUBLIC API
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
|
||||||
|
|
||||||
|
implementation() -> ?MODULE.
|
||||||
|
|
||||||
zones(TzName) ->
|
zones(TzName) ->
|
||||||
ets:select(zone, [{#zone{name=TzName, _='_'}, [], ['$_']}]).
|
ets:select(zone, [{#zone{name=TzName, _='_'}, [], ['$_']}]).
|
||||||
|
|
||||||
@@ -43,7 +47,11 @@ flatzone(Date, TzName) ->
|
|||||||
|
|
||||||
|
|
||||||
get_all(Tab) ->
|
get_all(Tab) ->
|
||||||
ets:lookup(Tab, Tab).
|
try ets:lookup(Tab, Tab) of X -> X
|
||||||
|
catch error:X -> {error, X}
|
||||||
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
insert_all(Records) ->
|
insert_all(Records) ->
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
, get_all/1
|
, get_all/1
|
||||||
, insert_all/1
|
, insert_all/1
|
||||||
, wipe/1
|
, wipe/1
|
||||||
|
, implementation/0
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-define(create(Record),
|
-define(create(Record),
|
||||||
@@ -23,6 +24,7 @@
|
|||||||
])).
|
])).
|
||||||
|
|
||||||
|
|
||||||
|
implementation() -> ?MODULE.
|
||||||
|
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
% READ - db reading methods
|
% READ - db reading methods
|
||||||
|
|||||||
Reference in New Issue
Block a user