SwatDB
|
#include <diskmgr.h>
Classes | |
struct | DiskFileInfo |
struct | SerializedFileHeader |
Public Member Functions | |
DiskManager (Catalog *catalog) | |
DiskManager constructor. Uses Catalog object pointer and a vector of FileIds to initialize file_map. More... | |
~DiskManager () | |
DiskManager destructor. All pages have been written to disk prior to this call. More... | |
void | createFile (FileId file_id) |
Creates a new Unix file, opens the fstream, initializes DiskFileInfo object, and adds <FileId, DiskFileInfo*> pair to fileMap. Adds header data to Unix file via SerializedFileInfo. More... | |
void | removeFile (FileId file_id) |
Deletes the Unix file corresponding to file_id, and removes the <FileId, DiskFileInfo*> key-value pair from fileMap. More... | |
PageId | allocatePage (FileId file_id) |
Allocates a Page to the file which corresponds to file_id. More... | |
void | deallocatePage (PageId page_id) |
Deallocates page by adding its offest to unused_pages in the appropriate DiskFileInfo struct. More... | |
void | readPage (PageId page_id, Page *page) |
Reads the page data from the Unix file into the Page object pointer. More... | |
void | writePage (PageId page_id, Page *page) |
Writes the page data of the given Page object at the right offset in the appropriate Unix file. More... | |
bool | isValidPage (PageId page_id) |
Checks if the page of a given pageId is valid. More... | |
void | printFile (FileId file_id) |
THIS METHOD IS FOR DEBUGGING ONLY. Prints contents of a file inlcuding FileId, size, capacity, and contents of each page. More... | |
std::uint32_t | getCapacity (FileId file_id) |
Get method for the capacity of a file. More... | |
std::uint32_t | getSize (FileId file_id) |
Get method for size of a file. More... | |
Private Member Functions | |
void | _getDiskFileInfo (FileId file_id, DiskFileInfo **disk_file_info) |
Initializes DiskFileInfo** of the given FileId. More... | |
void | _flushDiskFileInfo (FileId file_id) |
Writes data from DiskFileInfo struct to Unix file for the given FileId. More... | |
void | _serializeFileHeader (SerializedFileHeader *file_header, DiskFileInfo *disk_file_info) |
Serializes Unix file metadata by initializing SerializedFileHeader object given DiskFileInfo object. More... | |
void | _deserializeFileHeader (SerializedFileHeader *file_header, DiskFileInfo *disk_file_info) |
Intializes DiskFileInfo object given SerializedFileHeader object. More... | |
Private Attributes | |
std::unordered_map< FileId, DiskFileInfo * > | file_map |
Catalog * | catalog |
std::mutex | file_map_mtx |
char | temp_page [PAGE_SIZE] |
Static Private Attributes | |
static const std::uint32_t | DISK_HEADER_SIZE |
static const std::uint32_t | DISK_HEADER_BITMAP_SIZE |
SwatDB DiskManager Class. DiskManager manages page level disk operations of SwatDB, including writing, reading, allocating, and deallocating pages. As SwatDB is built for pedagogical reason, this layer is built on top of regular Unix file system, rather than raw device/DIRECT_IO.
DiskManager::DiskManager | ( | Catalog * | catalog | ) |
DiskManager constructor. Uses Catalog object pointer and a vector of FileIds to initialize file_map.
catalog | A pointer to the DBMS's catalog object (Catalog*). |
DiskErrorDiskMgr | if file operation fails |
DiskManager::~DiskManager | ( | ) |
DiskManager destructor. All pages have been written to disk prior to this call.
|
private |
Intializes DiskFileInfo object given SerializedFileHeader object.
file_header | SerializedFileHeader object to be used for initializing SerializedFileHeader. |
disk_file_info | DiskFileInfo to be initialized. |
|
private |
Writes data from DiskFileInfo struct to Unix file for the given FileId.
file_id | FileId of the file to have its data written. |
InvalidFileIdDiskMgr | If file_id is not in file_map. |
DiskErrorDiskMgr | If file operation fails. |
|
private |
Initializes DiskFileInfo** of the given FileId.
file_id | FileId of the DiskFileInfo* to be retrieved. |
InvalidFileIdDiskMgr | If file_id is not in file_map. |
|
private |
Serializes Unix file metadata by initializing SerializedFileHeader object given DiskFileInfo object.
file_header | SerializedFileHeader object to be initialized. |
disk_file_info | DiskFileInfo to be used for initializing SerializedFileHeader. |
Allocates a Page to the file which corresponds to file_id.
file_id | A FileId corresponding to the file in which a page will be allocated. |
InsufficientSpaceDiskMgr | If file_id is in file_map and of the file is equal to MAX_CAPCITY. |
InvalidFileIdDiskMgr | If file_id is not in file_map. |
DiskErrorDiskMgr | If file operation fails. |
void DiskManager::createFile | ( | FileId | file_id | ) |
Creates a new Unix file, opens the fstream, initializes DiskFileInfo object, and adds <FileId, DiskFileInfo*> pair to fileMap. Adds header data to Unix file via SerializedFileInfo.
file_id | A FileId of the file to be created. |
FileIdAlreadyExistDiskMgr | If file_id is already in file_map. |
FileAlreadyExistDiskMgr | If Unix file already exists. |
DiskErrorDiskMgr | If file operation fails. |
void DiskManager::deallocatePage | ( | PageId | page_id | ) |
Deallocates page by adding its offest to unused_pages in the appropriate DiskFileInfo struct.
page_id | A PageId corresponding to the page to be deallocated. |
InvalidPageNumDiskMgr | If page_id.page_num is out of range or if the page_id.page_num is in unused_pages. |
InvalidFileIdDiskMgr | If page_id.file_id is not in file_map. |
std::uint32_t DiskManager::getCapacity | ( | FileId | file_id | ) |
Get method for the capacity of a file.
file_id | A FileId corresponding to the Unix file to get capacity. |
InvalidFileIdDiskMgr | If file_id is not in file_map. |
std::uint32_t DiskManager::getSize | ( | FileId | file_id | ) |
Get method for size of a file.
file_id | A FileId corresponding to the Unix file to get size. |
InvalidFileIdDiskMgr | If file_id is not in file_map. |
bool DiskManager::isValidPage | ( | PageId | page_id | ) |
Checks if the page of a given pageId is valid.
page_id | A PageId for validity to be checked. |
void DiskManager::printFile | ( | FileId | file_id | ) |
THIS METHOD IS FOR DEBUGGING ONLY. Prints contents of a file inlcuding FileId, size, capacity, and contents of each page.
file_id | A FileId corresponding to the Unix file to be printed |
InvalidFileIdDiskMgr | If file_id is not in file_map. |
DiskErrorDiskMgr | If file operation fails. |
Reads the page data from the Unix file into the Page object pointer.
page_id | A PageId of the page to be read from Unix file. |
page | A Page pointer to be initialized. |
InvalidFileIdDiskMgr | If page_id.file_id is not in file_map. |
InvalidPageNumDiskMgr | If page_id.page_num is out of range or if the page_id.page_num is in unused_pages. |
DiskErrorDiskMgr | If file operation fails. |
void DiskManager::removeFile | ( | FileId | file_id | ) |
Deletes the Unix file corresponding to file_id, and removes the <FileId, DiskFileInfo*> key-value pair from fileMap.
file_id | A file_id of the file to be removed. |
InvalidFileIdDiskMgr | If file_id is not in file_map. |
Writes the page data of the given Page object at the right offset in the appropriate Unix file.
page_id | A PageId to write Page object data in the appropriate Unix file. |
page | A Page* object containing data to be written to the appropriate Unix file. |
InvalidFileIdDiskMgr | If page_id.file_id is not in file_map. |
InvalidPageNumDiskMgr | If page_id.page_num is out of range or if the page_id.page_num is in unused_pages. |
DiskErrorDiskMgr | If file operation fails. |
|
private |
Pointer to catalog object that stores the entire metadata and catalog information about SwatDB. Used by the DiskManager to retrieve file names of given FileIds and initialize file_map.
|
staticprivate |
number of bytes in the bitmap part of the SerializedFileHeader struct (it is the DISK_HEADER_SIZE minus the space for size and capacity fields)
|
staticprivate |
The size of the disk header. Computed based on the MAX_PAGE_NUM as the bitmap should be big enough to represent valid state of MAX_PAGE_NUM number of pages. The size is evenly divisible by PAGE_SIZE.
|
private |
A map that stores <FileId (uint32_t), DiskFileInfo pointer> key-value pairs. Allows access to file (read, write, rm) given FileId.
|
private |
std:mutex for synchronized access to file_map.
|
private |
Temporary Page for writing into Unix file when allocating page.