summaryrefslogtreecommitdiff
path: root/src/db.erl
diff options
context:
space:
mode:
authorLinus Nordberg <linus@nordberg.se>2014-09-20 15:33:06 +0200
committerLinus Nordberg <linus@nordberg.se>2014-09-20 15:33:06 +0200
commit4f1a715a3f2a6b049dcb9dcc5b38f6f2716e4101 (patch)
tree035e8e4e93da8629de125d5036b1f5c8c3b0b6cb /src/db.erl
parent209fc7ed4d44f0d613aabdb9b1c59b8621dc1339 (diff)
Store and retrieve extra-data in/from db.
Also, add more specs to db.
Diffstat (limited to 'src/db.erl')
-rw-r--r--src/db.erl19
1 files changed, 11 insertions, 8 deletions
diff --git a/src/db.erl b/src/db.erl
index 91e379e..3ab2d1b 100644
--- a/src/db.erl
+++ b/src/db.erl
@@ -72,20 +72,22 @@ stop() ->
%%%%%%%%%%%%%%%%%%%%
%% Public API.
--spec add(binary()) -> ok.
+-spec add(plop()) -> {atomic, ok}.
add(Entry) ->
gen_server:call(?MODULE, {add, Entry}).
%% @doc Find one entry.
--spec find(entryhash | mtlhash | index, binary()) -> ht:mtl().
+-spec find(entryhash | mtlhash | index, binary()) ->
+ [] | plop() | duplicate_hash_in_db.
find(Type, Hash) ->
gen_server:call(?MODULE, {find, Type, Hash}).
--spec get_by_index(non_neg_integer(), non_neg_integer()) -> [ht:mtl()].
+-spec get_by_index(non_neg_integer(), non_neg_integer()) -> [{mtl(), binary()}].
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()].
+-spec get_by_index_sorted(non_neg_integer(), non_neg_integer()) ->
+ [{mtl(), binary()}].
get_by_index_sorted(Start, End) ->
gen_server:call(?MODULE, {get_by_index_sorted, {Start, End}}).
@@ -144,12 +146,12 @@ handle_call({find, index, Index}, _From, State) ->
State};
handle_call({get_by_index, {Start, End}}, _From, State) ->
- Res = [X || [_, X] <- select_index(Start, End)],
+ Res = [{MTL, Extra} || [_Index, MTL, Extra] <- 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))],
+ Res = [{MTL, Extra} || [_Index, MTL, Extra] <- lists:sort(select_index(Start, End))],
{reply, Res, State}.
%%%%%%%%%%%%%%%%%%%%
@@ -157,8 +159,8 @@ handle_call({get_by_index_sorted, {Start, End}}, _From, State) ->
select_index(Start, End) ->
F = fun() ->
- %% Get index and mtl.
- MatchHead = {plop, '$1', '_', '_', '$2', '_'},
+ %% Get index, mtl and extra_data.
+ MatchHead = {plop, '$1', '_', '_', '$2', '$3', '_'},
Guard = [{'>=', '$1', Start}, {'=<', '$1', End}],
Result = ['$$'],
mnesia:select(plop, [{MatchHead, Guard, Result}])
@@ -166,6 +168,7 @@ select_index(Start, End) ->
{atomic, Res} = mnesia:transaction(F),
Res.
+-spec find_entry(fun()) -> [] | plop() | duplicate_hash_in_db.
find_entry(Fun) ->
{atomic, Result} = mnesia:transaction(Fun),
case length(Result) of