Class Directory

  • All Implemented Interfaces:
    Closeable, AutoCloseable
    Direct Known Subclasses:
    BaseDirectory, FilterDirectory, NRTCachingDirectory

    public abstract class Directory
    extends Object
    implements Closeable
    A Directory is a flat list of files. Files may be written once, when they are created. Once a file is created it may only be opened for read, or deleted. Random access is permitted both when reading and writing.

    Java's i/o APIs not used directly, but rather all i/o is through this API. This permits things such as:

    • implementation of RAM-based indices;
    • implementation indices stored in a database, via JDBC;
    • implementation of an index as a single file;
    Directory locking is implemented by an instance of LockFactory, and can be changed for each Directory instance using setLockFactory(org.apache.lucene.store.LockFactory).
    • Constructor Detail

      • Directory

        public Directory()
    • Method Detail

      • fileExists

        public abstract boolean fileExists​(String name)
                                    throws IOException
        Returns true iff a file with the given name exists.
        Throws:
        IOException
      • deleteFile

        public abstract void deleteFile​(String name)
                                 throws IOException
        Removes an existing file in the directory.
        Throws:
        IOException
      • fileLength

        public abstract long fileLength​(String name)
                                 throws IOException
        Returns the length of a file in the directory. This method follows the following contract:
        • Must throw FileNotFoundException if the file does not exist (not java.nio.file.NoSuchFileException of Java 7).
        • Returns a value ≥0 if the file exists, which specifies its length.
        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.
      • sync

        public abstract void sync​(Collection<String> names)
                           throws IOException
        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.
        Throws:
        IOException
      • openInput

        public abstract IndexInput openInput​(String name,
                                             IOContext context)
                                      throws IOException
        Returns a stream reading an existing file, with the specified read buffer size. The particular Directory implementation may ignore the buffer size. Currently the only Directory implementations that respect this parameter are FSDirectory and CompoundFileDirectory.
      • Must throw FileNotFoundException if the file does not exist (not java.nio.file.NoSuchFileException of Java 7).
Throws:
IOException
Throws:
IOException - if an IOException occurs