SwatDB
diskmgr.h
Go to the documentation of this file.
1 /*
2  * SwatDB
3  *
4  * @authors: See Contributors.doc for code contributors
5  *
6  * Copyright (c) 2020 Swarthmore College Computer Science Department
7  * Swarthmore PA, Professors Tia Newhall, Ameet Soni
8  */
9 
10 #pragma once
11 
16 #include<iostream>
17 #include<string>
18 #include <fstream>
19 #include <vector>
20 #include <unordered_set>
21 #include <unordered_map>
22 #include <mutex>
23 
24 #include "swatdb_types.h"
25 
26 class Catalog;
27 class Page;
28 
36 class DiskManager {
37 
38  public:
39 
55 
66  ~DiskManager();
67 
85  void createFile(FileId file_id);
86 
100  void removeFile(FileId file_id);
101 
126  PageId allocatePage(FileId file_id);
127 
143  void deallocatePage(PageId page_id);
144 
163  void readPage(PageId page_id, Page* page);
164 
184  void writePage(PageId page_id, Page* page);
185 
197  bool isValidPage(PageId page_id);
198 
212  void printFile(FileId file_id);
213 
225  std::uint32_t getCapacity(FileId file_id);
226 
238  std::uint32_t getSize(FileId file_id);
239 
240  private:
241 
247  // this needs a comment describing this expression!
248  static const std::uint32_t DISK_HEADER_SIZE =
249  ((((MAX_PAGE_NUM/8)+1+(sizeof(std::uint32_t)*2))/PAGE_SIZE)+1)*PAGE_SIZE;
250 
256  static const std::uint32_t DISK_HEADER_BITMAP_SIZE =
257  DISK_HEADER_SIZE - (sizeof(std::uint32_t)*2);
258 
267  std::uint32_t size;
271  std::uint32_t capacity;
277  };
278 
285  struct DiskFileInfo{
286 
290  std::fstream file;
291 
295  std::uint32_t capacity;
296 
300  std::unordered_set<PageNum> unused_pages;
301 
305  std::mutex file_mtx;
306  };
307 
312  std::unordered_map <FileId, DiskFileInfo*> file_map;
313 
320 
324  std::mutex file_map_mtx;
325 
330 
342  void _getDiskFileInfo(FileId file_id, DiskFileInfo**
343  disk_file_info);
344 
358  void _flushDiskFileInfo(FileId file_id);
359 
373  void _serializeFileHeader(SerializedFileHeader* file_header,
374  DiskFileInfo* disk_file_info);
375 
389  DiskFileInfo* disk_file_info);
390 
391 };
void _deserializeFileHeader(SerializedFileHeader *file_header, DiskFileInfo *disk_file_info)
Intializes DiskFileInfo object given SerializedFileHeader object.
void _serializeFileHeader(SerializedFileHeader *file_header, DiskFileInfo *disk_file_info)
Serializes Unix file metadata by initializing SerializedFileHeader object given DiskFileInfo object...
Definition: diskmgr.h:263
DiskManager(Catalog *catalog)
DiskManager constructor. Uses Catalog object pointer and a vector of FileIds to initialize file_map...
Catalog * catalog
Definition: diskmgr.h:319
~DiskManager()
DiskManager destructor. All pages have been written to disk prior to this call.
char temp_page[PAGE_SIZE]
Definition: diskmgr.h:329
std::unordered_map< FileId, DiskFileInfo * > file_map
Definition: diskmgr.h:312
PageId allocatePage(FileId file_id)
Allocates a Page to the file which corresponds to file_id.
static const std::uint32_t DISK_HEADER_SIZE
Definition: diskmgr.h:248
void _flushDiskFileInfo(FileId file_id)
Writes data from DiskFileInfo struct to Unix file for the given FileId.
std::unordered_set< PageNum > unused_pages
Definition: diskmgr.h:300
std::fstream file
Definition: diskmgr.h:290
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...
void _getDiskFileInfo(FileId file_id, DiskFileInfo **disk_file_info)
Initializes DiskFileInfo** of the given FileId.
std::mutex file_map_mtx
Definition: diskmgr.h:324
std::mutex file_mtx
Definition: diskmgr.h:305
std::uint32_t size
Definition: diskmgr.h:267
std::uint32_t getCapacity(FileId file_id)
Get method for the capacity of a file.
bool isValidPage(PageId page_id)
Checks if the page of a given pageId is valid.
static const std::uint32_t DISK_HEADER_BITMAP_SIZE
Definition: diskmgr.h:256
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.
Definition: swatdb_types.h:50
const std::uint32_t MAX_PAGE_NUM
Definition: swatdb_types.h:152
std::uint32_t getSize(FileId file_id)
Get method for size of a file.
const std::uint32_t PAGE_SIZE
Definition: swatdb_types.h:129
std::uint32_t capacity
Definition: diskmgr.h:271
char bitmap[DISK_HEADER_BITMAP_SIZE]
Definition: diskmgr.h:276
Definition: diskmgr.h:36
Definition: catalog.h:134
void deallocatePage(PageId page_id)
Deallocates page by adding its offest to unused_pages in the appropriate DiskFileInfo struct...
std::uint32_t capacity
Definition: diskmgr.h:295
void readPage(PageId page_id, Page *page)
Reads the page data from the Unix file into the Page object pointer.
std::uint32_t FileId
Definition: swatdb_types.h:29
Definition: diskmgr.h:285
void createFile(FileId file_id)
Creates a new Unix file, opens the fstream, initializes DiskFileInfo object, and adds <FileId...
void removeFile(FileId file_id)
Deletes the Unix file corresponding to file_id, and removes the <FileId, DiskFileInfo*> key-value pai...
Definition: page.h:25