simpledb
Class HeapFile

java.lang.Object
  extended by simpledb.HeapFile
All Implemented Interfaces:
DbFile

public class HeapFile
extends java.lang.Object
implements DbFile

HeapFile is an implementation of a DbFile that stores a collection of tuples in no particular order. Tuples are stored on pages, each of which is a fixed size, and the file is simply a collection of those pages. HeapFile works closely with HeapPage. The format of HeapPages is described in the HeapPage constructor.

Author:
Sam Madden
See Also:
HeapPage.HeapPage(simpledb.HeapPageId, byte[])

Constructor Summary
HeapFile(java.io.File f, TupleDesc td)
          Constructs a heap file backed by the specified file.
 
Method Summary
 Page deleteTuple(TransactionId tid, Tuple t)
          Removes the specifed tuple from the file on behalf of the specified transaction.
 java.io.File getFile()
          Returns the File backing this HeapFile on disk.
 int getId()
          Returns an ID uniquely identifying this HeapFile.
 TupleDesc getTupleDesc()
          Returns the TupleDesc of the table stored in this DbFile.
 java.util.ArrayList<Page> insertTuple(TransactionId tid, Tuple t)
          Inserts the specified tuple to the file on behalf of transaction.
 DbFileIterator iterator(TransactionId tid)
          Returns an iterator over all the tuples stored in this DbFile.
 int numPages()
          Returns the number of pages in this HeapFile.
 Page readPage(PageId pid)
          Read the specified page from disk.
 void writePage(Page page)
          Push the specified page to disk.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HeapFile

public HeapFile(java.io.File f,
                TupleDesc td)
Constructs a heap file backed by the specified file.

Parameters:
f - the file that stores the on-disk backing store for this heap file.
Method Detail

getFile

public java.io.File getFile()
Returns the File backing this HeapFile on disk.

Returns:
the File backing this HeapFile on disk.

getId

public int getId()
Returns an ID uniquely identifying this HeapFile. Implementation note: you will need to generate this tableid somewhere ensure that each HeapFile has a "unique id," and that you always return the same value for a particular HeapFile. We suggest hashing the absolute file name of the file underlying the heapfile, i.e. f.getAbsoluteFile().hashCode().

Specified by:
getId in interface DbFile
Returns:
an ID uniquely identifying this HeapFile.

getTupleDesc

public TupleDesc getTupleDesc()
Returns the TupleDesc of the table stored in this DbFile.

Specified by:
getTupleDesc in interface DbFile
Returns:
TupleDesc of this DbFile.

readPage

public Page readPage(PageId pid)
Description copied from interface: DbFile
Read the specified page from disk.

Specified by:
readPage in interface DbFile

writePage

public void writePage(Page page)
               throws java.io.IOException
Description copied from interface: DbFile
Push the specified page to disk.

Specified by:
writePage in interface DbFile
Parameters:
page - The page to write. page.getId().pageno() specifies the offset into the file where the page should be written.
Throws:
java.io.IOException - if the write fails

numPages

public int numPages()
Returns the number of pages in this HeapFile.


insertTuple

public java.util.ArrayList<Page> insertTuple(TransactionId tid,
                                             Tuple t)
                                      throws DbException,
                                             java.io.IOException,
                                             TransactionAbortedException
Description copied from interface: DbFile
Inserts the specified tuple to the file on behalf of transaction. This method will acquire a lock on the affected pages of the file, and may block until the lock can be acquired.

Specified by:
insertTuple in interface DbFile
Parameters:
tid - The transaction performing the update
t - The tuple to add. This tuple should be updated to reflect that it is now stored in this file.
Returns:
An ArrayList contain the pages that were modified
Throws:
DbException - if the tuple cannot be added
java.io.IOException - if the needed file can't be read/written
TransactionAbortedException

deleteTuple

public Page deleteTuple(TransactionId tid,
                        Tuple t)
                 throws DbException,
                        TransactionAbortedException
Description copied from interface: DbFile
Removes the specifed tuple from the file on behalf of the specified transaction. This method will acquire a lock on the affected pages of the file, and may block until the lock can be acquired.

Specified by:
deleteTuple in interface DbFile
Throws:
DbException - if the tuple cannot be deleted or is not a member of the file
TransactionAbortedException

iterator

public DbFileIterator iterator(TransactionId tid)
Description copied from interface: DbFile
Returns an iterator over all the tuples stored in this DbFile. The iterator must use BufferPool.getPage(simpledb.TransactionId, simpledb.PageId, simpledb.Permissions), rather than DbFile.readPage(simpledb.PageId) to iterate through the pages.

Specified by:
iterator in interface DbFile
Returns:
an iterator over all the tuples stored in this DbFile.