simpledb
Class BufferPool

java.lang.Object
  extended by simpledb.BufferPool

public class BufferPool
extends java.lang.Object

BufferPool manages the reading and writing of pages into memory from disk. Access methods call into it to retrieve pages, and it fetches pages from the appropriate location.

The BufferPool is also responsible for locking; when a transaction fetches a page, BufferPool checks that the transaction has the appropriate locks to read/write the page.


Field Summary
static int DEFAULT_PAGES
          Default number of pages passed to the constructor.
static int PAGE_SIZE
          Bytes per page, including header.
 
Constructor Summary
BufferPool(int numPages)
          Creates a BufferPool that caches up to numPages pages.
 
Method Summary
 void deleteTuple(TransactionId tid, Tuple t)
          Remove the specified tuple from the buffer pool.
 void discardPage(PageId pid)
          Remove the specific page id from the buffer pool.
private  void evictPage()
          Discards a page from the buffer pool.
 void flushAllPages()
          Flush all dirty pages to disk.
private  void flushPage(PageId pid)
          Flushes a certain page to disk
 void flushPages(TransactionId tid)
          Write all pages of the specified transaction to disk.
 Page getPage(TransactionId tid, PageId pid, Permissions perm)
          Retrieve the specified page with the associated permissions.
 boolean holdsLock(TransactionId tid, PageId p)
          Return true if the specified transaction has a lock on the specified page
 void insertTuple(TransactionId tid, int tableId, Tuple t)
          Add a tuple to the specified table behalf of transaction tid.
 void releasePage(TransactionId tid, PageId pid)
          Releases the lock on a page.
 void transactionComplete(TransactionId tid)
          Release all locks associated with a given transaction.
 void transactionComplete(TransactionId tid, boolean commit)
          Commit or abort a given transaction; release all locks associated to the transaction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PAGE_SIZE

public static final int PAGE_SIZE
Bytes per page, including header.

See Also:
Constant Field Values

DEFAULT_PAGES

public static final int DEFAULT_PAGES
Default number of pages passed to the constructor. This is used by other classes. BufferPool should use the numPages argument to the constructor instead.

See Also:
Constant Field Values
Constructor Detail

BufferPool

public BufferPool(int numPages)
Creates a BufferPool that caches up to numPages pages.

Parameters:
numPages - maximum number of pages in this buffer pool.
Method Detail

getPage

public Page getPage(TransactionId tid,
                    PageId pid,
                    Permissions perm)
             throws TransactionAbortedException,
                    DbException
Retrieve the specified page with the associated permissions. Will acquire a lock and may block if that lock is held by another transaction.

The retrieved page should be looked up in the buffer pool. If it is present, it should be returned. If it is not present, it should be added to the buffer pool and returned. If there is insufficient space in the buffer pool, an page should be evicted and the new page should be added in its place.

Parameters:
tid - the ID of the transaction requesting the page
pid - the ID of the requested page
perm - the requested permissions on the page
Throws:
TransactionAbortedException
DbException

releasePage

public void releasePage(TransactionId tid,
                        PageId pid)
Releases the lock on a page. Calling this is very risky, and may result in wrong behavior. Think hard about who needs to call this and why, and why they can run the risk of calling it.

Parameters:
tid - the ID of the transaction requesting the unlock
pid - the ID of the page to unlock

transactionComplete

public void transactionComplete(TransactionId tid)
                         throws java.io.IOException
Release all locks associated with a given transaction.

Parameters:
tid - the ID of the transaction requesting the unlock
Throws:
java.io.IOException

holdsLock

public boolean holdsLock(TransactionId tid,
                         PageId p)
Return true if the specified transaction has a lock on the specified page


transactionComplete

public void transactionComplete(TransactionId tid,
                                boolean commit)
                         throws java.io.IOException
Commit or abort a given transaction; release all locks associated to the transaction.

Parameters:
tid - the ID of the transaction requesting the unlock
commit - a flag indicating whether we should commit or abort
Throws:
java.io.IOException

insertTuple

public void insertTuple(TransactionId tid,
                        int tableId,
                        Tuple t)
                 throws DbException,
                        java.io.IOException,
                        TransactionAbortedException
Add a tuple to the specified table behalf of transaction tid. Will acquire a write lock on the page the tuple is added to(Lock acquisition is not needed for lab2). May block if the lock cannot be acquired. Marks any pages that were dirtied by the operation as dirty by calling their markDirty bit, and updates cached versions of any pages that have been dirtied so that future requests see up-to-date pages.

Parameters:
tid - the transaction adding the tuple
tableId - the table to add the tuple to
t - the tuple to add
Throws:
DbException
java.io.IOException
TransactionAbortedException

deleteTuple

public void deleteTuple(TransactionId tid,
                        Tuple t)
                 throws DbException,
                        TransactionAbortedException
Remove the specified tuple from the buffer pool. Will acquire a write lock on the page the tuple is removed from. May block if the lock cannot be acquired. Marks any pages that were dirtied by the operation as dirty by calling their markDirty bit. Does not need to update cached versions of any pages that have been dirtied, as it is not possible that a new page was created during the deletion (note difference from addTuple).

Parameters:
tid - the transaction adding the tuple.
t - the tuple to add
Throws:
DbException
TransactionAbortedException

flushAllPages

public void flushAllPages()
                   throws java.io.IOException
Flush all dirty pages to disk. NB: Be careful using this routine -- it writes dirty data to disk so will break simpledb if running in NO STEAL mode.

Throws:
java.io.IOException

discardPage

public void discardPage(PageId pid)
Remove the specific page id from the buffer pool. Needed by the recovery manager to ensure that the buffer pool doesn't keep a rolled back page in its cache.


flushPage

private void flushPage(PageId pid)
                throws java.io.IOException
Flushes a certain page to disk

Parameters:
pid - an ID indicating the page to flush
Throws:
java.io.IOException

flushPages

public void flushPages(TransactionId tid)
                throws java.io.IOException
Write all pages of the specified transaction to disk.

Throws:
java.io.IOException

evictPage

private void evictPage()
                throws DbException
Discards a page from the buffer pool. Flushes the page to disk to ensure dirty pages are updated on disk.

Throws:
DbException