simpledb
Class HeapPage

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

public class HeapPage
extends java.lang.Object
implements Page

HeapPage stores pages of HeapFiles and implements the Page interface that is used by BufferPool.

See Also:
HeapFile, BufferPool

Field Summary
(package private)  int[] header
           
(package private)  int numSlots
           
(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
 void addTuple(Tuple t)
          Adds the specified tuple to the page.
static byte[] createEmptyPageData(int tableid)
          Static method to generate a byte array corresponding to an empty HeapPage.
 boolean deleteTuple(Tuple t)
          Delete the specified tuple from the page.
 HeapPage getBeforeImage()
          Return a view of this page before it was modified -- used by recovery
 int getNumEmptySlots()
          Returns the number of empty slots on this page.
 byte[] getPageData()
          Generates a byte array representing the contents of this page.
 boolean getSlot(int i)
          Returns true if associated slot on this page is filled.
 HeapPageId id()
          Return the id of this page.
 TransactionId isDirty()
          Returns the tid of the transaction that last dirtied this page, or null if the page is not dirty
 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  Tuple readNextTuple(java.io.DataInputStream dis, int slotId)
          Suck up tuples from the source file.
private  void setSlot(int i, boolean value)
          Abstraction to fill a slot on this page.
 
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

int[] header

tuples

Tuple[] tuples

numSlots

int numSlots
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 32-bit header words indicating the slots of the page that are in use, plus (BufferPool.PAGE_SIZE/tuple size) tuple slots, where tuple size is the size of tuples in this database table, which can be determined via Catalog.getTupleDesc(int). The number of 32-bit header words is equal to:

(no. tuple slots / 32) + 1

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

getBeforeImage

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

Specified by:
getBeforeImage in interface Page

id

public HeapPageId id()
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:
id 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(int tableid)
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.

Parameters:
tableid - The id of the table that this empty page will belong to.
Returns:
The returned ByteArray.

deleteTuple

public boolean deleteTuple(Tuple t)
                    throws DbException
Delete the specified tuple from the page.

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

addTuple

public void addTuple(Tuple t)
              throws DbException
Adds the specified tuple to the 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.


getSlot

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


setSlot

private void setSlot(int i,
                     boolean value)
Abstraction to fill 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!)