-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuf.h
120 lines (93 loc) · 2.81 KB
/
buf.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#ifndef BUF_H
#define BUF_H
#include "db.h"
#include "bufMap.h"
// define if debug output wanted
//#define DEBUGBUF
class BufMgr; //forward declaration of BufMgr class
// class for maintaining information about buffer pool frame
class BufDesc {
friend class BufMgr;
private:
File* file; // pointer to file object
int pageNo; // page within file
int pinCnt; // number of times this page has been pinned
bool dirty; // true if dirty; false otherwise
bool valid; // true if page is valid
bool refbit; // has this buffer frame been referenced recently?
void Clear() { // initialize buffer frame for a new user
pinCnt = 0;
file = NULL;
pageNo = -1;
dirty = refbit = false;
valid = false;
};
void Set(File* filePtr, int pageNum) {
file = filePtr;
pageNo = pageNum;
pinCnt = 1;
dirty = false;
refbit = true;
valid = true;
}
BufDesc() {
Clear();
}
};
struct BufStats
{
unsigned accesses; // Total number of accesses to buffer pool
unsigned diskreads; // Number of pages read from disk
unsigned diskwrites; // Number of pages written back to disk
void clear()
{
accesses = diskreads = diskwrites = 0;
}
BufStats()
{
clear();
}
};
inline ostream & operator << (ostream & os, const BufStats & stats)
{
os << "accesses = " << stats.accesses
<< ", disk reads = " << stats.diskreads
<< ", disk writes = " << stats.diskwrites << endl;
return os;
}
class BufMgr
{
private:
unsigned int clockHand; // clock hand for clock algorithm
BufMap bufMap; // mapping of (File, page) to frame
BufDesc *bufTable; // vector of status info, 1 per page
unsigned int numBufs; // Number of pages in buffer pool
BufStats bufStats; // Statistics about buffer pool usage
const Status allocBuf(unsigned int & frame); // allocate a free frame.
void advanceClock()
{
clockHand = (clockHand + 1) % numBufs;
}
public:
Page *bufPool; // actual buffer pool
BufMgr(const unsigned int bufs);
~BufMgr();
const Status readPage(File* file, const int PageNo, Page*& page);
const Status unPinPage(File* file, const int PageNo, const bool dirty);
const Status allocPage(File* file, int& PageNo, Page*& page);
// allocates a new, empty page
const Status flushFile(File* file); // writing out all dirty pages of the file
const Status disposePage(File* file,
const int PageNo); // dispose of page in file
unsigned numUnpinnedPages(); // Return the number of pages that are currently unpinned
void printSelf(void); // Print the buffer pool contents
const BufStats & getBufStats() const // Get buffer pool usage
{
return bufStats;
}
void clearBufStats()
{
bufStats.clear();
}
};
#endif