Class BufferManager

java.lang.Object
  extended by BufferManager

public class BufferManager
extends java.lang.Object

Buffer manager. Manages a memory-based buffer pool of pages.

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

Nested Class Summary
static class BufferManager.PageNotPinnedException
           
static class BufferManager.PagePinnedException
           
 
Field Summary
static int INVALID_PAGE
          Value to use for an invalid page id.
 
Constructor Summary
BufferManager(int poolSize)
          Creates a buffer manager with the specified size.
 
Method Summary
 int findFrame(int pageId, java.lang.String fileName)
          Returns buffer pool location for a particular pageId.
 void flushAllPages()
          Flushes all dirty pages from the buffer pool to the underlying databases.
 void flushPage(int pageId, java.lang.String fileName)
          Flushes page from the buffer pool to the underlying database if it is dirty.
 void freePage(int pageId, java.lang.String fileName)
          Deallocates a page from the underlying database.
 Pair<java.lang.Integer,Page> newPage(int numPages, java.lang.String fileName)
          Requests a run of pages from the underlying database, then finds a frame in the buffer pool for the first page and pins it.
 Page pinPage(int pinPageId, java.lang.String fileName, boolean emptyPage)
          Checks if this page is in buffer pool.
 int poolSize()
          Returns the pool size.
 void unpinPage(int unpinPageId, java.lang.String fileName, boolean dirty)
          If the pin count for this page is greater than 0, it is decremented.
 
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

BufferManager

public BufferManager(int poolSize)
Creates a buffer manager with the specified size.

Parameters:
poolSize - the number of pages that the buffer pool can hold.
Method Detail

poolSize

public int poolSize()
Returns the pool size.

Returns:
the pool size.

pinPage

public Page pinPage(int pinPageId,
                    java.lang.String fileName,
                    boolean emptyPage)
             throws java.io.IOException
Checks if this page is in buffer pool. If it is, returns a pointer to it. Otherwise, it finds an available frame for this page, reads the page, and pins it. Writes out the old page, if it is dirty, before reading.

Parameters:
pinPageId - the page id for the page to be pinned
fileName - the name of the database that contains the page to be pinned
emptyPage - determines if the page is known to be empty. If true, then the page is not actually read from disk since it is assumed to be empty.
Returns:
a reference to the page in the buffer pool. If the buffer pool is full, null is returned.
Throws:
java.io.IOException - passed through from underlying file system.

unpinPage

public void unpinPage(int unpinPageId,
                      java.lang.String fileName,
                      boolean dirty)
               throws java.io.IOException
If the pin count for this page is greater than 0, it is decremented. If the pin count becomes zero, it is appropriately included in a group of replacement candidates.

Parameters:
unpinPageId - the page id for the page to be unpinned
fileName - the name of the database that contains the page to be unpinned
dirty - if false, then the page does not actually need to be written back to disk.
Throws:
BufferManager.PageNotPinnedException - if the page is not pinned, or if the page id is invalid in some other way.
java.io.IOException - passed through from underlying file system.

newPage

public Pair<java.lang.Integer,Page> newPage(int numPages,
                                            java.lang.String fileName)
                                     throws java.io.IOException
Requests a run of pages from the underlying database, then finds a frame in the buffer pool for the first page and pins it. If the buffer pool is full, no new pages are allocated from the database.

Parameters:
numPages - the number of pages in the run to be allocated.
fileName - the name of the database from where pages are to be allocated.
Returns:
an Integer containing the first page id of the run, and a references to the Page which has been pinned in the buffer pool. Returns null if there is not enough space in the buffer pool for the requested run.
Throws:
DBFile.FileFullException - if there are not enough free pages.
java.io.IOException - passed through from underlying file system.

freePage

public void freePage(int pageId,
                     java.lang.String fileName)
              throws java.io.IOException
Deallocates a page from the underlying database. Verifies that page is not pinned.

Parameters:
pageId - the page id to be deallocated.
fileName - the name of the database from where the page is to be deallocated.
Throws:
BufferManager.PagePinnedException - if the page is pinned
java.io.IOException - passed through from underlying file system.

flushPage

public void flushPage(int pageId,
                      java.lang.String fileName)
               throws java.io.IOException
Flushes page from the buffer pool to the underlying database if it is dirty. If page is not dirty, it is not flushed, especially since an undirty page may hang around even after the underlying database has been erased. If the page is not in the buffer pool, do nothing, since the page is effectively flushed already.

Parameters:
pageId - the page id to be flushed.
fileName - the name of the database where the page should be flushed.
Throws:
java.io.IOException - passed through from underlying file system.

flushAllPages

public void flushAllPages()
                   throws java.io.IOException
Flushes all dirty pages from the buffer pool to the underlying databases. If page is not dirty, it is not flushed, especially since an undirty page may hang around even after the underlying database has been erased.

Throws:
java.io.IOException - passed through from underlying file system.

findFrame

public int findFrame(int pageId,
                     java.lang.String fileName)
Returns buffer pool location for a particular pageId. This method is just used for testing purposes: it probably doesn't serve a real purpose in an actual database system.

Parameters:
pageId - the page id to be looked up.
fileName - the file name to be looked up.
Returns:
the frame location for the page of interested. Returns -1 if the page is not in the pool.