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)
          Constructor.
 
Method Summary
 java.util.ArrayList<Page> addTuple(TransactionId tid, Tuple t)
          Adds the specified tuple to the table under the specified TransactionId.
 int bytesPerPage()
           
 Page deleteTuple(TransactionId tid, Tuple t)
          Deletes the specified tuple from the table, under the specified TransactionId.
 java.io.File getFile()
          Return a Java File corresponding to the data from this HeapFile on disk.
 int id()
           
 DbFileIterator iterator(TransactionId tid)
          An iterator over all tuples on this file, over all pages.
 int numPages()
          Returns the number of pages in this HeapFile.
 Page readPage(PageId pid)
          Returns a Page from the file.
 void writePage(Page page)
          Writes the given page to the appropriate location in the file.
 
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)
Constructor. Creates a new heap file that stores pages in the specified buffer pool.

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

getFile

public java.io.File getFile()
Return a Java File corresponding to the data from this HeapFile on disk.


id

public int id()
Specified by:
id in interface DbFile
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. The implementation we suggest you use could hash the absolute file name of the file underlying the heapfile, i.e. f.getAbsoluteFile().hashCode() )

readPage

public Page readPage(PageId pid)
              throws java.util.NoSuchElementException
Returns a Page from the file.

Specified by:
readPage in interface DbFile
Throws:
java.util.NoSuchElementException - if the page does not exist in this file

writePage

public void writePage(Page page)
               throws java.io.IOException
Writes the given page to the appropriate location in the file.

Specified by:
writePage in interface DbFile
Throws:
java.io.IOException - if the write fails

numPages

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


addTuple

public java.util.ArrayList<Page> addTuple(TransactionId tid,
                                          Tuple t)
                                   throws DbException,
                                          java.io.IOException,
                                          TransactionAbortedException
Adds the specified tuple to the table under the specified TransactionId.

Specified by:
addTuple in interface DbFile
Parameters:
tid - The transaction performing the update
t - The tuple to add. This tuple will be updated to reflect that it is now stored in this file.
Returns:
An ArrayList contain the pages that were modified
Throws:
DbException
java.io.IOException
TransactionAbortedException

deleteTuple

public Page deleteTuple(TransactionId tid,
                        Tuple t)
                 throws DbException,
                        TransactionAbortedException
Deletes the specified tuple from the table, under the specified TransactionId.

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)
An iterator over all tuples on this file, over all pages. Note that this iterator should use BufferPool.getPage(), rather than HeapFile.getPage() to iterate through pages.

Specified by:
iterator in interface DbFile

bytesPerPage

public int bytesPerPage()
Returns:
the number of bytes on a page, including the number of bytes in the header.