/** * Searches the directory for a data page with enough free space to store a record of the given * size. If no suitable page is found, this creates a new data page. */ protected PageId getAvailPage(int reclen) { for (int i = 0; i < pages.size(); i++) { PageId pid = pages.get(i); Page page = new Page(); global.Minibase.BufferManager.pinPage(pid, page, false); HFPage hfpage = new HFPage(); hfpage.copyPage(page); if (hfpage.getFreeSpace() >= reclen) { global.Minibase.BufferManager.unpinPage(pid, true); return pid; } global.Minibase.BufferManager.unpinPage(pid, false); } Page page = new Page(); PageId pid = global.Minibase.BufferManager.newPage(page, 1); HFPage hfpage = new HFPage(page); // initialize HFPage hfpage.setCurPage(pid); pages.add(pid); pids.add(pid.pid); current.setNextPage(pid); hfpage.setPrevPage(current.getCurPage()); current = hfpage; global.Minibase.BufferManager.unpinPage(pid, true); return pid; }
/** * Deletes the specified record from the heap file. * * @throws IllegalArgumentException if the rid is invalid */ public boolean deleteRecord(RID rid) { PageId pid = rid.pageno; if (!pids.contains(pid.pid)) throw new IllegalArgumentException("the rid is invalid"); try { Page page = new Page(); global.Minibase.BufferManager.pinPage(pid, page, false); HFPage hfpage = new HFPage(); hfpage.copyPage(page); hfpage.deleteRecord(rid); global.Minibase.BufferManager.unpinPage(pid, true); recordNumber--; return true; } catch (Exception e) { System.err.print("fail to delete record"); return false; } }