summaryrefslogtreecommitdiff
path: root/src/db.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/db.erl')
-rw-r--r--src/db.erl29
1 files changed, 19 insertions, 10 deletions
diff --git a/src/db.erl b/src/db.erl
index 25cfc5e..91e379e 100644
--- a/src/db.erl
+++ b/src/db.erl
@@ -72,25 +72,26 @@ stop() ->
%%%%%%%%%%%%%%%%%%%%
%% Public API.
+-spec add(binary()) -> ok.
add(Entry) ->
gen_server:call(?MODULE, {add, Entry}).
-%% @doc Find entry with entryhash=Hash.
-find(entryhash, Hash) ->
- gen_server:call(?MODULE, {find, entryhash, Hash});
-%% @doc Find entry with mtlhash=Hash.
-find(mtlhash, Hash) ->
- gen_server:call(?MODULE, {find, mtlhash, Hash}).
-
-dump(Table) ->
- gen_server:call(?MODULE, {dump, Table}).
+%% @doc Find one entry.
+-spec find(entryhash | mtlhash | index, binary()) -> ht:mtl().
+find(Type, Hash) ->
+ gen_server:call(?MODULE, {find, Type, Hash}).
+-spec get_by_index(non_neg_integer(), non_neg_integer()) -> [ht:mtl()].
get_by_index(Start, End) ->
gen_server:call(?MODULE, {get_by_index, {Start, End}}).
+-spec get_by_index_sorted(non_neg_integer(), non_neg_integer()) -> [listht:mtl()].
get_by_index_sorted(Start, End) ->
gen_server:call(?MODULE, {get_by_index_sorted, {Start, End}}).
+%% Testing and debugging.
+dump(Table) ->
+ gen_server:call(?MODULE, {dump, Table}).
%%%%%%%%%%%%%%%%%%%%
%% gen_server callbacks.
@@ -108,18 +109,19 @@ terminate(_Reason, _State) ->
io:format("~p terminating~n", [?MODULE]),
ok.
-
%%%%%%%%%%%%%%%%%%%%
%% The meat.
handle_call(stop, _From, State) ->
{stop, normal, stopped, State};
+
handle_call({add, Entry}, _From, State) ->
F = fun() ->
mnesia:write(Entry)
end,
Res = mnesia:transaction(F),
{reply, Res, State};
+
handle_call({dump, Table}, _From, State) ->
F = fun() ->
Q = qlc:q([E || E <- mnesia:table(Table)]),
@@ -127,6 +129,7 @@ handle_call({dump, Table}, _From, State) ->
end,
Res = mnesia:transaction(F),
{reply, Res, State};
+
handle_call({find, entryhash, Hash}, _From, State) ->
{reply,
find_entry(fun() -> mnesia:index_read(plop, Hash, #plop.entryhash) end),
@@ -135,9 +138,15 @@ handle_call({find, mtlhash, Hash}, _From, State) ->
{reply,
find_entry(fun() -> mnesia:index_read(plop, Hash, #plop.mtlhash) end),
State};
+handle_call({find, index, Index}, _From, State) ->
+ {reply,
+ find_entry(fun() -> mnesia:read(plop, Index) end),
+ State};
+
handle_call({get_by_index, {Start, End}}, _From, State) ->
Res = [X || [_, X] <- select_index(Start, End)],
{reply, Res, State};
+
handle_call({get_by_index_sorted, {Start, End}}, _From, State) ->
%% FIXME: RAM hog -- how bad is it?
Res = [X || [_, X] <- lists:sort(select_index(Start, End))],