## org.apache.lucene.store
Class FSDirectory

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

Direct Known Subclasses:
  MMapDirectory, NIOFSDirectory, SimpleFSDirectory

    public class FSDirectory

extends Directory

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 java.io.RandomAccessFile. 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)` (on Java 1.5) should use `SimpleFSDirectory` 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 `SimpleFSDirectory.SimpleFSIndexInput.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. Applications using `Thread.interrupt()` or `Future#cancel(boolean)` (on Java 1.5) should use `SimpleFSDirectory` instead. See `MMapDirectory` java doc for details.

Unfortunately, because of system peculiarities, there is no single overall best implementation. Therefore, we've added the `open(java.io.File)` 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.io.File)`. For all others, you should instantiate the desired implementation directly.

The locking implementation is by default `NativeFSLockFactory`, but can be changed by passing in a custom `LockFactory` instance. The deprecated `getDirectory` methods default to use `SimpleFSLockFactory` for backwards compatibility. The system properties `org.apache.lucene.store.FSDirectoryLockFactoryClass` and `org.apache.lucene.FSDirectory.class` are deprecated and only used by the deprecated `getDirectory` methods. The system property `org.apache.lucene.lockDir` is ignored completely, If you really want to store locks elsewhere, you can create your own `SimpleFSLockFactory` (or `NativeFSLockFactory`, etc.) passing in your preferred lock directory.

In 3.0 this class will become abstract.

See Also:
  `Directory`

Nested Class Summary
`protected static class` `FSDirectory.FSIndexInput` Deprecated. Use SimpleFSDirectory.SimpleFSIndexInput instead
`protected static class` `FSDirectory.FSIndexOutput` Deprecated. Use SimpleFSDirectory.SimpleFSIndexOutput instead

Field Summary
`static int` `DEFAULT_READ_CHUNK_SIZE` Default read chunk size.
`protected java.io.File` `directory` The underlying filesystem directory

`static java.lang.String`

`LOCK_DIR`
Deprecated. As of 2.1, `LOCK_DIR` is unused because the write.lock is now stored by default in the index directory. If you really want to store locks elsewhere, you can create your own `SimpleFSLockFactory` (or `NativeFSLockFactory`, etc.) passing in your preferred lock directory. Then, pass this `LockFactory` instance to one of the `open` methods that take a `lockFactory` (for example, `open(File, LockFactory)`).

Fields inherited from class org.apache.lucene.store.Directory
`isOpen, lockFactory`

Constructor Summary
`protected` `FSDirectory()` Deprecated.
`protected` `FSDirectory(java.io.File path, LockFactory lockFactory)` Create a new FSDirectory for the named location (ctor for subclasses).

