Class FSDirectory
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
MMapDirectory,NIOFSDirectory
MMapDirectoryuses memory-mapped IO when reading. This is a good choice if you have plenty of virtual memory relative to your index size, eg if you are running on a 64 bit JRE, or you are running on a 32 bit JRE but your index sizes are small enough to fit into the virtual memory space. This class will use the modernMemorySegmentPREVIEW API available since Java 21 which allows to safely unmap previously mmapped files after closing theIndexInputs. 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 thejava.lang.foreignpackage and Uwe's blog post.NIOFSDirectoryuses java.nio's FileChannel's positional io when reading to avoid synchronization when reading from the same file. Unfortunately, due to a Windows-only Sun JRE bug this is a poor choice for Windows, but on all other platforms this is the preferred choice. Applications usingThread.interrupt()orFuture.cancel(boolean)should useRAFDirectoryinstead, which is provided in themiscmodule. SeeNIOFSDirectoryjavadoc for details.
Unfortunately, because of system peculiarities, there is no single overall best
implementation. Therefore, we've added the open(java.nio.file.Path) method, to allow Lucene to choose the
best FSDirectory implementation given your environment, and the known limitations of each
implementation. For users who have no reason to prefer a specific implementation, it's best to
simply use open(java.nio.file.Path). For all others, you should instantiate the desired implementation
directly.
NOTE: Accessing one of the above subclasses 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 the index will
throw a ClosedChannelException. Applications using Thread.interrupt() or Future.cancel(boolean) should use the slower legacy RAFDirectory from the misc
Lucene module instead.
The locking implementation is by default NativeFSLockFactory, but can be changed by
passing in a custom LockFactory instance.
- See Also:
-
Field Summary
FieldsFields inherited from class org.apache.lucene.store.BaseDirectory
isOpen, lockFactory -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedFSDirectory(Path path, LockFactory lockFactory) Create a new FSDirectory for the named location (ctor for subclasses). -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the directory.createOutput(String name, IOContext context) Creates a new, empty file in the directory and returns anIndexOutputinstance for appending data to this file.createTempOutput(String prefix, String suffix, IOContext context) Creates a new, empty, temporary file in the directory and returns anIndexOutputinstance for appending data to this file.voiddeleteFile(String name) Removes an existing file in the directory.voidTry 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.protected voidensureCanRead(String name) longfileLength(String name) Returns the byte length of a file in the directory.protected voidReturns a set of files currently pending deletion in this directory.String[]listAll()Returns names of all files stored in this directory.static String[]Lists all files (including subdirectories) in the directory.static FSDirectoryCreates an FSDirectory instance, trying to pick the best implementation given the current environment.static FSDirectoryopen(Path path, LockFactory lockFactory) Just likeopen(Path), but allows you to also specify a customLockFactory.voidRenamessourcefile todestfile wheredestmust not already exist in the directory.voidsync(Collection<String> names) Ensures that any writes to these files are moved to stable storage (made durable).voidEnsures that directory metadata, such as recent file renames, are moved to stable storage.toString()Methods inherited from class org.apache.lucene.store.BaseDirectory
ensureOpen, obtainLockMethods inherited from class org.apache.lucene.store.Directory
copyFrom, getTempFileName, openChecksumInput, openInput
-
Field Details
-
directory
-
-
Constructor Details
-
FSDirectory
Create a new FSDirectory for the named location (ctor for subclasses). The directory is created at the named location if it does not yet exist.FSDirectoryresolves the given Path to a canonical / real path to ensure it can correctly lock the index directory and no other process can interfere with changing possible symlinks to the index directory inbetween. If you want to use symlinks and change them dynamically, close allIndexWritersand create a newFSDirectoryinstance.- Parameters:
path- the path of the directorylockFactory- the lock factory to use, or null for the default (NativeFSLockFactory);- Throws:
IOException- if there is a low-level I/O error
-
-
Method Details
-
open
Creates an FSDirectory instance, trying to pick the best implementation given the current environment. The directory returned uses theNativeFSLockFactory. The directory is created at the named location if it does not yet exist.FSDirectoryresolves the given Path when calling this method to a canonical / real path to ensure it can correctly lock the index directory and no other process can interfere with changing possible symlinks to the index directory inbetween. If you want to use symlinks and change them dynamically, close allIndexWritersand create a newFSDirectoryinstance.Currently this returns
MMapDirectoryfor Linux, MacOSX, Solaris, and Windows 64-bit JREs, andNIOFSDirectoryfor other JREs. It is highly recommended that you consult the implementation's documentation for your platform before using this method.NOTE: this method may suddenly change which implementation is returned from release to release, in the event that higher performance defaults become possible; if the precise implementation is important to your application, please instantiate it directly, instead. For optimal performance you should consider using
MMapDirectoryon 64 bit JVMs.See above
- Throws:
IOException
-
open
Just likeopen(Path), but allows you to also specify a customLockFactory.- Throws:
IOException
-
listAll
Lists all files (including subdirectories) in the directory.- Throws:
IOException- if there was an I/O error during listing
-
listAll
Description copied from class:DirectoryReturns names of all files stored in this directory. The output must be in sorted (UTF-16, java'sString.compareTo(java.lang.String)) order.- Specified by:
listAllin classDirectory- Throws:
IOException- in case of I/O error
-
fileLength
Description copied from class:DirectoryReturns the byte length of a file in the directory.This method must throw either
NoSuchFileExceptionorFileNotFoundExceptionifnamepoints to a non-existing file.- Specified by:
fileLengthin classDirectory- Parameters:
name- the name of an existing file.- Throws:
IOException- in case of I/O error
-
createOutput
Description copied from class:DirectoryCreates a new, empty file in the directory and returns anIndexOutputinstance for appending data to this file.This method must throw
FileAlreadyExistsExceptionif the file already exists.- Specified by:
createOutputin classDirectory- Parameters:
name- the name of the file to create.- Throws:
IOException- in case of I/O error
-
createTempOutput
public IndexOutput createTempOutput(String prefix, String suffix, IOContext context) throws IOException Description copied from class:DirectoryCreates a new, empty, temporary file in the directory and returns anIndexOutputinstance for appending data to this file.The temporary file name (accessible via
IndexOutput.getName()) will start withprefix, end withsuffixand have a reserved file extension.tmp.- Specified by:
createTempOutputin classDirectory- Throws:
IOException
-
ensureCanRead
- Throws:
IOException
-
sync
Description copied from class:DirectoryEnsures that any writes to these files are moved to stable storage (made durable).Lucene uses this to properly commit changes to the index, to prevent a machine/OS crash from corrupting the index.
- Specified by:
syncin classDirectory- Throws:
IOException- See Also:
-
rename
Description copied from class:DirectoryRenamessourcefile todestfile wheredestmust not already exist in the directory.It is permitted for this operation to not be truly atomic, for example both
sourceanddestcan be visible temporarily inDirectory.listAll(). However, the implementation of this method must ensure the content ofdestappears as the entiresourceatomically. So oncedestis visible for readers, the entire content of previoussourceis visible.This method is used by IndexWriter to publish commits.
- Specified by:
renamein classDirectory- Throws:
IOException
-
syncMetaData
Description copied from class:DirectoryEnsures that directory metadata, such as recent file renames, are moved to stable storage.- Specified by:
syncMetaDatain classDirectory- Throws:
IOException- See Also:
-
close
Description copied from class:DirectoryCloses the directory.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classDirectory- Throws:
IOException
-
getDirectory
- Returns:
- the underlying filesystem directory
-
toString
- Overrides:
toStringin classBaseDirectory
-
fsync
- Throws:
IOException
-
deleteFile
Description copied from class:DirectoryRemoves an existing file in the directory.This method must throw either
NoSuchFileExceptionorFileNotFoundExceptionifnamepoints to a non-existing file.- Specified by:
deleteFilein classDirectory- Parameters:
name- the name of an existing file.- Throws:
IOException- in case of I/O error
-
deletePendingFiles
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.- Throws:
IOException
-
getPendingDeletions
Description copied from class:DirectoryReturns a set of files currently pending deletion in this directory.- Specified by:
getPendingDeletionsin classDirectory- Throws:
IOException
-