SwatDB
include
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
75
PageId
page_id
;
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
128
BufferMap
(){ }
129
133
~BufferMap
(){ }
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
248
class
BufferManager
{
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
478
BufferMap
buf_map
;
479
484
Frame
frame_table[
BUF_SIZE
];
485
489
Page
buf_pool[
BUF_SIZE
];
490
495
DiskManager
*
disk_mgr
;
496
501
std::uint32_t
clock_hand
;
502
506
std::mutex
buf_map_mtx
;
507
526
FrameId
_allocateFrame();
527
536
void
_advanceClock();
538
};
BufferMap::~BufferMap
~BufferMap()
Destructor for BufferMap.
Definition:
bufmgr.h:133
Frame::valid
bool valid
Definition:
bufmgr.h:85
BufHash
Definition:
bufmgr.h:108
Frame::page_id
PageId page_id
Definition:
bufmgr.h:75
BufferState::ref_bit
std::uint32_t ref_bit
Definition:
bufmgr.h:234
BufferState::total
std::uint32_t total
Definition:
bufmgr.h:209
Frame::frame_mtx
std::mutex frame_mtx
Definition:
bufmgr.h:102
Frame::ref_bit
bool ref_bit
Definition:
bufmgr.h:97
BufferState::dirty
std::uint32_t dirty
Definition:
bufmgr.h:229
page.h
Frame::pin_count
int pin_count
Definition:
bufmgr.h:80
BufferManager::clock_hand
std::uint32_t clock_hand
Definition:
bufmgr.h:501
BufferMap::buf_map
std::unordered_map< PageId, FrameId, BufHash > buf_map
Definition:
bufmgr.h:197
BufferMap::BufferMap
BufferMap()
Constructor for BufferMap.
Definition:
bufmgr.h:128
Frame::Frame
Frame()
Constructor. Calls resetFrame to reset the Frame.
BufferManager::buf_map
BufferMap buf_map
Definition:
bufmgr.h:478
BufferManager
Definition:
bufmgr.h:248
BufferManager::buf_map_mtx
std::mutex buf_map_mtx
Definition:
bufmgr.h:506
PageId
Definition:
swatdb_types.h:50
Frame::dirty
bool dirty
Definition:
bufmgr.h:90
BufferManager::disk_mgr
DiskManager * disk_mgr
Definition:
bufmgr.h:495
swatdb_types.h
BufferState::unpinned
std::uint32_t unpinned
Definition:
bufmgr.h:224
BufferState::pinned
std::uint32_t pinned
Definition:
bufmgr.h:219
FrameId
std::uint32_t FrameId
Index of each frame in the bufferpool of BufferManager.
Definition:
swatdb_types.h:40
BufferState::clock_hand
std::uint32_t clock_hand
Definition:
bufmgr.h:239
Frame::BufferManager
friend class BufferManager
Definition:
bufmgr.h:35
Frame
Definition:
bufmgr.h:30
Frame::loadFrame
void loadFrame(PageId page_id)
Updates the Frame data according to the loaded Page.
BUF_SIZE
const std::uint32_t BUF_SIZE
Definition:
swatdb_types.h:163
Frame::~Frame
~Frame()
Destructor.
DiskManager
Definition:
diskmgr.h:36
BufferMap
Definition:
bufmgr.h:121
Frame::resetFrame
void resetFrame()
Resets the metadata of the Frame.
FileId
std::uint32_t FileId
Definition:
swatdb_types.h:29
BufferState
Definition:
bufmgr.h:204
BufferState::valid
std::uint32_t valid
Definition:
bufmgr.h:214
Page
Definition:
page.h:25
Generated by
1.8.13