5252 fd :: option (file :io_device ()),
5353 index_size :: pos_integer (),
5454 access_pattern :: sequential | random ,
55+ file_advise = normal :: posix_file_advise (),
5556 mode = append :: read | append ,
5657 compute_checksums = true :: boolean ()}).
5758
7071 cache :: undefined | {non_neg_integer (), non_neg_integer (), binary ()}
7172 }).
7273
74+ -type posix_file_advise () :: 'normal' | 'sequential' | 'random'
75+ | 'no_reuse' | 'will_need' | 'dont_need' .
76+
7377-type ra_log_segment_options () :: #{max_count => non_neg_integer (),
7478 max_pending => non_neg_integer (),
7579 compute_checksums => boolean (),
7680 mode => append | read ,
77- access_pattern => sequential | random }.
81+ access_pattern => sequential | random ,
82+ file_advise => posix_file_advise ()}.
7883-opaque state () :: # state {}.
7984
8085-export_type ([state / 0 ,
86+ posix_file_advise / 0 ,
8187 ra_log_segment_options / 0 ]).
8288
8389-spec open (Filename :: file :filename_all ()) ->
@@ -116,9 +122,9 @@ open(Filename, Options) ->
116122
117123process_file (true , Mode , Filename , Fd , Options ) ->
118124 AccessPattern = maps :get (access_pattern , Options , random ),
119- if AccessPattern == random andalso
125+ FileAdvise = maps :get (file_advise , Options , normal ),
126+ if FileAdvise == random andalso
120127 Mode == read ->
121- % % this is a guess using the defaults
122128 Offs = maps :get (max_count , Options , ? SEGMENT_MAX_ENTRIES ) * ? INDEX_RECORD_SIZE_V2 ,
123129 _ = file :advise (Fd , Offs , 0 , random ),
124130 ok ;
@@ -133,7 +139,6 @@ process_file(true, Mode, Filename, Fd, Options) ->
133139 {NumIndexRecords , DataOffset , Range , Index } =
134140 recover_index (Fd , Version , MaxCount ),
135141 IndexOffset = ? HEADER_SIZE + NumIndexRecords * IndexRecordSize ,
136- Mode = maps :get (mode , Options , append ),
137142 ComputeChecksums = maps :get (compute_checksums , Options , true ),
138143 {ok , # state {cfg = # cfg {version = Version ,
139144 max_count = MaxCount ,
@@ -142,6 +147,7 @@ process_file(true, Mode, Filename, Fd, Options) ->
142147 mode = Mode ,
143148 index_size = IndexSize ,
144149 access_pattern = AccessPattern ,
150+ file_advise = FileAdvise ,
145151 compute_checksums = ComputeChecksums ,
146152 fd = Fd },
147153 data_start = ? HEADER_SIZE + IndexSize ,
@@ -165,6 +171,7 @@ process_file(false, Mode, Filename, Fd, Options) ->
165171 ComputeChecksums = maps :get (compute_checksums , Options , true ),
166172 IndexSize = MaxCount * ? INDEX_RECORD_SIZE_V2 ,
167173 ok = write_header (MaxCount , Fd ),
174+ FileAdvise = maps :get (file_advise , Options , dont_need ),
168175 {ok , # state {cfg = # cfg {version = ? VERSION ,
169176 max_count = MaxCount ,
170177 max_pending = MaxPending ,
@@ -173,6 +180,7 @@ process_file(false, Mode, Filename, Fd, Options) ->
173180 index_size = IndexSize ,
174181 fd = Fd ,
175182 compute_checksums = ComputeChecksums ,
183+ file_advise = FileAdvise ,
176184 access_pattern = random },
177185 index_write_offset = ? HEADER_SIZE ,
178186 index_offset = ? HEADER_SIZE ,
@@ -431,13 +439,15 @@ is_same_as(#state{cfg = #cfg{filename = Fn0}}, Fn) ->
431439 is_same_filename_all (Fn0 , Fn ).
432440
433441-spec close (state ()) -> ok .
434- close (# state {cfg = # cfg {fd = Fd , mode = append }} = State ) ->
442+ close (# state {cfg = # cfg {fd = Fd ,
443+ mode = append ,
444+ file_advise = FileAdvise }} = State ) ->
435445 % close needs to be defensive and idempotent so we ignore the return
436446 % values here
437447 _ = sync (State ),
438448 case is_full (State ) of
439449 true ->
440- _ = file :advise (Fd , 0 , 0 , dont_need );
450+ _ = file :advise (Fd , 0 , 0 , FileAdvise );
441451 false ->
442452 ok
443453 end ,
@@ -684,3 +694,4 @@ cache_length_test() ->
684694
685695-endif .
686696
697+
0 commit comments