Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to single parameterized datastore module
- Work around runtime limitations of records with helper functions for fields, queries and prestore - Retarget client code at single module - Remove separate data store modules - Remove use of functions in includes - Thank you Zed and Adam for StackOverflow help: http://stackoverflow.com/questions/1550850/how-do-you-debug-functions-from-includes-in-erlang
- Loading branch information
Showing
12 changed files
with
103 additions
and
78 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
-module (scrumjet_datastore). | ||
|
||
-include("scrumjet.hrl"). | ||
-include_lib("stdlib/include/qlc.hrl"). | ||
|
||
-export([start/0, store/1, find/2, range/3]). | ||
|
||
-define (TYPES, [scrumjet_task, scrumjet_category, scrumjet_board]). | ||
|
||
%% Internal Helper Functions | ||
|
||
fields(scrumjet_task) -> record_info(fields, scrumjet_task); | ||
fields(scrumjet_category) -> record_info(fields, scrumjet_category); | ||
fields(scrumjet_board) -> record_info(fields, scrumjet_board). | ||
|
||
prestore(Record=#scrumjet_task{ctime=undefined}) -> | ||
Now = erlang:now(), | ||
Record#scrumjet_task{ctime=Now, mtime=Now}; | ||
prestore(Record=#scrumjet_category{ctime=undefined}) -> | ||
Now = erlang:now(), | ||
Record#scrumjet_category{ctime=Now, mtime=Now}; | ||
prestore(Record=#scrumjet_board{ctime=undefined}) -> | ||
Now = erlang:now(), | ||
Record#scrumjet_board{ctime=Now, mtime=Now}; | ||
prestore(Record=#scrumjet_task{}) -> | ||
Now = erlang:now(), | ||
Record#scrumjet_task{mtime=Now}; | ||
prestore(Record=#scrumjet_category{}) -> | ||
Now = erlang:now(), | ||
Record#scrumjet_category{mtime=Now}; | ||
prestore(Record=#scrumjet_board{}) -> | ||
Now = erlang:now(), | ||
Record#scrumjet_board{mtime=Now}. | ||
|
||
select(scrumjet_task, {id, Id}) -> | ||
qlc:q([M || M <- mnesia:table(scrumjet_task), | ||
M#scrumjet_task.id =:= Id]); | ||
select(scrumjet_category, {id, Id}) -> | ||
qlc:q([M || M <- mnesia:table(scrumjet_category), | ||
M#scrumjet_category.id =:= Id]); | ||
select(scrumjet_board, {id, Id}) -> | ||
qlc:q([M || M <- mnesia:table(scrumjet_board), | ||
M#scrumjet_board.id =:= Id]). | ||
|
||
create_table(Type) -> | ||
try | ||
set = mnesia:table_info(Type, type) | ||
catch | ||
exit:{aborted, {no_exists, Type, type}} -> | ||
{atomic, ok} = mnesia:create_table(Type, | ||
[{attributes, fields(Type)}, | ||
{type, set}, | ||
{disc_copies, [node()]}]) | ||
end. | ||
|
||
%% Public API Functions | ||
|
||
start() -> [create_table(Type) || Type <- ?TYPES]. | ||
|
||
store(Record) -> | ||
F = fun() -> | ||
mnesia:write(prestore(Record)) | ||
end, | ||
mnesia:transaction(F). | ||
|
||
find(Type, Params) -> | ||
F = fun() -> | ||
Query = select(Type, Params), | ||
qlc:eval(Query) | ||
end, | ||
{atomic, Records} = mnesia:transaction(F), | ||
Records. | ||
|
||
range(Type, Start, End) -> | ||
F = fun() -> | ||
mnesia_ext:limit(Type, Start, End - Start) | ||
end, | ||
{atomic, Records} = mnesia:transaction(F), | ||
Records. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters