More implementation.

Modified intent of ts_db_records. timestamp value is no longer a
  {Date, Time} value, it is now gregorian seconds since year 0.
  This was done to make queries that are based on date comparisons
  easier to write and more efficient.
Added date time comparison function to ts_common.
Implemented update/1 and list/3 for ts_entry.
This commit is contained in:
Jonathan Bernard
2011-01-28 16:57:15 -06:00
parent 495336fc58
commit c04b73d9cc
4 changed files with 61 additions and 6 deletions

View File

@ -17,3 +17,41 @@ new(ER = #ts_entry()) ->
ok = mnesia:write(NewRow),
NewRow end),
{ok, NewRow}.
update(ER = #ts_entry()) ->
% look for existing record
case mnesia:dirty_read(ts_entry, ER#ts_entry.ref) of
% record does not exist
[] -> no_record;
% record exists, update it
[_Record] -> mnesia:dirty_write(ER)
end.
list({Username, Timeline}, Start, Length)
when is_integer(Start) and is_integer(Length) ->
ts_common:list(
qlc:q([E || E <- mnesia:table(ts_entry),
E#ts_entry.ref =:= {Username, Timeline, _}]_,
Start, Length).
list({Username, Timeline}, StartDateTime, EndDateTime) ->
{atomic, Entries} = mnesia:transaction(fun() ->
StartSeconds = calendar:datetime_to_gregorian_seconds(StartDateTime),
EndSeconds = calendar:datetime_to_gregorian_seconds(EndDateTime),
% create the query that will select these records
Q = qlc:q([E || E <- mnesia:table(ts_entry),
E#ts_entry.timestamp >= StartSeconds,
E#ts_entry.timestamp < EndSeconds]),
% sort by timestamp
SortedQ = qlc:sort(Q, {order, fun(A, B) ->
A#ts_entry.timestamp > B#ts_entry.timestamp end}),
% return
qlc:e(SortedQ)
end),
Entries.