00001 /* 00002 * PF_FileHandle.h 00003 * 00004 * Created on: Mar 15, 2010 00005 * Author: green 00006 */ 00007 00008 #ifndef FILEHANDLE_H_ 00009 #define FILEHANDLE_H_ 00010 00011 #include "Common.h" 00012 #include "PageFileManager.h" 00013 #include "AllocationPage.h" 00014 #include <limits.h> 00015 #include <stdio.h> 00016 00017 static const char STAT_PAGE_READS[] = "STAT_PAGE_READS"; 00018 static const char STAT_PAGE_WRITES[] = "STAT_PAGE_WRITES"; 00019 00023 typedef struct { 00027 int pageNo; 00031 char* data; 00032 } PageHandle; 00033 00039 class FileHandle { 00040 public: 00041 FileHandle(); 00042 ~FileHandle(); 00043 00055 ReturnCode initialize(PageFileManager* mgr, const char* fileName, FILE* file); 00056 00068 ReturnCode getFirstPage(PageHandle *pageHandle); 00069 00077 ReturnCode getLastPage(PageHandle *pageHandle); 00078 00092 ReturnCode getNextPage(int pageNo, PageHandle *pageHandle); 00093 00102 ReturnCode getPrevPage(int pageNo, PageHandle *pageHandle); 00103 00118 ReturnCode getThisPage(int pageNo, PageHandle *pageHandle); 00119 00132 ReturnCode allocatePage(PageHandle *pageHandle); 00133 00143 ReturnCode disposePage(int pageNo); 00144 00154 ReturnCode markDirty(int pageNo); 00155 00166 ReturnCode unpinPage(int pageNo); 00167 00178 ReturnCode forcePage(int pageNo); 00179 00186 ReturnCode forceAllPages(); 00187 00191 const char* getFileName(); 00192 00199 FILE* getFile(); 00200 00201 static ReturnCode onCreate(PageFileManager* mgr, const char* fileName, FILE* file); 00202 00206 int countPages(); 00207 00208 /* 00209 * \brief Gets the back-pointer to the PageFileManager. 00210 * 00211 * Gets the back-pointer to the PageFileManager. This object should not 00212 * be deleted by the caller. 00213 */ 00214 PageFileManager* getPageFileManager(); 00215 00216 private: 00217 uint getOffset(int pageNo); 00218 ReturnCode readPage(uint offset, char* buffer); 00219 ReturnCode writePage(uint offset, const char* buffer); 00220 ReturnCode pinPage(int pageNo); 00221 ReturnCode getBuffer(char* buffer); 00222 ReturnCode mgrForcePage(int pageNo); 00223 ReturnCode forceDirtyPage(int pageNo); 00224 ReturnCode forceAllocationPage(); 00225 ReturnCode updateFileLength(); 00226 00227 static const uint HEADER_SIZE; 00228 00229 AllocationPage alloc_; 00230 char fileName_[MAX_FILE_NAME]; 00231 FILE* file_; 00232 PageFileManager* mgr_; 00233 }; 00234 00235 #endif /* FILEHANDLE_H_ */