@@ -141,8 +141,40 @@ public class MMapDirectory extends FSDirectory {
141141 return Optional .of (groupKey );
142142 };
143143
144- private BiFunction <String , IOContext , Optional <ReadAdvice >> readAdviceOverride =
144+ /**
145+ * Argument for {@link #setReadAdvice(BiFunction)} that configures the read advice based on the
146+ * context type and hints.
147+ *
148+ * <p>Specifically, the advice is determined by evaluating the following conditions in order:
149+ *
150+ * <ol>
151+ * <li>If the context is either of {@link IOContext.Context#MERGE} or {@link
152+ * IOContext.Context#FLUSH}, then {@link ReadAdvice#SEQUENTIAL},
153+ * <li>If the context {@link IOContext#hints() hints} contains {@link DataAccessHint#RANDOM},
154+ * then {@link ReadAdvice#RANDOM},
155+ * <li>If the context {@link IOContext#hints() hints} contains {@link
156+ * DataAccessHint#SEQUENTIAL}, then {@link ReadAdvice#SEQUENTIAL},
157+ * <li>Otherwise, {@link Constants#DEFAULT_READADVICE} is returned.
158+ * </ol>
159+ */
160+ public static final BiFunction <String , IOContext , Optional <ReadAdvice >> ADVISE_BY_CONTEXT =
161+ (String _, IOContext context ) -> {
162+ if (context .context () == IOContext .Context .MERGE
163+ || context .context () == IOContext .Context .FLUSH ) {
164+ return Optional .of (ReadAdvice .SEQUENTIAL );
165+ }
166+ if (context .hints ().contains (DataAccessHint .RANDOM )) {
167+ return Optional .of (ReadAdvice .RANDOM );
168+ }
169+ if (context .hints ().contains (DataAccessHint .SEQUENTIAL )) {
170+ return Optional .of (ReadAdvice .SEQUENTIAL );
171+ }
172+ return Optional .of (Constants .DEFAULT_READADVICE );
173+ };
174+
175+ private BiFunction <String , IOContext , Optional <ReadAdvice >> readAdvice =
145176 (_ , _ ) -> Optional .empty ();
177+
146178 private BiPredicate <String , IOContext > preload = NO_FILES ;
147179
148180 /**
@@ -248,17 +280,16 @@ public void setPreload(BiPredicate<String, IOContext> preload) {
248280 }
249281
250282 /**
251- * Configure {@link ReadAdvice} overrides for certain files. If the function returns {@code
252- * Optional.empty()}, a default {@link ReadAdvice} will be used
283+ * Configure {@link ReadAdvice} for certain files. The default implementation uses the {@link
284+ * Constants#DEFAULT_READADVICE}.
253285 *
254286 * @param toReadAdvice a {@link Function} whose first argument is the file name, and second
255287 * argument is the {@link IOContext} used to open the file. Returns {@code
256288 * Optional.of(ReadAdvice)} to use a specific read advice, or {@code Optional.empty()} if a
257289 * default should be used
258290 */
259- public void setReadAdviceOverride (
260- BiFunction <String , IOContext , Optional <ReadAdvice >> toReadAdvice ) {
261- this .readAdviceOverride = toReadAdvice ;
291+ public void setReadAdvice (BiFunction <String , IOContext , Optional <ReadAdvice >> toReadAdvice ) {
292+ this .readAdvice = toReadAdvice ;
262293 }
263294
264295 /**
@@ -285,22 +316,6 @@ public final long getMaxChunkSize() {
285316 return 1L << chunkSizePower ;
286317 }
287318
288- private static ReadAdvice toReadAdvice (IOContext context ) {
289- if (context .context () == IOContext .Context .MERGE
290- || context .context () == IOContext .Context .FLUSH ) {
291- return ReadAdvice .SEQUENTIAL ;
292- }
293-
294- if (context .hints ().contains (DataAccessHint .RANDOM )) {
295- return ReadAdvice .RANDOM ;
296- }
297- if (context .hints ().contains (DataAccessHint .SEQUENTIAL )) {
298- return ReadAdvice .SEQUENTIAL ;
299- }
300-
301- return Constants .DEFAULT_READADVICE ;
302- }
303-
304319 /** Creates an IndexInput for the file with the given name. */
305320 @ Override
306321 public IndexInput openInput (String name , IOContext context ) throws IOException {
@@ -314,7 +329,7 @@ public IndexInput openInput(String name, IOContext context) throws IOException {
314329
315330 final boolean confined = context .hints ().contains (ReadOnceHint .INSTANCE );
316331 Function <IOContext , ReadAdvice > toReadAdvice =
317- c -> readAdviceOverride .apply (name , c ).orElseGet (() -> toReadAdvice ( c ) );
332+ c -> readAdvice .apply (name , c ).orElse ( Constants . DEFAULT_READADVICE );
318333 final Arena arena = confined ? Arena .ofConfined () : getSharedArena (name , arenas );
319334 try (var fc = FileChannel .open (path , StandardOpenOption .READ )) {
320335 final long fileSize = fc .size ();
0 commit comments