Skip to content

Commit 3c0771a

Browse files
committed
Merge remote-tracking branch 'upstream/main' into success-exception-handling
2 parents 624e82b + 8d45ad6 commit 3c0771a

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.apache.lucene.store.FileTypeHint;
4646
import org.apache.lucene.store.IOContext;
4747
import org.apache.lucene.store.IndexInput;
48+
import org.apache.lucene.store.PreloadHint;
4849
import org.apache.lucene.store.RandomAccessInput;
4950
import org.apache.lucene.util.BitSet;
5051
import org.apache.lucene.util.Bits;
@@ -109,7 +110,10 @@ public Lucene99HnswVectorsReader(SegmentReadState state, FlatVectorsReader flatV
109110
Lucene99HnswVectorsFormat.VECTOR_INDEX_EXTENSION,
110111
Lucene99HnswVectorsFormat.VECTOR_INDEX_CODEC_NAME,
111112
state.context.withHints(
112-
FileTypeHint.DATA, FileDataHint.KNN_VECTORS, DataAccessHint.RANDOM));
113+
FileTypeHint.DATA,
114+
FileDataHint.KNN_VECTORS,
115+
DataAccessHint.RANDOM,
116+
PreloadHint.INSTANCE));
113117
} catch (Throwable t) {
114118
IOUtils.closeWhileSuppressingExceptions(t, this);
115119
throw t;

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

+8
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ public class MMapDirectory extends FSDirectory {
9999
*/
100100
public static final BiPredicate<String, IOContext> NO_FILES = (_, _) -> false;
101101

102+
/**
103+
* Argument for {@link #setPreload(BiPredicate)} that configures files to be preloaded when they
104+
* are hinted to do so.
105+
*/
106+
public static final BiPredicate<String, IOContext> PRELOAD_HINT =
107+
(_, c) -> c.hints().contains(PreloadHint.INSTANCE);
108+
102109
/**
103110
* This sysprop allows to control the total maximum number of mmapped files that can be associated
104111
* with a single shared {@link java.lang.foreign.Arena foreign Arena}. For example, to set the max
@@ -233,6 +240,7 @@ public MMapDirectory(Path path, LockFactory lockFactory, long maxChunkSize) thro
233240
* @param preload a {@link BiPredicate} whose first argument is the file name, and second argument
234241
* is the {@link IOContext} used to open the file
235242
* @see #ALL_FILES
243+
* @see #PRELOAD_HINT
236244
* @see #NO_FILES
237245
*/
238246
public void setPreload(BiPredicate<String, IOContext> preload) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.lucene.store;
18+
19+
/** A hint that the file should be preloaded into memory */
20+
public enum PreloadHint implements IOContext.FileOpenHint {
21+
INSTANCE
22+
}

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

+25
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,31 @@ public void testWithNormal() throws Exception {
131131
}
132132
}
133133

134+
public void testPreload() throws Exception {
135+
assumeTrue("madvise for preloading only works on linux/mac", MMapDirectory.supportsMadvise());
136+
137+
final int size = 8 * 1024;
138+
byte[] bytes = new byte[size];
139+
random().nextBytes(bytes);
140+
141+
try (MMapDirectory dir = new MMapDirectory(createTempDir("testPreload"))) {
142+
dir.setPreload(MMapDirectory.PRELOAD_HINT);
143+
try (IndexOutput out =
144+
dir.createOutput("test", IOContext.DEFAULT.withHints(PreloadHint.INSTANCE))) {
145+
out.writeBytes(bytes, 0, bytes.length);
146+
}
147+
148+
try (final IndexInput in =
149+
dir.openInput("test", IOContext.DEFAULT.withHints(PreloadHint.INSTANCE))) {
150+
// the data should be loaded in memory
151+
assertTrue(in.isLoaded().orElse(false));
152+
final byte[] readBytes = new byte[size];
153+
in.readBytes(readBytes, 0, readBytes.length);
154+
assertArrayEquals(bytes, readBytes);
155+
}
156+
}
157+
}
158+
134159
// Opens the input with ReadAdvice.READONCE to ensure slice and clone are appropriately confined
135160
public void testConfined() throws Exception {
136161
final int size = 16;

0 commit comments

Comments
 (0)