📄 bufmgr.java
字号:
/* File BufMgr,java */package bufmgr;import java.io.IOException;import diskmgr.DiskMgrException;import diskmgr.Page;import global.GlobalConst;import global.PageId;import global.SystemDefs;// *****************************************************/** The buffer manager class, it allocates new pages for the * buffer pool, pins and unpins the frame, frees the frame * page, and uses the replacement algorithm to replace the * page. */public class BufMgr implements GlobalConst { /** The hash table, only allocated once. */ private BufHashTbl hashTable = new BufHashTbl(); /** Total number of buffer frames in the buffer pool. */ private int numBuffers; /** physical buffer pool. */ private byte[][] bufPool; // default = byte[NUMBUF][MAX_SPACE]; /** An array of Descriptors one per frame. */ private FrameDesc[] frmeTable; // default = new FrameDesc[NUMBUF]; /** The replacer object, which is only used in this class. */ private Replacer replacer; /** * Create a buffer manager object. * * @param numbufs number of buffers in the buffer pool. * @param replacerArg name of the buffer replacement policy. */ public BufMgr(int numbufs, String replacerArg) { // type your code here // Note: readability of your code will be considered during marking } /** Check if this page is in buffer pool, otherwise * find a frame for this page, read in and pin it. * Also write out the old page if it's dirty before reading * if emptyPage==TRUE, then actually no read is done to bring * the page in. * * @param pin_pgid page number in the minibase. * @param page the pointer poit to the page. * @param emptyPage true (empty page); false (non-empty page) * * @exception ReplacerException if there is a replacer error. * @exception HashOperationException if there is a hashtable error. * @exception PageUnpinnedException if there is a page that is already unpinned. * @exception InvalidFrameNumberException if there is an invalid frame number . * @exception PageNotReadException if a page cannot be read. * @exception BufferPoolExceededException if the buffer pool is full. * @exception PagePinnedException if a page is left pinned . * @exception BufMgrException other error occured in bufmgr layer * @exception IOException if there is other kinds of I/O error. */ public void pinPage(PageId pin_pgid, Page page, boolean emptyPage) throws ReplacerException, HashOperationException, PageUnpinnedException, InvalidFrameNumberException, PageNotReadException, BufferPoolExceededException, PagePinnedException, BufMgrException, IOException { // type your code here // Note: readability of your code will be considered during marking } /** * To unpin a page specified by a pageId. *If pincount>0, decrement it and if it becomes zero, * put it in a group of replacement candidates. * if pincount=0 before this call, return error. * * @param PageId_in_a_DB page number in the minibase. * @param dirty the dirty bit of the frame * * @exception ReplacerException if there is a replacer error. * @exception PageUnpinnedException if there is a page that is already unpinned. * @exception InvalidFrameNumberException if there is an invalid frame number . * @exception HashEntryNotFoundException if there is no entry of page in the hash table. */ public void unpinPage(PageId PageId_in_a_DB, boolean dirty) throws ReplacerException, PageUnpinnedException, HashEntryNotFoundException, InvalidFrameNumberException { // type your code here // Note: readability of your code will be considered during marking } /** Call DB object to allocate a run of new pages and * find a frame in the buffer pool for the first page * and pin it. If buffer is full, ask DB to deallocate * all these pages and return error (null if error). * * @param firstpage the address of the first page. * @param howmany total number of allocated new pages. * @return the first page id of the new pages. * * @exception BufferPoolExceededException if the buffer pool is full. * @exception HashOperationException if there is a hashtable error. * @exception ReplacerException if there is a replacer error. * @exception HashEntryNotFoundException if there is no entry of page in the hash table. * @exception InvalidFrameNumberException if there is an invalid frame number. * @exception PageUnpinnedException if there is a page that is already unpinned. * @exception PagePinnedException if a page is left pinned. * @exception PageNotReadException if a page cannot be read. * @exception IOException if there is other kinds of I/O error. * @exception BufMgrException other error occured in bufmgr layer * @exception DiskMgrException other error occured in diskmgr layer */ public PageId newPage(Page firstpage, int howmany) throws BufferPoolExceededException, HashOperationException, ReplacerException, HashEntryNotFoundException, InvalidFrameNumberException, PagePinnedException, PageUnpinnedException, PageNotReadException, BufMgrException, DiskMgrException, IOException { // type your code here // Note: readability of your code will be considered during marking return null; // replace it by your code } /** User should call this method if she needs to delete a page. * this routine will call DB to deallocate the page. * * @param globalPageId the page number in the data base. * @exception InvalidBufferException if buffer pool corrupted. * @exception ReplacerException if there is a replacer error. * @exception HashOperationException if there is a hash table error. * @exception InvalidFrameNumberException if there is an invalid frame number. * @exception PageNotReadException if a page cannot be read. * @exception BufferPoolExceededException if the buffer pool is already full. * @exception PagePinnedException if a page is left pinned. * @exception PageUnpinnedException if there is a page that is already unpinned. * @exception HashEntryNotFoundException if there is no entry * of page in the hash table. * @exception IOException if there is other kinds of I/O error. * @exception BufMgrException other error occured in bufmgr layer * @exception DiskMgrException other error occured in diskmgr layer */ public void freePage(PageId globalPageId) throws InvalidBufferException, ReplacerException, HashOperationException, InvalidFrameNumberException, PageNotReadException, BufferPoolExceededException, PagePinnedException, PageUnpinnedException, HashEntryNotFoundException, BufMgrException, DiskMgrException, IOException { // type your code here // Note: readability of your code will be considered during marking } /** Added to flush a particular page of the buffer pool to disk * @param pageid the page number in the database. * * @exception HashOperationException if there is a hashtable error. * @exception PageUnpinnedException if there is a page that is already unpinned. * @exception PagePinnedException if a page is left pinned. * @exception PageNotFoundException if a page is not found. * @exception BufMgrException other error occured in bufmgr layer * @exception IOException if there is other kinds of I/O error. */ public void flushPage(PageId pageid) throws HashOperationException, PageUnpinnedException, PagePinnedException, PageNotFoundException, BufMgrException, IOException { // type your code here // Note: readability of your code will be considered during marking } /** Flushes all pages of the buffer pool to disk * @exception HashOperationException if there is a hashtable error. * @exception PageUnpinnedException if there is a page that is already unpinned. * @exception PagePinnedException if a page is left pinned. * @exception PageNotFoundException if a page is not found. * @exception BufMgrException other error occured in bufmgr layer * @exception IOException if there is other kinds of I/O error. */ public void flushAllPages() throws HashOperationException, PageUnpinnedException, PagePinnedException, PageNotFoundException, BufMgrException, IOException { // type your code here // Note: readability of your code will be considered during marking } /** Gets the total number of buffers. * * @return total number of buffer frames. */ public int getNumBuffers() { return numBuffers; } /** Gets the total number of unpinned buffer frames. * * @return total number of unpinned buffer frames. */ public int getNumUnpinnedBuffers() { return replacer.getNumUnpinnedBuffers(); } /** A few routines currently need direct access to the FrameTable. */ public FrameDesc[] frameTable() { return frmeTable; } /* The following code tells you how to call methods of DB class in BufMgr */ private void write_page(PageId pageno, Page page) throws BufMgrException { try { SystemDefs.JavabaseDB.write_page(pageno, page); } catch (Exception e) { throw new BufMgrException(e, "BufMgr.java: write_page() failed"); } } // end of write_page private void read_page(PageId pageno, Page page) throws BufMgrException { try { SystemDefs.JavabaseDB.read_page(pageno, page); } catch (Exception e) { throw new BufMgrException(e, "BufMgr.java: read_page() failed"); } } // end of read_page private void allocate_page(PageId pageno, int num) throws BufMgrException { try { SystemDefs.JavabaseDB.allocate_page(pageno, num); } catch (Exception e) { throw new BufMgrException(e, "BufMgr.java: allocate_page() failed"); } } // end of allocate_page private void deallocate_page(PageId pageno) throws BufMgrException { try { SystemDefs.JavabaseDB.deallocate_page(pageno); } catch (Exception e) { throw new BufMgrException(e, "BufMgr.java: deallocate_page() failed"); } } // end of deallocate_page}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -