BadgerDB
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends
Public Member Functions | Protected Attributes | Friends
badgerdb::AttrCatalog Class Reference

The class implementing the Attribute Catalogs. Inherited from FileScan class. More...

#include <catalog.h>

Inheritance diagram for badgerdb::AttrCatalog:
badgerdb::FileScan

List of all members.

Public Member Functions

 AttrCatalog (BufMgr *bufferMgr)
void getInfo (const std::string &relation, const std::string &attrName, AttrDesc &attrdesc)
void addInfo (AttrDesc &attrdesc)
void removeInfo (const std::string &relation, const std::string &attrName)
void getRelInfo (const std::string &relation, int &attrCnt, AttrDesc *&attrs)
void dropRelation (const std::string &relation)
void addIndex (const std::string &relation, const std::string &attrName)
void dropIndex (const std::string &relation, const std::string &attrName)
 ~AttrCatalog ()
void insertRecord (const std::string &rec, RecordId &outRid)
void deleteRecord (RecordId rid)
void startScan (int offset, int length, Datatype type, std::string filter, Operator op)
void endScan ()
void scanNext (RecordId &outRid)
std::string getRecord ()
std::string getRandomRecord (RecordId rid)
void gotoMark (RecordId rid)
bool matchRec (const std::string &rec)
void flushFile ()
void markDirty ()

Protected Attributes

BufMgrbufMgr

Friends

class RelCatalog

Detailed Description

The class implementing the Attribute Catalogs. Inherited from FileScan class.

Definition at line 190 of file catalog.h.


Constructor & Destructor Documentation

Constructor

Definition at line 19 of file attrcat.cpp.

:FileScan(ATTRCATNAME, bufferMgr)
{
}

Destructor

Definition at line 182 of file attrcat.cpp.

{
}

Member Function Documentation

void badgerdb::AttrCatalog::addIndex ( const std::string &  relation,
const std::string &  attrName 
)

Add index to a relation.

Definition at line 199 of file attrcat.cpp.

{
  AttrDesc ad;

  // get attribute information
  getInfo(relation, attrName, ad);

  if(ad.indexed)
    throw IndexAlreadyExistsException();

  // create index file
  Index index(relation, ad.attrOffset, ad.attrLen, (Datatype)ad.attrType, 1, bufMgr);
  //index.printBucs();

  // modify entry in attrcat
  ad.indexed = true;

  removeInfo(relation, attrName);
  addInfo(ad);
}

Add tuple to catalog

Definition at line 53 of file attrcat.cpp.

{
  RecordId outRid;
  std::string new_data(reinterpret_cast<char*>(&attrdesc), sizeof(AttrDesc));
  insertRecord(new_data, outRid);
}
void badgerdb::FileScan::deleteRecord ( RecordId  rid) [inherited]

Delete a record. No need to mark the page dirty by calling markDirty().

Definition at line 79 of file filescan.cpp.

{
  Page *dPage;
  bufMgr->readPage(file, rid.page_number, dPage);
  dPage->deleteRecord(rid);
  bufMgr->unPinPage(file, rid.page_number, true);
}
void badgerdb::AttrCatalog::dropIndex ( const std::string &  relation,
const std::string &  attrName 
)

Drop index from a relation.

Definition at line 222 of file attrcat.cpp.

{
  AttrDesc ad;

  // get attribute information
  getInfo(relation, attrName, ad);

  if(ad.indexed == false)
    throw IndexNotFoundException();

  // destroy file
  std::ostringstream outputString;
  outputString << relation << '.' << ad.attrOffset << std::endl;
  std::string tmpString = outputString.str();

  File::remove(tmpString);

  // modify entry in attrcat
  ad.indexed = false;

  removeInfo(relation, attrName);
  addInfo(ad);
}
void badgerdb::AttrCatalog::dropRelation ( const std::string &  relation)

Delete all information about a relation.

Definition at line 151 of file attrcat.cpp.

{
  AttrDesc *attrs;
  int attrCnt, i;

  // get attribute information

  getRelInfo(relation, attrCnt, attrs);

  // first drop any index files
  // (this must occur before removeInfo() is called because
  // dropIndex computes offsets (needed in index filenames)
  // based on attrcat entries!)

  for(i = 0; i < attrCnt; i++)
  {
    if(attrs[i].indexed)
    {
      dropIndex(relation, attrs[i].attrName);
    }
  }

  // remove entries from catalog
  for(i = 0; i < attrCnt; i++)
  {
    removeInfo(relation, attrs[i].attrName);
  }

  delete [] attrs;
}
void badgerdb::FileScan::endScan ( ) [inherited]

Reset scan parameters.

Definition at line 126 of file filescan.cpp.

{
  if (curPage != NULL)
  {
    bufMgr->unPinPage(file, (*filePageIter).page_number(), curDirtyFlag);
  }

  curPage = NULL;
  curDirtyFlag = false;
  filePageIter = file->begin();
  filter.clear();
}
void badgerdb::FileScan::flushFile ( ) [inherited]

Flush file from buffer manager and reset all parameters.

