Github user davisp commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch/pull/185#discussion_r81774779
  
    --- Diff: src/couch_file.erl ---
    @@ -524,34 +529,83 @@ handle_info({'EXIT', _, Reason}, Fd) ->
         {stop, Reason, Fd}.
     
     
    -find_header(_Fd, -1) ->
    -    no_valid_header;
     find_header(Fd, Block) ->
         case (catch load_header(Fd, Block)) of
         {ok, Bin} ->
             {ok, Bin};
         _Error ->
    -        find_header(Fd, Block -1)
    +        ReadCount = config:get_integer(
    +            "couchdb", "find_header_read_count", ?DEFAULT_READ_COUNT),
    +        find_header(Fd, Block -1, ReadCount)
         end.
     
     load_header(Fd, Block) ->
         {ok, <<1, HeaderLen:32/integer, RestBlock/binary>>} =
             file:pread(Fd, Block * ?SIZE_BLOCK, ?SIZE_BLOCK),
    -    TotalBytes = calculate_total_read_len(5, HeaderLen),
    -    case TotalBytes > byte_size(RestBlock) of
    -    false ->
    -        <<RawBin:TotalBytes/binary, _/binary>> = RestBlock;
    -    true ->
    -        {ok, Missing} = file:pread(
    -            Fd, (Block * ?SIZE_BLOCK) + 5 + byte_size(RestBlock),
    -            TotalBytes - byte_size(RestBlock)),
    -        RawBin = <<RestBlock/binary, Missing/binary>>
    +    load_header(Fd, Block * ?SIZE_BLOCK, HeaderLen, RestBlock).
    +
    +load_header(Fd, Pos, HeaderLen) ->
    +    load_header(Fd, Pos, HeaderLen, <<>>).
    +
    +load_header(Fd, Pos, HeaderLen, RestBlock) ->
    +    TotalBytes = calculate_total_read_len(?PREFIX_SIZE, HeaderLen),
    +    RawBin = case TotalBytes =< byte_size(RestBlock) of
    +        true ->
    +            <<RawBin0:TotalBytes/binary, _/binary>> = RestBlock,
    +            RawBin0;
    +        false ->
    +            ReadStart = Pos + ?PREFIX_SIZE + byte_size(RestBlock),
    +            ReadLen = TotalBytes - byte_size(RestBlock),
    +            {ok, Missing} = file:pread(Fd, ReadStart, ReadLen),
    +            <<RestBlock/binary, Missing/binary>>
         end,
         <<Md5Sig:16/binary, HeaderBin/binary>> =
    -        iolist_to_binary(remove_block_prefixes(5, RawBin)),
    +        iolist_to_binary(remove_block_prefixes(?PREFIX_SIZE, RawBin)),
         Md5Sig = couch_crypto:hash(md5, HeaderBin),
         {ok, HeaderBin}.
     
    +
    +%% Read multiple block locations using a single file:pread/2.
    +-spec find_header(file:fd(), block_id(), non_neg_integer()) ->
    +    {ok, binary()} | no_valid_header.
    +find_header(_Fd, Block, _ReadCount) when Block < 0 ->
    +    no_valid_header;
    +find_header(Fd, Block, ReadCount) ->
    +    FirstBlock = max(0, Block - ReadCount + 1),
    +    BlockLocations = [?SIZE_BLOCK*B || B <- lists:seq(FirstBlock, Block)],
    +    {ok, DataL} = file:pread(Fd, [{L, ?PREFIX_SIZE} || L <- 
BlockLocations]),
    +    %% Since BlockLocations are ordered from oldest to newest, we rely
    +    %% on lists:foldl/3 to reverse the order, making HeaderLocations
    +    %% correctly ordered from newest to oldest.
    +    HeaderLocations = lists:foldl(fun
    +        ({Loc, <<1, HeaderSize:32/integer>>}, Acc) ->
    +            [{Loc, HeaderSize} | Acc];
    +        (_, Acc) ->
    +            Acc
    +        end, [], lists:zip(BlockLocations, DataL)),
    --- End diff --
    
    This needs to be dedented one level.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to