Method Summary
`void` `close()` Closes the store to future operations.
`IndexOutput` `createOutput(java.lang.String name)` Creates an IndexOutput for the file with the given name.
`void` `deleteFile(java.lang.String name)` Removes an existing file in the directory.
`boolean` `fileExists(java.lang.String name)` Returns true iff a file with the given name exists.
`long` `fileLength(java.lang.String name)` Returns the length in bytes of a file in the directory.
`static long` `fileModified(java.io.File directory, java.lang.String name)` Returns the time the named file was last modified.
`long` `fileModified(java.lang.String name)` Returns the time the named file was last modified.
`static FSDirectory` `getDirectory(java.io.File file)` Deprecated. Use `open(File)`
`static FSDirectory` `getDirectory(java.io.File file, boolean create)` Deprecated. Use IndexWriter's create flag, instead, to create a new index.
`static FSDirectory` `getDirectory(java.io.File file, LockFactory lockFactory)` Deprecated. Use `open(File, LockFactory)`
`static FSDirectory` `getDirectory(java.lang.String path)` Deprecated. Use `open(File)`
`static FSDirectory` `getDirectory(java.lang.String path, boolean create)` Deprecated. Use IndexWriter's create flag, instead, to create a new index.
`static FSDirectory` `getDirectory(java.lang.String path, LockFactory lockFactory)` Deprecated. Use `open(File, LockFactory)`
`static boolean` `getDisableLocks()` Deprecated. Use a constructor that takes a `LockFactory` and supply `NoLockFactory.getNoLockFactory()`.
`java.io.File` `getFile()`
`java.lang.String` `getLockID()` Return a string identifier that uniquely differentiates this Directory instance from other Directory instances.
`int` `getReadChunkSize()` The maximum number of bytes to read at once from the underlying file during `BufferedIndexInput.readBytes(byte[], int, int)`.
`protected void` `initOutput(java.lang.String name)` Initializes the directory to create a new file with the given name.
`java.lang.String[]` `list()` List the files in the directory.
`java.lang.String[]` `listAll()` Lists all files (not subdirectories) in the directory.
`static java.lang.String[]` `listAll(java.io.File dir)` Lists all files (not subdirectories) in the directory.
`static FSDirectory` `open(java.io.File path)` Creates an FSDirectory instance, trying to pick the best implementation given the current environment.
`static FSDirectory` `open(java.io.File path, LockFactory lockFactory)` Just like `open(File)`, but allows you to also specify a custom `LockFactory`.
`IndexInput` `openInput(java.lang.String name)` Returns a stream reading an existing file.
`IndexInput` `openInput(java.lang.String name, int bufferSize)` Creates an IndexInput for the file with the given name.
`void` `renameFile(java.lang.String from, java.lang.String to)` Deprecated.
`static void` `setDisableLocks(boolean doDisableLocks)` Deprecated. Use a `open(File, LockFactory)` or a constructor that takes a `LockFactory` and supply `NoLockFactory.getNoLockFactory()`. This setting does not work with `open(File)` only the deprecated `getDirectory` respect this setting.
`void` `setReadChunkSize(int chunkSize)` Sets the maximum number of bytes read at once from the underlying file during `BufferedIndexInput.readBytes(byte[], int, int)`.
`void` `sync(java.lang.String name)` Ensure that any writes to this file are moved to stable storage.
`java.lang.String` `toString()` For debug output.
`void` `touchFile(java.lang.String name)` Set the modified time of an existing file to now.

Methods inherited from class org.apache.lucene.store.Directory
`clearLock, copy, ensureOpen, getLockFactory, makeLock, setLockFactory`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

Field Detail

### LOCK_DIR

    public static final java.lang.String LOCK_DIR

  Deprecated. As of 2.1, `LOCK_DIR` is unused because the write.lock is now stored by default in the index directory. If you really want to store locks elsewhere, you can create your own `SimpleFSLockFactory` (or `NativeFSLockFactory`, etc.) passing in your preferred lock directory. Then, pass this `LockFactory` instance to one of the `open` methods that take a `lockFactory` (for example, `open(File, LockFactory)`).
  Directory specified by `org.apache.lucene.lockDir` or `java.io.tmpdir` system property.

### directory

    protected java.io.File directory

  The underlying filesystem directory

### DEFAULT_READ_CHUNK_SIZE

    public static final int DEFAULT_READ_CHUNK_SIZE

  Default read chunk size. This is a conditional default: on 32bit JVMs, it defaults to 100 MB. On 64bit JVMs, it's `Integer.MAX_VALUE`.

  See Also:
    `setReadChunkSize(int)`

Constructor Detail

### FSDirectory

    protected FSDirectory()

  Deprecated.

### FSDirectory

    protected FSDirectory(java.io.File path,
                          LockFactory lockFactory)
                   throws java.io.IOException

  Create a new FSDirectory for the named location (ctor for subclasses).

  Parameters:
    `path` - the path of the directory
    `lockFactory` - the lock factory to use, or null for the default (`NativeFSLockFactory`);
  Throws:
    `java.io.IOException`

Method Detail

### setDisableLocks

    public static void setDisableLocks(boolean doDisableLocks)

  Deprecated. Use a `open(File, LockFactory)` or a constructor that takes a `LockFactory` and supply `NoLockFactory.getNoLockFactory()`. This setting does not work with `open(File)` only the deprecated `getDirectory` respect this setting.

  Set whether Lucene's use of lock files is disabled. By default, lock files are enabled. They should only be disabled if the index is on a read-only medium like a CD-ROM.

