Performance fix: don't always go to beginning of leveldb table!

This commit is contained in:
Scott Lystig Fritchie 2016-03-28 17:51:30 +09:00
parent 8c21539fcb
commit 767f5d9e60

View file

@ -85,10 +85,18 @@ find(#machi_csum_table{table=T}, Offset, Size) ->
{ok, I} = eleveldb:iterator(T, [], keys_only), {ok, I} = eleveldb:iterator(T, [], keys_only),
EndKey = sext:encode({Offset+Size, 0}), EndKey = sext:encode({Offset+Size, 0}),
StartKey = sext:encode({Offset, Size}), StartKey = sext:encode({Offset, Size}),
{ok, FirstKey} = case eleveldb:iterator_move(I, StartKey) of {ok, FirstKey} = case eleveldb:iterator_move(I, StartKey) of
{error, invalid_iterator} -> {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 -> {ok, _} = R0 ->
case eleveldb:iterator_move(I, prev) of case eleveldb:iterator_move(I, prev) of
{error, invalid_iterator} -> {error, invalid_iterator} ->
@ -112,7 +120,6 @@ find(#machi_csum_table{table=T}, Offset, Size) ->
end, end,
lists:reverse(eleveldb_fold(T, FirstKey, EndKey, FoldFun, [])). lists:reverse(eleveldb_fold(T, FirstKey, EndKey, FoldFun, [])).
%% @doc Updates all chunk info, by deleting existing entries if exists %% @doc Updates all chunk info, by deleting existing entries if exists
%% and putting new chunk info %% and putting new chunk info
-spec write(table(), -spec write(table(),
@ -146,6 +153,8 @@ write(#machi_csum_table{table=T} = CsumT, Offset, Size, CSum,
DeleteOps = lists:map(fun({O, L, _}) -> DeleteOps = lists:map(fun({O, L, _}) ->
{delete, sext:encode({O, L})} {delete, sext:encode({O, L})}
end, Chunks), 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}]). eleveldb:write(T, DeleteOps ++ PutOps, [{sync, true}]).
-spec find_leftneighbor(table(), non_neg_integer()) -> -spec find_leftneighbor(table(), non_neg_integer()) ->