Apache Lucene open-source search software
Create file open hints on IOContext to replace ReadAdvice (#14482)
Simon Cooperbb76f0a2d981
diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene50/Lucene50CompoundReader.java b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene50/Lucene50CompoundReader.javaindex 8083a2de7d3..2d5502741df 100644--- a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene50/Lucene50CompoundReader.java+++ b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene50/Lucene50CompoundReader.java@@ -170,7 +170,7 @@ final class Lucene50CompoundReader extends CompoundDirectory {+ entries.keySet()+ ")");}- return handle.slice(name, entry.offset, entry.length, context.readAdvice());+ return handle.slice(name, entry.offset, entry.length, context);}/** Returns an array of strings, one for each file in the directory. */diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextCompoundFormat.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextCompoundFormat.javaindex 8cb48e36919..6a487b01680 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextCompoundFormat.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextCompoundFormat.java@@ -137,10 +137,7 @@ public class SimpleTextCompoundFormat extends CompoundFormat {ensureOpen();int index = getIndex(name);return in.slice(- name,- startOffsets[index],- endOffsets[index] - startOffsets[index],- context.readAdvice());+ name, startOffsets[index], endOffsets[index] - startOffsets[index], context);}@Overridediff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90CompoundReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90CompoundReader.javaindex 8f6211bc959..30db27b44fd 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90CompoundReader.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90CompoundReader.java@@ -169,7 +169,7 @@ final class Lucene90CompoundReader extends CompoundDirectory {+ entries.keySet()+ ")");}- return handle.slice(name, entry.offset, entry.length, context.readAdvice());+ return handle.slice(name, entry.offset, entry.length, context);}/** Returns an array of strings, one for each file in the directory. */diff --git a/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterPerThread.java b/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterPerThread.javaindex 13fe8e62fe1..ddd15657fea 100644--- a/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterPerThread.java+++ b/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterPerThread.java@@ -415,7 +415,7 @@ final class DocumentsWriterPerThread implements Accountable, Lock {segmentInfo,fieldInfos.finish(),pendingUpdates,- new IOContext(new FlushInfo(numDocsInRAM, lastCommittedBytesUsed)));+ IOContext.flush(new FlushInfo(numDocsInRAM, lastCommittedBytesUsed)));final double startMBUsed = lastCommittedBytesUsed / 1024. / 1024.;// Apply delete-by-docID now (delete-byDocID only@@ -599,7 +599,7 @@ final class DocumentsWriterPerThread implements Accountable, Lock {IndexWriter.setDiagnostics(newSegment.info, IndexWriter.SOURCE_FLUSH);IOContext context =- new IOContext(new FlushInfo(newSegment.info.maxDoc(), newSegment.sizeInBytes()));+ IOContext.flush(new FlushInfo(newSegment.info.maxDoc(), newSegment.sizeInBytes()));boolean success = false;try {diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.javaindex 772b35fad94..65f79e51b0e 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java@@ -3077,7 +3077,7 @@ public class IndexWriter}IOContext context =- new IOContext(new FlushInfo(info.info.maxDoc(), info.sizeInBytes()));+ IOContext.flush(new FlushInfo(info.info.maxDoc(), info.sizeInBytes()));FieldInfos fis = readFieldInfos(info);for (FieldInfo fi : fis) {@@ -3419,7 +3419,7 @@ public class IndexWritertestReserveDocs(numDocs);final IOContext context =- new IOContext(+ IOContext.merge(new MergeInfo(Math.toIntExact(numDocs), -1, false, UNBOUNDED_MAX_MERGE_SEGMENTS));TrackingDirectoryWrapper trackingDir = new TrackingDirectoryWrapper(mergeDirectory);@@ -3969,7 +3969,7 @@ public class IndexWriterboolean closeReaders = true;try {for (MergePolicy.OneMerge merge : pointInTimeMerges.merges) {- IOContext context = new IOContext(merge.getStoreMergeInfo());+ IOContext context = IOContext.merge(merge.getStoreMergeInfo());merge.initMergeReaders(sci -> {final ReadersAndUpdates rld = getPooledInstance(sci, true);@@ -5158,7 +5158,7 @@ public class IndexWritermerge.checkAborted();Directory mergeDirectory = mergeScheduler.wrapForMerge(merge, directory);- IOContext context = new IOContext(merge.getStoreMergeInfo());+ IOContext context = IOContext.merge(merge.getStoreMergeInfo());final TrackingDirectoryWrapper dirWrapper = new TrackingDirectoryWrapper(mergeDirectory);diff --git a/lucene/core/src/java/org/apache/lucene/index/ReadersAndUpdates.java b/lucene/core/src/java/org/apache/lucene/index/ReadersAndUpdates.javaindex c9be090742b..017645cbabb 100644--- a/lucene/core/src/java/org/apache/lucene/index/ReadersAndUpdates.java+++ b/lucene/core/src/java/org/apache/lucene/index/ReadersAndUpdates.java@@ -321,7 +321,7 @@ final class ReadersAndUpdates {}final long nextDocValuesGen = info.getNextDocValuesGen();final String segmentSuffix = Long.toString(nextDocValuesGen, Character.MAX_RADIX);- final IOContext updatesContext = new IOContext(new FlushInfo(info.info.maxDoc(), bytes));+ final IOContext updatesContext = IOContext.flush(new FlushInfo(info.info.maxDoc(), bytes));final FieldInfo fieldInfo = infos.fieldInfo(field);assert fieldInfo != null;fieldInfo.setDocValuesGen(nextDocValuesGen);@@ -536,7 +536,7 @@ final class ReadersAndUpdates {// HEADER + FOOTER: 40// 90 bytes per-field (over estimating long name and attributes map)final long estInfosSize = 40 + 90L * fieldInfos.size();- final IOContext infosContext = new IOContext(new FlushInfo(info.info.maxDoc(), estInfosSize));+ final IOContext infosContext = IOContext.flush(new FlushInfo(info.info.maxDoc(), estInfosSize));// separately also track which files were created for this genfinal TrackingDirectoryWrapper trackingDir = new TrackingDirectoryWrapper(dir);infosFormat.write(trackingDir, info.info, segmentSuffix, fieldInfos, infosContext);diff --git a/lucene/core/src/java/org/apache/lucene/index/SortingTermVectorsConsumer.java b/lucene/core/src/java/org/apache/lucene/index/SortingTermVectorsConsumer.javaindex f328af2c9d3..d5d4aceae85 100644--- a/lucene/core/src/java/org/apache/lucene/index/SortingTermVectorsConsumer.java+++ b/lucene/core/src/java/org/apache/lucene/index/SortingTermVectorsConsumer.java@@ -86,7 +86,7 @@ final class SortingTermVectorsConsumer extends TermVectorsConsumer {@Overridevoid initTermVectorsWriter() throws IOException {if (writer == null) {- IOContext context = new IOContext(new FlushInfo(lastDocID, bytesUsed.get()));+ IOContext context = IOContext.flush(new FlushInfo(lastDocID, bytesUsed.get()));tmpDirectory = new TrackingTmpOutputDirectoryWrapper(directory);writer = TEMP_TERM_VECTORS_FORMAT.vectorsWriter(tmpDirectory, info, context);lastDocID = 0;diff --git a/lucene/core/src/java/org/apache/lucene/index/TermVectorsConsumer.java b/lucene/core/src/java/org/apache/lucene/index/TermVectorsConsumer.javaindex b4af3f83740..832f0275fa9 100644--- a/lucene/core/src/java/org/apache/lucene/index/TermVectorsConsumer.java+++ b/lucene/core/src/java/org/apache/lucene/index/TermVectorsConsumer.java@@ -102,7 +102,7 @@ class TermVectorsConsumer extends TermsHash {void initTermVectorsWriter() throws IOException {if (writer == null) {- IOContext context = new IOContext(new FlushInfo(lastDocID, bytesUsed.get()));+ IOContext context = IOContext.flush(new FlushInfo(lastDocID, bytesUsed.get()));writer = codec.termVectorsFormat().vectorsWriter(directory, info, context);lastDocID = 0;accountable = writer;diff --git a/lucene/core/src/java/org/apache/lucene/store/DataAccessHint.java b/lucene/core/src/java/org/apache/lucene/store/DataAccessHint.javanew file mode 100644index 00000000000..ac08fbfd4ad--- /dev/null+++ b/lucene/core/src/java/org/apache/lucene/store/DataAccessHint.java@@ -0,0 +1,25 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.store;++/** Hint on the data access pattern likely to be used */+public enum DataAccessHint implements IOContext.FileOpenHint {+ /** The access pattern is completely random */+ RANDOM,+ /** The access pattern is only sequential (forwards-only) */+ SEQUENTIAL+}diff --git a/lucene/core/src/java/org/apache/lucene/store/DefaultIOContext.java b/lucene/core/src/java/org/apache/lucene/store/DefaultIOContext.javanew file mode 100644index 00000000000..a5fb3c10995--- /dev/null+++ b/lucene/core/src/java/org/apache/lucene/store/DefaultIOContext.java@@ -0,0 +1,83 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.store;++import java.util.Arrays;+import java.util.List;+import java.util.Map;+import java.util.Objects;+import java.util.Optional;+import java.util.Set;+import java.util.stream.Collectors;++record DefaultIOContext(Optional<ReadAdvice> readAdvice, Set<FileOpenHint> hints)+ implements IOContext {++ public DefaultIOContext {+ Objects.requireNonNull(readAdvice);+ Objects.requireNonNull(hints);+ if (readAdvice.isPresent() && !hints.isEmpty())+ throw new IllegalArgumentException("Either ReadAdvice or hints can be specified, not both");++ // there should only be one hint of each type in the IOContext+ Map<Class<? extends FileOpenHint>, List<FileOpenHint>> hintClasses =+ hints.stream().collect(Collectors.groupingBy(IOContext.FileOpenHint::getClass));+ for (var hintType : hintClasses.entrySet()) {+ if (hintType.getValue().size() > 1) {+ throw new IllegalArgumentException("Multiple hints of type " + hintType + " specified");+ }+ }+ }++ public DefaultIOContext(Optional<ReadAdvice> readAdvice, FileOpenHint... hints) {+ this(readAdvice, Set.of(hints));+ }++ @Override+ public Context context() {+ return Context.DEFAULT;+ }++ @Override+ public MergeInfo mergeInfo() {+ return null;+ }++ @Override+ public FlushInfo flushInfo() {+ return null;+ }++ @Override+ public IOContext withHints(FileOpenHint... hints) {+ if (readAdvice().isPresent())+ throw new IllegalArgumentException("ReadAdvice has been specified directly");+ // TODO: see if this is needed or not+ if (!hints().isEmpty()) throw new IllegalArgumentException("Hints have already been specified");+ return new DefaultIOContext(Optional.empty(), hints);+ }++ private static final DefaultIOContext[] READADVICE_TO_IOCONTEXT =+ Arrays.stream(ReadAdvice.values())+ .map(r -> new DefaultIOContext(Optional.of(r)))+ .toArray(DefaultIOContext[]::new);++ @Override+ public DefaultIOContext withReadAdvice(ReadAdvice advice) {+ return READADVICE_TO_IOCONTEXT[advice.ordinal()];+ }+}diff --git a/lucene/core/src/java/org/apache/lucene/store/FileDataHint.java b/lucene/core/src/java/org/apache/lucene/store/FileDataHint.javanew file mode 100644index 00000000000..bd5f50e7f96--- /dev/null+++ b/lucene/core/src/java/org/apache/lucene/store/FileDataHint.java@@ -0,0 +1,27 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.store;++/** Hints on the type of data stored in the file */+public enum FileDataHint implements IOContext.FileOpenHint {+ /** The file contains postings data */+ POSTINGS,+ /** The file contains stored fields */+ STORED_FIELDS,+ /** The file contains vector data for kNN search */+ KNN_VECTORS+}diff --git a/lucene/core/src/java/org/apache/lucene/store/FileTypeHint.java b/lucene/core/src/java/org/apache/lucene/store/FileTypeHint.javanew file mode 100644index 00000000000..94eb8e35bb5--- /dev/null+++ b/lucene/core/src/java/org/apache/lucene/store/FileTypeHint.java@@ -0,0 +1,27 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.store;++/** Hints on the type of file being opened */+public enum FileTypeHint implements IOContext.FileOpenHint {+ /** The file contains metadata */+ METADATA,+ /** The file contains field data */+ DATA,+ /** The file contains indexes */+ INDEX+}diff --git a/lucene/core/src/java/org/apache/lucene/store/IOContext.java b/lucene/core/src/java/org/apache/lucene/store/IOContext.javaindex 5f341609748..9ba6fd546e2 100644--- a/lucene/core/src/java/org/apache/lucene/store/IOContext.java+++ b/lucene/core/src/java/org/apache/lucene/store/IOContext.java@@ -16,26 +16,20 @@*/package org.apache.lucene.store;-import java.util.Arrays;import java.util.Objects;-import org.apache.lucene.util.Constants;+import java.util.Optional;+import java.util.Set;/*** IOContext holds additional details on the merge/search context. An IOContext object can never be* passed as a {@code null} parameter to either {@link* org.apache.lucene.store.Directory#openInput(String, IOContext)} or {@link* org.apache.lucene.store.Directory#createOutput(String, IOContext)}- *- * @param context An object of a enumerator Context type- * @param mergeInfo must be given when {@code context == MERGE}- * @param flushInfo must be given when {@code context == FLUSH}- * @param readAdvice Advice regarding the read access pattern*/-public record IOContext(- Context context, MergeInfo mergeInfo, FlushInfo flushInfo, ReadAdvice readAdvice) {+public interface IOContext {/** Context is an enumerator which specifies the context in which the Directory is being used. */- public enum Context {+ enum Context {/** Context for reads and writes that are associated with a merge. */MERGE,/** Context for writes that are associated with a segment flush. */@@ -44,6 +38,9 @@ public record IOContext(DEFAULT};+ /** Implemented by classes that can specify hints on how the file will be used */+ interface FileOpenHint {}+/*** A default context for normal reads/writes. Use {@link #withReadAdvice(ReadAdvice)} to specify* another {@link ReadAdvice}.@@ -51,7 +48,7 @@ public record IOContext(* <p>It will use {@link ReadAdvice#RANDOM} by default, unless set by system property {@code* org.apache.lucene.store.defaultReadAdvice}.*/- public static final IOContext DEFAULT = new IOContext(Constants.DEFAULT_READADVICE);+ IOContext DEFAULT = new DefaultIOContext(Optional.empty());/*** A default context for reads with {@link ReadAdvice#SEQUENTIAL}.@@ -59,56 +56,116 @@ public record IOContext(* <p>This context should only be used when the read operations will be performed in the same* thread as the thread that opens the underlying storage.*/- public static final IOContext READONCE = new IOContext(ReadAdvice.SEQUENTIAL);-- @SuppressWarnings("incomplete-switch")- public IOContext {- Objects.requireNonNull(context, "context must not be null");- Objects.requireNonNull(readAdvice, "readAdvice must not be null");- switch (context) {- case MERGE ->- Objects.requireNonNull(mergeInfo, "mergeInfo must not be null if context is MERGE");- case FLUSH ->- Objects.requireNonNull(flushInfo, "flushInfo must not be null if context is FLUSH");- }- if ((context == Context.FLUSH || context == Context.MERGE)- && readAdvice != ReadAdvice.SEQUENTIAL) {- throw new IllegalArgumentException(- "The FLUSH and MERGE contexts must use the SEQUENTIAL read access advice");- }- }+ IOContext READONCE = new DefaultIOContext(Optional.of(ReadAdvice.SEQUENTIAL));- /** Creates a default {@link IOContext} for reading/writing with the given {@link ReadAdvice} */- private IOContext(ReadAdvice accessAdvice) {- this(Context.DEFAULT, null, null, accessAdvice);- }+ /** Returns an {@link IOContext} for merging with the specified {@link MergeInfo} */+ static IOContext merge(MergeInfo mergeInfo) {+ Objects.requireNonNull(mergeInfo);+ return new IOContext() {+ @Override+ public Context context() {+ return Context.MERGE;+ }++ @Override+ public MergeInfo mergeInfo() {+ return mergeInfo;+ }++ @Override+ public FlushInfo flushInfo() {+ return null;+ }- /** Creates an {@link IOContext} for flushing. */- public IOContext(FlushInfo flushInfo) {- this(Context.FLUSH, null, flushInfo, ReadAdvice.SEQUENTIAL);+ @Override+ public Set<FileOpenHint> hints() {+ return Set.of();+ }++ @Override+ public IOContext withHints(FileOpenHint... hints) {+ return this;+ }++ @Override+ public Optional<ReadAdvice> readAdvice() {+ return Optional.of(ReadAdvice.SEQUENTIAL);+ }++ @Override+ public IOContext withReadAdvice(ReadAdvice advice) {+ return this;+ }+ };}- /** Creates an {@link IOContext} for merging. */- public IOContext(MergeInfo mergeInfo) {- // Merges read input segments sequentially.- this(Context.MERGE, mergeInfo, null, ReadAdvice.SEQUENTIAL);+ /** Returns an {@link IOContext} for flushing with the specified {@link FlushInfo} */+ static IOContext flush(FlushInfo flushInfo) {+ Objects.requireNonNull(flushInfo);+ return new IOContext() {+ @Override+ public Context context() {+ return Context.FLUSH;+ }++ @Override+ public MergeInfo mergeInfo() {+ return null;+ }++ @Override+ public FlushInfo flushInfo() {+ return flushInfo;+ }++ @Override+ public Set<FileOpenHint> hints() {+ return Set.of();+ }++ @Override+ public IOContext withHints(FileOpenHint... hints) {+ return this;+ }++ @Override+ public Optional<ReadAdvice> readAdvice() {+ return Optional.of(ReadAdvice.SEQUENTIAL);+ }++ @Override+ public IOContext withReadAdvice(ReadAdvice advice) {+ return this;+ }+ };}- private static final IOContext[] READADVICE_TO_IOCONTEXT =- Arrays.stream(ReadAdvice.values()).map(IOContext::new).toArray(IOContext[]::new);+ /** The {@link Context} this context is for */+ Context context();++ /** Merge info, if {@link #context()} is {@link Context#MERGE} */+ MergeInfo mergeInfo();++ /** Flush info, if {@link #context()} is {@link Context#FLUSH} */+ FlushInfo flushInfo();++ /** Any hints on how the file will be opened */+ Set<FileOpenHint> hints();++ /** Sets the hints on this IOContext, if it makes sense to do so for this specific context */+ IOContext withHints(FileOpenHint... hints);++ /** Advice on the expected read access pattern */+ @Deprecated+ Optional<ReadAdvice> readAdvice();/**- * Return an updated {@link IOContext} that has the provided {@link ReadAdvice} if the {@link- * Context} is a {@link Context#DEFAULT} context, otherwise return this existing instance. This+ * Return an updated {@link IOContext} that has the provided {@link ReadAdvice}, if the provided+ * advice is compatible with the current context, otherwise return this existing instance. This* helps preserve a {@link ReadAdvice#SEQUENTIAL} advice for merging, which is always the right* choice, while allowing {@link IndexInput}s open for searching to use arbitrary {@link* ReadAdvice}s.*/- public IOContext withReadAdvice(ReadAdvice advice) {- if (context == Context.DEFAULT) {- return READADVICE_TO_IOCONTEXT[advice.ordinal()];- } else {- return this;- }- }+ @Deprecated+ IOContext withReadAdvice(ReadAdvice advice);}diff --git a/lucene/core/src/java/org/apache/lucene/store/IndexInput.java b/lucene/core/src/java/org/apache/lucene/store/IndexInput.javaindex b649ace8c4d..4997732ec3f 100644--- a/lucene/core/src/java/org/apache/lucene/store/IndexInput.java+++ b/lucene/core/src/java/org/apache/lucene/store/IndexInput.java@@ -124,18 +124,16 @@ public abstract class IndexInput extends DataInput implements Closeable {throws IOException;/**- * Create a slice with a specific {@link ReadAdvice}. This is typically used by {@link- * CompoundFormat} implementations to honor the {@link ReadAdvice} of each file within the- * compound file.+ * Create a slice with a new {@link IOContext}. This is typically used by {@link CompoundFormat}+ * implementations to modify the {@link IOContext} for specific files within the compound file.*- * <p><b>NOTE</b>: it is only legal to call this method if this {@link IndexInput} has been open- * with {@link ReadAdvice#NORMAL}. However, this method accepts any {@link ReadAdvice} value but- * {@code null} as a read advice for the slice.+ * <p><b>NOTE</b>: only certain {@link IOContext} may be usable here, depending on how this+ * instance was opened.** <p>The default implementation delegates to {@link #slice(String, long, long)} and ignores the- * {@link ReadAdvice}.+ * {@link IOContext}.*/- public IndexInput slice(String sliceDescription, long offset, long length, ReadAdvice readAdvice)+ public IndexInput slice(String sliceDescription, long offset, long length, IOContext context)throws IOException {return slice(sliceDescription, offset, length);}diff --git a/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java b/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.javaindex d5abc136b9f..35b83f77db2 100644--- a/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java+++ b/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java@@ -26,6 +26,7 @@ import java.nio.file.Path;import java.util.Locale;import java.util.Optional;import java.util.concurrent.Future;+import java.util.function.BiFunction;import java.util.function.BiPredicate;import java.util.function.Function;import java.util.logging.Logger;@@ -129,13 +130,8 @@ public class MMapDirectory extends FSDirectory {return Optional.of(groupKey);};- /**- * Argument for {@link #setPreload(BiPredicate)} that configures files to be preloaded upon- * opening them if they use the {@link ReadAdvice#RANDOM_PRELOAD} advice.- */- public static final BiPredicate<String, IOContext> BASED_ON_LOAD_IO_CONTEXT =- (_, context) -> context.readAdvice() == ReadAdvice.RANDOM_PRELOAD;-+ private BiFunction<String, IOContext, Optional<ReadAdvice>> readAdvice =+ (_, _) -> Optional.empty();private BiPredicate<String, IOContext> preload = NO_FILES;/**@@ -235,6 +231,20 @@ public class MMapDirectory extends FSDirectory {this.preload = preload;}+ /**+ * Configure {@link ReadAdvice} overrides for certain files. If the function returns {@code+ * Optional.empty()}, a default {@link ReadAdvice} will be used+ *+ * @param toReadAdvice a {@link Function} whose first argument is the file name, and second+ * argument is the {@link IOContext} used to open the file. Returns {@code+ * Optional.of(ReadAdvice)} to use a specific read advice, or {@code Optional.empty()} if a+ * default should be used+ */+ public void setReadAdviceOverride(+ BiFunction<String, IOContext, Optional<ReadAdvice>> toReadAdvice) {+ this.readAdvice = toReadAdvice;+ }+/*** Configures a grouping function for files that are part of the same logical group. The gathering* of files into a logical group is a hint that allows for better handling of resources.@@ -267,8 +277,11 @@ public class MMapDirectory extends FSDirectory {Path path = directory.resolve(name);return PROVIDER.openInput(path,- context,chunkSizePower,+ readAdvice+ .apply(name, context)+ .orElseGet(() -> context.readAdvice().orElse(Constants.DEFAULT_READADVICE)),+ context == IOContext.READONCE,preload.test(name, context),groupingFunction.apply(name),attachment);@@ -280,8 +293,9 @@ public class MMapDirectory extends FSDirectory {interface MMapIndexInputProvider<A> {IndexInput openInput(Path path,- IOContext context,int chunkSizePower,+ ReadAdvice readAdvice,+ boolean readOnce,boolean preload,Optional<String> group,A attachment)diff --git a/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInput.java b/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInput.javaindex 800a66a2167..5404e1aafc7 100644--- a/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInput.java+++ b/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInput.java@@ -616,8 +616,9 @@ abstract class MemorySegmentIndexInput extends IndexInput@Overridepublic final MemorySegmentIndexInput slice(- String sliceDescription, long offset, long length, ReadAdvice advice) throws IOException {+ String sliceDescription, long offset, long length, IOContext context) throws IOException {MemorySegmentIndexInput slice = slice(sliceDescription, offset, length);+ ReadAdvice advice = context.readAdvice().orElse(Constants.DEFAULT_READADVICE);if (NATIVE_ACCESS.isPresent() && advice != ReadAdvice.NORMAL) {// No need to madvise with a normal advice, since it's the OS' default.final NativeAccess nativeAccess = NATIVE_ACCESS.get();diff --git a/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInputProvider.java b/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInputProvider.javaindex 7cbe376678b..f740b1a372b 100644--- a/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInputProvider.java+++ b/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInputProvider.java@@ -45,8 +45,9 @@ final class MemorySegmentIndexInputProvider@Overridepublic IndexInput openInput(Path path,- IOContext context,int chunkSizePower,+ ReadAdvice readAdvice,+ boolean confined,boolean preload,Optional<String> group,ConcurrentHashMap<String, RefCountedSharedArena> arenas)@@ -57,7 +58,6 @@ final class MemorySegmentIndexInputProviderpath = Unwrappable.unwrapAll(path);boolean success = false;- final boolean confined = context == IOContext.READONCE;final Arena arena = confined ? Arena.ofConfined() : getSharedArena(group, arenas);try (var fc = FileChannel.open(path, StandardOpenOption.READ)) {final long fileSize = fc.size();@@ -65,14 +65,7 @@ final class MemorySegmentIndexInputProviderMemorySegmentIndexInput.newInstance(resourceDescription,arena,- map(- arena,- resourceDescription,- fc,- context.readAdvice(),- chunkSizePower,- preload,- fileSize),+ map(arena, resourceDescription, fc, readAdvice, chunkSizePower, preload, fileSize),fileSize,chunkSizePower,confined);diff --git a/lucene/core/src/test/org/apache/lucene/index/TestDoc.java b/lucene/core/src/test/org/apache/lucene/index/TestDoc.javaindex 0cdd645e7e0..ffc9344f853 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestDoc.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestDoc.java@@ -210,7 +210,7 @@ public class TestDoc extends LuceneTestCase {String merged,boolean useCompoundFile)throws Exception {- IOContext context = newIOContext(random(), new IOContext(new MergeInfo(-1, -1, false, -1)));+ IOContext context = newIOContext(random(), IOContext.merge(new MergeInfo(-1, -1, false, -1)));SegmentReader r1 = new SegmentReader(si1, Version.LATEST.major, context);SegmentReader r2 = new SegmentReader(si2, Version.LATEST.major, context);diff --git a/lucene/core/src/test/org/apache/lucene/index/TestSegmentMerger.java b/lucene/core/src/test/org/apache/lucene/index/TestSegmentMerger.javaindex d3f7ec8a803..432f0258060 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestSegmentMerger.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestSegmentMerger.java@@ -106,7 +106,7 @@ public class TestSegmentMerger extends LuceneTestCase {InfoStream.getDefault(),mergedDir,new FieldInfos.FieldNumbers(null, null),- newIOContext(random(), new IOContext(new MergeInfo(-1, -1, false, -1))),+ newIOContext(random(), IOContext.merge(new MergeInfo(-1, -1, false, -1))),new SameThreadExecutorService());MergeState mergeState = merger.merge();int docsMerged = mergeState.segmentInfo.maxDoc();diff --git a/lucene/core/src/test/org/apache/lucene/index/TestStoredFieldsConsumer.java b/lucene/core/src/test/org/apache/lucene/index/TestStoredFieldsConsumer.javaindex e0c46bc58a7..984af398aa3 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestStoredFieldsConsumer.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestStoredFieldsConsumer.java@@ -74,7 +74,7 @@ public class TestStoredFieldsConsumer extends LuceneTestCase {si,new FieldInfos(new FieldInfo[0]),null,- new IOContext(new FlushInfo(numDocs, 10)));+ IOContext.flush(new FlushInfo(numDocs, 10)));consumer.flush(state, null);dir.close();diff --git a/lucene/core/src/test/org/apache/lucene/store/TestNRTCachingDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestNRTCachingDirectory.javaindex 1588baf9be9..ce047b1fa73 100644--- a/lucene/core/src/test/org/apache/lucene/store/TestNRTCachingDirectory.java+++ b/lucene/core/src/test/org/apache/lucene/store/TestNRTCachingDirectory.java@@ -149,7 +149,7 @@ public class TestNRTCachingDirectory extends BaseDirectoryTestCase {return cache;}};- IOContext ioContext = new IOContext(new FlushInfo(3, 42));+ IOContext ioContext = IOContext.flush(new FlushInfo(3, 42));nrtDir1.createOutput("foo", ioContext).close();nrtDir1.createTempOutput("bar", "baz", ioContext).close();@@ -170,7 +170,7 @@ public class TestNRTCachingDirectory extends BaseDirectoryTestCase {}public void testCacheSizeAfterDelete() throws IOException {- IOContext ioContext = new IOContext(new FlushInfo(3, 40));+ IOContext ioContext = IOContext.flush(new FlushInfo(3, 40));String fn = "f1";try (Directory dir = newDirectory();NRTCachingDirectory nrt = new NRTCachingDirectory(dir, 1, 1); ) {diff --git a/lucene/misc/src/test/org/apache/lucene/misc/store/TestByteWritesTrackingDirectoryWrapper.java b/lucene/misc/src/test/org/apache/lucene/misc/store/TestByteWritesTrackingDirectoryWrapper.javaindex 0b64ed71095..70c2d959b49 100644--- a/lucene/misc/src/test/org/apache/lucene/misc/store/TestByteWritesTrackingDirectoryWrapper.java+++ b/lucene/misc/src/test/org/apache/lucene/misc/store/TestByteWritesTrackingDirectoryWrapper.java@@ -43,7 +43,7 @@ public class TestByteWritesTrackingDirectoryWrapper extends BaseDirectoryTestCasint expectedMergeBytes = random().nextInt(100);IndexOutput output =- dir.createOutput("write", new IOContext(new FlushInfo(10, expectedFlushBytes)));+ dir.createOutput("write", IOContext.flush(new FlushInfo(10, expectedFlushBytes)));byte[] flushBytesArr = new byte[expectedFlushBytes];for (int i = 0; i < expectedFlushBytes; i++) {flushBytesArr[i] = (byte) random().nextInt(127);@@ -55,7 +55,7 @@ public class TestByteWritesTrackingDirectoryWrapper extends BaseDirectoryTestCas// now merge bytesoutput =- dir.createOutput("merge", new IOContext(new MergeInfo(10, expectedMergeBytes, false, 2)));+ dir.createOutput("merge", IOContext.merge(new MergeInfo(10, expectedMergeBytes, false, 2)));byte[] mergeBytesArr = new byte[expectedMergeBytes];for (int i = 0; i < expectedMergeBytes; i++) {mergeBytesArr[i] = (byte) random().nextInt(127);@@ -77,7 +77,8 @@ public class TestByteWritesTrackingDirectoryWrapper extends BaseDirectoryTestCasint expectedMergeBytes = random().nextInt(100);IndexOutput output =- dir.createTempOutput("temp", "write", new IOContext(new FlushInfo(10, expectedFlushBytes)));+ dir.createTempOutput(+ "temp", "write", IOContext.flush(new FlushInfo(10, expectedFlushBytes)));byte[] flushBytesArr = new byte[expectedFlushBytes];for (int i = 0; i < expectedFlushBytes; i++) {flushBytesArr[i] = (byte) random().nextInt(127);@@ -90,7 +91,7 @@ public class TestByteWritesTrackingDirectoryWrapper extends BaseDirectoryTestCas// now merge bytesoutput =dir.createTempOutput(- "temp", "merge", new IOContext(new MergeInfo(10, expectedMergeBytes, false, 2)));+ "temp", "merge", IOContext.merge(new MergeInfo(10, expectedMergeBytes, false, 2)));byte[] mergeBytesArr = new byte[expectedMergeBytes];for (int i = 0; i < expectedMergeBytes; i++) {mergeBytesArr[i] = (byte) random().nextInt(127);diff --git a/lucene/misc/src/test/org/apache/lucene/misc/store/TestDirectIODirectory.java b/lucene/misc/src/test/org/apache/lucene/misc/store/TestDirectIODirectory.javaindex 6d40dcce8e3..3b635dfe956 100644--- a/lucene/misc/src/test/org/apache/lucene/misc/store/TestDirectIODirectory.java+++ b/lucene/misc/src/test/org/apache/lucene/misc/store/TestDirectIODirectory.java@@ -167,45 +167,45 @@ public class TestDirectIODirectory extends BaseDirectoryTestCase {assertTrue(dir.useDirectIO("dummy",- new IOContext(new MergeInfo(numDocs, largeSize, true, -1)),+ IOContext.merge(new MergeInfo(numDocs, largeSize, true, -1)),OptionalLong.empty()));assertFalse(dir.useDirectIO("dummy",- new IOContext(new MergeInfo(numDocs, smallSize, true, -1)),+ IOContext.merge(new MergeInfo(numDocs, smallSize, true, -1)),OptionalLong.empty()));assertTrue(dir.useDirectIO("dummy",- new IOContext(new MergeInfo(numDocs, largeSize, true, -1)),+ IOContext.merge(new MergeInfo(numDocs, largeSize, true, -1)),OptionalLong.of(largeSize)));assertFalse(dir.useDirectIO("dummy",- new IOContext(new MergeInfo(numDocs, smallSize, true, -1)),+ IOContext.merge(new MergeInfo(numDocs, smallSize, true, -1)),OptionalLong.of(smallSize)));assertFalse(dir.useDirectIO("dummy",- new IOContext(new MergeInfo(numDocs, smallSize, true, -1)),+ IOContext.merge(new MergeInfo(numDocs, smallSize, true, -1)),OptionalLong.of(largeSize)));assertFalse(dir.useDirectIO("dummy",- new IOContext(new MergeInfo(numDocs, largeSize, true, -1)),+ IOContext.merge(new MergeInfo(numDocs, largeSize, true, -1)),OptionalLong.of(smallSize)));assertFalse(dir.useDirectIO(- "dummy", new IOContext(new FlushInfo(numDocs, largeSize)), OptionalLong.empty()));+ "dummy", IOContext.flush(new FlushInfo(numDocs, largeSize)), OptionalLong.empty()));assertFalse(dir.useDirectIO(- "dummy", new IOContext(new FlushInfo(numDocs, smallSize)), OptionalLong.empty()));+ "dummy", IOContext.flush(new FlushInfo(numDocs, smallSize)), OptionalLong.empty()));assertFalse(dir.useDirectIO("dummy",- new IOContext(new FlushInfo(numDocs, largeSize)),+ IOContext.flush(new FlushInfo(numDocs, largeSize)),OptionalLong.of(largeSize)));}}diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseCompoundFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseCompoundFormatTestCase.javaindex 0fe563d7550..4910562cf38 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseCompoundFormatTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseCompoundFormatTestCase.java@@ -171,7 +171,7 @@ public abstract class BaseCompoundFormatTestCase extends BaseIndexFileFormatTest// LUCENE-5724: actually test we play nice with NRTCachingDir and massive filepublic void testLargeCFS() throws IOException {final String testfile = "_123.test";- IOContext context = new IOContext(new FlushInfo(0, 512 * 1024 * 1024));+ IOContext context = IOContext.flush(new FlushInfo(0, 512 * 1024 * 1024));Directory dir = new NRTCachingDirectory(newFSDirectory(createTempDir()), 2.0, 25.0);diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseIndexFileFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseIndexFileFormatTestCase.javaindex a0aa15e8be3..3e70bfc652b 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseIndexFileFormatTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseIndexFileFormatTestCase.java@@ -376,7 +376,7 @@ public abstract class BaseIndexFileFormatTestCase extends LuceneTestCase {SegmentWriteState writeState =new SegmentWriteState(- null, dir, segmentInfo, fieldInfos, null, new IOContext(new FlushInfo(1, 20)));+ null, dir, segmentInfo, fieldInfos, null, IOContext.flush(new FlushInfo(1, 20)));SegmentReadState readState =new SegmentReadState(dir, segmentInfo, fieldInfos, IOContext.DEFAULT);diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/index/RandomPostingsTester.java b/lucene/test-framework/src/java/org/apache/lucene/tests/index/RandomPostingsTester.javaindex f36edfc876b..2c27f55211c 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/index/RandomPostingsTester.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/index/RandomPostingsTester.java@@ -763,7 +763,7 @@ public class RandomPostingsTester {segmentInfo,newFieldInfos,null,- new IOContext(new FlushInfo(maxDoc, bytes)));+ IOContext.flush(new FlushInfo(maxDoc, bytes)));Fields seedFields = new SeedFields(fields, newFieldInfos, maxAllowed, allowPayloads);diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockDirectoryWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockDirectoryWrapper.javaindex 5f329209d80..2f30a8cda50 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockDirectoryWrapper.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockDirectoryWrapper.java@@ -53,7 +53,6 @@ import org.apache.lucene.store.IOContext;import org.apache.lucene.store.IndexInput;import org.apache.lucene.store.IndexOutput;import org.apache.lucene.store.Lock;-import org.apache.lucene.store.ReadAdvice;import org.apache.lucene.tests.util.LuceneTestCase;import org.apache.lucene.tests.util.TestUtil;import org.apache.lucene.tests.util.ThrottledIndexOutput;@@ -813,8 +812,6 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {false);}- // record the read advice before randomizing the context- ReadAdvice readAdvice = context.readAdvice();context = LuceneTestCase.newIOContext(randomState, context);final boolean confined = context == IOContext.READONCE;if (name.startsWith(IndexFileNames.SEGMENTS) && confined == false) {@@ -834,15 +831,15 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {System.out.println("MockDirectoryWrapper: using SlowClosingMockIndexInputWrapper for file " + name);}- ii = new SlowClosingMockIndexInputWrapper(this, name, delegateInput, readAdvice, confined);+ ii = new SlowClosingMockIndexInputWrapper(this, name, delegateInput, confined);} else if (useSlowOpenClosers && randomInt == 1) {if (LuceneTestCase.VERBOSE) {System.out.println("MockDirectoryWrapper: using SlowOpeningMockIndexInputWrapper for file " + name);}- ii = new SlowOpeningMockIndexInputWrapper(this, name, delegateInput, readAdvice, confined);+ ii = new SlowOpeningMockIndexInputWrapper(this, name, delegateInput, confined);} else {- ii = new MockIndexInputWrapper(this, name, delegateInput, null, readAdvice, confined);+ ii = new MockIndexInputWrapper(this, name, delegateInput, null, confined);}addFileHandle(ii, name, Handle.Input);return ii;diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockIndexInputWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockIndexInputWrapper.javaindex 1dc5456d64e..7410c258954 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockIndexInputWrapper.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockIndexInputWrapper.java@@ -23,6 +23,7 @@ import java.util.Optional;import java.util.Set;import org.apache.lucene.internal.tests.TestSecrets;import org.apache.lucene.store.FilterIndexInput;+import org.apache.lucene.store.IOContext;import org.apache.lucene.store.IndexInput;import org.apache.lucene.store.ReadAdvice;@@ -43,7 +44,6 @@ public class MockIndexInputWrapper extends FilterIndexInput {private final MockIndexInputWrapper parent;private final boolean confined;private final Thread thread;- private ReadAdvice readAdvice;/** Sole constructor */public MockIndexInputWrapper(@@ -51,7 +51,6 @@ public class MockIndexInputWrapper extends FilterIndexInput {String name,IndexInput delegate,MockIndexInputWrapper parent,- ReadAdvice readAdvice,boolean confined) {super("MockIndexInputWrapper(name=" + name + " delegate=" + delegate + ")", delegate);@@ -61,7 +60,6 @@ public class MockIndexInputWrapper extends FilterIndexInput {this.parent = parent;this.name = name;this.dir = dir;- this.readAdvice = readAdvice;this.confined = confined;this.thread = Thread.currentThread();}@@ -112,8 +110,7 @@ public class MockIndexInputWrapper extends FilterIndexInput {dir.inputCloneCount.incrementAndGet();IndexInput iiclone = in.clone();MockIndexInputWrapper clone =- new MockIndexInputWrapper(- dir, name, iiclone, parent != null ? parent : this, readAdvice, confined);+ new MockIndexInputWrapper(dir, name, iiclone, parent != null ? parent : this, confined);// Pending resolution on LUCENE-686 we may want to// uncomment this code so that we also track that all// clones get closed:@@ -141,17 +138,13 @@ public class MockIndexInputWrapper extends FilterIndexInput {IndexInput slice = in.slice(sliceDescription, offset, length);MockIndexInputWrapper clone =new MockIndexInputWrapper(- dir, sliceDescription, slice, parent != null ? parent : this, readAdvice, confined);+ dir, sliceDescription, slice, parent != null ? parent : this, confined);return clone;}@Override- public IndexInput slice(String sliceDescription, long offset, long length, ReadAdvice readAdvice)+ public IndexInput slice(String sliceDescription, long offset, long length, IOContext context)throws IOException {- if (this.readAdvice != ReadAdvice.NORMAL) {- throw new IllegalStateException(- "slice() may only be called with a custom read advice on inputs that have been open with ReadAdvice.NORMAL");- }ensureOpen();if (dir.verboseClone) {new Exception("slice: " + this).printStackTrace(System.out);@@ -160,7 +153,7 @@ public class MockIndexInputWrapper extends FilterIndexInput {IndexInput slice = in.slice(sliceDescription, offset, length);MockIndexInputWrapper clone =new MockIndexInputWrapper(- dir, sliceDescription, slice, parent != null ? parent : this, readAdvice, confined);+ dir, sliceDescription, slice, parent != null ? parent : this, confined);return clone;}@@ -196,7 +189,6 @@ public class MockIndexInputWrapper extends FilterIndexInput {public void updateReadAdvice(ReadAdvice readAdvice) throws IOException {ensureOpen();ensureAccessible();- this.readAdvice = readAdvice;in.updateReadAdvice(readAdvice);}diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/store/SerialIOCountingDirectory.java b/lucene/test-framework/src/java/org/apache/lucene/tests/store/SerialIOCountingDirectory.javaindex 4d3c233257c..98793fc0f1e 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/store/SerialIOCountingDirectory.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/store/SerialIOCountingDirectory.java@@ -27,6 +27,7 @@ import org.apache.lucene.store.IOContext;import org.apache.lucene.store.IndexInput;import org.apache.lucene.store.ReadAdvice;import org.apache.lucene.util.CloseableThreadLocal;+import org.apache.lucene.util.Constants;/*** A {@link Directory} wrapper that counts the number of times that Lucene may wait for I/O to@@ -71,12 +72,13 @@ public class SerialIOCountingDirectory extends FilterDirectory {@Overridepublic IndexInput openInput(String name, IOContext context) throws IOException {- if (context.readAdvice() == ReadAdvice.RANDOM_PRELOAD) {+ ReadAdvice readAdvice = context.readAdvice().orElse(Constants.DEFAULT_READADVICE);+ if (readAdvice == ReadAdvice.RANDOM_PRELOAD) {// expected to be loaded in memory, only count 1 at open timecounter.increment();return super.openInput(name, context);}- return new SerializedIOCountingIndexInput(super.openInput(name, context), context.readAdvice());+ return new SerializedIOCountingIndexInput(super.openInput(name, context), readAdvice);}private class SerializedIOCountingIndexInput extends IndexInput {@@ -187,13 +189,16 @@ public class SerialIOCountingDirectory extends FilterDirectory {@Overridepublic IndexInput slice(String sliceDescription, long offset, long length) throws IOException {- return slice(sliceDescription, offset, length, readAdvice);+ return slice(offset, length, readAdvice);}@Override- public IndexInput slice(- String sliceDescription, long offset, long length, ReadAdvice readAdvice)+ public IndexInput slice(String sliceDescription, long offset, long length, IOContext context)throws IOException {+ return slice(offset, length, context.readAdvice().orElse(Constants.DEFAULT_READADVICE));+ }++ private IndexInput slice(long offset, long length, ReadAdvice readAdvice) throws IOException {if ((length | offset) < 0 || length > sliceLength - offset) {throw new IllegalArgumentException();}diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/store/SlowClosingMockIndexInputWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/tests/store/SlowClosingMockIndexInputWrapper.javaindex 851860f1c64..1f9e61f5195 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/store/SlowClosingMockIndexInputWrapper.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/store/SlowClosingMockIndexInputWrapper.java@@ -19,7 +19,6 @@ package org.apache.lucene.tests.store;import java.io.IOException;import org.apache.lucene.internal.tests.TestSecrets;import org.apache.lucene.store.IndexInput;-import org.apache.lucene.store.ReadAdvice;import org.apache.lucene.util.SuppressForbidden;import org.apache.lucene.util.ThreadInterruptedException;@@ -36,12 +35,8 @@ class SlowClosingMockIndexInputWrapper extends MockIndexInputWrapper {}public SlowClosingMockIndexInputWrapper(- MockDirectoryWrapper dir,- String name,- IndexInput delegate,- ReadAdvice readAdvice,- boolean confined) {- super(dir, name, delegate, null, readAdvice, confined);+ MockDirectoryWrapper dir, String name, IndexInput delegate, boolean confined) {+ super(dir, name, delegate, null, confined);}@SuppressForbidden(reason = "Thread sleep")diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/store/SlowOpeningMockIndexInputWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/tests/store/SlowOpeningMockIndexInputWrapper.javaindex 0d75408ec8e..033785af9c7 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/store/SlowOpeningMockIndexInputWrapper.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/store/SlowOpeningMockIndexInputWrapper.java@@ -19,7 +19,6 @@ package org.apache.lucene.tests.store;import java.io.IOException;import org.apache.lucene.internal.tests.TestSecrets;import org.apache.lucene.store.IndexInput;-import org.apache.lucene.store.ReadAdvice;import org.apache.lucene.util.SuppressForbidden;import org.apache.lucene.util.ThreadInterruptedException;@@ -36,13 +35,9 @@ class SlowOpeningMockIndexInputWrapper extends MockIndexInputWrapper {@SuppressForbidden(reason = "Thread sleep")public SlowOpeningMockIndexInputWrapper(- MockDirectoryWrapper dir,- String name,- IndexInput delegate,- ReadAdvice readAdvice,- boolean confined)+ MockDirectoryWrapper dir, String name, IndexInput delegate, boolean confined)throws IOException {- super(dir, name, delegate, null, readAdvice, confined);+ super(dir, name, delegate, null, confined);try {Thread.sleep(50);} catch (InterruptedException ie) {diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.javaindex 922762a1e1a..ddf707a2a68 100644--- a/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/util/LuceneTestCase.java@@ -1816,13 +1816,13 @@ public abstract class LuceneTestCase extends Assert {if (oldContext.flushInfo() != null) {// Always return at least the estimatedSegmentSize of// the incoming IOContext:- return new IOContext(+ return IOContext.flush(new FlushInfo(randomNumDocs, Math.max(oldContext.flushInfo().estimatedSegmentSize(), size)));} else if (oldContext.mergeInfo() != null) {// Always return at least the estimatedMergeBytes of// the incoming IOContext:- return new IOContext(+ return IOContext.merge(new MergeInfo(randomNumDocs,Math.max(oldContext.mergeInfo().estimatedMergeBytes(), size),@@ -1836,10 +1836,10 @@ public abstract class LuceneTestCase extends Assert {context = IOContext.DEFAULT;break;case 1:- context = new IOContext(new MergeInfo(randomNumDocs, size, true, -1));+ context = IOContext.merge(new MergeInfo(randomNumDocs, size, true, -1));break;case 2:- context = new IOContext(new FlushInfo(randomNumDocs, size));+ context = IOContext.flush(new FlushInfo(randomNumDocs, size));break;default:context = IOContext.DEFAULT;