summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Nordberg <linus@nordberg.se>2014-05-26 10:21:26 +0200
committerLinus Nordberg <linus@nordberg.se>2014-05-26 10:21:26 +0200
commit44d52c56e68a2153f1b2266a55e73f641f8533d6 (patch)
treeab26c8bc8f34d683469f5cd3f144cee9bcb7cbf5
parent6f9598d777961f663422b5407c8ab81dc2169a71 (diff)
Add db:get_by_index/2.
-rw-r--r--src/db.erl16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/db.erl b/src/db.erl
index 8789757..50117e4 100644
--- a/src/db.erl
+++ b/src/db.erl
@@ -4,7 +4,7 @@
%% API.
-export([start_link/0, stop/0]).
-export([init_db/0, init_db/1, init_tables/0, init_tables/1]).
--export([add/1, find/1]).
+-export([add/1, find/1, get_by_index/2]).
%% API for testing.
-export([dump/1, destroy_tables/0, info_tables/0, dump_to_file/1]).
%% gen_server callbacks.
@@ -68,6 +68,9 @@ find(Hash) ->
dump(Table) ->
gen_server:call(?MODULE, {dump, Table}).
+get_by_index(Start, End) ->
+ gen_server:call(?MODULE, {get_by_index, {Start, End}}).
+
%%%%%%%%%%%%%%%%%%%%
handle_cast(_Request, State) ->
{noreply, State}.
@@ -108,4 +111,13 @@ handle_call({find, Hash}, _From, State) ->
1 -> hd(Result);
_ -> duplicate_hash_in_db % FIXME: log an error
end,
- {reply, Record, State}.
+ {reply, Record, State};
+handle_call({get_by_index, {Start, End}}, _From, State) ->
+ F = fun() ->
+ MatchHead = #plop{index = '$1', mtl = '$2', _ = '_'},
+ Guard = [{'>=', '$1', Start}, {'=<', '$1', End}],
+ Result = ['$2'],
+ mnesia:select(plop, [{MatchHead, Guard, Result}])
+ end,
+ Res = mnesia:transaction(F),
+ {reply, Res, State}.