Apache Lucene open-source search software
LUCENE-6549: add preload option to MMapDirectory
Robert Muir414b7696eb78
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 8d82755d22a..59779ded566 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -70,6 +70,8 @@ New Featuresthe per-hit polygon check when a leaf cell is fully contained by thepolygon. (Nick Knize, Mike McCandless)+* LUCENE-6549: Add preload option to MMapDirectory. (Robert Muir)+API Changes* LUCENE-6508: Simplify Lock api, there is now justdiff --git a/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java b/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.javaindex 21b4d88124b..4d828a603cf 100644--- a/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java+++ b/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java@@ -19,6 +19,7 @@ package org.apache.lucene.store;import java.io.IOException;import java.nio.ByteBuffer;+import java.nio.MappedByteBuffer;import java.nio.channels.ClosedChannelException; // javadoc @linkimport java.nio.channels.FileChannel;import java.nio.channels.FileChannel.MapMode;@@ -86,6 +87,8 @@ import org.apache.lucene.util.Constants;*/public class MMapDirectory extends FSDirectory {private boolean useUnmapHack = UNMAP_SUPPORTED;+ private boolean preload;+/*** Default max chunk size.* @see #MMapDirectory(Path, LockFactory, int)@@ -197,6 +200,24 @@ public class MMapDirectory extends FSDirectory {return useUnmapHack;}+ /**+ * Set to {@code true} to ask mapped pages to be loaded+ * into physical memory on init. The behavior is best-effort+ * and operating system dependent.+ * @see MappedByteBuffer#load+ */+ public void setPreload(boolean preload) {+ this.preload = preload;+ }++ /**+ * Returns {@code true} if mapped pages should be loaded.+ * @see #setPreload+ */+ public boolean getPreload() {+ return preload;+ }+/*** Returns the current mmap chunk size.* @see #MMapDirectory(Path, LockFactory, int)@@ -237,11 +258,16 @@ public class MMapDirectory extends FSDirectory {? chunkSize: (length - bufferStart));+ MappedByteBuffer buffer;try {- buffers[bufNr] = fc.map(MapMode.READ_ONLY, offset + bufferStart, bufSize);+ buffer = fc.map(MapMode.READ_ONLY, offset + bufferStart, bufSize);} catch (IOException ioe) {throw convertMapFailedIOException(ioe, resourceDescription, bufSize);}+ if (preload) {+ buffer.load();+ }+ buffers[bufNr] = buffer;bufferStart += bufSize;}diff --git a/lucene/core/src/test/org/apache/lucene/store/TestMmapDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestMmapDirectory.javaindex bd7dd219ae7..a92c1c0ce01 100644--- a/lucene/core/src/test/org/apache/lucene/store/TestMmapDirectory.java+++ b/lucene/core/src/test/org/apache/lucene/store/TestMmapDirectory.java@@ -27,6 +27,8 @@ public class TestMmapDirectory extends BaseDirectoryTestCase {@Overrideprotected Directory getDirectory(Path path) throws IOException {- return new MMapDirectory(path);+ MMapDirectory m = new MMapDirectory(path);+ m.setPreload(random().nextBoolean());+ return m;}}