diff options
author | Magnus Ahltorp <map@kth.se> | 2015-08-20 17:09:27 +0200 |
---|---|---|
committer | Magnus Ahltorp <map@kth.se> | 2015-08-20 17:09:27 +0200 |
commit | c0582dd7dfac9accfd9aa913d26fadabf5ad9be5 (patch) | |
tree | b3fe74e1458f299f7682a91d3c8358b51e1da3fa | |
parent | 084719bb67ebf78479ca2a5eb038d3d2b901c975 (diff) |
Refactor check_entries*sendsth-verified
-rw-r--r-- | src/frontend.erl | 62 |
1 files changed, 24 insertions, 38 deletions
diff --git a/src/frontend.erl b/src/frontend.erl index 1c007cf..dbd0611 100644 --- a/src/frontend.erl +++ b/src/frontend.erl @@ -203,7 +203,7 @@ check_entries(Treesize) -> check_entries_chunked(Start, End) -> lager:debug("Checking entries ~p-~p", [Start, End]), - Chunksize = 1, + Chunksize = 2, PartialEnd = min(Start + Chunksize - 1, End), case check_entries_onechunk(Start, PartialEnd) of [] when PartialEnd == End -> @@ -214,17 +214,23 @@ check_entries_chunked(Start, End) -> Errors end. +create_reverse_nosync(Entries, Start, End) -> + lists:foreach(fun ({Hash, Index}) -> + ok = db:indexforhash_nosync(Hash, Index) + end, lists:zip(Entries, lists:seq(Start, End))). + +create_reverse_sync(Entries, Start, End) -> + lists:foreach(fun ({Hash, Index}) -> + ok = db:indexforhash_sync(Hash, Index) + end, lists:zip(Entries, lists:seq(Start, End))). + check_entries_onechunk(Start, End) -> Entries = get_new_entries(Start, End + 1), lager:debug("Checking chunk ~p-~p: ~p", [Start, End, Entries]), - lists:foreach(fun ({Hash, Index}) -> - ok = db:indexforhash_nosync(Hash, Index) - end, lists:zip(Entries, lists:seq(Start, End))), + create_reverse_nosync(Entries, Start, End), case check_entries_int(Entries, Start, End) of [] -> - lists:foreach(fun ({Hash, Index}) -> - ok = db:indexforhash_sync(Hash, Index) - end, lists:zip(Entries, lists:seq(Start, End))), + create_reverse_sync(Entries, Start, End), case Entries of [] -> none; @@ -237,10 +243,12 @@ check_entries_onechunk(Start, End) -> end. check_entries_int(Entries, Start, End) -> - lists:foldl(fun ({Hash, Index}, Acc) -> - case check_entry(Hash, Index) of - ok -> - Acc; + lists:foldl(fun ({Hash, _Index}, Acc) -> + case check_entry_noreverse(Hash) of + {ok, Entry} -> + EntryHash = entryhash_from_entry(Entry), + db:add_entryhash(Hash, EntryHash), % Returns ok|differ... + Acc; % ... both are OK. Error -> [Error | Acc] end @@ -249,8 +257,8 @@ check_entries_int(Entries, Start, End) -> check_entries_noreverse(Entries, Start, End) -> lists:foldl(fun ({Hash, Index}, Acc) -> lager:info("checking entry ~p", [Index]), - case check_entry_noreverse(Hash, Index) of - ok -> + case check_entry_noreverse(Hash) of + {ok, _Entry} -> lager:info("entry ~p is correct", [Index]), Acc; Error -> @@ -266,36 +274,14 @@ verify_entry(Entry) -> {ok, {Module, Function}} = application:get_env(plop, verify_entry), Module:Function(Entry). -check_entry(LeafHash, Index) -> - case plop:get_by_leaf_hash(LeafHash) of - notfound -> - {notfound, Index}; - {Index, LeafHash, Entry} -> - case verify_entry(Entry) of - {ok, LeafHash} -> - EntryHash = entryhash_from_entry(Entry), - db:add_entryhash(LeafHash, EntryHash), % Returns ok|differ... - ok; % ... both are OK. - {ok, DifferentLeafHash} -> - lager:error("leaf hash not correct: filename is ~p " ++ - "and contents are ~p", - [hex:bin_to_hexstr(LeafHash), - hex:bin_to_hexstr(DifferentLeafHash)]), - {error, differentleafhash}; - {error, Reason} -> - lager:error("verification failed: ~p", [Reason]), - {error, verificationfailed} - end - end. - -check_entry_noreverse(LeafHash, Index) -> +check_entry_noreverse(LeafHash) -> case plop:entry_for_leafhash(LeafHash) of notfound -> - {notfound, Index}; + {error, notfound}; Entry -> case verify_entry(Entry) of {ok, LeafHash} -> - ok; + {ok, Entry}; {ok, DifferentLeafHash} -> lager:error("leaf hash not correct: filename is ~p " ++ "and contents are ~p", |