db.java
来自「minibase diskmanger buffermanger」· Java 代码 · 共 345 行
JAVA
345 行
package diskmgr;import java.io.*;import bufmgr.*;import global.*;public class DB implements GlobalConst { /** the number of bit in one page */ private static final int bits_per_page = MINIBASE_PAGESIZE * 8; /** the database file */ private RandomAccessFile fp; /** total number of page in the database file */ private int num_pages; /** name of the database file */ private String name; /** Functions to return some characteristics of the database. */ public String db_name() { return name; } // end of db_name public int db_num_pages() { return num_pages; } // end of db_num_pages public int db_page_size() { return MINIBASE_PAGESIZE; } // end of db_page_size /** default constructor. */ public DB() { } // end of DB /** DB Constructors. * Create a database with the specified number of pages where the page * size is the default page size. * * @param name DB name * @param num_pages number of pages in DB * * @exception IOException I/O errors * @exception InvalidPageNumberException invalid page number * @exception FileIOException file I/O error * @exception DiskMgrException error caused by other layers */ public void openDB(String fname, int num_pgs) throws IOException, InvalidPageNumberException, FileIOException, DiskMgrException { // type your code here // Note: readability of your code will be considered during marking } // end of openDB /** Open the database with the given name. * * @param name DB_name * * @exception IOException I/O errors * @exception FileIOException file I/O error * @exception InvalidPageNumberException invalid page number * @exception DiskMgrException error caused by other layers */ public void openDB(String fname) throws IOException, InvalidPageNumberException, FileIOException, DiskMgrException { // type your code here // Note: readability of your code will be considered during marking } // end of openDB /** Close DB file. * @exception IOException I/O errors. */ public void closeDB() throws IOException { fp.close(); } // end of closeDB /** Destroy the database, removing the file that stores it. * @exception IOException I/O errors. */ public void DBDestroy() throws IOException { fp.close(); File DBfile = new File(name); DBfile.delete(); } // end of DBDestroy /** Allocate a set of pages where the run size is taken to be 1 by default. * Gives back the page number of the first page of the allocated run. * with default run_size =1 * * @param start_page_num page number to start with * * @exception OutOfSpaceException database is full * @exception InvalidRunSizeException invalid run size * @exception InvalidPageNumberException invalid page number * @exception FileIOException DB file I/O errors * @exception IOException I/O errors * @exception DiskMgrException error caused by other layers */ public void allocate_page(PageId start_page_num) throws OutOfSpaceException, InvalidRunSizeException, InvalidPageNumberException, FileIOException, DiskMgrException, IOException { allocate_page(start_page_num, 1); } // allocate_page /** user specified run_size * * @param start_page_num the starting page id of the run of pages * @param run_size the number of page need allocated * * @exception OutOfSpaceException No space left * @exception InvalidRunSizeException invalid run size * @exception InvalidPageNumberException invalid page number * @exception FileIOException file I/O error * @exception IOException I/O errors * @exception DiskMgrException error caused by other layers */ public void allocate_page(PageId start_page_num, int run_size) throws OutOfSpaceException, InvalidRunSizeException, InvalidPageNumberException, FileIOException, DiskMgrException, IOException { // type your code here // Note: readability of your code will be considered during marking } // end of allocate_page /** Deallocate a set of pages starting at the specified page number * with run size = 1 * * @param start_page_num the start pageId to be deallocate * @param run_size the number of pages to be deallocated * * @exception InvalidRunSizeException invalid run size * @exception InvalidPageNumberException invalid page number * @exception FileIOException file I/O error * @exception IOException I/O errors * @exception DiskMgrException error caused by other layers * */ public void deallocate_page(PageId start_page_num) throws InvalidRunSizeException, InvalidPageNumberException, IOException, FileIOException, DiskMgrException { deallocate_page(start_page_num, 1); } // end of deallocate_page /** Deallocate a set of pages starting at the specified page number and * a run size can be specified. * * @param start_page_num the start pageId to be deallocate * @param run_size the number of pages to be deallocated * * @exception InvalidRunSizeException invalid run size * @exception InvalidPageNumberException invalid page number * @exception FileIOException file I/O error * @exception IOException I/O errors * @exception DiskMgrException error caused by other layers */ public void deallocate_page(PageId start_page_num, int run_size) throws InvalidRunSizeException, InvalidPageNumberException, IOException, FileIOException, DiskMgrException { // type your code here // Note: readability of your code will be considered during marking } // deallocate_page /** Adds a file entry to the header page(s). * * @param fname file entry name * @param start_page_num the start page number of the file entry * * @exception FileNameTooLongException invalid file name (too long) * @exception InvalidPageNumberException invalid page number * @exception InvalidRunSizeException invalid DB run size * @exception DuplicateEntryException entry for DB is not unique * @exception OutOfSpaceException database is full * @exception FileIOException file I/O error * @exception IOException I/O errors * @exception DiskMgrException error caused by other layers */ public void add_file_entry(String fname, PageId start_page_num) throws FileNameTooLongException, InvalidPageNumberException, InvalidRunSizeException, DuplicateEntryException, OutOfSpaceException, FileIOException, IOException, DiskMgrException { // type your code here // Note: readability of your code will be considered during marking } // end of add_file_entry /** Delete the entry corresponding to a file from the header page(s). * * @param fname file entry name * * @exception FileEntryNotFoundException file does not exist * @exception FileIOException file I/O error * @exception IOException I/O errors * @exception InvalidPageNumberException invalid page number * @exception DiskMgrException error caused by other layers */ public void delete_file_entry(String fname) throws FileEntryNotFoundException, IOException, FileIOException, InvalidPageNumberException, DiskMgrException { // type your code here // Note: readability of your code will be considered during marking } // end of delete_file_entry /** Get the entry corresponding to the given file. * * @param name file entry name * * @exception IOException I/O errors * @exception FileIOException file I/O error * @exception InvalidPageNumberException invalid page number * @exception DiskMgrException error caused by other layers */ public PageId get_file_entry(String name) throws IOException, FileIOException, InvalidPageNumberException, DiskMgrException { // type your code here // Note: readability of your code will be considered during marking return null; } // end of get_file_entry /** Read the contents of the specified page into a Page object * * @param pageno pageId which will be read * @param apage page object which holds the contents of page * * @exception InvalidPageNumberException invalid page number * @exception FileIOException file I/O error * @exception IOException I/O errors */ public void read_page(PageId pageno, Page apage) throws InvalidPageNumberException, FileIOException, IOException { // type your code here // Note: readability of your code will be considered during marking } // end of read_page /** Write the contents in a page object to the specified page. * * @param pageno pageId will be wrote to disk * @param apage the page object will be wrote to disk * * @exception InvalidPageNumberException invalid page number * @exception FileIOException file I/O error * @exception IOException I/O errors */ public void write_page(PageId pageno, Page apage) throws InvalidPageNumberException, FileIOException, IOException { // type your code here // Note: readability of your code will be considered during marking } // end of write_page /** short cut to access the pinPage function in bufmgr package. * * @see bufmgr.pinPage */ private void pinPage(PageId pageno, Page page, boolean emptyPage) throws DiskMgrException { try { SystemDefs.JavabaseBM.pinPage(pageno, page, emptyPage); } catch(Exception e) { throw new DiskMgrException(e, "DB.java:pinPage() failed"); } } // end of pinPage /** short cut to access the unpinPage function in bufmgr package. * * @see bufmgr.unpinPage */ private void unpinPage(PageId pageno, boolean dirty) throws DiskMgrException { try { SystemDefs.JavabaseBM.unpinPage(pageno, dirty); } catch(Exception e) { throw new DiskMgrException(e, "DB.java:unpinPage() failed"); } } // end of unpinPage}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?