Class HFPage

java.lang.Object
  extended by HFPage

public class HFPage
extends java.lang.Object

Heap file page. This is a wrapper around a traditional Page that adds the appropriate struture to it.

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

Nested Class Summary
static class HFPage.BadPageIdException
           
static class HFPage.BadSlotIdException
           
static class HFPage.PageFullException
           
 
Field Summary
static int INVALID_PAGE
          Value to use for an invalid page id.
 
Constructor Summary
HFPage(Page page)
          Creates a heap file page object by wrapping around a page object already provided.
 
Method Summary
 boolean deleteRecord(RID rid)
          Deletes the record with the given RID from the page, compacting the hole created.
 void dumpPage()
          Dumps out to the screen the id for this page, as well as the ids for the next and previous pages.
 boolean empty()
          Whether or not the page is empty.
 RID firstRecord()
          Returns RID of first record on page.
 int getAvailableSpace()
          Determines how much space is actually available on the page, which depends on whether or not a new slot in the slot array is needed.
 int getNextPageId()
          Gets the next page id.
 int getPageId()
          Gets the page id.
 int getPrevPageId()
          Gets the previous page id.
 byte[] getRecord(RID rid)
          Returns the record associated with an RID.
 void init()
          Initializes values on the heap file page as necessary.
 RID insertRecord(byte[] record)
          Inserts a new record onto the page.
 RID nextRecord(RID curRid)
          Returns RID of next record on the page, where "next on the page" means "next in the slot array." Remember that some slots may be empty, so you should skip over these.
 void setNextPageId(int pageId)
          Sets the next page id.
 void setPageId(int pageId)
          Sets the page id.
 void setPrevPageId(int pageId)
          Sets the previous page id.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INVALID_PAGE

public static final int INVALID_PAGE
Value to use for an invalid page id.

See Also:
Constant Field Values
Constructor Detail

HFPage

public HFPage(Page page)
Creates a heap file page object by wrapping around a page object already provided.

Parameters:
page - the page to be wrapped.
Method Detail

init

public void init()
Initializes values on the heap file page as necessary. This is separated out from the constructor since it actually modifies the page at hand, where as the constructor simply sets up the mechanism.


setPageId

public void setPageId(int pageId)
Sets the page id.

Parameters:
pageId - the new page id.

getPageId

public int getPageId()
Gets the page id.

Returns:
the page id.

setNextPageId

public void setNextPageId(int pageId)
Sets the next page id.

Parameters:
pageId - the next page id.

getNextPageId

public int getNextPageId()
Gets the next page id.

Returns:
the next page id.

setPrevPageId

public void setPrevPageId(int pageId)
Sets the previous page id.

Parameters:
pageId - the previous page id.

getPrevPageId

public int getPrevPageId()
Gets the previous page id.

Returns:
the previous page id.

getAvailableSpace

public int getAvailableSpace()
Determines how much space is actually available on the page, which depends on whether or not a new slot in the slot array is needed. If a new spot in the slot array is needed, then the amount of available space has to take this into consideration.

Returns:
the amount of available space in bytes

dumpPage

public void dumpPage()
Dumps out to the screen the id for this page, as well as the ids for the next and previous pages. It also displays the slot array to the screen in a readable dashion. (This method merely exists for debugging and testing purposes.


insertRecord

public RID insertRecord(byte[] record)
Inserts a new record onto the page.

Parameters:
record - the record to be inserted. A copy of the data is placed on the page.
Returns:
the RID of the new record
Throws:
HFPage.PageFullException - if there is not enough room for the record on the page.

deleteRecord

public boolean deleteRecord(RID rid)
Deletes the record with the given RID from the page, compacting the hole created. Compacting the hole, in turn, requires that all the offsets (in the slot array) of all records after the hole be adjusted by the size of the hole, because you are moving these records to "fill" the hole. You should leave a "hole" in the slot array for the slot which pointed to the deleted record, if necessary, to make sure that the rids of the remaining records do not change. The slot array should be compacted only if the record corresponding to the last slot is being deleted.

Parameters:
rid - the RID to be deleted.
Returns:
true if successful, false if the rid is actually not found on the page.

firstRecord

public RID firstRecord()
Returns RID of first record on page.

Returns:
the RID of the first record on the page. Returns null if the page is empty.

nextRecord

public RID nextRecord(RID curRid)
Returns RID of next record on the page, where "next on the page" means "next in the slot array." Remember that some slots may be empty, so you should skip over these.

Parameters:
curRid - an RID
Returns:
the RID immediately following curRid. Returns null if curRID is the last record on the page.
Throws:
HFPage.BadPageIdException - if the page id within curRid is invalid
HFPage.BadSlotIdException - if the slot id within curRid is invalid

getRecord

public byte[] getRecord(RID rid)
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:
HFPage.BadPageIdException - if the page id within curRid is invalid
HFPage.BadSlotIdException - if the slot id within curRid is invalid

empty

public boolean empty()
Whether or not the page is empty.

Returns:
true if the page is empty, false otherwise.