Definition at line 87 of file filescan.cpp.

{
  // generally must unpin last page of the scan
  if (curPage != NULL)
  {
    bufMgr->unPinPage(file, (*filePageIter).page_number(), curDirtyFlag);
    curPage = NULL;
    curDirtyFlag = false;
    filePageIter = file->begin();
  }

  bufMgr->flushFile(file);
}
void badgerdb::AttrCatalog::getInfo ( const std::string &  relation,
const std::string &  attrName,
AttrDesc attrdesc 
)

Get attribute tuple from catalog.

Definition at line 24 of file attrcat.cpp.

{
  RecordId rid;

  startScan(0, relation.size() + 1, STRING, relation, EQ);

  while(1)
  {
    try
    {
      scanNext(rid);
      std::string recordStr = getRecord();

      assert(sizeof(AttrDesc) == recordStr.size());

      memcpy(&attrdesc, recordStr.c_str(), sizeof(AttrDesc));
      if(std::string(attrdesc.attrName) == attrName)
        break;
    }
    catch(EndOfFileException e)
    {
      throw AttributeNotFoundException();
    }
  }

  endScan();
}
std::string badgerdb::FileScan::getRandomRecord ( RecordId  rid) [inherited]

Read a record with RecordId rid from file.

Definition at line 226 of file filescan.cpp.

{
  // Solution Starts
  Page* page;

  bufMgr->readPage(file, rid.page_number, page);
  std::string retStr = page->getRecord(rid);
  bufMgr->unPinPage(file, rid.page_number, false);

  return retStr;
}
std::string badgerdb::FileScan::getRecord ( ) [inherited]

Read current record, whose RecordId was returned by last call to scanNext().

Definition at line 221 of file filescan.cpp.

{
  return *pageRecordIter;
}
void badgerdb::AttrCatalog::getRelInfo ( const std::string &  relation,
int &  attrCnt,
AttrDesc *&  attrs 
)

Get all attributes of a relation.

Definition at line 92 of file attrcat.cpp.

{
  RecordId rid;

  startScan(0, relation.size() + 1, STRING, relation, EQ);

  attrCnt = 0;
  int curSize = 8;
  attrs = new AttrDesc [curSize];

  memset (attrs, 0, curSize*sizeof(AttrDesc)); // keep purify happy.

  while(1)
  {
    try
    {
      scanNext(rid);
      std::string recordStr = getRecord();

      assert(sizeof(AttrDesc) == recordStr.size());

      ++attrCnt;

      // Check if the attrs array can hold this entry.
      if (attrCnt > curSize)
      {
        // reallocate the attrs array and start over.
        AttrDesc* newArray = new AttrDesc [curSize*2]; // double the size of the array.
        memset (newArray, 0, curSize*2*sizeof(AttrDesc)); // keep purify happy.
        memcpy (newArray, attrs, curSize*sizeof(AttrDesc)); // copy the old contents
        delete [] attrs;  // free up the previously allocated memory.
        attrs = newArray; // point to the new (larger) array.
        curSize *= 2;
      }

      memcpy(&attrs[attrCnt - 1], recordStr.c_str(), sizeof(AttrDesc));
    }
    catch(EndOfFileException e)
    {
      break;
    }
  }

  endScan();

  qsort(attrs, attrCnt, sizeof(AttrDesc), badgerdb::attrCmp);
}
void badgerdb::FileScan::gotoMark ( RecordId  rid) [inherited]

Point to particular record in the file so that next calls to scanNext() scan file beyond this particular record.

Definition at line 35 of file filescan.cpp.

{
  if(curPage != NULL)
  {
    bufMgr->unPinPage(file, (*filePageIter).page_number(), curDirtyFlag);
  }

  curDirtyFlag = false;
  filePageIter = file->beginAt(rid.page_number);
  bufMgr->readPage(file, rid.page_number, curPage); 
  pageRecordIter = curPage->beginAt(rid.slot_number);
}
void badgerdb::FileScan::insertRecord ( const std::string &  rec,
RecordId outRid 
) [inherited]

Insert a new record in file, RecordId of inserted record returned in outRid. No need to mark the page dirty by calling markDirty().

Definition at line 48 of file filescan.cpp.

{
  Page *iPage;
  PageId iPageNo;

  for (FileIterator iter = file->begin(); iter != file->end(); ++iter)
  {
    iPageNo = iter.getCurrentPageNo();

    if(iPageNo == Page::INVALID_NUMBER)
      break;

    bufMgr->readPage(file, iPageNo, iPage);

    try
    {
      outRid = iPage->insertRecord(rec);
      bufMgr->unPinPage(file, iPageNo, true);
      return;
    }
    catch(InsufficientSpaceException &e)
    {
      bufMgr->unPinPage(file, iPageNo, false);
    }
  }

  bufMgr->allocPage(file, iPageNo, iPage);
  outRid = iPage->insertRecord(rec);
  bufMgr->unPinPage(file, iPageNo, true);
}
void badgerdb::FileScan::markDirty ( ) [inherited]

