Package edu.caltech.nanodb.storage
Class StorageManager
- java.lang.Object
-
- edu.caltech.nanodb.storage.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 Summary
Fields Modifier and Type Field Description private java.io.File
baseDir
The base directory, in which all database files are stored.private BufferManager
bufferManager
The buffer manager stores data pages in memory, to avoid disk IOs.private FileManager
fileManager
The file manager performs basic operations against the filesystem, without performing any buffering whatsoever.private IndexManager
indexManager
private boolean
initialized
A flag recording whether the Storage Manager instance has been initialized.private static org.apache.logging.log4j.Logger
logger
A logging object for reporting anything interesting that happens.private NanoDBServer
server
The database server that this storage manager is a part of.private TableManager
tableManager
private TransactionManager
transactionManager
If transactions are enabled, this will be the transaction manager instance; otherwise, it will benull
.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 Summary
Constructors Constructor Description StorageManager()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
closeDBFile(DBFile dbFile)
DBFile
createDBFile(java.lang.String filename, DBFileType type)
void
flushAllData()
This method allows all data to be flushed from the Buffer Manager.java.io.File
getBaseDir()
Returns the base directory where all database files are stored.BufferManager
getBufferManager()
FileManager
getFileManager()
IndexManager
getIndexManager()
NanoDBServer
getServer()
TableManager
getTableManager()
TransactionManager
getTransactionManager()
TupleFileManager
getTupleFileManager(DBFileType type)
Returns the tuple-file manager for the specified file type.void
initialize(NanoDBServer server)
This method initializes the storage manager.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.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.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.DBFile
openDBFile(java.lang.String filename)
TupleFile
openTupleFile(java.lang.String filename)
void
shutdown()
This method shuts down the storage manager.
-
-
-
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 benull
.
-
tableManager
private TableManager tableManager
-
indexManager
private IndexManager indexManager
-
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.
-
-
Method Detail
-
initialize
public void initialize(NanoDBServer server)
This method initializes the storage manager. It should only be called once.- Throws:
StorageException
- ifjava.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 calledStorageException
- if the storage manager cannot save all data for some reason
-
getServer
public NanoDBServer getServer()
-
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
-
getFileManager
public FileManager getFileManager()
-
getBufferManager
public BufferManager getBufferManager()
-
getTransactionManager
public TransactionManager getTransactionManager()
-
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)
-
getTableManager
public TableManager getTableManager()
-
getIndexManager
public IndexManager getIndexManager()
-
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 frompageNo
- the number of the page to loadcreate
- 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 andcreate
isfalse
. - 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 frompageNo
- 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, theDBPage.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()
-
-