Class FileManagerImpl

  • All Implemented Interfaces:
    FileManager

    public class FileManagerImpl
    extends java.lang.Object
    implements FileManager
    The File Manager provides unbuffered, low-level operations for working with paged data files. It really doesn't know anything about the internal file formats of the data files, except that the first two bytes of the first page must specify the type and page size for the data file. (This is a requirement of openDBFile(java.lang.String).)
    Design Note:
    Although it might make more sense to put per-file operations like "load page" and "store page" on the DBFile class, we provide higher-level operations on the Storage Manager so that we can provide global buffering capabilities in one place., This class only requires minimal synchronization for thread-safety. The only internal state maintained by the class is the performance information, so the updateFileIOPerfStats(edu.caltech.nanodb.storage.DBFile, int, boolean, int) method includes a synchronized block, but everything else is unprotected because the OS filesystem will be thread-safe.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.io.File baseDir
      The base directory that the file-manager should use for creating and opening files.
      private DBFile lastFileAccessed
      The DBFile object of the file that was accessed most recently.
      private int lastPageNoAccessed
      The page number of the file that was accessed most recently.
      private static org.apache.logging.log4j.Logger logger
      A logging object for reporting anything interesting that happens.
    • Constructor Summary

      Constructors 
      Constructor Description
      FileManagerImpl​(java.io.File baseDir)
      Create a file-manager instance that uses the specified base directory.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void closeDBFile​(DBFile dbFile)
      Closes the underlying data file.
      DBFile createDBFile​(java.lang.String filename, DBFileType type, int pageSize)
      This method creates a new database file in the directory used by the storage manager.
      void deleteDBFile​(DBFile dbFile)
      Deletes the specified database file.
      void deleteDBFile​(java.io.File f)
      Deletes the specified database file.
      void deleteDBFile​(java.lang.String filename)
      Deletes the database file with the specified filename from the storage manager's directory.
      boolean fileExists​(java.lang.String filename)
      Returns true if the specified file exists, or false otherwise.
      java.io.File[] getDBFiles()
      Returns an array of the files in the database.
      private long getPageStart​(DBFile dbFile, int pageNo)
      This helper function calculates the file-position of the specified page.
      boolean loadPage​(DBFile dbFile, int pageNo, byte[] buffer)
      Loads a page from the underlying data file, and returns a new DBPage object containing the data.
      boolean loadPage​(DBFile dbFile, int pageNo, byte[] buffer, boolean create)
      Loads a page from the underlying data file, and returns a new DBPage object containing the data.
      DBFile openDBFile​(java.lang.String filename)
      This method opens a database file, and reads in the file's type and page size from the first two bytes of the first page.
      boolean renameDBFile​(DBFile dbFile, java.lang.String newFilename)
      Attempts to rename the specified DBFile to a new filename.
      void savePage​(DBFile dbFile, int pageNo, byte[] buffer)
      Saves a page to the DB file, and then clears the page's dirty flag.
      void syncDBFile​(DBFile dbFile)
      This method ensures that all file-writes on the specified DB-file have actually been synchronized to the disk.
      private void updateFileIOPerfStats​(DBFile dbFile, int pageNo, boolean read, int bufSize)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • logger

        private static org.apache.logging.log4j.Logger logger
        A logging object for reporting anything interesting that happens.
      • baseDir

        private java.io.File baseDir
        The base directory that the file-manager should use for creating and opening files.
      • lastFileAccessed

        private DBFile lastFileAccessed
        The DBFile object of the file that was accessed most recently. Used for recording performance metrics regarding disk IOs.
      • lastPageNoAccessed

        private int lastPageNoAccessed
        The page number of the file that was accessed most recently. Used for recording performance metrics regarding disk IOs.
    • Constructor Detail

      • FileManagerImpl

        public FileManagerImpl​(java.io.File baseDir)
        Create a file-manager instance that uses the specified base directory.
        Parameters:
        baseDir - the base-directory that the file-manager should use
    • Method Detail

      • getDBFiles

        public java.io.File[] getDBFiles()
        Description copied from interface: FileManager
        Returns an array of the files in the database.
        Specified by:
        getDBFiles in interface FileManager
        Returns:
        list of database files
      • updateFileIOPerfStats

        private void updateFileIOPerfStats​(DBFile dbFile,
                                           int pageNo,
                                           boolean read,
                                           int bufSize)
      • getPageStart

        private long getPageStart​(DBFile dbFile,
                                  int pageNo)
        This helper function calculates the file-position of the specified page. Obviously, this value is dependent on the page size.
        Parameters:
        dbFile - the database file to compute the page-start for
        pageNo - the page number to access
        Returns:
        the offset of the specified page from the start of the database file
        Throws:
        java.lang.IllegalArgumentException - if the page number is negative
      • fileExists

        public boolean fileExists​(java.lang.String filename)
        Description copied from interface: FileManager
        Returns true if the specified file exists, or false otherwise.
        Specified by:
        fileExists in interface FileManager
        Parameters:
        filename - the name of the file to check for existence
        Returns:
        true if the file exists, or false otherwise
      • createDBFile

        public DBFile createDBFile​(java.lang.String filename,
                                   DBFileType type,
                                   int pageSize)
        Description copied from interface: FileManager
        This method creates a new database file in the directory used by the storage manager. An exception is thrown if the file already exists.
        Specified by:
        createDBFile in interface FileManager
        Parameters:
        filename - the name of the file to open to create the database file
        type - the type of database file being created
        pageSize - the page size to use when reading and writing the file
        Returns:
        a new database file object for the newly created file
      • renameDBFile

        public boolean renameDBFile​(DBFile dbFile,
                                    java.lang.String newFilename)
        Description copied from interface: FileManager
        Attempts to rename the specified DBFile to a new filename. If successful, the DBFile object itself is updated with a new File object reflecting the new name. If failure, the DBFile object is left untouched.
        Specified by:
        renameDBFile in interface FileManager
        Parameters:
        dbFile - the database file to rename
        newFilename - the new name to give to the database file
        Returns:
        true if the rename succeeded, or false otherwise.
      • openDBFile

        public DBFile openDBFile​(java.lang.String filename)
        Description copied from interface: FileManager
        This method opens a database file, and reads in the file's type and page size from the first two bytes of the first page. The method uses the RandomAccessFile.readUnsignedShort() method to read the page size from the data file when it is opened.
        Specified by:
        openDBFile in interface FileManager
        Parameters:
        filename - the name of the database file to open
        Returns:
        the successfully opened database file, or null if the file doesn't exist
      • loadPage

        public boolean loadPage​(DBFile dbFile,
                                int pageNo,
                                byte[] buffer,
                                boolean create)
        Description copied from interface: FileManager
        Loads a page from the underlying data file, and returns a new DBPage object containing the data. The create flag controls whether an error is propagated, if the requested page is past the end of the file. (Note that if a new page is created, the file's size will not reflect the new page until it is actually written to the file.)

        This function does no page caching whatsoever. Requesting a particular page multiple times will return multiple page objects, with data loaded from the file each time.

        Specified by:
        loadPage in interface FileManager
        Parameters:
        dbFile - the database file to load the page from
        pageNo - the number of the page to load
        create - a flag specifying whether the page should be created if it doesn't already exist
        Returns:
        true if the page's contents were successfully loaded, or false otherwise.
      • loadPage

        public boolean loadPage​(DBFile dbFile,
                                int pageNo,
                                byte[] buffer)
        Description copied from interface: FileManager
        Loads a page from the underlying data file, and returns a new DBPage object containing the data. This method always reports an EOFException if the specified page is past the end of the database file.

        This function does no page caching whatsoever. Requesting a particular page multiple times will return multiple page objects, with data loaded from the file each time.

        (This method is simply a wrapper of FileManager.loadPage(DBFile, int, byte[], boolean), passing false for create.)

        Specified by:
        loadPage in interface FileManager
        Parameters:
        dbFile - the database file to load the page from
        pageNo - the number of the page to load
        Returns:
        true if the page's contents were successfully loaded, or false otherwise.
      • savePage

        public void savePage​(DBFile dbFile,
                             int pageNo,
                             byte[] buffer)
        Description copied from interface: FileManager
        Saves a page to the DB file, and then clears the page's dirty flag. Note that the data might not actually be written to disk until a sync operation is performed.
        Specified by:
        savePage in interface FileManager
        Parameters:
        dbFile - the data file to write to
        pageNo - the page number to write the buffer to
        buffer - the data to write back to the page
      • syncDBFile

        public void syncDBFile​(DBFile dbFile)
        Description copied from interface: FileManager
        This method ensures that all file-writes on the specified DB-file have actually been synchronized to the disk. Note that even after a call to FileManager.savePage(edu.caltech.nanodb.storage.DBFile, int, byte[]), the filesystem may postpone the write for various reasons, or disks may also buffer the write operations in order to optimize their storage to disk. This method ensures that any buffered writes will actually be written to the disk.
        Specified by:
        syncDBFile in interface FileManager
        Parameters:
        dbFile - the database file to synchronize
      • closeDBFile

        public void closeDBFile​(DBFile dbFile)
        Description copied from interface: FileManager
        Closes the underlying data file. Obviously, subsequent read or write attempts will fail after this method is called.
        Specified by:
        closeDBFile in interface FileManager
        Parameters:
        dbFile - the database file to close
      • deleteDBFile

        public void deleteDBFile​(java.lang.String filename)
        Description copied from interface: FileManager
        Deletes the database file with the specified filename from the storage manager's directory.
        Specified by:
        deleteDBFile in interface FileManager
        Parameters:
        filename - the name of the file to delete
      • deleteDBFile

        public void deleteDBFile​(java.io.File f)
        Description copied from interface: FileManager
        Deletes the specified database file.
        Specified by:
        deleteDBFile in interface FileManager
        Parameters:
        f - the file to delete
      • deleteDBFile

        public void deleteDBFile​(DBFile dbFile)
        Description copied from interface: FileManager
        Deletes the specified database file. The caller should ensure that the database file is closed and is going to be unused.
        Specified by:
        deleteDBFile in interface FileManager
        Parameters:
        dbFile - the database file to delete