BadgerDB
|
00001 00008 #pragma once 00009 00010 #include "file.h" 00011 #include "bufHashTbl.h" 00012 00013 namespace badgerdb { 00014 00018 class BufMgr; 00019 00023 class BufDesc { 00024 00025 friend class BufMgr; 00026 00027 private: 00031 File* file; 00032 00036 PageId pageNo; 00037 00041 FrameId frameNo; 00042 00046 int pinCnt; 00047 00051 bool dirty; 00052 00056 bool valid; 00057 00061 bool refbit; 00062 00066 void Clear() 00067 { 00068 pinCnt = 0; 00069 file = NULL; 00070 pageNo = Page::INVALID_NUMBER; 00071 dirty = false; 00072 refbit = false; 00073 valid = false; 00074 }; 00075 00083 void Set(File* filePtr, PageId pageNum) 00084 { 00085 file = filePtr; 00086 pageNo = pageNum; 00087 pinCnt = 1; 00088 dirty = false; 00089 valid = true; 00090 refbit = true; 00091 } 00092 00093 void Print() 00094 { 00095 if(file) 00096 { 00097 std::cout << "file:" << file->filename() << " "; 00098 std::cout << "pageNo:" << pageNo << " "; 00099 } 00100 else 00101 std::cout << "file:NULL "; 00102 00103 std::cout << "valid:" << valid << " "; 00104 std::cout << "pinCnt:" << pinCnt << " "; 00105 std::cout << "dirty:" << dirty << " "; 00106 std::cout << "refbit:" << refbit << "\n"; 00107 } 00108 00112 BufDesc() 00113 { 00114 Clear(); 00115 } 00116 }; 00117 00118 00122 struct BufStats 00123 { 00127 int accesses; 00128 00132 int diskreads; 00133 00137 int diskwrites; 00138 00142 void clear() 00143 { 00144 accesses = diskreads = diskwrites = 0; 00145 } 00146 00150 BufStats() 00151 { 00152 clear(); 00153 } 00154 }; 00155 00156 00160 class BufMgr 00161 { 00162 private: 00166 FrameId clockHand; 00167 00171 std::uint32_t numBufs; 00172 00176 BufHashTbl *hashTable; 00177 00181 BufDesc *bufDescTable; 00182 00186 BufStats bufStats; 00187 00191 void advanceClock(); 00192 00199 void allocBuf(FrameId & frame); 00200 00201 public: 00205 Page* bufPool; 00206 00210 BufMgr(std::uint32_t bufs); 00211 00215 ~BufMgr(); 00216 00226 void readPage(File* file, const PageId PageNo, Page*& page); 00227 00236 void unPinPage(File* file, const PageId PageNo, const bool dirty); 00237 00246 void allocPage(File* file, PageId &PageNo, Page*& page); 00247 00257 void flushFile(const File* file); 00258 00266 void disposePage(File* file, const PageId PageNo); 00267 00271 void printSelf(); 00272 00276 BufStats & getBufStats() 00277 { 00278 return bufStats; 00279 } 00280 00284 void clearBufStats() 00285 { 00286 bufStats.clear(); 00287 } 00288 }; 00289 00290 }