00001 00009 #pragma once 00010 00011 #include "include/file.h" 00012 #include "bufferHashTable.h" 00013 00014 namespace wiscdb { 00015 00019 class BufferManager; 00020 00025 class Frame { 00026 00027 friend class BufferManager; 00028 00029 private: 00033 File* file; 00034 00038 PageId pageNo; 00039 00043 FrameId frameNo; 00044 00048 int pinCnt; 00049 00053 bool dirty; 00054 00058 bool valid; 00059 00063 bool refbit; 00064 00068 void reset(); 00069 00078 void load(File* filePtr, PageId pageNum); 00079 00080 void print(); 00081 00085 Frame(); 00086 }; 00087 00088 00092 struct BufferStats 00093 { 00097 int accesses; 00098 00102 int diskreads; 00103 00107 int diskwrites; 00108 00112 void clear() 00113 { 00114 accesses = diskreads = diskwrites = 0; 00115 } 00116 00120 BufferStats() 00121 { 00122 clear(); 00123 } 00124 }; 00125 00126 00131 class BufferManager 00132 { 00133 private: 00138 FrameId clockHand; 00139 00143 std::uint32_t numFrames; 00144 00148 BufferHashTable *hashTable; 00149 00154 Frame *frameTable; 00155 00159 BufferStats bufferStats; 00160 00164 void advanceClock(); 00165 00175 void allocateFrame(FrameId & frame); 00176 00177 public: 00182 Page* bufPool; 00183 00187 BufferManager(std::uint32_t bufs); 00188 00192 ~BufferManager(); 00193 00206 void readPage(File* file, const PageId PageNo, Page*& page); 00207 00217 void unPinPage(File* file, const PageId PageNo, const bool dirty); 00218 00229 void allocatePage(File* file, PageId &PageNo, Page*& page); 00230 00243 void flushFile(const File* file); 00244 00253 void disposePage(File* file, const PageId PageNo); 00254 00258 void printSelf(); 00259 00263 BufferStats & getBufferStats() 00264 { 00265 return bufferStats; 00266 } 00267 00271 void clearBufferStats() 00272 { 00273 bufferStats.clear(); 00274 } 00275 }; 00276 00277 }