-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuf.h
More file actions
142 lines (114 loc) · 3.65 KB
/
Copy pathbuf.h
File metadata and controls
142 lines (114 loc) · 3.65 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifndef BUF_H
#define BUF_H
#include "db.h"
// define if debug output wanted
//#define DEBUGBUF
// declarations for buffer pool hash table
struct hashBucket
{
File* file; // pointer a file object (more on this below)
int pageNo; // page number within a file
int frameNo; // frame number of page in the buffer pool
hashBucket* next; // next node in the hash table
};
// hash table to keep track of pages in the buffer pool
class BufHashTbl
{
private:
int HTSIZE;
hashBucket** ht; // actual hash table
int hash(const File* file, const int pageNo); // returns value between 0 and HTSIZE-1
public:
BufHashTbl(const int htSize); // constructor
~BufHashTbl(); // destructor
// insert entry into hash table mapping (file,pageNo) to frameNo;
// returns 0 if OK, HASHTBLERROR if an error occurred
Status insert(const File* file, const int pageNo, const int frameNo);
// Check if (file,pageNo) is currently in the buffer pool (ie. in
// the hash table). If so, return corresponding frameNo. else return
// HASHNOTFOUND
Status lookup(const File* file, const int pageNo, int & frameNo);
// delete entry (file,pageNo) from hash table. REturn OK if page was
// found. Else return HASHTBLERROR
Status remove(const File* file, const int pageNo);
};
class BufMgr; //forward declaration of BufMgr class
// class for maintaining information about buffer pool frames
class BufDesc {
friend class BufMgr;
private:
File* file; // pointer to file object
int pageNo; // page within file
int frameNo; // frame # of frame
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 reference recently
void Clear() { // initialize buffer frame for a new user
pinCnt = 0;
file = NULL;
pageNo = -1;
dirty = false;
valid = false;
};
void Set(File* filePtr, int pageNum) {
file = filePtr;
pageNo = pageNum;
pinCnt = 1;
dirty = false;
valid = true;
refbit = true;
}
BufDesc() {
Clear();
}
};
struct BufStats
{
int accesses; // Total number of accesses to buffer pool
int diskreads; // Number of pages read from disk (including allocs)
int diskwrites; // Number of pages written back to disk
void clear()
{
accesses = diskreads = diskwrites = 0;
}
BufStats()
{
clear();
}
};
class BufMgr
{
private:
unsigned int clockHand;
int numBufs; // Number of pages in buffer pool
BufHashTbl* hashTable; // hash table mapping (File, page) to frame
BufDesc* bufTable; // vector of status info, 1 per page
BufStats bufStats; // buffer pool statistics
const Status allocBuf(int & frame); // allocate a free frame.
const void releaseBuf(int frame); // return unused frame to end of list
void advanceClock()
{
clockHand = (clockHand + 1) % numBufs;
}
public:
Page* bufPool; // actual buffer pool
BufMgr(const 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(const File* file); // writing out all dirty pages of the file
const Status disposePage(File* file, const int PageNo); // dispose of page in file
void printSelf();
const BufStats & getBufStats() const // get buffer pool usage
{
return bufStats;
}
const void clearBufStats()
{
bufStats.clear();
}
};
#endif