Skip to content

Commit 440ddf1

Browse files
committed
Map from IOContext to ReadAdvice
1 parent edfc21e commit 440ddf1

File tree

11 files changed

+59
-48
lines changed

11 files changed

+59
-48
lines changed

lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/lucene50/Lucene50CompoundReader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public IndexInput openInput(String name, IOContext context) throws IOException {
171171
+ entries.keySet()
172172
+ ")");
173173
}
174-
return handle.slice(name, entry.offset, entry.length, context.readAdvice());
174+
return handle.slice(name, entry.offset, entry.length, toReadAdvice(context));
175175
}
176176

177177
/** Returns an array of strings, one for each file in the directory. */

lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextCompoundFormat.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public IndexInput openInput(String name, IOContext context) throws IOException {
140140
name,
141141
startOffsets[index],
142142
endOffsets[index] - startOffsets[index],
143-
context.readAdvice());
143+
toReadAdvice(context));
144144
}
145145

146146
@Override

lucene/core/src/java/org/apache/lucene/codecs/lucene90/Lucene90CompoundReader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public IndexInput openInput(String name, IOContext context) throws IOException {
169169
+ entries.keySet()
170170
+ ")");
171171
}
172-
return handle.slice(name, entry.offset, entry.length, context.readAdvice());
172+
return handle.slice(name, entry.offset, entry.length, toReadAdvice(context));
173173
}
174174

175175
/** Returns an array of strings, one for each file in the directory. */

lucene/core/src/java/org/apache/lucene/store/DefaultIOContext.java

+15-8
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,16 @@
1818

1919
import java.util.Arrays;
2020
import java.util.Objects;
21-
import org.apache.lucene.util.Constants;
21+
import java.util.Optional;
2222

