SwatDB
Classes | Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
DiskManager Class Reference

#include <diskmgr.h>

Collaboration diagram for DiskManager:
Collaboration graph
[legend]

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
 
Catalogcatalog
 
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
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ DiskManager()

DiskManager::DiskManager ( Catalog catalog)

DiskManager constructor. Uses Catalog object pointer and a vector of FileIds to initialize file_map.

Precondition
Valid Catalog* is passed as an input.
Postcondition
A vector of FileIds are retrieved from the Catalog and appropriate fstream is opened for each FileId. Then a DiskManager object is initialized and each <FileId, DiskFileInfo*> pair is added to file_map.
Parameters
catalogA pointer to the DBMS's catalog object (Catalog*).
Exceptions
DiskErrorDiskMgrif file operation fails

◆ ~DiskManager()

DiskManager::~DiskManager ( )

DiskManager destructor. All pages have been written to disk prior to this call.

Precondition
None
Postcondition
The metadata for each relation/index file is written to the appropriate Unix file via _flushDiskFileInfo().
See also
DiskManager::_flushDiskFileInfo()

Member Function Documentation

◆ _deserializeFileHeader()

void DiskManager::_deserializeFileHeader ( SerializedFileHeader file_header,
DiskFileInfo disk_file_info 
)
private

Intializes DiskFileInfo object given SerializedFileHeader object.

Precondition
A valid SerializedFileHeader* and a valid DiskFileInfo* are provided as input.
Postcondition
Data members of disk_file_info, including capacity and unused_pages are initialized to a state consistent with file_header.
Parameters
file_headerSerializedFileHeader object to be used for initializing SerializedFileHeader.
disk_file_infoDiskFileInfo to be initialized.

◆ _flushDiskFileInfo()

void DiskManager::_flushDiskFileInfo ( FileId  file_id)
private

Writes data from DiskFileInfo struct to Unix file for the given FileId.

Precondition
FileId in file_map is provided as input.
Postcondition
The metadata from the corresponding DiskFileInfo struct is written to the Unix file.
Parameters
file_idFileId of the file to have its data written.
Exceptions
InvalidFileIdDiskMgrIf file_id is not in file_map.
DiskErrorDiskMgrIf file operation fails.

◆ _getDiskFileInfo()

void DiskManager::_getDiskFileInfo ( FileId  file_id,
DiskFileInfo **  disk_file_info 
)
private

Initializes DiskFileInfo** of the given FileId.

Precondition
FileId in file_map and a valid DiskFileInfo** are provided as input. file_map_mtx is locked.
Postcondition
Corresponding DiskFileInfo* is initialized.
Parameters
file_idFileId of the DiskFileInfo* to be retrieved.
Exceptions
InvalidFileIdDiskMgrIf file_id is not in file_map.

◆ _serializeFileHeader()

void DiskManager::_serializeFileHeader ( SerializedFileHeader file_header,
DiskFileInfo disk_file_info 
)
private

Serializes Unix file metadata by initializing SerializedFileHeader object given DiskFileInfo object.

Precondition
_serializeFileHeader must recieve a valid SerializedFileHeader* and a valid DiskFileInfo*.
Postcondition
Data members of file_header, including size, capacity and bitmap are initialized to a state consistent with disk_file_info.
Parameters
file_headerSerializedFileHeader object to be initialized.
disk_file_infoDiskFileInfo to be used for initializing SerializedFileHeader.

◆ allocatePage()

PageId DiskManager::allocatePage ( FileId  file_id)

Allocates a Page to the file which corresponds to file_id.

Precondition
A FileId in file_map is provided as input and there is enough space in the corresponding Unix file.
Postcondition
If unused_pages set of the corresponding DiskFileInfo struct is not empty, then a PageNum in the set is popped, and is used with the file_id to construct an appropriate PageId, which is returned. If the unused_pages set of the corresponding DiskFileInfo struct is empty, and capacity is less than MAX_PAGE_NUM, then it is used with the file_id to construct an appropriate PageId, which is returned. Capacity is incremented and the size of underlying Unix file is increased by PAGE_SIZE. The updated metadata about the file is not immediately written to the file to minimize disk I/O.
Parameters
file_idA FileId corresponding to the file in which a page will be allocated.
Returns
PageId of the allocated page.
Exceptions
InsufficientSpaceDiskMgrIf file_id is in file_map and of the file is equal to MAX_CAPCITY.
InvalidFileIdDiskMgrIf file_id is not in file_map.
DiskErrorDiskMgrIf file operation fails.

◆ createFile()

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.

Precondition
A FileId that is added to catalog and is not in file_map.
Postcondition
A new Unix file is created, the fstream is opened, DiskFileInfo is initialized for that file and <FileId, DiskFileInfo*> pair is added to file_map.
Parameters
file_idA FileId of the file to be created.
Exceptions
FileIdAlreadyExistDiskMgrIf file_id is already in file_map.
FileAlreadyExistDiskMgrIf Unix file already exists.
DiskErrorDiskMgrIf file operation fails.

◆ deallocatePage()

void DiskManager::deallocatePage ( PageId  page_id)

Deallocates page by adding its offest to unused_pages in the appropriate DiskFileInfo struct.

