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

    https://github.com/apache/couchdb-couch/pull/185#discussion_r75904032
  
    --- Diff: src/couch_file.erl ---
    @@ -524,6 +528,214 @@ handle_info({'EXIT', _, Reason}, Fd) ->
         {stop, Reason, Fd}.
     
     
    +find_last_header(Fd, FileSize) ->
    +    find_header_in_chunks(Fd, start_pos(FileSize), 1).
    +
    +start_pos(FileSize) when FileSize rem ?SIZE_BLOCK =:= 0 ->
    +    FileSize;
    +start_pos(FileSize) ->
    +    %% set position to nearest block boundary past eof
    +    %% if file size isn't multiple of block size
    +    (FileSize div ?SIZE_BLOCK) * ?SIZE_BLOCK + ?SIZE_BLOCK.
    +
    +chunk_max_size() ->
    +    config:get_integer("couchdb", "chunk_max_size", 
?DEFAULT_CHUNK_MAX_SIZE).
    +
    +%% chunk size exponentially increases by iteration, up to some maximum,
    +%% and should never exceed the current position in the file
    +chunk_size(Pos, Multiplier) ->
    +    Size = min(Multiplier * ?SIZE_BLOCK, chunk_max_size()),
    +    min(Pos, Size).
    +
    +multiplier(Size, Multiplier) ->
    +    case Size < chunk_max_size() of
    +        true ->
    +            Multiplier * config:get_integer(
    +                "couchdb", "chunk_exponent_base", 
?DEFAULT_CHUNK_EXPONENT_BASE);
    +        false ->
    +            Multiplier
    +    end.
    +
    +find_header_in_chunks(_Fd, Pos, _Multiplier) when Pos < 0 ->
    +    no_valid_header;
    +find_header_in_chunks(Fd, Pos, Multiplier) ->
    +    Size = chunk_size(Pos, Multiplier),
    +    case Size > 0 of
    +        false ->
    +            no_valid_header;
    +        true ->
    +            {ok, Chunk} = file:pread(Fd, Pos - Size, Size),
    +            case find_header_in_chunk(Fd, Pos, Chunk, Size - ?SIZE_BLOCK) 
of
    +                {ok, HeaderBin, _Offset} ->
    +                    %% io:format("found header at ~p multiplier ~p 
chunksize ~p~n",
    +                    %%     [Pos - Size + _Offset, Multiplier, Size]),
    +                    {ok, HeaderBin};
    +                not_found ->
    +                    %% tell kernel we don't need that chunk any more
    +                    %% to minimize disk cache pollution
    +                    ok = file:advise(Fd, Pos - Size, Size, dont_need),
    +                    NewMultiplier = multiplier(Size, Multiplier),
    +                    find_header_in_chunks(Fd, Pos - Size, NewMultiplier)
    +            end
    +    end.
    +
    +-define(DEFAULT_SPAN_MAX_SIZE, ?SIZE_BLOCK * ?SIZE_BLOCK).
    +-define(DEFAULT_SPAN_EXPONENT_BASE, 2).
    +-define(PREFIX_SIZE, 5).
    +
    +system_milli_seconds() ->
    +    {Mega, Sec, Micro} = os:timestamp(),
    +    (Mega*1000000 + Sec)*1000 + round(Micro/1000).    
    +
    +last_block_pos(FileSize) when FileSize rem ?SIZE_BLOCK =:= 0 ->
    +    FileSize - ?SIZE_BLOCK;
    --- End diff --
    
    This clause isn't needed.


---
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