Mark current page being scanned in FileScan dirty.

Definition at line 287 of file filescan.cpp.

{
  curDirtyFlag = true;
}
bool badgerdb::FileScan::matchRec ( const std::string &  rec) [inherited]

Check if a record satisfies the scan parameters.

Definition at line 238 of file filescan.cpp.

{
  // no filtering requested
  if (filter.empty()) return true;

  // see if offset + length is beyond end of record
  // maybe this should be an error???
  if ((offset + length -1 ) >= rec.size())
    return false;

  double diff = 0;                       // < 0 if attr < fltr
  switch(type) {

    case INTEGER:
      int iattr, ifltr;                 // word-alignment problem possible
      memcpy(&iattr, rec.c_str() + offset,  length);
      memcpy(&ifltr, filter.c_str(),  length);
      //ifltr = atoi(filter.c_str());
      diff = iattr - ifltr;
      break;

    case DOUBLE:
      double fattr, ffltr;               // word-alignment problem possible
      memcpy(&fattr, rec.c_str() + offset,  length);
      memcpy(&ffltr, filter.c_str(),  length);
      //ffltr = atof(filter.c_str());
      diff = fattr - ffltr;
      break;

    case STRING:
      diff = strncmp(rec.c_str() + offset,  filter.c_str(), length);
      break;
  }

  switch(op) {
    case LT:  if (diff < 0.0) return true; break;
    case LTE: if (diff <= 0.0) return true; break;
    case EQ:  if (diff == 0.0) return true; break;
    case GTE: if (diff >= 0.0) return true; break;
    case GT:  if (diff > 0.0) return true; break;
    case NE:  if (diff != 0.0) return true; break;
    default: break;
  }

  return false;

}
void badgerdb::AttrCatalog::removeInfo ( const std::string &  relation,
const std::string &  attrName 
)

Remove tuple from catalog.

Definition at line 61 of file attrcat.cpp.

{
  RecordId rid;
  AttrDesc attrdesc;

  startScan(0, relation.size() + 1, STRING, relation, EQ);

  while(1)
  {
    try
    {
      scanNext(rid);
      std::string recordStr = getRecord();

      assert(sizeof(AttrDesc) == recordStr.size());

      memcpy(&attrdesc, recordStr.c_str(), sizeof(AttrDesc));
      if (std::string(attrdesc.attrName) == attrName)
        break;
    }
    catch(EndOfFileException e)
    {
      throw AttributeNotFoundException();
    }
  }

  endScan();

  deleteRecord(rid);
}
void badgerdb::FileScan::scanNext ( RecordId outRid) [inherited]

Returns RecordId of next record that satisfies the scan parameters, if any.

Definition at line 139 of file filescan.cpp.

{
  std::string rec;

  if(filePageIter == file->end())
  {
    throw EndOfFileException();
  }

  // special case of the first record of the first page of the file
  if (curPage == NULL)
  {
    // need to get the first page of the file
    filePageIter = file->begin();
    if(filePageIter == file->end())
    {
      throw EndOfFileException();
    }
   
    // read the first page of the file
    bufMgr->readPage(file, (*filePageIter).page_number(), curPage); 
    curDirtyFlag = false;

    // get the first record off the page
    pageRecordIter = curPage->begin(); 

    if(pageRecordIter != curPage->end()) 
    {
      // get pointer to record
      rec = *pageRecordIter;
      if(matchRec(rec))
      {
        outRid = pageRecordIter.getCurrentRecord();
        return;
      }
    }
  }

  // Loop, looking for a record that satisfied the predicate.
  // First try and get the next record off the current page
  pageRecordIter++;

  while(1)
  {
    while (pageRecordIter == curPage->end())
    {
      // unpin the current page
      bufMgr->unPinPage(file, (*filePageIter).page_number(), curDirtyFlag);
      curPage = NULL;
      curDirtyFlag = false;

      filePageIter++;
      if (filePageIter == file->end())
      {
        curPage = NULL;
        throw EndOfFileException();
      }

      // read the next page of the file
      bufMgr->readPage(file, (*filePageIter).page_number(), curPage);

      // get the first record off the page
      pageRecordIter = curPage->begin(); 
    }

    while (pageRecordIter != curPage->end())
    {
      rec = *pageRecordIter;

      if(matchRec(rec))
      {
        // return rid of the record
        outRid = pageRecordIter.getCurrentRecord();
        return;
      }
      pageRecordIter++;
    }
  }
}
void badgerdb::FileScan::startScan ( int  offset,
int  length,
Datatype  type,
std::string  filter,
Operator  op 
) [inherited]

To set up scan parameters used by scanNext. If just want to scan all the records in the file, no need to call this, you may directly call scanNext().

Definition at line 108 of file filescan.cpp.

{
  curPage = NULL;
  curDirtyFlag = false;
  filePageIter = file->begin();

  if (filterIn.empty()) {                        // no filtering requested
    filter.clear();
    return;
  }

  offset = offsetIn;
  length = lengthIn;
  type = typeIn;
  filter = filterIn;
  op = opIn;
}

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