BadgerDB
 All Classes Namespaces Functions Variables Typedefs Friends
Public Member Functions
badgerdb::BufHashTbl Class Reference

Hash table class to keep track of pages in the buffer pool. More...

#include <bufHashTbl.h>

List of all members.

Public Member Functions

 BufHashTbl (const int htSize)
 ~BufHashTbl ()
void insert (const File *file, const PageId pageNo, const FrameId frameNo)
void lookup (const File *file, const PageId pageNo, FrameId &frameNo)
void remove (const File *file, const PageId pageNo)

Detailed Description

Hash table class to keep track of pages in the buffer pool.

Warning:
This class is not threadsafe.

Definition at line 45 of file bufHashTbl.h.


Constructor & Destructor Documentation

badgerdb::BufHashTbl::BufHashTbl ( const int  htSize)

Constructor of BufHashTbl class

Definition at line 26 of file bufHashTbl.cpp.

  : HTSIZE(htSize)
{
  // allocate an array of pointers to hashBuckets
  ht = new hashBucket* [htSize];
  for(int i=0; i < HTSIZE; i++)
    ht[i] = NULL;
}

Destructor of BufHashTbl class

Definition at line 35 of file bufHashTbl.cpp.

{
  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;
}

Member Function Documentation

void badgerdb::BufHashTbl::insert ( const File file,
const PageId  pageNo,
const FrameId  frameNo 
)

Insert entry into hash table mapping (file, pageNo) to frameNo.

Parameters:
fileFile object
pageNoPage number in the file
frameNoFrame number assigned to that page of the file
Exceptions:
HashAlreadyPresentExceptionif the corresponding page already exists in the hash table
HashTableException(optional) if could not create a new bucket as running of memory

Definition at line 48 of file bufHashTbl.cpp.

{
  int index = hash(file, pageNo);

  hashBucket* tmpBuc = ht[index];
  while (tmpBuc) {
    if (tmpBuc->file == file && tmpBuc->pageNo == pageNo)
      throw HashAlreadyPresentException(tmpBuc->file->filename(), tmpBuc->pageNo, tmpBuc->frameNo);
    tmpBuc = tmpBuc->next;
  }

  tmpBuc = new hashBucket;
  if (!tmpBuc)
    throw HashTableException();

  tmpBuc->file = (File*) file;
  tmpBuc->pageNo = pageNo;
  tmpBuc->frameNo = frameNo;
  tmpBuc->next = ht[index];
  ht[index] = tmpBuc;
}
void badgerdb::BufHashTbl::lookup ( const File file,
const PageId  pageNo,
FrameId frameNo 
)

Check if (file, pageNo) is currently in the buffer pool (ie. in the hash table).

Parameters:
fileFile object
pageNoPage number in the file
frameNoFrame number reference
Exceptions:
HashNotFoundExceptionif the page entry is not found in the hash table

Definition at line 70 of file bufHashTbl.cpp.

{
  int index = hash(file, pageNo);
  hashBucket* tmpBuc = ht[index];
  while (tmpBuc) {
    if (tmpBuc->file == file && tmpBuc->pageNo == pageNo)
    {
      frameNo = tmpBuc->frameNo; // return frameNo by reference
      return;
    }
    tmpBuc = tmpBuc->next;
  }

  throw HashNotFoundException(file->filename(), pageNo);
}
void badgerdb::BufHashTbl::remove ( const File file,
const PageId  pageNo 
)

Delete entry (file,pageNo) from hash table.

Parameters:
fileFile object
pageNoPage number in the file
Exceptions:
HashNotFoundExceptionif the page entry is not found in the hash table

Definition at line 86 of file bufHashTbl.cpp.

                                                             {

  int index = hash(file, pageNo);
  hashBucket* tmpBuc = ht[index];
  hashBucket* prevBuc = NULL;

  while (tmpBuc)
  {
    if (tmpBuc->file == file && tmpBuc->pageNo == pageNo)
    {
      if(prevBuc) 
        prevBuc->next = tmpBuc->next;
      else
        ht[index] = tmpBuc->next;

      delete tmpBuc;
      return;
    }
    else
    {
      prevBuc = tmpBuc;
      tmpBuc = tmpBuc->next;
    }
  }

  throw HashNotFoundException(file->filename(), pageNo);
}

The documentation for this class was generated from the following files:
 All Classes Namespaces Functions Variables Typedefs Friends