📄 bufhash.c
字号:
#include <memory.h>#include <unistd.h>#include <errno.h>#include <stdlib.h>#include <fcntl.h>#include <iostream>#include <stdio.h>#include "page.h"#include "buf.h"// buffer pool hash table implementationint BufHashTbl::hash(const File* file, const int pageNo){ int tmp, value; tmp = (int)file; // cast of pointer to the file object to an integer value = (tmp + pageNo) % HTSIZE; return value;}BufHashTbl::BufHashTbl(int htSize){ HTSIZE = htSize; // allocate an array of pointers to hashBuckets ht = new hashBucket* [htSize]; for(int i=0; i < HTSIZE; i++) ht[i] = NULL;}BufHashTbl::~BufHashTbl(){ for(int i = 0; i < HTSIZE; i++) { hashBucket* tmpBuf = ht[i]; while (ht[i]) { tmpBuf = ht[i]; ht[i] = ht[i]->next; delete tmpBuf; } } delete [] ht;}//---------------------------------------------------------------// insert entry into hash table mapping (file,pageNo) to frameNo;// returns OK if OK, HASHTBLERROR if an error occurred//---------------------------------------------------------------Status BufHashTbl::insert(const File *file, const int pageNo, const int frameNo){ int index ; hashBucket *mybk = NULL ; index = hash(file,pageNo) ; mybk = ht[index] ; while(mybk != NULL){ if((mybk->file == file) && (mybk->pageNo == pageNo)) return HASHTBLERROR ; mybk = mybk->next ; } mybk = new hashBucket ; if(mybk != NULL){ mybk->file =(File *)file ; mybk->pageNo = pageNo ; mybk->frameNo = frameNo ; mybk->next = ht[index] ; ht[index] = mybk ; return OK ; } return HASHTBLERROR ;}//------------------------------------------------------------------- // Check if (file,pageNo) is currently in the buffer pool (ie. in// the hash table). If so, return corresponding frameNo. else return // HASHNOTFOUND//-------------------------------------------------------------------Status BufHashTbl::lookup(const File* file, const int pageNo, int& frameNo) { int index ; hashBucket * mybk = NULL ; index = hash(file,pageNo) ; mybk = ht[index] ; while(mybk != NULL){// cout<<"look "<<mybk->file<<" "<<pageNo<<endl ; if((mybk->file == file) && (mybk->pageNo == pageNo)){ frameNo = mybk->frameNo ; return OK ; } mybk = mybk->next ; } return HASHNOTFOUND ;}//-------------------------------------------------------------------// delete entry (file,pageNo) from hash table. REturn OK if page was// found. Else return HASHTBLERROR//-------------------------------------------------------------------Status BufHashTbl::remove(const File* file, const int pageNo) { int index ; hashBucket * mybk = NULL ; hashBucket * mybk1 = NULL ; index = hash(file, pageNo) ; mybk1 = mybk = ht[index] ; while(mybk){ if ((mybk->file == file) && (mybk->pageNo == pageNo)){ if (ht[index] == mybk) ht[index] = mybk->next ; else mybk1->next = mybk->next ; mybk->file = NULL ; delete mybk ; return OK ; } else{ mybk1 = mybk ; mybk = mybk->next ; } } return HASHTBLERROR ;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -