Class StorageManager


  • public class StorageManager
    extends java.lang.Object
    The Storage Manager provides facilities for managing files of tuples, including in-memory buffering of data pages and support for transactions.
    To Do:
    This class requires synchronization, once we support multiple clients.
    • Field Detail

      • logger

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

        private NanoDBServer server
        The database server that this storage manager is a part of. It provides a number of the components that the storage manager depends on, such as the event dispatcher and property registry.
      • baseDir

        private java.io.File baseDir
        The base directory, in which all database files are stored.
      • initialized

        private boolean initialized
        A flag recording whether the Storage Manager instance has been initialized.
      • bufferManager

        private BufferManager bufferManager
        The buffer manager stores data pages in memory, to avoid disk IOs.
      • fileManager

        private FileManager fileManager
        The file manager performs basic operations against the filesystem, without performing any buffering whatsoever.
      • transactionManager

        private TransactionManager transactionManager
        If transactions are enabled, this will be the transaction manager instance; otherwise, it will be null.
      • tupleFileManagers

        private java.util.HashMap<DBFileType,​TupleFileManager> tupleFileManagers
        This mapping is used to keep track of the tuple-file managers for all the kinds of tuple-files we support.
    • Constructor Detail

      • StorageManager

        public StorageManager()
    • Method Detail

      • initialize

        public void initialize​(NanoDBServer server)
        This method initializes the storage manager. It should only be called once.
        Throws:
        StorageException - if
        java.lang.IllegalStateException - if init() has already been called
      • shutdown

        public void shutdown()
        This method shuts down the storage manager. It should only be called once.
        Throws:
        java.lang.IllegalStateException - if init() has not been called
        StorageException - if the storage manager cannot save all data for some reason
      • getBaseDir

        public java.io.File getBaseDir()
        Returns the base directory where all database files are stored.
        Returns:
        the base directory where all database files are stored
      • createDBFile

        public DBFile createDBFile​(java.lang.String filename,
                                   DBFileType type)
      • openDBFile

        public DBFile openDBFile​(java.lang.String filename)
      • openTupleFile

        public TupleFile openTupleFile​(java.lang.String filename)
      • closeDBFile

        private void closeDBFile​(DBFile dbFile)
      • getTupleFileManager

        public TupleFileManager getTupleFileManager​(DBFileType type)
        Returns the tuple-file manager for the specified file type.
        Parameters:
        type - the database file type to get the tuple-file manager for.
        Returns:
        the tuple-file manager instance for the specified file type
        Throws:
        java.lang.IllegalArgumentException - if the file-type is null, or if the file-type is currently unsupported.
      • loadDBPage

        public DBPage loadDBPage​(DBFile dbFile,
                                 int pageNo,
                                 boolean create)
        This method returns a database page to use, retrieving it from the buffer manager if it is already loaded, or reading it from the specified data file if it is not already loaded. If the page must be loaded from the file, it will be added to the buffer manager. This operation may cause other database pages to be evicted from the buffer manager, and written back to disk if the evicted pages are dirty.

        The create flag controls whether an error is propagated, if the requested page is past the current end of the data 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.)

        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:
        the database page (either from cache or from the data file), or null if the requested page is not in the data file and create is false.
        Throws:
        java.lang.IllegalArgumentException - if the page number is negative
      • loadDBPage

        public DBPage loadDBPage​(DBFile dbFile,
                                 int pageNo)
        This method returns a database page to use, retrieving it from the buffer manager if it is already loaded, or reading it from the specified data file if it is not already loaded. If the page must be loaded from the file, it will be added to the buffer manager. This operation may cause other database pages to be evicted from the buffer manager, and written back to disk if the evicted pages are dirty.

        (This method is simply a wrapper of loadDBPage(DBFile, int, boolean), passing false for create.)

        Parameters:
        dbFile - the database file to load the page from
        pageNo - the number of the page to load
        Returns:
        the database page (either from cache or from the data file), or null if the requested page is not in the data file
        Throws:
        java.lang.IllegalArgumentException - if the page number is negative
      • logDBPageWrite

        public void logDBPageWrite​(DBPage dbPage)
        This method causes any changes to the specified page to be logged by the transaction manager's write-ahead log, so that the changes can be redone or undone as may be appropriate. Once the page's changes have been logged, the DBPage.syncOldPageData() method is called on the page, since the page's changes have been recorded in the WAL.
        Parameters:
        dbPage - the page to record changes for
      • flushAllData

        public void flushAllData()
        This method allows all data to be flushed from the Buffer Manager. It should not be used in practice, but it is useful to remove buffering to expose performance issues in the storage layer.
        See Also:
        BufferManager.flushAll()