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

Commit

Permalink
working implementation of mnesia and ets based ezic.
Browse files Browse the repository at this point in the history
  • Loading branch information
lsowen committed Dec 5, 2012
1 parent 2866c9c commit e535590
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/ezic.app.src
@@ -1,11 +1,11 @@
{application, ezic,
[
{registered, []},
{vsn, "0.1.0"},
{vsn, "0.2.0"},
{applications, [
kernel,
stdlib
]},
{mod, {ezic_app, []}},
{mod, {ezic_app, ezic_db_ets}},
{env, [{db_dir, "db"}]}
]}.
59 changes: 46 additions & 13 deletions src/ezic_db.erl
Expand Up @@ -14,10 +14,11 @@
, insert_all/1
, wipe/1
, flatten/0
, get_implementation/0
]).

-export([
start_link/0
start_link/1
, init/1
, code_change/3
, handle_call/3
Expand All @@ -27,6 +28,8 @@
]).


-record(state, {zones, rules, get_all, flatzone, insert_all, wipe, implementation}).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PUBLIC API
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -63,48 +66,78 @@ wipe(Tab) ->
flatten() ->
gen_server:call(?MODULE, {flatten}).

get_implementation() ->
gen_server:call(?MODULE, {implementation}).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% GEN_SERVER
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
start_link(StartArgs) ->
gen_server:start_link({local, ?MODULE}, ?MODULE, StartArgs, []).

init(ezic_db_mnesia) ->
ezic_db_mnesia:init(),
State = #state{zones = fun ezic_db_mnesia:zones/1,
rules = fun ezic_db_mnesia:rules/1,
get_all = fun ezic_db_mnesia:get_all/1,
flatzone = fun ezic_db_mnesia:flatzone/2,
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(),
{ok, []}.
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}.

%%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%% DB Lookup
%%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


handle_call({zones, Name}, _, State) ->
Matches= ezic_db_ets:zones(Name),
ZoneFun= State#state.zones,
Matches= ZoneFun(Name),
{reply, Matches, State};
handle_call({rules, Name}, _, State) ->
Matches= ezic_db_ets:rules(Name),
RuleFun= State#state.rules,
Matches= RuleFun(Name),
{reply, Matches, State};
handle_call({all, Tab}, _, State) ->
Matches= ezic_db_ets:get_all(Tab),
GetAllFun= State#state.get_all,
Matches= GetAllFun(Tab),
{reply, Matches, State};
handle_call({flatzone, Date, Name}, _, State) ->
Result= ezic_db_ets:flatzone(Date, Name),
FlatzoneFun= State#state.flatzone,
Result= FlatzoneFun(Date, Name),
{reply, Result, State};
handle_call({insert_all, Records}, _, State) ->
ezic_db_ets:insert_all(Records),
InsertAllFun= State#state.insert_all,
InsertAllFun(Records),
{noreply, State};
handle_call({wipe, Tab}, _, State) ->
Result= ezic_db_ets:wipe(Tab),
WipeFun= State#state.wipe,
Result= WipeFun(Tab),
{reply, Result, State};
handle_call({flatten}, _, State) ->
Zones= ezic_db_ets:get_all(zone),
Rules= ezic_db_ets:get_all(rule),
GetAllFun= State#state.get_all,
Zones= GetAllFun(zone),
Rules= GetAllFun(rule),
FlatZone= ezic_flatten:flatten(Zones, Rules),
Result= ezic_db_ets:insert_all(FlatZone),
InsertAllFun= State#state.insert_all,
Result= InsertAllFun(FlatZone),
{reply, Result, State};
handle_call({implementation}, _, State) ->
{reply, State#state.implementation, State};
handle_call(_, _, State) ->
{noreply, State}.

Expand Down
1 change: 1 addition & 0 deletions src/ezic_db_mnesia.erl
Expand Up @@ -127,6 +127,7 @@ create_tabs(ok) ->
insert_all(Zones),
insert_all(Rules),
FlatZones = ezic_flatten:flatten(Zones, Rules),
io:format("~p~n", [FlatZones]),
insert_all(FlatZones),

ok;
Expand Down
2 changes: 1 addition & 1 deletion src/ezic_flatten.erl
Expand Up @@ -143,7 +143,7 @@ flatten_zone_set(FromTimeStub=#flatzone{utc_from=UTCFrom, dstoffset=DSTOffset}

%% we gather all rules that _may_ apply
%Rules= ezic_db:rules(RuleName),
Rules= [R || R <- AllRules, R = #rule{name = RuleName}],
Rules= [R || R <- AllRules, R#rule.name =:= RuleName ],

?debugVal(FromTime),
?debugVal(Rules),
Expand Down
10 changes: 5 additions & 5 deletions src/ezic_sup.erl
Expand Up @@ -24,8 +24,8 @@
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
%% Description: Starts the supervisor
%%--------------------------------------------------------------------
start_link(_) ->
supervisor:start_link(?MODULE, []).
start_link(StartArgs) ->
supervisor:start_link(?MODULE, StartArgs).

%%====================================================================
%% Supervisor callbacks
Expand All @@ -39,9 +39,9 @@ start_link(_) ->
%% to find out about restart strategy, maximum restart frequency and child
%% specifications.
%%--------------------------------------------------------------------
init(_) ->
EzicDb = {ezic_db,{ezic_db, start_link, []},
permanent,2000,worker,[ezic_db_ets]},
init(StartArgs) ->
EzicDb = {ezic_db,{ezic_db, start_link, [StartArgs]},
permanent,2000,worker,[ezic_db]},
{ok,{{one_for_all,3,30}, [EzicDb]}}.

%%====================================================================
Expand Down

0 comments on commit e535590

Please sign in to comment.