/** Suck up tuples from the source file. */ private Tuple readNextTuple(DataInputStream dis, int slotId) throws NoSuchElementException { // if associated bit is not set, read forward to the next tuple, and // return null. if (!isSlotUsed(slotId)) { for (int i = 0; i < td.getSize(); i++) { try { dis.readByte(); } catch (IOException e) { throw new NoSuchElementException("error reading empty tuple"); } } return null; } // read fields in the tuple Tuple t = new Tuple(td); RecordId rid = new RecordId(pid, slotId); t.setRecordId(rid); try { for (int j = 0; j < td.numFields(); j++) { Field f = td.getFieldType(j).parse(dis); t.setField(j, f); } } catch (java.text.ParseException e) { e.printStackTrace(); throw new NoSuchElementException("parsing error!"); } return t; }
/** Unit test for Tuple.getRecordId() and Tuple.setRecordId() */ @Test public void modifyRecordId() { Tuple tup1 = new Tuple(Utility.getTupleDesc(1)); HeapPageId pid1 = new HeapPageId(0, 0); RecordId rid1 = new RecordId(pid1, 0); tup1.setRecordId(rid1); try { assertEquals(rid1, tup1.getRecordId()); } catch (java.lang.UnsupportedOperationException e) { // rethrow the exception with an explanation throw new UnsupportedOperationException( "modifyRecordId() test failed due to " + "RecordId.equals() not being implemented. This is not required for Lab 1, " + "but should pass when you do implement the RecordId class."); } }
/** * Adds the specified tuple to the page; the tuple should be updated to reflect that it is now * stored on this page. * * @throws DbException if the page is full (no empty slots) or tupledesc is mismatch. * @param t The tuple to add. */ public void insertTuple(Tuple t) throws DbException { // some code goes here // not necessary for lab1 RecordId targetrid = t.getRecordId(); if (getNumEmptySlots() == 0 || !td.equals(t.getTupleDesc())) { throw new DbException("Either page is full or tuple desc doesn't match"); } for (int i = 0; i < getNumTuples(); i++) { if (!isSlotUsed(i)) { markSlotUsed(i, true); RecordId rid = new RecordId(pid, i); t.setRecordId(rid); tuples[i] = t; return; } } }