summaryrefslogtreecommitdiff
path: root/src/db.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/db.erl')
-rw-r--r--src/db.erl85
1 files changed, 70 insertions, 15 deletions
diff --git a/src/db.erl b/src/db.erl
index a0855b9..943c70e 100644
--- a/src/db.erl
+++ b/src/db.erl
@@ -6,18 +6,24 @@
%% API.
-export([start_link/0, stop/0]).
--export([add/4, size/0]).
--export([get_by_index/1, get_by_indices/3, get_by_leaf_hash/1, get_by_entry_hash/1]).
+-export([add/4, add/2, add_entryhash/2, add_index/2, set_treesize/1, size/0]).
+-export([get_by_index/1, get_by_indices/3, get_by_leaf_hash/1]).
+-export([get_by_entry_hash/1, entry_for_leafhash/1, leafhash_for_index/1]).
+-export([leafhash_for_indices/2, indexsize/0]).
%% gen_server callbacks.
-export([init/1, handle_call/3, terminate/2, handle_cast/2, handle_info/2,
code_change/3]).
+-import(stacktrace, [call/2]).
-include_lib("stdlib/include/qlc.hrl").
-include("db.hrl").
size() ->
binary_to_integer(atomic:readfile(treesize_path())).
+indexsize() ->
+ index:indexsize(index_path()).
+
init(_Args) ->
{ok, []}.
@@ -25,34 +31,50 @@ start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
stop() ->
- gen_server:call(?MODULE, stop).
+ call(?MODULE, stop).
%%%%%%%%%%%%%%%%%%%%
%% Public API.
-spec add(binary(), binary(), binary(), non_neg_integer()) -> ok.
add(LeafHash, EntryHash, Data, Index) ->
- gen_server:call(?MODULE, {add, {LeafHash, EntryHash, Data, Index}}).
+ call(?MODULE, {add, {LeafHash, EntryHash, Data, Index}}).
+
+-spec add(binary(), binary()) -> ok.
+add(LeafHash, Data) ->
+ call(?MODULE, {add, {LeafHash, Data}}).
+
+-spec add_entryhash(binary(), binary()) -> ok.
+add_entryhash(LeafHash, EntryHash) ->
+ call(?MODULE, {add_entryhash, {LeafHash, EntryHash}}).
+
+-spec add_index(binary(), non_neg_integer()) -> ok.
+add_index(LeafHash, Index) ->
+ call(?MODULE, {add_index, {LeafHash, Index}}).
+
+-spec set_treesize(non_neg_integer()) -> ok.
+set_treesize(Size) ->
+ call(?MODULE, {set_treesize, Size}).
-spec get_by_indices(integer(), integer(), {sorted, true|false}) ->
[{non_neg_integer(), binary(), binary()}].
get_by_indices(Start, End, {sorted, Sorted}) ->
- gen_server:call(?MODULE, {get_by_indices, {Start, End, Sorted}}).
+ call(?MODULE, {get_by_indices, {Start, End, Sorted}}).
-spec get_by_index(binary()) -> notfound |
{non_neg_integer(), binary(), binary()}.
get_by_index(Index) ->
- gen_server:call(?MODULE, {get_by_index, Index}).
+ call(?MODULE, {get_by_index, Index}).
-spec get_by_leaf_hash(binary()) -> notfound |
{non_neg_integer(), binary(), binary()}.
get_by_leaf_hash(LeafHash) ->
- gen_server:call(?MODULE, {get_by_leaf_hash, LeafHash}).
+ call(?MODULE, {get_by_leaf_hash, LeafHash}).
-spec get_by_entry_hash(binary()) -> notfound |
{non_neg_integer(), binary(), binary()}.
get_by_entry_hash(EntryHash) ->
- gen_server:call(?MODULE, {get_by_entry_hash, EntryHash}).
+ call(?MODULE, {get_by_entry_hash, EntryHash}).
%%%%%%%%%%%%%%%%%%%%
%% gen_server callbacks.
@@ -103,7 +125,12 @@ entry_for_leafhash(LeafHash) ->
perm:readfile(entry_root_path(), LeafHash).
index_for_leafhash(LeafHash) ->
- binary_to_integer(perm:readfile(indexforhash_root_path(), LeafHash)).
+ case perm:readfile(indexforhash_root_path(), LeafHash) of
+ noentry ->
+ noentry;
+ Index ->
+ binary_to_integer(Index)
+ end.
leafhash_for_index(Index) ->
index:get(index_path(), Index).
@@ -137,7 +164,27 @@ handle_call({add, {LeafHash, EntryHash, Data, Index}}, _From, State) ->
ok = perm:ensurefile(indexforhash_root_path(),
LeafHash, integer_to_binary(Index)),
ok = index:add(index_path(), Index, LeafHash),
- ok = atomic:replacefile(treesize_path(), integer_to_list(Index+1)),
+ ok = atomic:replacefile(treesize_path(), integer_to_binary(Index+1)),
+ {reply, ok, State};
+
+handle_call({add, {LeafHash, Data}}, _From, State) ->
+ lager:debug("add leafhash ~p", [LeafHash]),
+ ok = perm:ensurefile(entry_root_path(), LeafHash, Data),
+ lager:debug("leafhash ~p added", [LeafHash]),
+ {reply, ok, State};
+
+handle_call({add_entryhash, {LeafHash, EntryHash}}, _From, State) ->
+ ok = perm:ensurefile(entryhash_root_path(), EntryHash, LeafHash),
+ {reply, ok, State};
+
+handle_call({add_index, {LeafHash, Index}}, _From, State) ->
+ ok = perm:ensurefile(indexforhash_root_path(),
+ LeafHash, integer_to_binary(Index)),
+ ok = index:add(index_path(), Index, LeafHash),
+ {reply, ok, State};
+
+handle_call({set_treesize, Size}, _From, State) ->
+ ok = atomic:replacefile(treesize_path(), integer_to_binary(Size)),
{reply, ok, State};
handle_call({get_by_indices, {Start, End, _Sorted}}, _From, State) ->
@@ -150,9 +197,17 @@ handle_call({get_by_index, Index}, _From, State) ->
{reply, R, State};
handle_call({get_by_leaf_hash, LeafHash}, _From, State) ->
- Entry = entry_for_leafhash(LeafHash),
- Index = index_for_leafhash(LeafHash),
- R = {Index, LeafHash, Entry},
+ R = case entry_for_leafhash(LeafHash) of
+ noentry ->
+ notfound;
+ Entry ->
+ case index_for_leafhash(LeafHash) of
+ noentry ->
+ notfound;
+ Index ->
+ {Index, LeafHash, Entry}
+ end
+ end,
{reply, R, State};
handle_call({get_by_entry_hash, EntryHash}, _From, State) ->
@@ -161,7 +216,7 @@ handle_call({get_by_entry_hash, EntryHash}, _From, State) ->
notfound;
LeafHash ->
Entry = entry_for_leafhash(LeafHash),
- Index = index_for_leafhash(LeafHash),
- {Index, LeafHash, Entry}
+ %% Don't fetch index, isn't used and might not exist
+ {notfetched, LeafHash, Entry}
end,
{reply, R, State}.