public abstract class FSDirectory extends Directory
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)
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 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.
Applications using Thread.interrupt()
or
Future.cancel(boolean)
should use
SimpleFSDirectory
instead. See MMapDirectory
java doc for details.
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.
Directory
Modifier and Type | Class and Description |
---|---|
protected static class |
FSDirectory.FSIndexOutput |
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_READ_CHUNK_SIZE
Default read chunk size.
|
protected File |
directory |
protected Set<String> |
staleFiles |
isOpen, lockFactory
Modifier | Constructor and Description |
---|---|
protected |
FSDirectory(File path,
LockFactory lockFactory)
Create a new FSDirectory for the named location (ctor for subclasses).
|
Modifier and Type | Method and Description |
---|---|
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.
|
clearLock, copy, copy, ensureOpen, getLockFactory, makeLock, openInput
public static final int DEFAULT_READ_CHUNK_SIZE
Integer.MAX_VALUE
.setReadChunkSize(int)
protected final File directory
protected FSDirectory(File path, LockFactory lockFactory) throws IOException
path
- the path of the directorylockFactory
- the lock factory to use, or null for the default
(NativeFSLockFactory
);IOException
public static FSDirectory open(File path) throws IOException
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
IOException
public static FSDirectory open(File path, LockFactory lockFactory) throws IOException
open(File)
, but allows you to
also specify a custom LockFactory
.IOException
public void setLockFactory(LockFactory lockFactory) throws IOException
Directory
setLockFactory
in class Directory
lockFactory
- instance of LockFactory
.IOException
public static String[] listAll(File dir) throws IOException
IOException
instead).NoSuchDirectoryException
- if the directory
does not exist, or does exist but is not a
directory.IOException
- if list() returns nullpublic String[] listAll() throws IOException
listAll
in class Directory
NoSuchDirectoryException
- if the directory is not prepared for any
write operations (such as Directory.createOutput(String)
).IOException
- in case of other IO errorslistAll(File)
public boolean fileExists(String name)
fileExists
in class Directory
public long fileModified(String name)
fileModified
in class Directory
public static long fileModified(File directory, String name)
@Deprecated public void touchFile(String name)
public long fileLength(String name) throws IOException
fileLength
in class Directory
name
- the name of the file for which to return the length.FileNotFoundException
- if the file does not exist.IOException
- if there was an IO error while retrieving the file's
length.public void deleteFile(String name) throws IOException
deleteFile
in class Directory
IOException
public IndexOutput createOutput(String name) throws IOException
createOutput
in class Directory
IOException
protected void ensureCanWrite(String name) throws IOException
IOException
protected void onIndexOutputClosed(FSDirectory.FSIndexOutput io)
@Deprecated public void sync(String name) throws IOException
Directory
sync
in class Directory
IOException
public void sync(Collection<String> names) throws IOException
Directory
sync
in class Directory
IOException
public IndexInput openInput(String name) throws IOException
Directory
openInput
in class Directory
IOException
public String getLockID()
Directory
public void close()
@Deprecated public File getFile()
getDirectory()
instead.public File getDirectory()
public final void setReadChunkSize(int chunkSize)
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 IndexInput
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.
public final int getReadChunkSize()
DataInput.readBytes(byte[], int, int)
.setReadChunkSize(int)
protected void fsync(String name) throws IOException
IOException