diff options
author | Linus Nordberg <linus@nordberg.se> | 2014-09-19 18:11:37 +0200 |
---|---|---|
committer | Linus Nordberg <linus@nordberg.se> | 2014-09-19 18:11:37 +0200 |
commit | 209fc7ed4d44f0d613aabdb9b1c59b8621dc1339 (patch) | |
tree | bf97a5a7336e7139a3157d7aab7b14ede4a80049 /src/db.erl | |
parent | c2d7fcc2e1274c148f0ac94436c138022390ffc4 (diff) |
Fix crash in inclusion() and add inclusion-and-leaf().
Diffstat (limited to 'src/db.erl')
-rw-r--r-- | src/db.erl | 29 |
1 files changed, 19 insertions, 10 deletions
@@ -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))], |