taco-db  0.1.0
Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
taco::Table Class Reference

Table implements a heap file over the virtual file provided by the FileManager. More...

#include <storage/Table.h>

Classes

class  Iterator
 The Iterator interface for scanning the heap file. More...
 

Public Member Functions

 ~Table ()
 Destructor. More...
 
const TableDescGetTableDesc () const
 Returns the table descriptor of this table. More...
 
void InsertRecord (Record &rec)
 Inserts the record into table. More...
 
void EraseRecord (RecordId rid)
 Erases the record specified by the record ID. More...
 
void UpdateRecord (RecordId rid, Record &rec)
 Updates the record specified by the record ID. More...
 
Iterator StartScan ()
 Starts a scan of the table from the beginning. More...
 
Iterator StartScanFrom (RecordId rid)
 Starts a scan of the table so that the first call to Iterator::Next() will return the first record with an ID greater than or equal to rid. More...
 

Static Public Member Functions

static void Initialize (const TableDesc *tabdesc)
 Inititalizes a table on a newly created virtual file. More...
 
static std::unique_ptr< TableCreate (std::shared_ptr< const TableDesc > tabdesc)
 Constructs a table object on a heap file that was previously initialized by Table::Initialize(). More...
 

Private Member Functions

 Table (std::shared_ptr< const TableDesc > tabdesc, std::unique_ptr< File > file)
 

Private Attributes

std::shared_ptr< const TableDescm_tabdesc
 The descriptor of the table. More...
 
std::unique_ptr< Filem_file
 An open file that stores the heap file for this table. More...
 
PageNumber m_insertion_pid
 The last known page possibly with empty space. More...
 

Friends

class Iterator
 

Detailed Description

Table implements a heap file over the virtual file provided by the FileManager.

A heap file manages a list of data pages, and supports inserting, deleting, updating and forward-iterating the records stored in the file. A Table object and its iterators should use the buffer manager to access its pages, rather than directly going through the File interface.

Note:

  1. You may assume tabisvarlen in the table entry of the table descriptor passed to the Table class is always true.
  2. Table may have a non-trivial constructor and/or a non-trivial destructor, but they must not throw any error.
  3. A Table object should not store any heap file's meta data or content locally, so that two Table objects created on the same heap file should be able to see the effect of each other as long as there's no data race.

Constructor & Destructor Documentation

◆ Table()

taco::Table::Table ( std::shared_ptr< const TableDesc tabdesc,
std::unique_ptr< File file 
)
inlineprivate

◆ ~Table()

taco::Table::~Table ( )

Destructor.

It must not throw any error.

Member Function Documentation

◆ Create()

std::unique_ptr< Table > taco::Table::Create ( std::shared_ptr< const TableDesc tabdesc)
static

Constructs a table object on a heap file that was previously initialized by Table::Initialize().

For all user tables, the caller should pass a table descriptor returned by the catalog cache.

For bootstrapping and initialization of the catalog cache, it may pass a fake table descriptor which at minimum needs to have the following set:

  • tabdesc->GetTableEntry()->tabissys()
  • tabdesc->GetTableEntry()->tabfid()
  • tabdesc->GetTableEntry()->tabisvarlen

All other members may or may not be valid (nullptr), so you may only access the above members of the table descriptor in Table::Create().

It is undefined if the file referenced by the table descriptor was not previously initialized by Initialize().

◆ EraseRecord()

void taco::Table::EraseRecord ( RecordId  rid)

Erases the record specified by the record ID.

This function will obtain an additional page pin on the target page, even if the caller already has one in an iterator, but it must release the additional pin upon exit. If a page becomes empty, EraseRecord() must call File::FreePage() to return the page to the file manager.

It is an error if it cannot erase the record (e.g., because it's not found), or rid does not belong to this table.

◆ GetTableDesc()

const TableDesc* taco::Table::GetTableDesc ( ) const
inline

Returns the table descriptor of this table.

◆ Initialize()

void taco::Table::Initialize ( const TableDesc tabdesc)
static

Inititalizes a table on a newly created virtual file.

For all user tables, the file should have been inserted into the catalog cache and the caller should supply a table descriptor returned by the catalog cache.

For bootstrapping and initialization of the catalog cache, it may pass a fake table descriptor which at minimum has the following set:

  • tabdesc->GetTableEntry()->tabissys()
  • tabdesc->GetTableEntry()->tabfid()
  • tabdesc->GetTableEntry()->tabisvarlen

All other members may or may not be valid (nullptr), so you may only access the above members of the table descriptor in Table::Initialize().

It is undefined if Initialize() is called more than once on a file, or the file has been modified since it is created.

◆ InsertRecord()

void taco::Table::InsertRecord ( Record rec)

Inserts the record into table.

It may insert a the record on an existing page or create a new page to insert the record if necessary. Upon successful return, rec.GetRecordId() should be set to the record ID of the newly inserted record. Otherwise, rec.GetRecordId() is undefined.

Note: for the heap file project, you may always insert to the end of the file without reusing empty spaces in the middle of the file.

It is an error if the record cannot be inserted. It is undefined if the record rec does not have its data pointer and length correctly set.

◆ StartScan()

Table::Iterator taco::Table::StartScan ( )

Starts a scan of the table from the beginning.

◆ StartScanFrom()

Table::Iterator taco::Table::StartScanFrom ( RecordId  rid)

Starts a scan of the table so that the first call to Iterator::Next() will return the first record with an ID greater than or equal to rid.

◆ UpdateRecord()

void taco::Table::UpdateRecord ( RecordId  rid,
Record rec 
)

Updates the record specified by the record ID.

Upon return, the ‘rec.GetRecordID()’ is updated to the new record ID of the updated record (which may be different from rid). This function will obtain an additional page pin on the target page, even if the caller already has one in an iterator, but it must release the additional pin upon exit. If a page becomes empty, UpdateRecord() must call File::FreePage() to return the page to the file manager.

Note that the function must perform in-place update whenever the new record's length is no larger than the old record's length at rid.

It is an error if the update cannot be completed, in which case, the old record at rid must not be erased. It is also an error if rid does not belong to this table.

Friends And Related Function Documentation

◆ Iterator

friend class Iterator
friend

Member Data Documentation

◆ m_file

std::unique_ptr<File> taco::Table::m_file
private

An open file that stores the heap file for this table.

◆ m_insertion_pid

PageNumber taco::Table::m_insertion_pid
private

The last known page possibly with empty space.

This serves as a hint where our insertion will happen.

◆ m_tabdesc

std::shared_ptr<const TableDesc> taco::Table::m_tabdesc
private

The descriptor of the table.


The documentation for this class was generated from the following files: