From e84b362eb7f5dbdea44c811534521f89707f66b4 Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Thu, 18 Sep 2014 13:00:44 +0200 Subject: Add field 'mtlhash' to the database, for get-proof-by-hash. Also, in db: Add field 'mtlhash' to record 'plop'. Rename 'hash' -> 'entryhash'. Add leaf_hash(), calculating a leaf hash from data. Fix a bug where print_tree() print half a byte of the hashes. Rename tree_hash() -> root(). Closes CATLFISH-3. --- src/db.erl | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) (limited to 'src/db.erl') diff --git a/src/db.erl b/src/db.erl index 8f398cb..25cfc5e 100644 --- a/src/db.erl +++ b/src/db.erl @@ -7,7 +7,7 @@ %% API. -export([start_link/0, stop/0]). -export([init_db/0, init_db/1, init_tables/0, init_tables/1]). --export([add/1, find/1, get_by_index/2, get_by_index_sorted/2, size/0]). +-export([add/1, find/2, get_by_index/2, get_by_index_sorted/2, size/0]). %% API for testing. -export([dump/1, destroy_tables/0, info_tables/0, dump_to_file/1]). %% gen_server callbacks. @@ -48,7 +48,8 @@ init_tables(Nodes) -> {disc_only_copies, DiscOnlyCopies}, {attributes, record_info(fields, plop)}, {majority, true}]), - {atomic, ok} = mnesia:add_table_index(plop, hash). + {atomic, ok} = mnesia:add_table_index(plop, entryhash), + {atomic, ok} = mnesia:add_table_index(plop, mtlhash). destroy_tables() -> mnesia:delete_table(plop). @@ -74,8 +75,12 @@ stop() -> add(Entry) -> gen_server:call(?MODULE, {add, Entry}). -find(Hash) -> - gen_server:call(?MODULE, {find, Hash}). +%% @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}). @@ -122,17 +127,14 @@ handle_call({dump, Table}, _From, State) -> end, Res = mnesia:transaction(F), {reply, Res, State}; -handle_call({find, Hash}, _From, State) -> - F = fun() -> - mnesia:index_read(plop, Hash, #plop.hash) - end, - {atomic, Result} = mnesia:transaction(F), - Record = case length(Result) of - 0 -> []; - 1 -> hd(Result); - _ -> duplicate_hash_in_db % FIXME: log an error - end, - {reply, Record, State}; +handle_call({find, entryhash, Hash}, _From, State) -> + {reply, + find_entry(fun() -> mnesia:index_read(plop, Hash, #plop.entryhash) end), + State}; +handle_call({find, mtlhash, Hash}, _From, State) -> + {reply, + find_entry(fun() -> mnesia:index_read(plop, Hash, #plop.mtlhash) end), + State}; handle_call({get_by_index, {Start, End}}, _From, State) -> Res = [X || [_, X] <- select_index(Start, End)], {reply, Res, State}; @@ -146,10 +148,19 @@ handle_call({get_by_index_sorted, {Start, End}}, _From, State) -> select_index(Start, End) -> F = fun() -> - MatchHead = {plop, '$1', '_', '$2', '_'}, + %% Get index and mtl. + MatchHead = {plop, '$1', '_', '_', '$2', '_'}, Guard = [{'>=', '$1', Start}, {'=<', '$1', End}], Result = ['$$'], mnesia:select(plop, [{MatchHead, Guard, Result}]) end, {atomic, Res} = mnesia:transaction(F), Res. + +find_entry(Fun) -> + {atomic, Result} = mnesia:transaction(Fun), + case length(Result) of + 0 -> []; + 1 -> hd(Result); + _ -> duplicate_hash_in_db % FIXME: log an error? + end. -- cgit v1.1