diff --git a/src/ra_log_segment.erl b/src/ra_log_segment.erl index 54d82a8d..f830b2fd 100644 --- a/src/ra_log_segment.erl +++ b/src/ra_log_segment.erl @@ -115,17 +115,23 @@ open(Filename, Options) -> end. process_file(true, Mode, Filename, Fd, Options) -> + AccessPattern = maps:get(access_pattern, Options, random), + if AccessPattern == random andalso + Mode == read -> + %% advise for random access for any data beyond our best guess + %% index size, readers could pass `max_count` in the config to + %% ensure this is correct + ConfMaxCount = maps:get(max_count, Options, ?SEGMENT_MAX_ENTRIES), + Offset = ?HEADER_SIZE + (ConfMaxCount * ?INDEX_RECORD_SIZE_V2), + ok = file:advise(Fd, Offset, 0, random); + true -> + ok + end, case read_header(Fd) of {ok, Version, MaxCount} -> MaxPending = maps:get(max_pending, Options, ?SEGMENT_MAX_PENDING), IndexRecordSize = index_record_size(Version), IndexSize = MaxCount * IndexRecordSize, - AccessPattern = maps:get(access_pattern, Options, random), - if AccessPattern == random -> - ok = file:advise(Fd, 0, 0, random); - true -> - ok - end, {NumIndexRecords, DataOffset, Range, Index} = recover_index(Fd, Version, MaxCount), IndexOffset = ?HEADER_SIZE + NumIndexRecords * IndexRecordSize,