23-
record DefaultIOContext(ReadAdvice readAdvice, FileOpenHint... hints) implements IOContext {
23+
record DefaultIOContext(Optional<ReadAdvice> readAdvice, FileOpenHint... hints)
24+
implements IOContext {
2425

2526
public DefaultIOContext {
2627
Objects.requireNonNull(readAdvice);
2728
Objects.requireNonNull(hints);
28-
29-
// either hints or readadvice should be specified, not both
30-
if (hints.length > 0 && readAdvice != Constants.DEFAULT_READADVICE) {
31-
throw new IllegalArgumentException("Either readAdvice or hints should be specified");
32-
}
29+
if (readAdvice.isPresent() && hints.length > 0)
30+
throw new IllegalArgumentException("Either ReadAdvice or hints can be specified, not both");
3331
}
3432

3533
@Override
@@ -47,9 +45,18 @@ public FlushInfo flushInfo() {
4745
return null;
4846
}
4947

48+
@Override
49+
public IOContext withHints(FileOpenHint... hints) {
50+
if (readAdvice.isPresent())
51+
throw new IllegalArgumentException("ReadAdvice has been specified directly");
52+
if (hints.length > 0) // TODO: see if this is needed or not
53+
throw new IllegalArgumentException("Hints have already been specified");
54+
return new DefaultIOContext(Optional.empty(), hints);
55+
}
56+
5057
private static final DefaultIOContext[] READADVICE_TO_IOCONTEXT =
5158
Arrays.stream(ReadAdvice.values())
52-
.map(DefaultIOContext::new)
59+
.map(r -> new DefaultIOContext(Optional.of(r)))
5360
.toArray(DefaultIOContext[]::new);
5461

5562
@Override

lucene/core/src/java/org/apache/lucene/store/Directory.java

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Set;
2828
import java.util.stream.Collectors;
2929
import org.apache.lucene.index.IndexFileNames;
30+
import org.apache.lucene.util.Constants;
3031
import org.apache.lucene.util.IOUtils;
3132

3233
/**
@@ -105,6 +106,10 @@ protected void validateIOContext(IOContext context) {
105106
}
106107
}
107108

109+
protected ReadAdvice toReadAdvice(IOContext context) {
110+
return context.readAdvice().orElse(Constants.DEFAULT_READADVICE);
111+
}
112+
108113
/**
109114
* Creates a new, empty file in the directory and returns an {@link IndexOutput} instance for
110115
* appending data to this file.

lucene/core/src/java/org/apache/lucene/store/IOContext.java

+21-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.apache.lucene.store;
1818

1919
import java.util.Objects;
20-
import org.apache.lucene.util.Constants;
20+
import java.util.Optional;
2121

2222
/**
2323
* IOContext holds additional details on the merge/search context. An IOContext object can never be
@@ -47,19 +47,15 @@ interface FileOpenHint {}
4747
* <p>It will use {@link ReadAdvice#RANDOM} by default, unless set by system property {@code
4848
* org.apache.lucene.store.defaultReadAdvice}.
4949
*/
50-
IOContext DEFAULT = new DefaultIOContext(Constants.DEFAULT_READADVICE);
50+
IOContext DEFAULT = new DefaultIOContext(Optional.empty());
5151

5252
/**
5353
* A default context for reads with {@link ReadAdvice#SEQUENTIAL}.
5454
*
5555
* <p>This context should only be used when the read operations will be performed in the same
5656
* thread as the thread that opens the underlying storage.
5757
*/
58-
IOContext READONCE = new DefaultIOContext(ReadAdvice.SEQUENTIAL);
59-
60-
static IOContext defaultContext(FileOpenHint... hints) {
61-
return new DefaultIOContext(Constants.DEFAULT_READADVICE, hints);
62-
}
58+
IOContext READONCE = new DefaultIOContext(Optional.of(ReadAdvice.SEQUENTIAL));
6359

6460
/** Returns an {@link IOContext} for merging with the specified {@link MergeInfo} */
6561
static IOContext merge(MergeInfo mergeInfo) {
@@ -86,8 +82,13 @@ public FileOpenHint[] hints() {
8682
}
8783

8884
@Override
89-
public ReadAdvice readAdvice() {
90-
return ReadAdvice.SEQUENTIAL;
85+
public IOContext withHints(FileOpenHint... hints) {
86+
return this;
87+
}
88+
89+
@Override
90+
public Optional<ReadAdvice> readAdvice() {
91+
return Optional.of(ReadAdvice.SEQUENTIAL);
9192
}
9293

9394
@Override
@@ -122,8 +123,13 @@ public FileOpenHint[] hints() {
122123
}
123124

124125
@Override
125-
public ReadAdvice readAdvice() {
126-
return ReadAdvice.SEQUENTIAL;
126+
public IOContext withHints(FileOpenHint... hints) {
127+
return this;
128+
}
129+
130+
@Override
131+
public Optional<ReadAdvice> readAdvice() {
132+
return Optional.of(ReadAdvice.SEQUENTIAL);
127133
}
128134

129135
@Override
@@ -145,9 +151,12 @@ public IOContext withReadAdvice(ReadAdvice advice) {
145151
/** Any hints on how the file will be opened */
146152
FileOpenHint[] hints();
147153

154+
/** Sets the hints on this IOContext, if it makes sense to do so for this specific context */
155+
IOContext withHints(FileOpenHint... hints);
156+
148157
/** Advice on the expected read access pattern */
149158
@Deprecated
150-
ReadAdvice readAdvice();
159+
Optional<ReadAdvice> readAdvice();
151160

152161
/**
153162
* Return an updated {@link IOContext} that has the provided {@link ReadAdvice}, if the provided

lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,6 @@ public class MMapDirectory extends FSDirectory {
129129
return Optional.of(groupKey);
130130
};
131131

132-
/**
133-
* Argument for {@link #setPreload(BiPredicate)} that configures files to be preloaded upon
134-
* opening them if they use the {@link ReadAdvice#RANDOM_PRELOAD} advice.
135-
*/
136-
public static final BiPredicate<String, IOContext> BASED_ON_LOAD_IO_CONTEXT =
137-
(_, context) -> context.readAdvice() == ReadAdvice.RANDOM_PRELOAD;
138-
139132
private BiPredicate<String, IOContext> preload = NO_FILES;
140133

141134
/**
@@ -268,8 +261,9 @@ public IndexInput openInput(String name, IOContext context) throws IOException {
268261
Path path = directory.resolve(name);
269262
return PROVIDER.openInput(
270263
path,
271-
context,
272264
chunkSizePower,
265+
toReadAdvice(context),
266+
context == IOContext.READONCE,
273267
preload.test(name, context),
274268
groupingFunction.apply(name),
275269
attachment);
@@ -281,8 +275,9 @@ public IndexInput openInput(String name, IOContext context) throws IOException {
281275
interface MMapIndexInputProvider<A> {
282276
IndexInput openInput(
283277
Path path,
284-
IOContext context,
285278
int chunkSizePower,
279+
ReadAdvice readAdvice,
280+
boolean readOnce,
286281
boolean preload,
287282
Optional<String> group,
288283
A attachment)

lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInputProvider.java

+3-10
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ final class MemorySegmentIndexInputProvider
4545
@Override
4646
public IndexInput openInput(
4747
Path path,
48-
IOContext context,
4948
int chunkSizePower,
49+
ReadAdvice readAdvice,
50+
boolean confined,
5051
boolean preload,
5152
Optional<String> group,
5253
ConcurrentHashMap<String, RefCountedSharedArena> arenas)
@@ -57,22 +58,14 @@ public IndexInput openInput(
5758
path = Unwrappable.unwrapAll(path);
5859

5960
boolean success = false;
60-
final boolean confined = context == IOContext.READONCE;
6161
final Arena arena = confined ? Arena.ofConfined() : getSharedArena(group, arenas);
6262
try (var fc = FileChannel.open(path, StandardOpenOption.READ)) {
6363
final long fileSize = fc.size();
6464
final IndexInput in =
6565
MemorySegmentIndexInput.newInstance(
6666
resourceDescription,
6767
arena,
68-
map(
69-
arena,
70-
resourceDescription,
71-
fc,
72-
context.readAdvice(),
73-
chunkSizePower,
74-
preload,
75-
fileSize),
68+
map(arena, resourceDescription, fc, readAdvice, chunkSizePower, preload, fileSize),
7669
fileSize,
7770
chunkSizePower,
7871
confined);

lucene/core/src/test/org/apache/lucene/store/TestDirectory.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,19 @@ public void testValidateIOContext() throws Exception {
134134
IllegalArgumentException.class,
135135
() ->
136136
fsDir.createOutput(
137-
"file", IOContext.defaultContext(FileTypeHint.DATA, FileTypeHint.METADATA)));
137+
"file", IOContext.DEFAULT.withHints(FileTypeHint.DATA, FileTypeHint.METADATA)));
138138
expectThrows(
139139
IllegalArgumentException.class,
140140
() ->
141141
fsDir.createOutput(
142-
"file", IOContext.defaultContext(FileDataHint.VECTORS, FileDataHint.POSTINGS)));
142+
"file",
143+
IOContext.DEFAULT.withHints(FileDataHint.VECTORS, FileDataHint.POSTINGS)));
143144
expectThrows(
144145
IllegalArgumentException.class,
145146
() ->
146147
fsDir.createOutput(
147148
"file",
148-
IOContext.defaultContext(DataAccessHint.RANDOM, DataAccessHint.SEQUENTIAL)));
149+
IOContext.DEFAULT.withHints(DataAccessHint.RANDOM, DataAccessHint.SEQUENTIAL)));
149150
}
150151
}
151152
}

lucene/test-framework/src/java/org/apache/lucene/tests/store/MockDirectoryWrapper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ public synchronized IndexInput openInput(String name, IOContext context) throws
814814
}
815815

816816
// record the read advice before randomizing the context
817-
ReadAdvice readAdvice = context.readAdvice();
817+
ReadAdvice readAdvice = toReadAdvice(context);
818818
context = LuceneTestCase.newIOContext(randomState, context);
819819
final boolean confined = context == IOContext.READONCE;
820820
if (name.startsWith(IndexFileNames.SEGMENTS) && confined == false) {

lucene/test-framework/src/java/org/apache/lucene/tests/store/SerialIOCountingDirectory.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,13 @@ public ChecksumIndexInput openChecksumInput(String name) throws IOException {
7171

7272
@Override
7373
public IndexInput openInput(String name, IOContext context) throws IOException {
74-
if (context.readAdvice() == ReadAdvice.RANDOM_PRELOAD) {
74+
if (toReadAdvice(context) == ReadAdvice.RANDOM_PRELOAD) {
7575
// expected to be loaded in memory, only count 1 at open time
7676
counter.increment();
7777
return super.openInput(name, context);
7878
}
79-
return new SerializedIOCountingIndexInput(super.openInput(name, context), context.readAdvice());
79+
return new SerializedIOCountingIndexInput(
80+
super.openInput(name, context), toReadAdvice(context));
8081
}
8182

8283
private class SerializedIOCountingIndexInput extends IndexInput {

0 commit comments

Comments
 (0)