SwatDB
Loading...
Searching...
No Matches
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
11#ifndef _SWATDB_DISKMGR_H_
12#define _SWATDB_DISKMGR_H_
13
18#include<iostream>
19#include<string>
20#include <fstream>
21#include <vector>
22#include <unordered_set>
23#include <unordered_map>
24#include <mutex>
25
26#include "swatdb_types.h"
27
28class Catalog;
29class Page;
30
39
40 public:
41
57
69
87 void createFile(FileId file_id);
88
102 void removeFile(FileId file_id);
103
129
145 void deallocatePage(PageId page_id);
146
165 void readPage(PageId page_id, Page* page);
166
186 void writePage(PageId page_id, Page* page);
187
199 bool isValidPage(PageId page_id);
200
214 void printFile(FileId file_id);
215
227 std::uint32_t getCapacity(FileId file_id);
228
240 std::uint32_t getSize(FileId file_id);
241
242 private:
243
249 // this needs a comment describing this expression!
250 static const std::uint32_t DISK_HEADER_SIZE =
251 ((((MAX_PAGE_NUM/8)+1+(sizeof(std::uint32_t)*2))/PAGE_SIZE)+1)*PAGE_SIZE;
252
258 static const std::uint32_t DISK_HEADER_BITMAP_SIZE =
259 DISK_HEADER_SIZE - (sizeof(std::uint32_t)*2);
260
269 std::uint32_t size;
273 std::uint32_t capacity;
279 };
280
288
292 std::fstream file;
293
297 std::uint32_t capacity;
298
302 std::unordered_set<PageNum> unused_pages;
303
307 std::mutex file_mtx;
308 };
309
314 std::unordered_map <FileId, DiskFileInfo*> file_map;
315
322
326 std::mutex file_map_mtx;
327
332
345 disk_file_info);
346
361
376 DiskFileInfo* disk_file_info);
377
391 DiskFileInfo* disk_file_info);
392
393};
394
395#endif
Definition catalog.h:161
Definition diskmgr.h:38
void _serializeFileHeader(SerializedFileHeader *file_header, DiskFileInfo *disk_file_info)
Serializes Unix file metadata by initializing SerializedFileHeader object given DiskFileInfo object.
void readPage(PageId page_id, Page *page)
Reads the page data from the Unix file into the Page object pointer.
std::uint32_t getSize(FileId file_id)
Get method for size of a file.
void deallocatePage(PageId page_id)
Deallocates page by adding its offest to unused_pages in the appropriate DiskFileInfo struct.
void removeFile(FileId file_id)
Deletes the Unix file corresponding to file_id, and removes the <FileId, DiskFileInfo*> key-value pai...
static const std::uint32_t DISK_HEADER_SIZE
Definition diskmgr.h:250
PageId allocatePage(FileId file_id)
Allocates a Page to the file which corresponds to file_id.
void printFile(FileId file_id)
THIS METHOD IS FOR DEBUGGING ONLY. Prints contents of a file inlcuding FileId, size,...
void _getDiskFileInfo(FileId file_id, DiskFileInfo **disk_file_info)
Initializes DiskFileInfo** of the given FileId.
bool isValidPage(PageId page_id)
Checks if the page of a given pageId is valid.
void createFile(FileId file_id)
Creates a new Unix file, opens the fstream, initializes DiskFileInfo object, and adds <FileId,...
DiskManager(Catalog *catalog)
DiskManager constructor. Uses Catalog object pointer and a vector of FileIds to initialize file_map.
std::unordered_map< FileId, DiskFileInfo * > file_map
Definition diskmgr.h:314
std::mutex file_map_mtx
Definition diskmgr.h:326
char temp_page[PAGE_SIZE]
Definition diskmgr.h:331
~DiskManager()
DiskManager destructor. All pages have been written to disk prior to this call.
void _flushDiskFileInfo(FileId file_id)
Writes data from DiskFileInfo struct to Unix file for the given FileId.
void _deserializeFileHeader(SerializedFileHeader *file_header, DiskFileInfo *disk_file_info)
Intializes DiskFileInfo object given SerializedFileHeader object.
Catalog * catalog
Definition diskmgr.h:321
static const std::uint32_t DISK_HEADER_BITMAP_SIZE
Definition diskmgr.h:258
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.
std::uint32_t getCapacity(FileId file_id)
Get method for the capacity of a file.
Definition page.h:27
Definition diskmgr.h:287
std::mutex file_mtx
Definition diskmgr.h:307
std::fstream file
Definition diskmgr.h:292
std::unordered_set< PageNum > unused_pages
Definition diskmgr.h:302
std::uint32_t capacity
Definition diskmgr.h:297
Definition diskmgr.h:265
std::uint32_t capacity
Definition diskmgr.h:273
char bitmap[DISK_HEADER_BITMAP_SIZE]
Definition diskmgr.h:278
std::uint32_t size
Definition diskmgr.h:269
Definition swatdb_types.h:54
std::uint32_t FileId
Definition swatdb_types.h:33
const std::uint32_t PAGE_SIZE
Definition swatdb_types.h:258
const std::uint32_t MAX_PAGE_NUM
Definition swatdb_types.h:281