neustore.base
Class DBIndex

java.lang.Object
  extended by neustore.base.DBIndex
Direct Known Subclasses:
HeapFile, NaiveHeapFile

public abstract class DBIndex
extends java.lang.Object

Abstract base class for a disk-based, paginated and buffered index. The index reads and writes DBPage using a DBBuffer.

Usage:

Author:
Tian Xia <tianxia@ccs.neu.edu>
Donghui Zhang <donghui@ccs.neu.edu>
See Also:
DBPage, DBBuffer

Field Summary
 DBBuffer buffer
          The buffer manager.
static boolean CREATE
           
protected  java.io.RandomAccessFile file
          The random access file.
protected  int firstEmpty
          The first empty page ID.
protected  int numPages
          The number of non-empty pages (excluding the head page).
static boolean OPEN
           
protected static int OVERHEAD
          The length of the system information in bytes.
protected  int pageSize
          The size of a page.
 
Constructor Summary
DBIndex(DBBuffer buffer, java.lang.String filename, boolean isCreate)
          Creates or Opens an index file.
 
Method Summary
 int allocate()
          Assigns a new pageID.
 void close()
          Saves the header page to disk and flushes the buffer.
 void freePage(int pageID)
          Frees an empty page.
protected abstract  void initIndexHead()
          Initializes index head information.
protected abstract  void readIndexHead(byte[] indexHead)
          Reads index head information from a byte array.
protected abstract  void writeIndexHead(byte[] indexHead)
          Writes index head information to a byte array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CREATE

public static final boolean CREATE
See Also:
Constant Field Values

OPEN

public static final boolean OPEN
See Also:
Constant Field Values

pageSize

protected int pageSize
The size of a page.


numPages

protected int numPages
The number of non-empty pages (excluding the head page).


firstEmpty

protected int firstEmpty
The first empty page ID. It is -1 if there is no empty page in the file.


buffer

public DBBuffer buffer
The buffer manager.


OVERHEAD

protected static final int OVERHEAD
The length of the system information in bytes.

See Also:
Constant Field Values

file

protected java.io.RandomAccessFile file
The random access file.

Constructor Detail

DBIndex

public DBIndex(DBBuffer buffer,
               java.lang.String filename,
               boolean isCreate)
        throws java.io.IOException
Creates or Opens an index file.

Parameters:
buffer - the buffer manager
filename - the filename
isCreate - whether create (or open)
Throws:
java.io.IOException
Method Detail

allocate

public int allocate()
Assigns a new pageID.

Returns:
the new page ID.

freePage

public void freePage(int pageID)
              throws java.io.IOException
Frees an empty page.

Parameters:
pageID - the to-be-freed page ID
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Saves the header page to disk and flushes the buffer. It is important to call close() before exiting. Otherwise the file may be corrupted.

Throws:
java.io.IOException

readIndexHead

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

Parameters:
indexHead - the byte array of length pageSize-OVERHEAD

writeIndexHead

protected abstract void writeIndexHead(byte[] indexHead)
Writes index head information to a byte array. Called before closing a DBIndex.

Parameters:
indexHead - the byte array of length pageSize-OVERHEAD

initIndexHead

protected abstract void initIndexHead()
Initializes index head information. Called when creating a DBIndex.