### getDisableLocks

    public static boolean getDisableLocks()

  Deprecated. Use a constructor that takes a `LockFactory` and supply `NoLockFactory.getNoLockFactory()`.

  Returns whether Lucene's use of lock files is disabled.

  Returns:
    true if locks are disabled, false if locks are enabled.
  See Also:
    `setDisableLocks(boolean)`

### getDirectory

    public static FSDirectory getDirectory(java.lang.String path)
                                    throws java.io.IOException

  Deprecated. Use `open(File)`

  Returns the directory instance for the named location.

  Parameters:
    `path` - the path to the directory.
  Returns:
    the FSDirectory for the named file.
  Throws:
    `java.io.IOException`

### getDirectory

    public static FSDirectory getDirectory(java.lang.String path,
                                           LockFactory lockFactory)
                                    throws java.io.IOException

  Deprecated. Use `open(File, LockFactory)`

  Returns the directory instance for the named location.

  Parameters:
    `path` - the path to the directory.
    `lockFactory` - instance of `LockFactory` providing the locking implementation.
  Returns:
    the FSDirectory for the named file.
  Throws:
    `java.io.IOException`

### getDirectory

    public static FSDirectory getDirectory(java.io.File file)
                                    throws java.io.IOException

  Deprecated. Use `open(File)`

  Returns the directory instance for the named location.

  Parameters:
    `file` - the path to the directory.
  Returns:
    the FSDirectory for the named file.
  Throws:
    `java.io.IOException`

### getDirectory

    public static FSDirectory getDirectory(java.io.File file,
                                           LockFactory lockFactory)
                                    throws java.io.IOException

  Deprecated. Use `open(File, LockFactory)`

  Returns the directory instance for the named location.

  Parameters:
    `file` - the path to the directory.
    `lockFactory` - instance of `LockFactory` providing the locking implementation.
  Returns:
    the FSDirectory for the named file.
  Throws:
    `java.io.IOException`

### getDirectory

    public static FSDirectory getDirectory(java.lang.String path,
                                           boolean create)
                                    throws java.io.IOException

  Deprecated. Use IndexWriter's create flag, instead, to create a new index.

  Returns the directory instance for the named location.

  Parameters:
    `path` - the path to the directory.
    `create` - if true, create, or erase any existing contents.
  Returns:
    the FSDirectory for the named file.
  Throws:
    `java.io.IOException`

### getDirectory

    public static FSDirectory getDirectory(java.io.File file,
                                           boolean create)
                                    throws java.io.IOException

  Deprecated. Use IndexWriter's create flag, instead, to create a new index.

  Returns the directory instance for the named location.

  Parameters:
    `file` - the path to the directory.
    `create` - if true, create, or erase any existing contents.
  Returns:
    the FSDirectory for the named file.
  Throws:
    `java.io.IOException`

### initOutput

    protected final void initOutput(java.lang.String name)
                             throws java.io.IOException

  Initializes the directory to create a new file with the given name. This method should be used in `createOutput(java.lang.String)`.

  Throws:
    `java.io.IOException`

### open

    public static FSDirectory open(java.io.File path)
                            throws java.io.IOException

  Creates an FSDirectory instance, trying to pick the best implementation given the current environment. The directory returned uses the `NativeFSLockFactory`.

  Currently this returns `NIOFSDirectory` on non-Windows JREs and `SimpleFSDirectory` 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. On 64 bit systems, it may also good to return `MMapDirectory`, but this is disabled because of officially missing unmap support in Java. For optimal performance you should consider using this implementation on 64 bit JVMs.

  See above

  Throws:
    `java.io.IOException`

### open

    public static FSDirectory open(java.io.File path,
                                   LockFactory lockFactory)
                            throws java.io.IOException

  Just like `open(File)`, but allows you to also specify a custom `LockFactory`.

  Throws:
    `java.io.IOException`

### listAll

    public static java.lang.String[] listAll(java.io.File dir)
                                      throws java.io.IOException

  Lists all files (not subdirectories) in the directory. This method never returns null (throws `IOException` instead).

  Throws:
    `NoSuchDirectoryException` - if the directory does not exist, or does exist but is not a directory.
    `java.io.IOException` - if list() returns null

### list

    public java.lang.String[] list()

  Description copied from class: `Directory`
  List the files in the directory.

  Specified by:
    `list` in class `Directory`

