Class HeapFile

java.lang.Object
  extended by HeapFile

public class HeapFile
extends java.lang.Object

Heap file. This manages a linked list of heap file pages.

Author:
Dave Musicant, with considerable material reused from the UW-Madison Minibase project

Constructor Summary
HeapFile(BufferManager bufMgr, java.lang.String name)
          Opens the heap file with the given name.
HeapFile(BufferManager bufMgr, java.lang.String name, int numPages)
          Constructs a heap file with the specified number of pages.
 
Method Summary
static boolean erase(java.lang.String name)
          Erases the heap file entirely from the filesystem.
 byte[] getRecord(RID rid)
          Returns the record associated with an RID.
 RID insertRecord(byte[] record)
          Inserts a new record onto the heap file.
 PageIterator pageIterator()
          Returns a page iterator on this heap file starting with the first page.
 RecordIterator recordIterator()
          Returns a record iterator on this heap file starting with the first page.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HeapFile

public HeapFile(BufferManager bufMgr,
                java.lang.String name,
                int numPages)
         throws java.io.IOException
Constructs a heap file with the specified number of pages.

Parameters:
bufMgr - Reference to a buffer manager to be used for the life of this HeapFile object.
name - Name to be used for heap file
numPages - maximum number of pages in heap file.
Throws:
java.io.IOException - passed through from underlying file system.

HeapFile

public HeapFile(BufferManager bufMgr,
                java.lang.String name)
         throws java.io.IOException
Opens the heap file with the given name.

Parameters:
bufMgr - Reference to a buffer manager to be used for the life of this HeapFile object.
name - Name to be used for heap file
Throws:
java.io.IOException - passed through from underlying file system.
Method Detail

erase

public static boolean erase(java.lang.String name)
                     throws java.io.IOException
Erases the heap file entirely from the filesystem. Dangerous to do if still have a heap file object that refers to this file. Make sure to flush the buffer pool before calling this method to ensure that no dirty pages remain in memory that might get flushed later.

Parameters:
name - name of the database.
Returns:
true if operation succeeded.
Throws:
java.io.IOException - passed through from underlying file system.

insertRecord

public RID insertRecord(byte[] record)
                 throws java.io.IOException
Inserts a new record onto the heap file.

Parameters:
record - the record to be inserted. A copy of the data is inserted into the heap file.
Returns:
the RID of the new record
Throws:
DBFile.FileFullException - if there are not enough free pages.
java.io.IOException - passed through from underlying file system.

getRecord

public byte[] getRecord(RID rid)
                 throws java.io.IOException
Returns the record associated with an RID.

Parameters:
rid - the rid of interest
Returns:
a byte array containing a copy of the record. The array has precisely the length of the record (there is no padded space).
Throws:
DBFile.BadPageNumberException - if the page id is invalid
HFPage.BadSlotIdException - if the slot id within curRid is invalid
java.io.IOException

pageIterator

public PageIterator pageIterator()
                          throws java.io.IOException
Returns a page iterator on this heap file starting with the first page.

Returns:
a page iterator starting with the first page
Throws:
java.io.IOException - passed through from underlying file system.

recordIterator

public RecordIterator recordIterator()
                              throws java.io.IOException
Returns a record iterator on this heap file starting with the first page.

Returns:
a record iterator starting with the first page
Throws:
java.io.IOException - passed through from underlying file system.