SwatDB
bufmgr.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 
17 #include <utility>
18 #include <unordered_map>
19 #include <mutex>
20 
21 #include "swatdb_types.h"
22 #include "page.h" // need for alignment of Page object
23 
24 class BufferManager;
25 class DiskManager;
26 
30 class Frame {
31 
35  friend class BufferManager;
36 
37  public:
38 
42  Frame();
43 
47  ~Frame();
48 
56  void resetFrame();
57 
67  void loadFrame(PageId page_id);
68 
69  private:
70 
76 
80  int pin_count;
81 
85  bool valid;
86 
90  bool dirty;
91 
97  bool ref_bit;
98 
102  std::mutex frame_mtx;
103 };
104 
108 struct BufHash{
109  std::size_t operator()(const PageId& page_id) const{
110  return (std::hash<unsigned int>() (page_id.file_id)) ^
111  (std::hash<unsigned int>() (page_id.page_num));
112  }
113 };
114 
121 class BufferMap {
122 
123  public:
124 
129 
134 
147  FrameId get(PageId page_id);
148 
160  bool contains(PageId page_id);
161 
177  void insert(PageId page_id, FrameId frame_id);
178 
190  void remove(PageId page_id);
191 
192  private:
193 
197  std::unordered_map<PageId,FrameId,BufHash> buf_map;
198 };
199 
204 struct BufferState{
205 
209  std::uint32_t total;
210 
214  std::uint32_t valid;
215 
219  std::uint32_t pinned;
220 
224  std::uint32_t unpinned;
225 
229  std::uint32_t dirty;
230 
234  std::uint32_t ref_bit;
235 
239  std::uint32_t clock_hand;
240 };
241 
249 
250  public:
251 
264  BufferManager(DiskManager* disk_mgr);
265 
272  ~BufferManager();
273 
296  std::pair<Page*,PageId> allocatePage(FileId file_id);
297 
315  void deallocatePage(PageId page_id);
316 
339  Page* getPage(PageId page_id);
340 
355  void releasePage(PageId page_id, bool dirty);
356 
367  void setDirty(PageId page_id);
368 
382  void flushPage(PageId page_id);
383 
395  void createFile(FileId file_id);
396 
414  void removeFile(FileId file_id);
415 
422  void clearBuffer();
423 
429  BufferState getBufferState();
430 
437  void printAllFrames();
438 
444  void printValidFrames();
445 
452  void printFrame(FrameId frame_id);
453 
460  void printPage(PageId page_id);
461 
469  void printBufferState();
470 
471  private:
472 
479 
484  Frame frame_table[BUF_SIZE];
485 
489  Page buf_pool[BUF_SIZE];
490 
496 
501  std::uint32_t clock_hand;
502 
506  std::mutex buf_map_mtx;
507 
526  FrameId _allocateFrame();
527 
536  void _advanceClock();
538 };
~BufferMap()
Destructor for BufferMap.
Definition: bufmgr.h:133
bool valid
Definition: bufmgr.h:85
Definition: bufmgr.h:108
PageId page_id
Definition: bufmgr.h:75
std::uint32_t ref_bit
Definition: bufmgr.h:234
std::uint32_t total
Definition: bufmgr.h:209
std::mutex frame_mtx
Definition: bufmgr.h:102
bool ref_bit
Definition: bufmgr.h:97
std::uint32_t dirty
Definition: bufmgr.h:229
int pin_count
Definition: bufmgr.h:80
std::uint32_t clock_hand
Definition: bufmgr.h:501
std::unordered_map< PageId, FrameId, BufHash > buf_map
Definition: bufmgr.h:197
BufferMap()
Constructor for BufferMap.
Definition: bufmgr.h:128
Frame()
Constructor. Calls resetFrame to reset the Frame.
BufferMap buf_map
Definition: bufmgr.h:478
Definition: bufmgr.h:248
std::mutex buf_map_mtx
Definition: bufmgr.h:506
Definition: swatdb_types.h:50
bool dirty
Definition: bufmgr.h:90
DiskManager * disk_mgr
Definition: bufmgr.h:495
std::uint32_t unpinned
Definition: bufmgr.h:224
std::uint32_t pinned
Definition: bufmgr.h:219
std::uint32_t FrameId
Index of each frame in the bufferpool of BufferManager.
Definition: swatdb_types.h:40
std::uint32_t clock_hand
Definition: bufmgr.h:239
friend class BufferManager
Definition: bufmgr.h:35
Definition: bufmgr.h:30
void loadFrame(PageId page_id)
Updates the Frame data according to the loaded Page.
const std::uint32_t BUF_SIZE
Definition: swatdb_types.h:163
~Frame()
Destructor.
Definition: diskmgr.h:36
Definition: bufmgr.h:121
void resetFrame()
Resets the metadata of the Frame.
std::uint32_t FileId
Definition: swatdb_types.h:29
Definition: bufmgr.h:204
std::uint32_t valid
Definition: bufmgr.h:214
Definition: page.h:25