neustore.heapfile
Class HeapFile

java.lang.Object
  extended by neustore.base.DBIndex
      extended by neustore.heapfile.HeapFile

public class HeapFile
extends DBIndex

A generic heap file that stores a list of (key, data) pairs.

Author:
Donghui Zhang <donghui@ccs.neu.edu>
See Also:
HeapFilePage

Field Summary
protected  int firstFullPageID
          pageID of the first full page.
protected  int firstNonFullPageID
          pageID of the first non-full page.
protected  int numRecs
          number of records
 
Fields inherited from class neustore.base.DBIndex
buffer, CREATE, file, firstEmpty, numPages, OPEN, OVERHEAD, pageSize
 
Constructor Summary
HeapFile(DBBuffer _buffer, java.lang.String filename, boolean isCreate, Key _sampleKey, Data _sampleData)
          The HeapFile constructor.
 
Method Summary
protected  void addPageToFullList(int pageID, HeapFilePage page)
          Adds a page to the beginning of the full-page list.
protected  void addPageToNonFullList(int pageID, HeapFilePage page)
          Adds a page to the beginning of the nonfull-page list.
 boolean delete(Key key)
          Deletes the record by the given key.
protected  void initIndexHead()
          Initializes index head information.
 void insert(Key key, Data data)
          Inserts a new record into the heap file.
protected  HeapFilePage myReadPage(int pageID)
          Reads a page from buffer and parse it if not parsed already.
 int numRecs()
          Returns the number of records.
 void printInformation()
          Prints some information about the index.
protected  void readIndexHead(byte[] indexHead)
          Reads index head information from a byte array.
protected  void removePageFromList(int pageID, HeapFilePage page)
          Removes a page from its (full or nonfull) linked list.
 Data search(Key key)
          Searches the record by the given key.
protected  void writeIndexHead(byte[] indexHead)
          Writes index head information to a byte array.
 
Methods inherited from class neustore.base.DBIndex
allocate, close, freePage
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

numRecs

protected int numRecs
number of records


firstFullPageID

protected int firstFullPageID
pageID of the first full page. -1 if none.


firstNonFullPageID

protected int firstNonFullPageID
pageID of the first non-full page. -1 if none.

Constructor Detail

HeapFile

public HeapFile(DBBuffer _buffer,
                java.lang.String filename,
                boolean isCreate,
                Key _sampleKey,
                Data _sampleData)
         throws java.io.IOException
The HeapFile constructor. It takes as additional input a sample key and a sample data.

Parameters:
_buffer - the buffer
isCreate - whether create (or open)
_sampleKey - the sample key
_sampleData - the sample data
Throws:
java.io.IOException
Method Detail

numRecs

public int numRecs()
Returns the number of records.

Returns:
number of records

myReadPage

protected HeapFilePage myReadPage(int pageID)
                           throws java.io.IOException
Reads a page from buffer and parse it if not parsed already.

Parameters:
pageID -
Returns:
an object of class HeapFilePage
Throws:
java.io.IOException

removePageFromList

protected void removePageFromList(int pageID,
                                  HeapFilePage page)
                           throws java.io.IOException
Removes a page from its (full or nonfull) linked list. Note that this function does NOT write the page to buffer.

Parameters:
pageID - the pageID
page - the HeapFilePage object
Throws:
java.io.IOException

addPageToFullList

protected void addPageToFullList(int pageID,
                                 HeapFilePage page)
                          throws java.io.IOException
Adds a page to the beginning of the full-page list. Note that this function writes the page to buffer.

Parameters:
pageID - the page ID
page - the HeapFilePage object
Throws:
java.io.IOException

addPageToNonFullList

protected void addPageToNonFullList(int pageID,
                                    HeapFilePage page)
                             throws java.io.IOException
Adds a page to the beginning of the nonfull-page list. Note that this function writes the page to buffer.

Parameters:
pageID - the page ID
page - the HeapFilePage object
Throws:
java.io.IOException

insert

public void insert(Key key,
                   Data data)
            throws java.io.IOException
Inserts a new record into the heap file. Algorithm:

Parameters:
key - the key to be inserted
data - the data to be inserted
Throws:
java.io.IOException

delete

public boolean delete(Key key)
               throws java.io.IOException
Deletes the record by the given key. After deletion, if the page is empty it should be set free. Otherwise, if the page change from full to nonfull, it should be removed from the full list and added to the nonfull list.

Parameters:
key - the key of the record to be deleted
Returns:
whether deleted
Throws:
java.io.IOException

search

public Data search(Key key)
            throws java.io.IOException
Searches the record by the given key.

Parameters:
key - the key to search
Returns:
data if found; null otherwise
Throws:
java.io.IOException

printInformation

public void printInformation()
Prints some information about the index.


readIndexHead

protected void readIndexHead(byte[] indexHead)
Description copied from class: DBIndex
Reads index head information from a byte array. Called when opening a DBIndex.

Specified by:
readIndexHead in class DBIndex
Parameters:
indexHead - the byte array of length pageSize-OVERHEAD

writeIndexHead

protected void writeIndexHead(byte[] indexHead)
Description copied from class: DBIndex
Writes index head information to a byte array. Called before closing a DBIndex.

Specified by:
writeIndexHead in class DBIndex
Parameters:
indexHead - the byte array of length pageSize-OVERHEAD

initIndexHead

protected void initIndexHead()
Description copied from class: DBIndex
Initializes index head information. Called when creating a DBIndex.

Specified by:
initIndexHead in class DBIndex