From 44d52c56e68a2153f1b2266a55e73f641f8533d6 Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Mon, 26 May 2014 10:21:26 +0200 Subject: Add db:get_by_index/2. --- src/db.erl | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/db.erl') 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}. -- cgit v1.1