org.apache.lucene.store

## Class FSDirectory

- java.lang.Object
- - org.apache.lucene.store.Directory
  - - org.apache.lucene.store.BaseDirectory
    - - org.apache.lucene.store.FSDirectory

- All Implemented Interfaces:
    Closeable, AutoCloseable
  Direct Known Subclasses:
    MMapDirectory, NIOFSDirectory, SimpleFSDirectory

      public abstract class FSDirectory
      extends BaseDirectory

  Base class for Directory implementations that store index files in the file system. There are currently three core subclasses:
  - `SimpleFSDirectory` is a straightforward implementation using Files.newByteChannel. However, it has poor concurrent performance (multiple threads will bottleneck) as it synchronizes when multiple threads read from the same file.
  - `NIOFSDirectory` uses 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 using `Thread.interrupt()` or `Future.cancel(boolean)` should use `RAFDirectory` instead. See `NIOFSDirectory` java doc for details.
  - `MMapDirectory` uses 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. Java has currently the limitation of not being able to unmap files from user code. The files are unmapped, when GC releases the byte buffers. Due to this bug in Sun's JRE, MMapDirectory's `IndexInput.close()` is unable to close the underlying OS file handle. Only when GC finally collects the underlying objects, which could be quite some time later, will the file handle be closed. This will consume additional transient disk usage: on Windows, attempts to delete or overwrite the files will result in an exception; on other platforms, which typically have a "delete on last close" semantics, while such operations will succeed, the bytes are still consuming space on disk. For many applications this limitation is not a problem (e.g. if you have plenty of disk space, and you don't rely on overwriting files on Windows) but it's still an important limitation to be aware of. This class supplies a (possibly dangerous) workaround mentioned in the bug report, which may fail on non-Sun JVMs.

  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:
    `Directory`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected Path` | `directory`

    - ### Fields inherited from class org.apache.lucene.store.BaseDirectory

      `isOpen, lockFactory`

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `FSDirectory(Path path, LockFactory lockFactory)` Create a new FSDirectory for the named location (ctor for subclasses).

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `checkPendingDeletions()` Tries to delete any pending deleted files, and returns true if there are still files that could not be deleted.
    `void` | `close()` Closes the store.
    `IndexOutput` | `createOutput(String name, IOContext context)` Creates a new, empty file in the directory with the given name.
    `IndexOutput` | `createTempOutput(String prefix, String suffix, IOContext context)` Creates a new, empty file for writing in the directory, with a temporary file name including prefix and suffix, ending with the reserved extension `.tmp`.
    `void` | `deleteFile(String name)` Removes an existing file in the directory.
    `void` | `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.
    `protected void` | `ensureCanRead(String name)`
    `long` | `fileLength(String name)` Returns the length of a file in the directory.
    `protected void` | `fsync(String name)`
    `Path` | `getDirectory()`
    `String[]` | `listAll()` Returns an array of strings, one for each entry in the directory, in sorted (UTF16, java's String.compare) order.
    `static String[]` | `listAll(Path dir)` Lists all files (including subdirectories) in the directory.
    `static FSDirectory` | `open(Path path)` Creates an FSDirectory instance, trying to pick the best implementation given the current environment.
    `static FSDirectory` | `open(Path path, LockFactory lockFactory)` Just like `open(Path)`, but allows you to also specify a custom `LockFactory`.
    `void` | `rename(String source, String dest)` Renames `source` to `dest` as an atomic operation, where `dest` does not yet exist in the directory.
    `void` | `sync(Collection<String> names)` Ensure that any writes to these files are moved to stable storage.
    `void` | `syncMetaData()` Ensure that directory metadata, such as recent file renames, are made durable.
    `String` | `toString()`

    - ### Methods inherited from class org.apache.lucene.store.BaseDirectory

      `ensureOpen, obtainLock`

    - ### Methods inherited from class org.apache.lucene.store.Directory

      `copyFrom, openChecksumInput, openInput, renameFile`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### directory

          protected final Path directory

  - ### Constructor Detail

    - #### FSDirectory

          protected FSDirectory(Path path,
                                LockFactory lockFactory)
                         throws IOException

      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.

      `FSDirectory` resolves 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 all `IndexWriters` and create a new `FSDirecory` instance.

      Parameters:
        `path` - the path of the directory
        `lockFactory` - the lock factory to use, or null for the default (`NativeFSLockFactory`);
      Throws:
        `IOException` - if there is a low-level I/O error

  - ### Method Detail

    - #### open

          public static FSDirectory open(Path path)
                                  throws IOException

      Creates an FSDirectory instance, trying to pick the best implementation given the current environment. The directory returned uses the `NativeFSLockFactory`. The directory is created at the named location if it does not yet exist.

      `FSDirectory` resolves 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 all `IndexWriters` and create a new `FSDirecory` instance.

      Currently this returns `MMapDirectory` for Linux, MacOSX, Solaris, and Windows 64-bit JREs, `NIOFSDirectory` for other non-Windows JREs, and `SimpleFSDirectory` for other JREs on Windows. 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 `MMapDirectory` on 64 bit JVMs.

      See above

      Throws:
        `IOException`

    - #### open

          public static FSDirectory open(Path path,
                                         LockFactory lockFactory)
                                  throws IOException

      Just like `open(Path)`, but allows you to also specify a custom `LockFactory`.
      Throws:
        `IOException`

    - #### listAll

          public static String[] listAll(Path dir)
                                  throws IOException

      Lists all files (including subdirectories) in the directory.
      Throws:
        `IOException` - if there was an I/O error during listing

    - #### listAll

          public String[] listAll()
                           throws IOException

      Description copied from class: `Directory`
      Returns an array of strings, one for each entry in the directory, in sorted (UTF16, java's String.compare) order.
      Specified by:
        `listAll` in class `Directory`
      Throws:
        `IOException` - in case of IO error

    - #### fileLength

          public long fileLength(String name)
                          throws IOException

      Description copied from class: `Directory`
      Returns the length of a file in the directory. This method follows the following contract:
      - Throws `FileNotFoundException` or `NoSuchFileException` if the file does not exist.
      - Returns a value ≥0 if the file exists, which specifies its length.
      Specified by:
        `fileLength` in class `Directory`
      Parameters:
        `name` - the name of the file for which to return the length.
      Throws:
        `IOException` - if there was an IO error while retrieving the file's length.

    - #### createOutput

          public IndexOutput createOutput(String name,
                                          IOContext context)
                                   throws IOException

      Description copied from class: `Directory`
      Creates a new, empty file in the directory with the given name. Returns a stream writing this file.
      Specified by:
        `createOutput` in class `Directory`
      Throws:
        `IOException`

    - #### createTempOutput

          public IndexOutput createTempOutput(String prefix,
                                              String suffix,
                                              IOContext context)
                                       throws IOException

      Description copied from class: `Directory`
      Creates a new, empty file for writing in the directory, with a temporary file name including prefix and suffix, ending with the reserved extension `.tmp`. Use `IndexOutput.getName()` to see what name was used.
      Specified by:
        `createTempOutput` in class `Directory`
      Throws:
        `IOException`

    - #### ensureCanRead

          protected void ensureCanRead(String name)
                                throws IOException

      Throws:
        `IOException`

    - #### sync

          public void sync(Collection<String> names)
                    throws IOException

      Description copied from class: `Directory`
      Ensure that any writes to these files are moved to stable storage. Lucene uses this to properly commit changes to the index, to prevent a machine/OS crash from corrupting the index.
      NOTE: Clients may call this method for same files over and over again, so some impls might optimize for that. For other impls the operation can be a noop, for various reasons.
      Specified by:
        `sync` in class `Directory`
      Throws:
        `IOException`

    - #### rename

          public void rename(String source,
                             String dest)
                      throws IOException

      Description copied from class: `Directory`
      Renames `source` to `dest` as an atomic operation, where `dest` does not yet exist in the directory.

      Notes: This method is used by IndexWriter to publish commits. It is ok if this operation is not truly atomic, for example both `source` and `dest` can be visible temporarily. It is just important that the contents of `dest` appear atomically, or an exception is thrown.

      Specified by:
        `rename` in class `Directory`
      Throws:
        `IOException`

    - #### syncMetaData

          public void syncMetaData()
                            throws IOException

      Description copied from class: `Directory`
      Ensure that directory metadata, such as recent file renames, are made durable.
      Specified by:
        `syncMetaData` in class `Directory`
      Throws:
        `IOException`

    - #### close

          public void close()
                     throws IOException

      Description copied from class: `Directory`
      Closes the store.
      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Specified by:
        `close` in class `Directory`
      Throws:
        `IOException`

    - #### getDirectory

          public Path getDirectory()

      Returns:
        the underlying filesystem directory

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `BaseDirectory`

    - #### fsync

          protected void fsync(String name)
                        throws IOException

      Throws:
        `IOException`

    - #### deleteFile

          public void deleteFile(String name)
                          throws IOException

      Description copied from class: `Directory`
      Removes an existing file in the directory.
      Specified by:
        `deleteFile` in class `Directory`
      Throws:
        `IOException`

    - #### checkPendingDeletions

          public boolean checkPendingDeletions()
                                        throws IOException

      Tries to delete any pending deleted files, and returns true if there are still files that could not be deleted.
      Throws:
        `IOException`

    - #### deletePendingFiles

          public void deletePendingFiles()
                                  throws IOException

      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`

Copyright © 2000-2018 Apache Software Foundation. All Rights Reserved.