### listAll

    public java.lang.String[] listAll()
                               throws java.io.IOException

  Lists all files (not subdirectories) in the directory.

  Overrides:
    `listAll` in class `Directory`
  Throws:
    `java.io.IOException`
  See Also:
    `listAll(File)`

### fileExists

    public boolean fileExists(java.lang.String name)

  Returns true iff a file with the given name exists.

  Specified by:
    `fileExists` in class `Directory`

### fileModified

    public long fileModified(java.lang.String name)

  Returns the time the named file was last modified.

  Specified by:
    `fileModified` in class `Directory`

### fileModified

    public static long fileModified(java.io.File directory,
                                    java.lang.String name)

  Returns the time the named file was last modified.

### touchFile

    public void touchFile(java.lang.String name)

  Set the modified time of an existing file to now.

  Specified by:
    `touchFile` in class `Directory`

### fileLength

    public long fileLength(java.lang.String name)

  Returns the length in bytes of a file in the directory.

  Specified by:
    `fileLength` in class `Directory`

### deleteFile

    public void deleteFile(java.lang.String name)
                    throws java.io.IOException

  Removes an existing file in the directory.

  Specified by:
    `deleteFile` in class `Directory`
  Throws:
    `java.io.IOException`

### renameFile

    public void renameFile(java.lang.String from,
                           java.lang.String to)
                    throws java.io.IOException

  Deprecated.

  Renames an existing file in the directory. Warning: This is not atomic.

  Specified by:
    `renameFile` in class `Directory`
  Throws:
    `java.io.IOException`

### createOutput

    public IndexOutput createOutput(java.lang.String name)
                             throws java.io.IOException

  Creates an IndexOutput for the file with the given name. In 3.0 this method will become abstract.

  Specified by:
    `createOutput` in class `Directory`
  Throws:
    `java.io.IOException`

### sync

    public void sync(java.lang.String name)
              throws java.io.IOException

  Description copied from class: `Directory`
  Ensure that any writes to this file 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.

  Overrides:
    `sync` in class `Directory`
  Throws:
    `java.io.IOException`

### openInput

    public IndexInput openInput(java.lang.String name)
                         throws java.io.IOException

  Description copied from class: `Directory`
  Returns a stream reading an existing file.

  Specified by:
    `openInput` in class `Directory`
  Throws:
    `java.io.IOException`

### openInput

    public IndexInput openInput(java.lang.String name,
                                int bufferSize)
                         throws java.io.IOException

  Creates an IndexInput for the file with the given name. In 3.0 this method will become abstract.

  Overrides:
    `openInput` in class `Directory`
  Throws:
    `java.io.IOException`

### getLockID

    public java.lang.String getLockID()

  Description copied from class: `Directory`
  Return a string identifier that uniquely differentiates this Directory instance from other Directory instances. This ID should be the same if two Directory instances (even in different JVMs and/or on different machines) are considered "the same index". This is how locking "scopes" to the right index.

  Overrides:
    `getLockID` in class `Directory`

### close

    public void close()

  Closes the store to future operations.

  Specified by:
    `close` in class `Directory`

### getFile

    public java.io.File getFile()

### toString

    public java.lang.String toString()

  For debug output.

  Overrides:
    `toString` in class `Directory`

### setReadChunkSize

    public final void setReadChunkSize(int chunkSize)

  Sets the maximum number of bytes read at once from the underlying file during `BufferedIndexInput.readBytes(byte[], int, int)`. The default value is `DEFAULT_READ_CHUNK_SIZE`;

  This was introduced due to Sun JVM Bug 6478546, which throws an incorrect OutOfMemoryError when attempting to read too many bytes at once. It only happens on 32bit JVMs with a large maximum heap size.

  Changes to this value will not impact any already-opened `FSDirectory.FSIndexInput`s. You should call this before attempting to open an index on the directory.

  NOTE: This value should be as large as possible to reduce any possible performance impact. If you still encounter an incorrect OutOfMemoryError, trying lowering the chunk size.

### getReadChunkSize

    public final int getReadChunkSize()

  The maximum number of bytes to read at once from the underlying file during `BufferedIndexInput.readBytes(byte[], int, int)`.

  See Also:
    `setReadChunkSize(int)`

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