00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 #ifndef BUFFERPOOL_H_
00009 #define BUFFERPOOL_H_
00010 
00011 #include "Common.h"
00012 #include <list>
00013 #include <map>
00014 #include <string>
00015 
00016 class PageFileHandle;
00017 
00021 class PageBlock {
00022 private:
00023         friend class BufferPool;
00024 
00025         PageBlock();
00026         ~PageBlock();
00027 
00028         void clear();
00029         bool isConsistent();
00030         int pin();
00031         int unpin();
00032 
00033         bool isFree_;
00034         int pinCount_;
00035         bool isDirty_;
00036         PageFileHandle* fileHandle_;
00037         int pageNo_;
00038         std::list<PageBlock*>::iterator iteratorUnpinned_;
00039         std::list<PageBlock*>::iterator iteratorFree_;
00040         const ulong guardPre_;
00041         char data_[PF_PAGE_SIZE];
00042         const ulong guardPost_;
00043 
00044         static const ulong GUARD_VALUE;
00045 };
00046 
00047 
00048 
00049 
00065 class BufferPool {
00066 public:
00067 
00078         ReturnCode allocateBlock(char **buffer);
00079 
00090         ReturnCode disposeBlock(char *buffer);
00091 
00104         ReturnCode lookupBlock(PageFileHandle* fileHandle, int pageNo, char** data);
00105 
00118         ReturnCode setBlockDirty(PageFileHandle* fileHandle, int pageNo, bool isDirty);
00119 
00132         ReturnCode getBlockDirty(PageFileHandle* fileHandle, int pageNo, bool *isDirty);
00133 
00147         ReturnCode allocateBlock(PageFileHandle* fileHandle, int pageNo, char** data);
00148 
00161         ReturnCode disposeBlock(PageFileHandle* fileHandle, int pageNo);
00162 
00168         ReturnCode disposeAllBlocks(PageFileHandle* fileHandle);
00169 
00182         ReturnCode unpinBlock(PageFileHandle* fileHandle, int pageNo);
00183 
00197         ReturnCode getDirtyPageNumber(PageFileHandle* fileHandle, int* pageNo);
00198 
00211         ReturnCode getPinCount(PageFileHandle* fileHandle, int pageNo, int* pinCount);
00212 
00218         static BufferPool* getInstance();
00219 
00220 private:
00221 
00228         BufferPool();
00229 
00237         ~BufferPool();
00238 
00239         PageBlock* getPageBlock(char* data);
00240         PageBlock* getPageBlock(PageFileHandle* fileHandle, int pageNo);
00241         void disposePageBlock(PageBlock* pageBlock);
00242         void setPageBlock(PageBlock* pageBlock, PageFileHandle* fileHandle, int pageNo,
00243                         char** data);
00244         bool hasPinnedPage(PageFileHandle* fileHandle);
00245 
00246         PageBlock pageBlocks_[PF_BUFFER_SIZE];          
00247         std::list<PageBlock*> blocksUnpinned_;          
00248         std::list<PageBlock*> blocksFree_;                      
00249 };
00250 
00251 #endif