neustore.base
Class DBBuffer

java.lang.Object
  extended by neustore.base.DBBuffer
Direct Known Subclasses:
LRUBuffer

public abstract class DBBuffer
extends java.lang.Object

Abstract base class for a buffer manager. It helps index structures (derived from DBIndex) manage disk pages.

Usage:

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

Field Summary
protected  int bufferReadIO
          number of read IOs on the buffer
protected  int bufferSize
          how many pages the buffer has
protected  int bufferWriteIO
          number of write IOs on the buffer
protected  int diskReadIO
          number of read IOs on the disk
protected  int diskWriteIO
          number of write IOs on the disk
protected  int pageSize
          page size
 
Constructor Summary
DBBuffer(int bufferSize, int pageSize)
          Opens an existing Database file.
 
Method Summary
protected abstract  void add(neustore.base.DBBuffer.DBBufferStoredElement stored)
          Adds a page to the buffer.
 void clearIOs()
          Resets the four counters bufferReadIO, etc.
protected abstract  neustore.base.DBBuffer.DBBufferStoredElement find(java.io.RandomAccessFile file, int pageID)
          Finds a page in buffer.
protected abstract  void flush(java.io.RandomAccessFile file)
          Flushes the buffer pages corresponding to a give file.
 int[] getIOs()
          Reports the disk/buffer I/Os.
protected  void load(java.io.RandomAccessFile file, byte[] page, int pageID)
          Loads a page (the byte array) from disk.
 void pin(java.io.RandomAccessFile file, int pageID)
          Pins a page.
 DBBufferReturnElement readPage(java.io.RandomAccessFile file, int pageID)
          Returns a buffered page.
protected  void save(java.io.RandomAccessFile file, byte[] page, int pageID)
          Saves a page (the byte array) to the disk.
 void unpin(java.io.RandomAccessFile file, int pageID)
          Unpins a page.
 void writePage(java.io.RandomAccessFile file, int pageID, DBPage dbpage)
          Writes a DBPage to buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

bufferReadIO

protected int bufferReadIO
number of read IOs on the buffer


bufferWriteIO

protected int bufferWriteIO
number of write IOs on the buffer


diskReadIO

protected int diskReadIO
number of read IOs on the disk


diskWriteIO

protected int diskWriteIO
number of write IOs on the disk


bufferSize

protected int bufferSize
how many pages the buffer has


pageSize

protected int pageSize
page size

Constructor Detail

DBBuffer

public DBBuffer(int bufferSize,
                int pageSize)
Opens an existing Database file.

Parameters:
bufferSize - total number of buffer pages
pageSize - page size
Method Detail

getIOs

public int[] getIOs()
Reports the disk/buffer I/Os. There are four I/Os reported: disk-read, disk-write, buffer-read and buffer-write. They are stored in the integer array in the above order.

Returns:
the four I/Os

load

protected void load(java.io.RandomAccessFile file,
                    byte[] page,
                    int pageID)
             throws java.io.IOException
Loads a page (the byte array) from disk. Increases diskReadIO by one.

Parameters:
file - the file
page - the byte array of length pageSize
pageID - the page ID
Throws:
java.io.IOException

save

protected void save(java.io.RandomAccessFile file,
                    byte[] page,
                    int pageID)
             throws java.io.IOException
Saves a page (the byte array) to the disk. Increases diskWriteIO by one.

Parameters:
page - the byte array of length pageSize
pageID - the page ID
Throws:
java.io.IOException

clearIOs

public void clearIOs()
Resets the four counters bufferReadIO, etc.


readPage

public DBBufferReturnElement readPage(java.io.RandomAccessFile file,
                                      int pageID)
                               throws java.io.IOException
Returns a buffered page. If it is in buffer, returns it. Otherwise, loads it from disk and then returns it. Increases bufferReadIO.

Parameters:
file - which file
pageID - page ID
Returns:
the buffered page
Throws:
java.io.IOException

writePage

public void writePage(java.io.RandomAccessFile file,
                      int pageID,
                      DBPage dbpage)
               throws java.io.IOException
Writes a DBPage to buffer.

Parameters:
file - the file
pageID - the page ID
dbpage - the DBPage object
Throws:
java.io.IOException

pin

public void pin(java.io.RandomAccessFile file,
                int pageID)
Pins a page. Such pages can not be swapped out of the buffer. The pinCount in DBBufferStoredElement is increased by 1.

Parameters:
file - the file
pageID - the page ID of the page to be pinned

unpin

public void unpin(java.io.RandomAccessFile file,
                  int pageID)
Unpins a page. The pinCount in DBBufferStoredElement is decreased by 1.

Parameters:
file - the file
pageID - the page ID of the page to be unpinned

find

protected abstract neustore.base.DBBuffer.DBBufferStoredElement find(java.io.RandomAccessFile file,
                                                                     int pageID)
Finds a page in buffer. Note: do not increase bufferReadIO.

Parameters:
file - the file
pageID - the page ID
Returns:
an instance of DBBufferStoredElement if found; nullotherwise.

add

protected abstract void add(neustore.base.DBBuffer.DBBufferStoredElement stored)
                     throws java.io.IOException
Adds a page to the buffer. Note: do not increase bufferReadIO.

Parameters:
stored - the stored element
Throws:
java.io.IOException

flush

protected abstract void flush(java.io.RandomAccessFile file)
                       throws java.io.IOException
Flushes the buffer pages corresponding to a give file. Makes the buffer empty, while saving modified pages to disk.

Parameters:
file - the file
Throws:
java.io.IOException