Class NIOFSDirectory

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class NIOFSDirectory
    extends FSDirectory
    An FSDirectory implementation that uses java.nio's FileChannel's positional read, which allows multiple threads to read from the same file without synchronizing.

    This class only uses FileChannel when reading; writing is achieved with FSDirectory.FSIndexOutput.

    NOTE: NIOFSDirectory is not recommended on Windows because of a bug in how FileChannel.read is implemented in Sun's JRE. Inside of the implementation the position is apparently synchronized. See here for details.

    NOTE: Accessing this class either directly or indirectly from a thread while it's interrupted can close the underlying file descriptor immediately if at the same time the thread is blocked on IO. The file descriptor will remain closed and subsequent access to NIOFSDirectory will throw a ClosedChannelException. If your application uses either Thread.interrupt() or Future.cancel(boolean) you should use SimpleFSDirectory in favor of NIOFSDirectory.

    • Constructor Detail

      • NIOFSDirectory

        public NIOFSDirectory​(java.io.File path,
                              LockFactory lockFactory)
                       throws java.io.IOException
        Create a new NIOFSDirectory for the named location.
        Parameters:
        path - the path of the directory
        lockFactory - the lock factory to use, or null for the default (NativeFSLockFactory);
        Throws:
        java.io.IOException - if there is a low-level I/O error
      • NIOFSDirectory

        public NIOFSDirectory​(java.io.File path)
                       throws java.io.IOException
        Create a new NIOFSDirectory for the named location and NativeFSLockFactory.
        Parameters:
        path - the path of the directory
        Throws:
        java.io.IOException - if there is a low-level I/O error
    • Method Detail

      • openInput

        public IndexInput openInput​(java.lang.String name,
                                    IOContext context)
                             throws java.io.IOException
        Creates an IndexInput for the file with the given name.
        Specified by:
        openInput in class Directory
        Throws:
        java.io.IOException
      • createSlicer

        public Directory.IndexInputSlicer createSlicer​(java.lang.String name,
                                                       IOContext context)
                                                throws java.io.IOException
        Description copied from class: Directory
        Creates an Directory.IndexInputSlicer for the given file name. IndexInputSlicer allows other Directory implementations to efficiently open one or more sliced IndexInput instances from a single file handle. The underlying file handle is kept open until the Directory.IndexInputSlicer is closed.
      • Must throw FileNotFoundException if the file does not exist (not java.nio.file.NoSuchFileException of Java 7).
Overrides:
createSlicer in class Directory
Throws:
java.io.IOException - if an IOException occurs