simpledb.buffer
Class Buffer

java.lang.Object
  extended by simpledb.buffer.Buffer

public class Buffer
extends java.lang.Object

An individual buffer. A buffer wraps a page and stores information about its status, such as the disk block associated with the page, the number of times the block has been pinned, whether the contents of the page have been modified, and if so, the id of the modifying transaction and the LSN of the corresponding log record.

Author:
Edward Sciore

Constructor Summary
Buffer()
          Creates a new buffer, wrapping a new page.
 
Method Summary
(package private)  void assignToBlock(Block b)
          Reads the contents of the specified block into the buffer's page.
(package private)  void assignToNew(java.lang.String filename, PageFormatter fmtr)
          Initializes the buffer's page according to the specified formatter, and appends the page to the specified file.
 Block block()
          Returns a reference to the disk block that the buffer is pinned to.
(package private)  void flush()
          Writes the page to its disk block if the page is dirty.
 int getInt(int offset)
          Returns the integer value at the specified offset of the buffer's page.
 java.lang.String getString(int offset)
          Returns the string value at the specified offset of the buffer's page.
(package private)  boolean isModifiedBy(int txnum)
          Returns true if the buffer is dirty due to a modification by the specified transaction.
(package private)  boolean isPinned()
          Returns true if the buffer is currently pinned (that is, if it has a nonzero pin count).
(package private)  void pin()
          Increases the buffer's pin count.
 void setInt(int offset, int val, int txnum, int lsn)
          Writes an integer to the specified offset of the buffer's page.
 void setString(int offset, java.lang.String val, int txnum, int lsn)
          Writes a string to the specified offset of the buffer's page.
(package private)  void unpin()
          Decreases the buffer's pin count.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Buffer

public Buffer()
Creates a new buffer, wrapping a new page. This constructor is called exclusively by the class BasicBufferMgr. It depends on the LogMgr object that it gets from the class SimpleDB. That object is created during system initialization. Thus this constructor cannot be called until SimpleDB.initFileAndLogMgr(String) or is called first.

Method Detail

getInt

public int getInt(int offset)
Returns the integer value at the specified offset of the buffer's page. If an integer was not stored at that location, the behavior of the method is unpredictable.

Parameters:
offset - the byte offset of the page
Returns:
the integer value at that offset

getString

public java.lang.String getString(int offset)
Returns the string value at the specified offset of the buffer's page. If a string was not stored at that location, the behavior of the method is unpredictable.

Parameters:
offset - the byte offset of the page
Returns:
the string value at that offset

setInt

public void setInt(int offset,
                   int val,
                   int txnum,
                   int lsn)
Writes an integer to the specified offset of the buffer's page. This method assumes that the transaction has already written an appropriate log record. The buffer saves the id of the transaction and the LSN of the log record. A negative lsn value indicates that a log record was not necessary.

Parameters:
offset - the byte offset within the page
val - the new integer value to be written
txnum - the id of the transaction performing the modification
lsn - the LSN of the corresponding log record

setString

public void setString(int offset,
                      java.lang.String val,
                      int txnum,
                      int lsn)
Writes a string to the specified offset of the buffer's page. This method assumes that the transaction has already written an appropriate log record. A negative lsn value indicates that a log record was not necessary. The buffer saves the id of the transaction and the LSN of the log record.

Parameters:
offset - the byte offset within the page
val - the new string value to be written
txnum - the id of the transaction performing the modification
lsn - the LSN of the corresponding log record

block

public Block block()
Returns a reference to the disk block that the buffer is pinned to.

Returns:
a reference to a disk block

flush

void flush()
Writes the page to its disk block if the page is dirty. The method ensures that the corresponding log record has been written to disk prior to writing the page to disk.


pin

void pin()
Increases the buffer's pin count.


unpin

void unpin()
Decreases the buffer's pin count.


isPinned

boolean isPinned()
Returns true if the buffer is currently pinned (that is, if it has a nonzero pin count).

Returns:
true if the buffer is pinned

isModifiedBy

boolean isModifiedBy(int txnum)
Returns true if the buffer is dirty due to a modification by the specified transaction.

Parameters:
txnum - the id of the transaction
Returns:
true if the transaction modified the buffer

assignToBlock

void assignToBlock(Block b)
Reads the contents of the specified block into the buffer's page. If the buffer was dirty, then the contents of the previous page are first written to disk.

Parameters:
b - a reference to the data block

assignToNew

void assignToNew(java.lang.String filename,
                 PageFormatter fmtr)
Initializes the buffer's page according to the specified formatter, and appends the page to the specified file. If the buffer was dirty, then the contents of the previous page are first written to disk.

Parameters:
filename - the name of the file
fmtr - a page formatter, used to initialize the page