Class DBFile

java.lang.Object
  extended by DBFile

public class DBFile
extends java.lang.Object

Low level database file. This abstraction allows the user to treat a database as a collection of pages.

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

Nested Class Summary
static class DBFile.BadPageNumberException
           
static class DBFile.EmptyFileException
           
static class DBFile.FileFullException
           
static class DBFile.NonPositiveRunSizeException
           
static class DBFile.PageNotAllocatedException
           
 
Constructor Summary
DBFile(java.lang.String name)
          Opens the database with the given name.
DBFile(java.lang.String name, int numPages)
          Creates a database with the specified number of pages.
 
Method Summary
 int allocatePages(int runSize)
          Allocates a set of pages.
 void deallocatePages(int startPageNum, int runSize)
          Deallocates a set of pages.
static boolean erase(java.lang.String name)
          Erases the database entirely from the filesystem.
static void main(java.lang.String[] args)
           
 void readPage(int pageNum, Page page)
          Reads the contents of the specified page from disk into the page object provided.
 void writePage(int pageNum, Page page)
          Writes the contents of the specified page to disk.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DBFile

public DBFile(java.lang.String name,
              int numPages)
       throws java.io.IOException
Creates a database with the specified number of pages. The number of pages in the database can never be increased.

Parameters:
name - name to be given to database.
numPages - maximum number of pages in database.
Throws:
java.io.IOException - passed through from underlying filesystem.

DBFile

public DBFile(java.lang.String name)
       throws java.io.IOException
Opens the database with the given name.

Parameters:
name - name of the database.
Throws:
java.io.IOException - passed through from underlying file system.
Method Detail

erase

public static boolean erase(java.lang.String name)
Erases the database entirely from the filesystem. Dangerous to do if still have a DBFile object that refers to this file.

Parameters:
name - name of the database.
Returns:
true if operation succeeded.

allocatePages

public int allocatePages(int runSize)
                  throws java.io.IOException
Allocates a set of pages.

Parameters:
runSize - number of pages to be allocated in the run.
Returns:
page number of the first page of the allocated run.
Throws:
DBFile.NonPositiveRunSizeException - if the run size is less than or equal to zero.
DBFile.FileFullException - if there are not enough free pages.
java.io.IOException - passed through from underlying file system.

deallocatePages

public void deallocatePages(int startPageNum,
                            int runSize)
                     throws java.io.IOException
Deallocates a set of pages. Does not ensure that the pages being deallocated are in fact allocated to begin with. If the pages were already deallocated, they remain so.

Parameters:
startPageNum - page number at the beginning of the run to be deallocated.
runSize - number of pages to deallocate.
Throws:
DBFile.NonPositiveRunSizeException - if the run size is less than or equal to zero.
DBFile.BadPageNumberException - if startPageNum is illegal.
java.io.IOException - passed through from underlying file system.

readPage

public void readPage(int pageNum,
                     Page page)
              throws java.io.IOException
Reads the contents of the specified page from disk into the page object provided.

Parameters:
pageNum - the page number to be read.
page - a reference to an already allocated Page object.
Throws:
DBFile.BadPageNumberException - if pageNum is not in the file.
java.io.IOException - passed through from underlying file system.
DBFile.PageNotAllocatedException - if pageNum is not allocaated.

writePage

public void writePage(int pageNum,
                      Page page)
               throws java.io.IOException
Writes the contents of the specified page to disk.

Parameters:
pageNum - the page number to be written.
page - a Page object with data to be written.
Throws:
EmptyFileException() - if the file has no pages within it.
DBFile.BadPageNumberException - if pageNum is not in the file.
java.io.IOException - passed through from underlying file system.
DBFile.PageNotAllocatedException - if pageNum is not allocaated.

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
Throws:
java.io.IOException