diff options
Diffstat (limited to 'src/frontend.erl')
-rw-r--r-- | src/frontend.erl | 55 |
1 files changed, 46 insertions, 9 deletions
diff --git a/src/frontend.erl b/src/frontend.erl index f0c0614..5b9157b 100644 --- a/src/frontend.erl +++ b/src/frontend.erl @@ -29,10 +29,7 @@ request(post, "plop/v1/frontend/sendlog", Input) -> Indices = lists:seq(Start, Start + length(Hashes) - 1), lists:foreach(fun ({Hash, Index}) -> - ok = db:add_index_nosync(Hash, Index) - end, lists:zip(Hashes, Indices)), - lists:foreach(fun ({Hash, Index}) -> - ok = db:indexforhash_sync(Hash, Index) + ok = db:add_index_nosync_noreverse(Hash, Index) end, lists:zip(Hashes, Indices)), ok = db:index_sync(), success({[{result, <<"ok">>}]}) @@ -59,11 +56,10 @@ request(post, "plop/v1/frontend/sendsth", Input) -> Treesize > Indexsize -> html("Has too few entries", Indexsize); true -> - NewEntries = get_new_entries(OldSize, Treesize), - lager:debug("old size: ~p new size: ~p entries: ~p", - [OldSize, Treesize, NewEntries]), + lager:debug("old size: ~p new size: ~p", + [OldSize, Treesize]), - Errors = check_entries(NewEntries, OldSize, Treesize - 1), + Errors = check_entries(Treesize), case Errors of [] -> @@ -199,7 +195,48 @@ get_new_entries(OldSize, Treesize) when OldSize < Treesize -> get_new_entries(OldSize, Treesize) when OldSize == Treesize -> []. -check_entries(Entries, Start, End) -> +check_entries(Treesize) -> + End = Treesize - 1, + Start = db:sendsth_verified() + 1, + lager:debug("Top level checking entries ~p-~p", [Start, End]), + check_entries_chunked(Start, End). + +check_entries_chunked(Start, End) -> + lager:debug("Checking entries ~p-~p", [Start, End]), + Chunksize = 1, + PartialEnd = min(Start + Chunksize - 1, End), + case check_entries_onechunk(Start, PartialEnd) of + [] when PartialEnd == End -> + []; + [] -> + check_entries_chunked(PartialEnd + 1, End); + Errors -> + Errors + 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))), + 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))), + case Entries of + [] -> + none; + Entries -> + db:set_sendsth_verified(End, lists:nth(End - Start + 1, Entries)) + end, + []; + Errors -> + Errors + end. + +check_entries_int(Entries, Start, End) -> lists:foldl(fun ({Hash, Index}, Acc) -> case check_entry(Hash, Index) of ok -> |