simpledb
Class HeapPage

java.lang.Object
  extended by simpledb.HeapPage
All Implemented Interfaces:
Page

public class HeapPage
extends java.lang.Object
implements Page

Each instance of HeapPage stores data for one page of HeapFiles and implements the Page interface that is used by BufferPool.

See Also:
HeapFile, BufferPool

Field Summary
(package private)  byte[] header
           
(package private)  int numSlots
           
(package private)  byte[] oldData
           
(package private)  HeapPageId pid
           
(package private)  TupleDesc td
           
(package private)  Tuple[] tuples
           
 
Constructor Summary
HeapPage(HeapPageId id, byte[] data)
          Create a HeapPage from a set of bytes of data read from disk.
 
Method Summary
static byte[] createEmptyPageData()
          Static method to generate a byte array corresponding to an empty HeapPage.
 void deleteTuple(Tuple t)
          Delete the specified tuple from the page; the tuple should be updated to reflect that it is no longer stored on any page.
 HeapPage getBeforeImage()
          Return a view of this page before it was modified -- used by recovery
private  int getHeaderSize()
          Computes the number of bytes in the header of a page in a HeapFile with each tuple occupying tupleSize bytes
 HeapPageId getId()
          Return the id of this page.
 int getNumEmptySlots()
          Returns the number of empty slots on this page.
private  int getNumTuples()
          Retrieve the number of tuples on this page.
 byte[] getPageData()
          Generates a byte array representing the contents of this page.
 void insertTuple(Tuple t)
          Adds the specified tuple to the page; the tuple should be updated to reflect that it is now stored on this page.
 TransactionId isDirty()
          Returns the tid of the transaction that last dirtied this page, or null if the page is not dirty
 boolean isSlotFree(int i)
          Returns true if associated slot on this page is filled.
 java.util.Iterator<Tuple> iterator()
           
 void markDirty(boolean dirty, TransactionId tid)
          Marks this page as dirty/not dirty and record that transaction that did the dirtying
private  void markSlotUsed(int i, boolean value)
          Abstraction to fill or clear a slot on this page.
private  Tuple readNextTuple(java.io.DataInputStream dis, int slotId)
          Suck up tuples from the source file.
 void setBeforeImage()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

pid

HeapPageId pid

td

TupleDesc td

header

byte[] header

tuples

Tuple[] tuples

numSlots

int numSlots

oldData

byte[] oldData
Constructor Detail

HeapPage

public HeapPage(HeapPageId id,
                byte[] data)
         throws java.io.IOException
Create a HeapPage from a set of bytes of data read from disk. The format of a HeapPage is a set of header bytes indicating the slots of the page that are in use, some number of tuple slots. Specifically, the number of tuples is equal to:

floor((BufferPool.PAGE_SIZE*8) / (tuple size * 8 + 1))

where tuple size is the size of tuples in this database table, which can be determined via Catalog.getTupleDesc(int). The number of 8-bit header words is equal to:

ceiling(no. tuple slots / 8)

Throws:
java.io.IOException
See Also:
Database.getCatalog(), Catalog.getTupleDesc(int), BufferPool.PAGE_SIZE
Method Detail

getNumTuples

private int getNumTuples()
Retrieve the number of tuples on this page.

Returns:
the number of tuples on this page

getHeaderSize

private int getHeaderSize()
Computes the number of bytes in the header of a page in a HeapFile with each tuple occupying tupleSize bytes

Returns:
the number of bytes in the header of a page in a HeapFile with each tuple occupying tupleSize bytes

getBeforeImage

public HeapPage getBeforeImage()
Return a view of this page before it was modified -- used by recovery

Specified by:
getBeforeImage in interface Page

setBeforeImage

public void setBeforeImage()
Specified by:
setBeforeImage in interface Page

getId

public HeapPageId getId()
Description copied from interface: Page
Return the id of this page. The id is a unique identifier for a page that can be used to look up the page on disk or determine if the page is resident in the buffer pool.

Specified by:
getId in interface Page
Returns:
the PageId associated with this page.

readNextTuple

private Tuple readNextTuple(java.io.DataInputStream dis,
                            int slotId)
                     throws java.util.NoSuchElementException
Suck up tuples from the source file.

Throws:
java.util.NoSuchElementException

getPageData

public byte[] getPageData()
Generates a byte array representing the contents of this page. Used to serialize this page to disk.

The invariant here is that it should be possible to pass the byte array generated by getPageData to the HeapPage constructor and have it produce an identical HeapPage object.

Specified by:
getPageData in interface Page
Returns:
A byte array correspond to the bytes of this page.
See Also:
HeapPage(simpledb.HeapPageId, byte[])

createEmptyPageData

public static byte[] createEmptyPageData()
Static method to generate a byte array corresponding to an empty HeapPage. Used to add new, empty pages to the file. Passing the results of this method to the HeapPage constructor will create a HeapPage with no valid tuples in it.

Returns:
The returned ByteArray.

deleteTuple

public void deleteTuple(Tuple t)
                 throws DbException
Delete the specified tuple from the page; the tuple should be updated to reflect that it is no longer stored on any page.

Parameters:
t - The tuple to delete
Throws:
DbException - if this tuple is not on this page, or tuple slot is already empty.

insertTuple

public void insertTuple(Tuple t)
                 throws DbException
Adds the specified tuple to the page; the tuple should be updated to reflect that it is now stored on this page.

Parameters:
t - The tuple to add.
Throws:
DbException - if the page is full (no empty slots) or tupledesc is mismatch.

markDirty

public void markDirty(boolean dirty,
                      TransactionId tid)
Marks this page as dirty/not dirty and record that transaction that did the dirtying

Specified by:
markDirty in interface Page

isDirty

public TransactionId isDirty()
Returns the tid of the transaction that last dirtied this page, or null if the page is not dirty

Specified by:
isDirty in interface Page
Returns:
The id of the transaction that last dirtied this page, or null

getNumEmptySlots

public int getNumEmptySlots()
Returns the number of empty slots on this page.


isSlotFree

public boolean isSlotFree(int i)
Returns true if associated slot on this page is filled.


markSlotUsed

private void markSlotUsed(int i,
                          boolean value)
Abstraction to fill or clear a slot on this page.


iterator

public java.util.Iterator<Tuple> iterator()
Returns:
an iterator over all tuples on this page (calling remove on this iterator throws an UnsupportedOperationException) (note that this iterator shouldn't return tuples in empty slots!)