From 767f5d9e60a7ecf1a191f56ce6943c79e1975b57 Mon Sep 17 00:00:00 2001 From: Scott Lystig Fritchie Date: Mon, 28 Mar 2016 17:51:30 +0900 Subject: [PATCH] Performance fix: don't always go to beginning of leveldb table! --- src/machi_csum_table.erl | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/machi_csum_table.erl b/src/machi_csum_table.erl index ae6cd3a..051e2bd 100644 --- a/src/machi_csum_table.erl +++ b/src/machi_csum_table.erl @@ -85,10 +85,18 @@ find(#machi_csum_table{table=T}, Offset, Size) -> {ok, I} = eleveldb:iterator(T, [], keys_only), EndKey = sext:encode({Offset+Size, 0}), StartKey = sext:encode({Offset, Size}), - {ok, FirstKey} = case eleveldb:iterator_move(I, StartKey) of {error, invalid_iterator} -> - eleveldb:iterator_move(I, first); + try + %% Assume that the invalid_iterator is because + %% we tried to move to the end via StartKey. + %% Instead, move there directly. + {ok, _} = eleveldb:iterator_move(I, last), + {ok, _} = eleveldb:iterator_move(I, prev) + catch + _:_ -> + {ok, _} = eleveldb:iterator_move(I, first) + end; {ok, _} = R0 -> case eleveldb:iterator_move(I, prev) of {error, invalid_iterator} -> @@ -112,7 +120,6 @@ find(#machi_csum_table{table=T}, Offset, Size) -> end, lists:reverse(eleveldb_fold(T, FirstKey, EndKey, FoldFun, [])). - %% @doc Updates all chunk info, by deleting existing entries if exists %% and putting new chunk info -spec write(table(), @@ -146,6 +153,8 @@ write(#machi_csum_table{table=T} = CsumT, Offset, Size, CSum, DeleteOps = lists:map(fun({O, L, _}) -> {delete, sext:encode({O, L})} end, Chunks), +%% io:format(user, "PutOps: ~P\n", [PutOps, 20]), +%% io:format(user, "DelOps: ~P\n", [DeleteOps, 20]), eleveldb:write(T, DeleteOps ++ PutOps, [{sync, true}]). -spec find_leftneighbor(table(), non_neg_integer()) ->