MMapDirectory

class MMapDirectory(path: Path, lockFactory: LockFactory = FSLockFactory.default, maxChunkSize: Long = DEFAULT_MAX_CHUNK_SIZE) : FSDirectory

File-based Directory implementation that uses mmap for reading, and [ ] for writing.

NOTE: memory mapping uses up a portion of the virtual memory address space in your process equal to the size of the file being mapped. Before using this class, be sure your have plenty of virtual address space, e.g. by using a 64 bit JRE, or a 32 bit JRE with indexes that are guaranteed to fit within the address space. On 32 bit platforms also consult .MMapDirectory if you have problems with mmap failing because of fragmented address space. If you get an IOException about mapping failed, it is recommended to reduce the chunk size, until it works.

This class supports preloading files into physical memory upon opening. This can help improve performance of searches on a cold page cache at the expense of slowing down opening an index. See .setPreload for more details.

This class supports grouping of files that are part of the same logical group. This is a hint that allows for better handling of resources. For example, individual files that are part of the same segment can be considered part of the same logical group. See .setGroupingFunction for more details.

This class will use the modern foreign.MemorySegment API available since Java 21 which allows to safely unmap previously mmapped files after closing the [ ]s. There is no need to enable the "preview feature" of your Java version; it works out of box with some compilation tricks. For more information about the foreign memory API read documentation of the foreign package.

On some platforms like Linux and MacOS X, this class will invoke the syscall madvise() to advise how OS kernel should handle paging after opening a file. For this to work, Java code must be able to call native code. If this is not allowed, a warning is logged. To enable native access for Lucene in a modularized application, pass --enable-native-access=org.apache.lucene.core to the Java command line. If Lucene is running in a classpath-based application, use --enable-native-access=ALL-UNNAMED.

NOTE: Accessing this class either directly or indirectly from a thread while it's interrupted can close the underlying channel immediately if at the same time the thread is blocked on IO. The channel will remain closed and subsequent access to MMapDirectory will throw a ClosedChannelException. If your application uses either Thread.interrupt or Future.cancel you should use the legacy RAFDirectory from the Lucene misc module in favor of MMapDirectory.

NOTE: If your application requires external synchronization, you should not synchronize on the MMapDirectory instance as this may cause deadlock; use your own (non-Lucene) objects instead.

See also

Blog post about MMapDirectory

KMP note: On Kotlin Multiplatform targets this class provides an mmap-compatible API but uses a non-mmap implementation backed by regular file reads (okio + positional IO). This keeps Lucene API behavior (random-access IndexInput, clone/slice semantics, thread-safe positional reads, close semantics) without relying on JDK 21+ foreign memory APIs.

Constructors

Link copied to clipboard
constructor(path: Path, lockFactory: LockFactory = FSLockFactory.default, maxChunkSize: Long = DEFAULT_MAX_CHUNK_SIZE)
constructor(path: Path, maxChunkSize: Long)

Create a new MMapDirectory for the named location and FSLockFactory.getDefault. The directory is created at the named location if it does not yet exist.

Types

Link copied to clipboard
object Companion
Link copied to clipboard

Properties

Link copied to clipboard

A provider specific context object or null, that will be passed to openInput.

Link copied to clipboard
Link copied to clipboard
val directory: Path
Link copied to clipboard
Link copied to clipboard
open override val pendingDeletions: MutableSet<String>

Functions

Link copied to clipboard
open override fun close()

Closes the directory.

Link copied to clipboard
open fun copyFrom(from: Directory, src: String, dest: String, context: IOContext)

Copies an existing src file from directory from to a non-existent file dest in this directory. The given IOContext is only used for opening the destination file.

Link copied to clipboard
open override fun createOutput(name: String, context: IOContext): IndexOutput

Creates a new, empty file in the directory and returns an IndexOutput instance for appending data to this file.

Link copied to clipboard
open override fun createTempOutput(prefix: String, suffix: String, context: IOContext): IndexOutput

Creates a new, empty, temporary file in the directory and returns an IndexOutput instance for appending data to this file.

Link copied to clipboard
open override fun deleteFile(name: String)

Removes an existing file in the directory.

Link copied to clipboard

Try to delete any pending files that we had previously tried to delete but failed because we are on Windows and the files were still held open.

Link copied to clipboard
open override fun ensureOpen()

Ensures this directory is still open.

Link copied to clipboard
open override fun fileLength(name: String): Long

Returns the byte length of a file in the directory.

Link copied to clipboard
open override fun listAll(): Array<String>

Returns names of all files stored in this directory. The output must be in sorted (UTF-16, java's String.compareTo) order.

Link copied to clipboard
open override fun obtainLock(name: String): Lock

Acquires and returns a Lock for a file with the given name.

Link copied to clipboard

Opens a checksum-computing stream for reading an existing file.

Link copied to clipboard
open override fun openInput(name: String, context: IOContext): IndexInput

Creates an IndexInput for the file with the given name.

Link copied to clipboard
open override fun rename(source: String, dest: String)

Renames source file to dest file where dest must not already exist in the directory.

Link copied to clipboard
fun setGroupingFunction(groupingFunction: (String) -> Optional<String>)

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.

Link copied to clipboard
fun setPreload(preload: (String, IOContext) -> Boolean)

Configure which files to preload in physical memory upon opening. The default implementation does not preload anything. The behavior is best effort and operating system-dependent.

Link copied to clipboard
open override fun sync(names: MutableCollection<String>)

Ensures that any writes to these files are moved to stable storage (made durable).

Link copied to clipboard
open override fun syncMetaData()

Ensures that directory metadata, such as recent file renames, are moved to stable storage.

Link copied to clipboard
open override fun toString(): String