moving from mnesia to ets. matchspecs are complicated when dates and atoms are involved.

This commit is contained in:
aj
2010-11-18 00:28:50 -08:00
parent 63c8a29198
commit a90d6246ec
10 changed files with 433 additions and 45 deletions

101
src/ezic_db_ets.erl Normal file
View File

@@ -0,0 +1,101 @@
-module(ezic_db_ets).
-include("include/ezic.hrl").
-include_lib("eunit/include/eunit.hrl").
-behaviour(gen_server).
-export([
zones/1
, rules/1
, flatzone/2
, all/1
]).
-export([
start_link/0
, init/1
, code_change/3
, handle_call/3
, handle_cast/2
, handle_info/2
, terminate/2
]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PUBLIC API
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
zones(TzName) ->
gen_server:call(?MODULE, {zones, TzName}).
rules(TzName) ->
gen_server:call(?MODULE, {rules, TzName}).
flatzone(Date, TzName) ->
gen_server:call(?MODULE, {flatzone, Date, TzName}).
all(Tab) ->
gen_server:call(?MODULE, {all, Tab}).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% GEN_SERVER
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
init(_) ->
ezic_db_ets_admin:init().
%%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%% DB Lookup
%%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
handle_call({zones, Name}, _, Ets) ->
Matches= ets:select(Ets, [{#zone{name=Name, _='_'}, [], ['$_']}]),
{reply, Matches, Ets};
handle_call({rules, Name}, _, Ets) ->
Matches= ets:select(Ets, [{#rule{name=Name, _='_'}, [], ['$_']}]),
{reply, Matches, Ets};
handle_call({all, Table}, _, Ets) ->
Matches= ets:lookup(Ets, Table),
{reply, Matches, Ets};
handle_call({flatzone, Date, Name}, _, Ets) ->
Matches= ets:select(Ets, ezic_flatten:ms(Date, Name)),
{reply, Matches, Ets};
handle_call(_, _, Ets) ->
{noreply, Ets}.
%%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%% Other gen_server
%%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
code_change(_, State, _) ->
{ok, State}.
handle_cast(_, State) ->
{noreply, State}.
handle_info(_, State) ->
{noreply, State}.
terminate(_, State) ->
ok.