BadgerDB
/afs/cs.wisc.edu/u/m/o/mohan/private/cs564/btree/btree/src/buffer.h
00001 
00008 #pragma once
00009 
00010 #include "file.h"
00011 #include "bufHashTbl.h"
00012 #include <iostream>
00013 
00014 namespace badgerdb {
00015 
00019 class BufMgr;
00020 
00024 class BufDesc {
00025 
00026   friend class BufMgr;
00027 
00028  private:
00032   File* file;
00033 
00037   PageId pageNo;
00038 
00042   FrameId frameNo;
00043 
00047   int pinCnt;
00048 
00052   bool dirty;
00053 
00057   bool valid;
00058 
00062   bool refbit;
00063 
00067   void Clear()
00068   {
00069     pinCnt = 0;
00070     file = NULL;
00071     pageNo = Page::INVALID_NUMBER;
00072     dirty = false;
00073     refbit = false;
00074     valid = false;
00075   };
00076 
00084   void Set(File* filePtr, PageId pageNum)
00085   { 
00086     file = filePtr;
00087     pageNo = pageNum;
00088     pinCnt = 1;
00089     dirty = false;
00090     valid = true;
00091     refbit = true;
00092   }
00093 
00094   void Print()
00095   {
00096     if(file != NULL)
00097     {
00098       std::cout << "file:" << file->filename() << " ";
00099       std::cout << "pageNo:" << pageNo << " ";
00100     }
00101     else
00102       std::cout << "file:NULL ";
00103 
00104     std::cout << "valid:" << valid << " ";
00105     std::cout << "pinCnt:" << pinCnt << " ";
00106     std::cout << "dirty:" << dirty << " ";
00107     std::cout << "refbit:" << refbit << "\n";
00108   }
00109 
00113   BufDesc()
00114   {
00115     Clear();
00116   }
00117 };
00118 
00119 
00123 struct BufStats
00124 {
00128   int accesses;
00129 
00133   int diskreads;
00134 
00138   int diskwrites;
00139 
00143   void clear()
00144   {
00145     accesses = diskreads = diskwrites = 0;
00146   }
00147       
00151   BufStats()
00152   {
00153     clear();
00154   }
00155 };
00156 
00157 
00161 class BufMgr 
00162 {
00163  private:
00167   FrameId clockHand;
00168 
00172   std::uint32_t numBufs;
00173   
00177   BufHashTbl *hashTable;
00178 
00182   BufDesc *bufDescTable;
00183 
00187   BufStats bufStats;
00188 
00195   void allocBuf(FrameId & frame);
00196 
00200   void advanceClock()
00201   {
00202     clockHand = (clockHand + 1) % numBufs;
00203   }
00204 
00205 
00206  public:
00210   Page* bufPool;
00211 
00215   BufMgr(std::uint32_t bufs);
00216   
00220   ~BufMgr();
00221 
00231   void readPage(File* file, const PageId PageNo, Page*& page);
00232 
00241   void unPinPage(File* file, const PageId PageNo, const bool dirty);
00242 
00251   void allocPage(File* file, PageId &PageNo, Page*& page); 
00252 
00262   void flushFile(const File* file);
00263 
00271   void disposePage(File* file, const PageId PageNo);
00272 
00276   void  printSelf();
00277 
00281   BufStats & getBufStats()
00282   {
00283     return bufStats;
00284   }
00285 
00289   void clearBufStats() 
00290   {
00291     bufStats.clear();
00292   }
00293 };
00294 
00295 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends