summaryrefslogtreecommitdiff
path: root/src/db.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/db.erl')
-rw-r--r--src/db.erl38
1 files changed, 36 insertions, 2 deletions
diff --git a/src/db.erl b/src/db.erl
index 6fce8a3..413f4b9 100644
--- a/src/db.erl
+++ b/src/db.erl
@@ -6,8 +6,8 @@
%% 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, get_by_entry_hash/1, entry_for_leafhash/1, leafhash_for_index/1]).
%% gen_server callbacks.
-export([init/1, handle_call/3, terminate/2, handle_cast/2, handle_info/2,
code_change/3]).
@@ -34,6 +34,22 @@ stop() ->
add(LeafHash, EntryHash, Data, Index) ->
gen_server:call(?MODULE, {add, {LeafHash, EntryHash, Data, Index}}).
+-spec add(binary(), binary()) -> ok.
+add(LeafHash, Data) ->
+ gen_server:call(?MODULE, {add, {LeafHash, Data}}).
+
+-spec add_entryhash(binary(), binary()) -> ok.
+add_entryhash(LeafHash, EntryHash) ->
+ gen_server:call(?MODULE, {add_entryhash, {LeafHash, EntryHash}}).
+
+-spec add_index(binary(), non_neg_integer()) -> ok.
+add_index(LeafHash, Index) ->
+ gen_server:call(?MODULE, {add_index, {LeafHash, Index}}).
+
+-spec set_treesize(non_neg_integer()) -> ok.
+set_treesize(Size) ->
+ gen_server: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}) ->
@@ -143,6 +159,24 @@ handle_call({add, {LeafHash, EntryHash, Data, Index}}, _From, State) ->
ok = atomic:replacefile(treesize_path(), integer_to_list(Index+1)),
{reply, ok, State};
+handle_call({add, {LeafHash, Data}}, _From, State) ->
+ ok = perm:ensurefile(entry_root_path(), LeafHash, Data),
+ {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_list(Size)),
+ {reply, ok, State};
+
handle_call({get_by_indices, {Start, End, _Sorted}}, _From, State) ->
{reply, get_by_indices_helper(Start, End), State};