Github user jaydoane commented on a diff in the pull request: https://github.com/apache/couchdb-couch/pull/185#discussion_r81645651 --- Diff: src/couch_file.erl --- @@ -531,27 +537,96 @@ find_header(Fd, Block) -> {ok, Bin} -> {ok, Bin}; _Error -> - find_header(Fd, Block -1) + find_header_pread2(Fd, Block -1) 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 + TotalSize = total_read_size(5, HeaderLen), + case TotalSize > byte_size(RestBlock) of false -> - <<RawBin:TotalBytes/binary, _/binary>> = RestBlock; + <<RawBin:TotalSize/binary, _/binary>> = RestBlock; true -> {ok, Missing} = file:pread( Fd, (Block * ?SIZE_BLOCK) + 5 + byte_size(RestBlock), - TotalBytes - byte_size(RestBlock)), + TotalSize - byte_size(RestBlock)), RawBin = <<RestBlock/binary, Missing/binary>> end, <<Md5Sig:16/binary, HeaderBin/binary>> = iolist_to_binary(remove_block_prefixes(5, RawBin)), Md5Sig = couch_crypto:hash(md5, HeaderBin), {ok, HeaderBin}. + +%% Read a configurable number of block locations at a time using +%% file:pread/2. At each block location, read ?PREFIX_SIZE (5) bytes; +%% if the first byte is <<1>>, assume it's a header block, and the +%% next 4 bytes hold the size of the header. +-spec find_header_pread2(file:fd(), block_id()) -> + {ok, binary()} | no_valid_header. +find_header_pread2(Fd, Block) -> + find_header_pread2(Fd, Block, read_count()). + +-spec find_header_pread2(file:fd(), block_id(), non_neg_integer()) -> + {ok, binary()} | no_valid_header. +find_header_pread2(_Fd, Block, _ReadCount) when Block < 0 -> + no_valid_header; +find_header_pread2(Fd, Block, ReadCount) -> + Locations = block_locations(Block, ReadCount), + {ok, DataL} = file:pread(Fd, [{L, ?PREFIX_SIZE} || L <- Locations]), + case locate_header(Fd, header_locations(Locations, DataL)) of + {ok, _Location, HeaderBin} -> + io:format("header at block ~p~n", [_Location div ?SIZE_BLOCK]), % TEMP + {ok, HeaderBin}; + _ -> + ok = file:advise( + Fd, hd(Locations), ReadCount * ?SIZE_BLOCK, dont_need), + NextBlock = hd(Locations) div ?SIZE_BLOCK - 1, + find_header_pread2(Fd, NextBlock, ReadCount) + end. + +-spec read_count() -> non_neg_integer(). +read_count() -> + config:get_integer("couchdb", "find_header_read_count", ?DEFAULT_READ_COUNT). + +-spec block_locations(block_id(), non_neg_integer()) -> [location()]. +block_locations(Block, ReadCount) -> + First = max(0, Block - ReadCount + 1), + [?SIZE_BLOCK*B || B <- lists:seq(First, Block)]. + +-spec header_locations([location()], [data()]) -> [{location(), header_size()}]. +header_locations(Locations, DataL) -> + lists:foldl(fun + ({Loc, <<1, HeaderSize:32/integer>>}, Acc) -> + [{Loc, HeaderSize} | Acc]; + (_, Acc) -> + Acc + end, [], lists:zip(Locations, DataL)). + +-spec locate_header(file:fd(), [{location(), header_size()}]) -> + {ok, binary()} | not_found. +locate_header(_Fd, []) -> + not_found; +locate_header(Fd, [{Location, Size}|LocationSizes]) -> + case (catch load_header(Fd, Location, Size)) of + {ok, HeaderBin} -> + {ok, Location, HeaderBin}; + _Error -> + locate_header(Fd, LocationSizes) + end. + +-spec load_header(file:fd(), location(), header_size()) -> + {ok, binary()} | no_return(). +load_header(Fd, Location, Size) -> --- End diff -- @davisp, thank you for your patience. Your solution seems fine, even though it somewhat obscures the simplicity of the vectored algorithm. But I agree it's more idiomatic and easier to maintain your way, so I made the change pretty much verbatim. Finally, I noted that the `find_header/2` recursion termination clause is no longer being used, and removed it. Do you see any other changes that need to be made?
--- 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. ---