Skip to content

Commit 7a36602

Browse files
committed
xMap from IOContext to ReadAdvice
1 parent edfc21e commit 7a36602

File tree

10 files changed

+67
-43
lines changed

10 files changed

+67
-43
lines changed

Diff for: 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. */

Diff for: 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

Diff for: 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. */

Diff for: lucene/core/src/java/org/apache/lucene/store/DefaultIOContext.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,12 @@
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) implements IOContext {
2424

2525
public DefaultIOContext {
2626
Objects.requireNonNull(readAdvice);
27-
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-
}
3327
}
3428

3529
@Override
@@ -47,9 +41,14 @@ public FlushInfo flushInfo() {
4741
return null;
4842
}
4943

44+
@Override
45+
public FileOpenHint[] hints() {
46+
return new FileOpenHint[0];
47+
}
48+
5049
private static final DefaultIOContext[] READADVICE_TO_IOCONTEXT =
5150
Arrays.stream(ReadAdvice.values())
52-
.map(DefaultIOContext::new)
51+
.map(r -> new DefaultIOContext(Optional.of(r)))
5352
.toArray(DefaultIOContext[]::new);
5453

5554
@Override

Diff for: 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.

Diff for: lucene/core/src/java/org/apache/lucene/store/IOContext.java

+40-9
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,18 +47,49 @@ 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);
58+
IOContext READONCE = new DefaultIOContext(Optional.of(ReadAdvice.SEQUENTIAL));
5959

6060
static IOContext defaultContext(FileOpenHint... hints) {
61-
return new DefaultIOContext(Constants.DEFAULT_READADVICE, hints);
61+
Objects.requireNonNull(hints);
62+
return new IOContext() {
63+
@Override
64+
public Context context() {
65+
return Context.DEFAULT;
66+
}
67+
68+
@Override
69+
public MergeInfo mergeInfo() {
70+
return null;
71+
}
72+
73+
@Override
74+
public FlushInfo flushInfo() {
75+
return null;
76+
}
77+
78+
@Override
79+
public FileOpenHint[] hints() {
80+
return hints;
81+
}
82+
83+
@Override
84+
public Optional<ReadAdvice> readAdvice() {
85+
return Optional.empty();
86+
}
87+
88+
@Override
89+
public IOContext withReadAdvice(ReadAdvice advice) {
90+
return this;
91+
}
92+
};
6293
}
6394

6495
/** Returns an {@link IOContext} for merging with the specified {@link MergeInfo} */
@@ -86,8 +117,8 @@ public FileOpenHint[] hints() {
86117
}
87118

88119
@Override
89-
public ReadAdvice readAdvice() {
90-
return ReadAdvice.SEQUENTIAL;
120+
public Optional<ReadAdvice> readAdvice() {
121+
return Optional.of(ReadAdvice.SEQUENTIAL);
91122
}
92123

93124
@Override
@@ -122,8 +153,8 @@ public FileOpenHint[] hints() {
122153
}
123154

124155
@Override
125-
public ReadAdvice readAdvice() {
126-
return ReadAdvice.SEQUENTIAL;
156+
public Optional<ReadAdvice> readAdvice() {
157+
return Optional.of(ReadAdvice.SEQUENTIAL);
127158
}
128159

129160
@Override
@@ -147,7 +178,7 @@ public IOContext withReadAdvice(ReadAdvice advice) {
147178

148179
/** Advice on the expected read access pattern */
149180
@Deprecated
150-
ReadAdvice readAdvice();
181+
Optional<ReadAdvice> readAdvice();
151182

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

Diff for: 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)

Diff for: 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);

Diff for: 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) {

Diff for: 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)