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

Commit

Permalink
Much cleaner, but not working, because I have a circular loop
Browse files Browse the repository at this point in the history
  • Loading branch information
lsowen committed Dec 5, 2012
1 parent 676e112 commit 4f3364a
Show file tree
Hide file tree
Showing 10 changed files with 344 additions and 362 deletions.
30 changes: 15 additions & 15 deletions src/ezic.erl
Expand Up @@ -36,7 +36,7 @@ localtime(TzName) ->

utc_to_local(UTCDatetime, TzName) ->
NormalDatetime= ezic_date:normalize(UTCDatetime, u),
#flatzone{offset=Offset, dstoffset=DSTOffset}= ezic_db_ets:flatzone(NormalDatetime, TzName),
#flatzone{offset=Offset, dstoffset=DSTOffset}= ezic_db:flatzone(NormalDatetime, TzName),

ezic_date:add_offset(
ezic_date:add_offset(
Expand All @@ -47,7 +47,7 @@ utc_to_local(UTCDatetime, TzName) ->

local_to_utc(LocalDatetime, TzName) ->
NormalDatetime= ezic_date:normalize(LocalDatetime, w),
#flatzone{offset=Offset, dstoffset=DSTOffset}= ezic_db_ets:flatzone(NormalDatetime, TzName),
#flatzone{offset=Offset, dstoffset=DSTOffset}= ezic_db:flatzone(NormalDatetime, TzName),

ezic_date:add_offset(
ezic_date:add_offset(
Expand Down Expand Up @@ -84,7 +84,7 @@ dev() ->
% ezic_flatten:flatten_all_zones(Zones),

application:start(ezic),
ezic_db_ets:flatzone({{2010,11,17}, #tztime{time={23,42,0}}}, "America/Los_Angeles"),
ezic_db:flatzone({{2010,11,17}, #tztime{time={23,42,0}}}, "America/Los_Angeles"),

ok.

Expand All @@ -94,8 +94,8 @@ zf() ->


reflatten() ->
ezic_db_ets:wipe(flatzone),
ezic_db_ets:flatten().
ezic_db:wipe(flatzone),
ezic_db:flatten().



Expand All @@ -113,20 +113,20 @@ test() ->
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


current_as_of_utc(UTCDatetime, TzName) ->
CZone= ezic_zone:current_as_of_utc(UTCDatetime, TzName),
RuleName= CZone#zone.rule,
Rules= ezic_db_ets:rules(RuleName),
CRule= ezic_rule:current_as_of_utc(UTCDatetime, Rules),
{ok, CZone, CRule}.
%current_as_of_utc(UTCDatetime, TzName) ->
% CZone= ezic_zone:current_as_of_utc(UTCDatetime, TzName),
% RuleName= CZone#zone.rule,
% Rules= ezic_db:rules(RuleName),
% CRule= ezic_rule:current_as_of_utc(UTCDatetime, Rules),
% {ok, CZone, CRule}.


%%current_as_of_local(Datetime, TzName) ->
%% not_done.


time_offset(Zone, Rule) ->
OffsetSec= ezic_zone:offset_sec(Zone),
DSTSec= ezic_rule:dst_sec(Rule),
OffsetSec + DSTSec.
%time_offset(Zone, Rule) ->
% OffsetSec= ezic_zone:offset_sec(Zone),
% DSTSec= ezic_rule:dst_sec(Rule),
% OffsetSec + DSTSec.

2 changes: 1 addition & 1 deletion src/ezic_date.erl
Expand Up @@ -251,7 +251,7 @@ when is_integer(Y1), is_integer(Y2)
compare({Date1, #tztime{time=Time1, flag=F}}
, {Date2, #tztime{time=Time2, flag=F}}) ->

Date1 =< Date2 orelse Time1 =< time2;
Date1 =< Date2 orelse Time1 =< Time2;

compare(X,Y) ->
erlang:error(bad_dates, [X,Y]).
Expand Down
202 changes: 89 additions & 113 deletions src/ezic_db.erl
@@ -1,149 +1,125 @@
-module(ezic_db).
-include("include/ezic.hrl").
-include_lib("stdlib/include/qlc.hrl").
-include_lib("eunit/include/eunit.hrl").

-behaviour(gen_server).


-export([
zones/1
, rules/1
, flatzone/2
%, insert/2
, get_all/1
, insert_all/1
, wipe/1
, flatten/0
]).

-export([
wipe/0
, wipe/1
, init/0
, insert_all/1
, get_all/1
]).
start_link/0
, init/1
, code_change/3
, handle_call/3
, handle_cast/2
, handle_info/2
, terminate/2
]).


-define(create(Record),
{atomic, ok} = mnesia:create_table(Record,
[{type, bag}
, {disc_copies, [node()]}
, {attributes, record_info(fields, Record)}
])).



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% READ - db reading methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



% retrieve all zones by name
zones(Name) ->
F = fun()->
Q = qlc:q([Z || Z=#zone{name=N} <- mnesia:table(zone), N=:=Name]),
qlc:e(Q)
end,
{atomic, Zones}= mnesia:transaction(F),
Zones.

% retrieve all rules by name
rules(Name) ->
F = fun()->
Q = qlc:q([R || R=#rule{name=N} <- mnesia:table(rule), N=:=Name]),
qlc:e(Q)
end,
{atomic, Rules}= mnesia:transaction(F),
Rules.

% get all records from table
get_all(Tab) when is_atom(Tab) ->
F = fun() ->
Q = qlc:q([R || R<- mnesia:table(Tab)]),
qlc:e(Q)
end,
{atomic, Ret}= mnesia:transaction(F),
Ret.

%% get a flatzone for a specific date
%% Date :: {date(), #tztime{}}
%% returns #flatzone{}
%% or throws either error:
%% * {ambiguous_zone, [Z1,Z2,...]}
%% * {no_zone}
flatzone(Date, TzName) ->
%% @todo validate Date
F = fun()->
Q = qlc:q(
[Fz || Fz=#flatzone{tzname=N} <- mnesia:table(flatzone)
, N=:=TzName
, ezic_flatten:contains_date(Fz, Date)]),
qlc:e(Q)
end,
{atomic, FlatZones}= mnesia:transaction(F),
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PUBLIC API
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

case length(FlatZones) of
1 ->
hd(FlatZones);
2 ->
erlang:error(ambiguous_zone, FlatZones);
0 ->
erlang:error(no_zone);
_ ->
erlang:error(should_not_happen, {FlatZones, Date, TzName})
end.


zones(TzName) ->
gen_server:call(?MODULE, {zones, TzName}).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ADMIN - initialization and administration methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rules(TzName) ->
gen_server:call(?MODULE, {rules, TzName}).

% initialize the db
init() ->
create_tabs(mnesia:create_schema([node()])),
mnesia:wait_for_tables([rule, zone, link, leap, flatzone], 3000).

flatzone(Date, TzName) ->
gen_server:call(?MODULE, {flatzone, Date, TzName}).


% WARNING: deletes all db files
wipe() ->
mnesia:stop(),
mnesia:delete_schema([node()]).
get_all(Tab) ->
gen_server:call(?MODULE, {all, Tab}).


insert_all(Records) ->
gen_server:call(?MODULE, {insert_all, Records}).


%wipe() ->
% gen_server:call(?MODULE, {wipe}).


% WARNING: deletes all entries in table Tab
wipe(Tab) ->
mnesia:transaction(
fun()->
mnesia:delete(Tab, '_', write)
end).
gen_server:call(?MODULE, {wipe, Tab}).


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

create_tabs(ok) ->
mnesia:start(),

?create(rule),
?create(zone),
?create(link),
?create(leap),
?create(flatzone),
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% GEN_SERVER
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

ok;
create_tabs({error, {_, {already_exists,_}}}) ->
mnesia:start(),
ok;
create_tabs(E) ->
?debugVal(E).

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

insert_all(Records) ->
mnesia:transaction(
fun() ->
lists:foreach(
fun(R)-> mnesia:write(R) end,
Records)
end).

init(_) ->
ezic_db_ets:init(),
{ok, []}.

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


handle_call({zones, Name}, _, State) ->
Matches= ezic_db_ets:zones(Name),
{reply, Matches, State};
handle_call({rules, Name}, _, State) ->
Matches= ezic_db_ets:rules(Name),
{reply, Matches, State};
handle_call({all, Tab}, _, State) ->
Matches= ezic_db_ets:get_all(Tab),
{reply, Matches, State};
handle_call({flatzone, Date, Name}, _, State) ->
Result= ezic_db_ets:flatzone(Date, Name),
{reply, Result, State};
handle_call({insert_all, Records}, _, State) ->
ezic_db_ets:insert_all(Records),
{noreply, State};
handle_call({wipe, Tab}, _, State) ->
Result= ezic_db_ets:wipe(Tab),
{reply, Result, State};
handle_call({flatten}, _, State) ->
Result= ezic_flatten:flatten(),
{reply, Result, State};
handle_call(_, _, State) ->
{noreply, State}.


%%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%% Other gen_server
%%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


code_change(_, State, _) ->
{ok, State}.


handle_cast(_, State) ->
{noreply, State}.

handle_info(_, State) ->
{noreply, State}.

terminate(_, _State) ->
ok.

0 comments on commit 4f3364a

Please sign in to comment.