Precondition
PageId of an allocated Page is provided as input.
Postcondition
page_id.page_num is added to the unused_pages set in the appropriate DiskFileInfo struct. The updated metadata about the file is not immediately written to the file due to performance reasons.
Parameters
page_idA PageId corresponding to the page to be deallocated.
Exceptions
InvalidPageNumDiskMgrIf page_id.page_num is out of range or if the page_id.page_num is in unused_pages.
InvalidFileIdDiskMgrIf page_id.file_id is not in file_map.

◆ getCapacity()

std::uint32_t DiskManager::getCapacity ( FileId  file_id)

Get method for the capacity of a file.

Precondition
A FileId in file_map is provided as input.
Postcondition
capacity of the corresponding file is returned.
Parameters
file_idA FileId corresponding to the Unix file to get capacity.
Returns
capacity of the corresponding file.
Exceptions
InvalidFileIdDiskMgrIf file_id is not in file_map.

◆ getSize()

std::uint32_t DiskManager::getSize ( FileId  file_id)

Get method for size of a file.

Precondition
A FileId in file_map is provided as input.
Postcondition
size of the corresponding file is returned.
Parameters
file_idA FileId corresponding to the Unix file to get size.
Returns
size of the corresponding file.
Exceptions
InvalidFileIdDiskMgrIf file_id is not in file_map.

◆ isValidPage()

bool DiskManager::isValidPage ( PageId  page_id)

Checks if the page of a given pageId is valid.

Precondition
PageId of a Page is provided as input.
Postcondition
If page_id.file_id is not in file_map or page_id.page_num is out of range or is in unused_pages, false is returned. Otherwise true is returned.
Parameters
page_idA PageId for validity to be checked.
Returns
bool indicating whether the page is valid.

◆ printFile()

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.

Precondition
A FileId in file_map is provided as input.
Postcondition
The content of the corresponding file,including size, capacity and content of each page is printed.
Parameters
file_idA FileId corresponding to the Unix file to be printed
Exceptions
InvalidFileIdDiskMgrIf file_id is not in file_map.
DiskErrorDiskMgrIf file operation fails.

◆ readPage()

void DiskManager::readPage ( PageId  page_id,
Page page 
)

Reads the page data from the Unix file into the Page object pointer.

Precondition
PageId of an allocated Page, and a valid Page object pointer are provided as input.
Postcondition
The data of the page is read from the Unix file into the Page object.
Parameters
page_idA PageId of the page to be read from Unix file.
pageA Page pointer to be initialized.
Exceptions
InvalidFileIdDiskMgrIf page_id.file_id is not in file_map.
InvalidPageNumDiskMgrIf page_id.page_num is out of range or if the page_id.page_num is in unused_pages.
DiskErrorDiskMgrIf file operation fails.

◆ removeFile()

void DiskManager::removeFile ( FileId  file_id)

Deletes the Unix file corresponding to file_id, and removes the <FileId, DiskFileInfo*> key-value pair from fileMap.

Precondition
A FileId in file_map is provided as input.
Postcondition
The corresponding file's fstream is closed, the Unix file is removed, and the <FileId, DiskFileInfo*> pair is removed from file_map.
Parameters
file_idA file_id of the file to be removed.
Exceptions
InvalidFileIdDiskMgrIf file_id is not in file_map.

◆ writePage()

void DiskManager::writePage ( PageId  page_id,
Page page 
)

Writes the page data of the given Page object at the right offset in the appropriate Unix file.

Precondition
PageId of an allocated Page, and a valid Page reference are provided as input.
Postcondition
The Page object data is written to the Unix file at the right offset.
Parameters
page_idA PageId to write Page object data in the appropriate Unix file.
pageA Page* object containing data to be written to the appropriate Unix file.
Exceptions
InvalidFileIdDiskMgrIf page_id.file_id is not in file_map.
InvalidPageNumDiskMgrIf page_id.page_num is out of range or if the page_id.page_num is in unused_pages.
DiskErrorDiskMgrIf file operation fails.

Member Data Documentation

◆ catalog

Catalog* DiskManager::catalog
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.

◆ DISK_HEADER_BITMAP_SIZE

const std::uint32_t DiskManager::DISK_HEADER_BITMAP_SIZE
staticprivate
Initial value:
=
DISK_HEADER_SIZE - (sizeof(std::uint32_t)*2)

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)

◆ DISK_HEADER_SIZE

const std::uint32_t DiskManager::DISK_HEADER_SIZE
staticprivate
Initial value:
=
((((MAX_PAGE_NUM/8)+1+(sizeof(std::uint32_t)*2))/PAGE_SIZE)+1)*PAGE_SIZE

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.

◆ file_map

std::unordered_map<FileId, DiskFileInfo*> DiskManager::file_map
private

A map that stores <FileId (uint32_t), DiskFileInfo pointer> key-value pairs. Allows access to file (read, write, rm) given FileId.

◆ file_map_mtx

std::mutex DiskManager::file_map_mtx
private

std:mutex for synchronized access to file_map.

◆ temp_page

char DiskManager::temp_page[PAGE_SIZE]
private

Temporary Page for writing into Unix file when allocating page.


The documentation for this class was generated from the following file: