Update to review comments

This commit is contained in:
UENISHI Kota 2015-10-21 10:58:00 +09:00
parent ebb9bc3f5a
commit a43397a7b8
3 changed files with 18 additions and 16 deletions

View file

@ -531,7 +531,7 @@ do_read_chunk2(File, Offset, Size, Depth, STime, TO,
case ?FLU_PC:read_chunk(orddict:fetch(Tail, PD), EpochID, case ?FLU_PC:read_chunk(orddict:fetch(Tail, PD), EpochID,
File, Offset, Size, ?TIMEOUT) of File, Offset, Size, ?TIMEOUT) of
{ok, Chunks0} when is_list(Chunks0) -> {ok, Chunks0} when is_list(Chunks0) ->
Chunks = trim_both_side(Chunks0, Offset, Size), Chunks = trim_both_side(Chunks0, Offset, Offset + Size),
{reply, {ok, Chunks}, S}; {reply, {ok, Chunks}, S};
%% {ok, BadChunk} -> %% {ok, BadChunk} ->
%% %% TODO cleaner handling of bad chunks %% %% TODO cleaner handling of bad chunks
@ -924,21 +924,23 @@ timeout(infinity) ->
timeout(Timeout0) -> timeout(Timeout0) ->
{Timeout0, Timeout0 + 30*1000}. {Timeout0, Timeout0 + 30*1000}.
trim_both_side([], _Offset, _Size) -> []; %% @doc Trim both right and left border of chunks to fit in to given
trim_both_side([{F, Offset0, Chunk, _Csum}|L], Offset, Size) %% range [LeftPos, RightPos]. TODO: write unit tests for this function.
when Offset0 < Offset -> trim_both_side([], _, _) -> [];
TrashLen = 8 * (Offset - Offset0), trim_both_side([{F, Offset, Chunk, _Csum}|L], LeftPos, RightPos)
when Offset < LeftPos andalso LeftPos < RightPos ->
TrashLen = 8 * (LeftPos - Offset),
<<_:TrashLen/binary, NewChunk/binary>> = Chunk, <<_:TrashLen/binary, NewChunk/binary>> = Chunk,
NewH = {F, Offset, NewChunk, <<>>}, NewH = {F, LeftPos, NewChunk, <<>>},
trim_both_side([NewH|L], Offset, Size); trim_both_side([NewH|L], LeftPos, RightPos);
trim_both_side(Chunks, Offset, Size) -> trim_both_side(Chunks, LeftPos, RightPos) when LeftPos =< RightPos ->
%% TODO: optimize %% TODO: optimize
[{F, Offset1, Chunk1, _Csum1}|L] = lists:reverse(Chunks), [{F, Offset, Chunk, _Csum}|L] = lists:reverse(Chunks),
Size1 = iolist_size(Chunk1), Size = iolist_size(Chunk),
if Offset + Size < Offset1 + Size1 -> if RightPos < Offset + Size ->
Size2 = Offset + Size - Offset1, NewSize = RightPos - Offset,
<<NewChunk1:Size2/binary, _/binary>> = Chunk1, <<NewChunk:NewSize/binary, _/binary>> = Chunk,
lists:reverse([{F, Offset1, NewChunk1, <<>>}|L]); lists:reverse([{F, Offset, NewChunk, <<>>}|L]);
true -> true ->
Chunks Chunks
end. end.

View file

@ -127,7 +127,7 @@ sync(_Pid, Type) ->
% @doc Read file at offset for length. This returns a sequence of all % @doc Read file at offset for length. This returns a sequence of all
% chunks that overlaps with requested offset and length. Note that % chunks that overlaps with requested offset and length. Note that
% borders are not alinged, not to mess up repair at cr_client with % borders are not aligned, not to mess up repair at cr_client with
% checksums. They should be cut at cr_client. % checksums. They should be cut at cr_client.
-spec read(Pid :: pid(), -spec read(Pid :: pid(),
Offset :: non_neg_integer(), Offset :: non_neg_integer(),

View file

@ -203,7 +203,7 @@ read_next(S, _Res, _Args) -> S.
read(Pid, Offset, Length) -> read(Pid, Offset, Length) ->
case machi_file_proxy:read(Pid, Offset, Length) of case machi_file_proxy:read(Pid, Offset, Length) of
{ok, Chunks} -> {ok, Chunks} ->
[{_, Offset, Data, Csum}] = machi_cr_client:trim_both_side(Chunks, Offset, Length), [{_, Offset, Data, Csum}] = machi_cr_client:trim_both_side(Chunks, Offset, Offset+Length),
{ok, Data, Csum}; {ok, Data, Csum};
E -> E ->
E E