org.apache.lucene.store
Class FSDirectory

java.lang.Object
  extended by org.apache.lucene.store.Directory
      extended by org.apache.lucene.store.FSDirectory
All Implemented Interfaces:
Closeable
Direct Known Subclasses:
DirectIOLinuxDirectory, MMapDirectory, NIOFSDirectory, SimpleFSDirectory, WindowsDirectory

public abstract class FSDirectory
extends Directory

Base class for Directory implementations that store index files in the file system. There are currently three core subclasses:

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.

See Also:
Directory

Nested Class Summary
protected static class FSDirectory.FSIndexOutput
           
 
Field Summary
static int DEFAULT_READ_CHUNK_SIZE
          Default read chunk size.
protected  File directory
           
protected  Set<String> staleFiles
           
 
Fields inherited from class org.apache.lucene.store.Directory
isOpen, lockFactory
 
Constructor Summary
protected FSDirectory(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(String name)
          Creates an IndexOutput for the file with the given name.
 void deleteFile(String name)
          Removes an existing file in the directory.
protected  void ensureCanWrite(String name)
           
 boolean fileExists(String name)
          Returns true iff a file with the given name exists.
 long fileLength(String name)
          Returns the length in bytes of a file in the directory.
static long fileModified(File directory, String name)
          Returns the time the named file was last modified.
 long fileModified(String name)
          Returns the time the named file was last modified.
protected  void fsync(String name)
           
 File getDirectory()
           
 File getFile()
          Deprecated. Use getDirectory() instead.
 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 DataInput.readBytes(byte[], int, int).
 String[] listAll()
          Lists all files (not subdirectories) in the directory.
static String[] listAll(File dir)
          Lists all files (not subdirectories) in the directory.
protected  void onIndexOutputClosed(FSDirectory.FSIndexOutput io)
           
static FSDirectory open(File path)
          Creates an FSDirectory instance, trying to pick the best implementation given the current environment.
static FSDirectory open(File path, LockFactory lockFactory)
          Just like open(File), but allows you to also specify a custom LockFactory.
 IndexInput openInput(String name)
          Returns a stream reading an existing file.
 void setLockFactory(LockFactory lockFactory)
          Set the LockFactory that this Directory instance should use for its locking implementation.
 void setReadChunkSize(int chunkSize)
          Sets the maximum number of bytes read at once from the underlying file during DataInput.readBytes(byte[], int, int).
 void sync(Collection<String> names)
          Ensure that any writes to these files are moved to stable storage.
 void sync(String name)
          Deprecated. 
 String toString()
          For debug output.
 void touchFile(String name)
          Deprecated. Lucene never uses this API; it will be removed in 4.0.
 
Methods inherited from class org.apache.lucene.store.Directory
clearLock, copy, copy, ensureOpen, getLockFactory, makeLock, openInput
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

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)

directory

protected final File directory

staleFiles

protected final Set<String> staleFiles
Constructor Detail

FSDirectory

protected FSDirectory(File path,
                      LockFactory lockFactory)
               throws 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:
IOException
Method Detail

open

public static FSDirectory open(File path)
                        throws IOException
Creates an FSDirectory instance, trying to pick the best implementation given the current environment. The directory returned uses the NativeFSLockFactory.

Currently this returns MMapDirectory for most 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(File path,
                               LockFactory lockFactory)
                        throws IOException
Just like open(File), but allows you to also specify a custom LockFactory.

Throws:
IOException

setLockFactory

public void setLockFactory(LockFactory lockFactory)
                    throws IOException
Description copied from class: Directory
Set the LockFactory that this Directory instance should use for its locking implementation. Each * instance of LockFactory should only be used for one directory (ie, do not share a single instance across multiple Directories).

Overrides:
setLockFactory in class Directory
Parameters:
lockFactory - instance of LockFactory.
Throws:
IOException

listAll

public static String[] listAll(File dir)
                        throws 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.
IOException - if list() returns null

listAll

public String[] listAll()
                 throws IOException
Lists all files (not subdirectories) in the directory.

Specified by:
listAll in class Directory
Throws:
NoSuchDirectoryException - if the directory is not prepared for any write operations (such as Directory.createOutput(String)).
IOException - in case of other IO errors
See Also:
listAll(File)

fileExists

public boolean fileExists(String name)
Returns true iff a file with the given name exists.

Specified by:
fileExists in class Directory

fileModified

public long fileModified(String name)
Returns the time the named file was last modified.

Specified by:
fileModified in class Directory

fileModified

public static long fileModified(File directory,
                                String name)
Returns the time the named file was last modified.


touchFile

@Deprecated
public void touchFile(String name)
Deprecated. Lucene never uses this API; it will be removed in 4.0.

Set the modified time of an existing file to now.

Specified by:
touchFile in class Directory

fileLength

public long fileLength(String name)
                throws IOException
Returns the length in bytes of a file in the directory.

Specified by:
fileLength in class Directory
Parameters:
name - the name of the file for which to return the length.
Throws:
FileNotFoundException - if the file does not exist.
IOException - if there was an IO error while retrieving the file's length.

deleteFile

public void deleteFile(String name)
                throws IOException
Removes an existing file in the directory.

Specified by:
deleteFile in class Directory
Throws:
IOException

createOutput

public IndexOutput createOutput(String name)
                         throws IOException
Creates an IndexOutput for the file with the given name.

Specified by:
createOutput in class Directory
Throws:
IOException

ensureCanWrite

protected void ensureCanWrite(String name)
                       throws IOException
Throws:
IOException

onIndexOutputClosed

protected void onIndexOutputClosed(FSDirectory.FSIndexOutput io)

sync

@Deprecated
public void sync(String name)
          throws IOException
Deprecated. 

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:
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.

Overrides:
sync in class Directory
Throws:
IOException

openInput

public IndexInput openInput(String name)
                     throws IOException
Description copied from class: Directory
Returns a stream reading an existing file.

Specified by:
openInput in class Directory
Throws:
IOException

getLockID

public 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 interface Closeable
Specified by:
close in class Directory

getFile

@Deprecated
public File getFile()
Deprecated. Use getDirectory() instead.


getDirectory

public File getDirectory()
Returns:
the underlying filesystem directory

toString

public 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 DataInput.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 IndexInputs. 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 DataInput.readBytes(byte[], int, int).

See Also:
setReadChunkSize(int)

fsync

protected void fsync(String name)
              throws IOException
Throws:
IOException