simpledb.file
Class Page

java.lang.Object
  extended by simpledb.file.Page

public class Page
extends Object

The contents of a disk block in memory. A page is treated as an array of BLOCK_SIZE bytes. There are methods to get/set values into this array, and to read/write the contents of this array to a disk block. For an example of how to use Page and Block objects, consider the following code fragment. The first portion increments the integer at offset 792 of block 6 of file junk. The second portion stores the string "hello" at offset 20 of a page, and then appends it to a new block of the file. It then reads that block into another page and extracts the value "hello" into variable s.

 Page p1 = new Page();
 Block blk = new Block("junk", 6);
 p1.read(blk);
 int n = p1.getInt(792);
 p1.setInt(792, n+1);
 p1.write(blk);

 Page p2 = new Page();
 p2.setString(20, "hello");
 blk = p2.append("junk");
 Page p3 = new Page();
 p3.read(blk);
 String s = p3.getString(20);
 

Author:
Edward Sciore

Field Summary
static int BLOCK_SIZE
          The number of bytes in a block.
static int INT_SIZE
          The size of an integer in bytes.
 
Constructor Summary
Page()
          Creates a new page.
 
Method Summary
 Block append(String filename)
          Appends the contents of the page to the specified file.
 int getInt(int offset)
          Returns the integer value at a specified offset of the page.
 String getString(int offset)
          Returns the string value at the specified offset of the page.
 void read(Block blk)
          Populates the page with the contents of the specified disk block.
 void setInt(int offset, int val)
          Writes an integer to the specified offset on the page.
 void setString(int offset, String val)
          Writes a string to the specified offset on the page.
static int STR_SIZE(int n)
          The maximum size, in bytes, of a string of length n.
 void write(Block blk)
          Writes the contents of the page to the specified disk block.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BLOCK_SIZE

public static final int BLOCK_SIZE
The number of bytes in a block. This value is set unreasonably low, so that it is easier to create and test databases having a lot of blocks. A more realistic value would be 4K.

See Also:
Constant Field Values

INT_SIZE

public static final int INT_SIZE
The size of an integer in bytes. This value is almost certainly 4, but it is a good idea to encode this value as a constant.

See Also:
Constant Field Values
Constructor Detail

Page

public Page()
Creates a new page. Although the constructor takes no arguments, it depends on a FileMgr object that it gets from the method SimpleDB.fileMgr(). That object is created during system initialization. Thus this constructor cannot be called until either SimpleDB.init(String) or SimpleDB.initFileMgr(String) or SimpleDB.initFileAndLogMgr(String) or SimpleDB.initFileLogAndBufferMgr(String) is called first.

Method Detail

STR_SIZE

public static final int STR_SIZE(int n)
The maximum size, in bytes, of a string of length n. A string is represented as the encoding of its characters, preceded by an integer denoting the number of bytes in this encoding. If the JVM uses the US-ASCII encoding, then each char is stored in one byte, so a string of n characters has a size of 4+n bytes.

Parameters:
n - the size of the string
Returns:
the maximum number of bytes required to store a string of size n

read

public void read(Block blk)
Populates the page with the contents of the specified disk block.

Parameters:
blk - a reference to a disk block

write

public void write(Block blk)
Writes the contents of the page to the specified disk block.

Parameters:
blk - a reference to a disk block

append

public Block append(String filename)
Appends the contents of the page to the specified file.

Parameters:
filename - the name of the file
Returns:
the reference to the newly-created disk block

getInt

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

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

setInt

public void setInt(int offset,
                   int val)
Writes an integer to the specified offset on the page.

Parameters:
offset - the byte offset within the page
val - the integer to be written to the page

getString

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

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

setString

public void setString(int offset,
                      String val)
Writes a string to the specified offset on the page.

Parameters:
offset - the byte offset within the page
val - the string to be written to the page


Copyright © 2011. All Rights Reserved.