// see DbFile.java for javadocs public ArrayList<Page> insertTuple(TransactionId tid, Tuple t) throws DbException, IOException, TransactionAbortedException { // some code goes here BufferPool bp = Database.getBufferPool(); int id = getId(), i, slots; ArrayList<Page> retlist = new ArrayList<Page>(); PageId pid = null; HeapPage p = null; for (i = 0; i < numPages(); i++) { pid = new HeapPageId(id, i); p = (HeapPage) bp.getPage(tid, pid, Permissions.READ_WRITE); slots = p.getNumEmptySlots(); if (slots > 0) { p.insertTuple(t); retlist.add(p); return retlist; } } // create new page and add tuple to it pid = new HeapPageId(id, i); raf.setLength(raf.length() + BufferPool.PAGE_SIZE); p = (HeapPage) bp.getPage(tid, pid, Permissions.READ_WRITE); p.insertTuple(t); retlist.add(p); return retlist; }
/** Unit test for HeapFile.readPage() */ @Test public void readPage() throws Exception { HeapPageId pid = new HeapPageId(hf.getId(), 0); HeapPage page = (HeapPage) hf.readPage(pid); // NOTE(ghuo): we try not to dig too deeply into the Page API here; we // rely on HeapPageTest for that. perform some basic checks. assertEquals(484, page.getNumEmptySlots()); assertTrue(page.isSlotUsed(1)); assertFalse(page.isSlotUsed(20)); }