@Test public void new_fieldsTest() { int length = 10; String name = "td"; Tuple t = new Tuple(Utility.getTupleDesc(length, name)); Iterator<Field> fs = t.fields(); int i = 0; while (fs.hasNext()) { i++; fs.next(); } assertEquals(length, i); }
/** Unit test for fields() iterator */ @Test public void fields() { int numfields = 4; TupleDesc td = Utility.getTupleDesc(numfields); // set up the tuple Tuple tup = new Tuple(td); for (int i = 0; i < numfields; i++) tup.setField(i, new IntField(i)); // use the iterator, make sure get the same number of fields out Iterator<Field> iter = tup.fields(); int count = 0; while (iter.hasNext()) { iter.next(); count++; } assertEquals(numfields, count); }
public static void writeHeapFile( ArrayList<Tuple> tuples, File outFile, int npagebytes, Type[] typeAr) throws IOException { int nrecbytes = 0; for (int i = 0; i < typeAr.length; i++) { nrecbytes += typeAr[i].getLen(); } int maxNumRecordsInAPage = (npagebytes * 8) / (nrecbytes * 8 + 1); // floor comes // for free // per record, we need one bit; there are nrecords per page, so we need // nrecords bits, i.e., ((nrecords/32)+1) integers. int nheaderbytes = (maxNumRecordsInAPage / 8); if (nheaderbytes * 8 < maxNumRecordsInAPage) nheaderbytes++; // ceiling int nheaderbits = nheaderbytes * 8; FileOutputStream os = new FileOutputStream(outFile); int npages = 0; int numRecordInCurrPage = 0; int totalRecordCount = 0; ByteArrayOutputStream headerBAOS = new ByteArrayOutputStream(nheaderbytes); DataOutputStream headerStream = new DataOutputStream(headerBAOS); ByteArrayOutputStream pageBAOS = new ByteArrayOutputStream(npagebytes); DataOutputStream pageStream = new DataOutputStream(pageBAOS); for (Tuple t : tuples) { int fieldNo = 0; numRecordInCurrPage++; totalRecordCount++; Iterator<Field> it = t.fields(); while (it.hasNext()) { Field f = it.next(); if (typeAr[fieldNo] == Type.INT_TYPE) { IntField i = (IntField) f; pageStream.writeInt(i.getValue()); } else if (typeAr[fieldNo] == Type.STRING_TYPE) { StringField sf = (StringField) f; String s = sf.getValue(); int overflow = Type.STRING_LEN - s.length(); if (overflow < 0) { String news = s.substring(0, Type.STRING_LEN); s = news; } pageStream.writeInt(s.length()); pageStream.writeBytes(s); while (overflow-- > 0) pageStream.write((byte) 0); } fieldNo++; } // if we wrote a full page of records, or if we're done altogether, // write out the header of the page. // // in the header, write a 1 for bits that correspond to records // we've // written and 0 for empty slots. // // when we're done, also flush the page to disk, but only if it has // records on it. however, if this file is empty, do flush an empty // page to disk. if (numRecordInCurrPage >= maxNumRecordsInAPage || totalRecordCount == tuples.size() && numRecordInCurrPage > 0 || totalRecordCount == tuples.size() && npages == 0) { int i = 0; byte headerbyte = 0; for (i = 0; i < nheaderbits; i++) { if (i < numRecordInCurrPage) headerbyte |= (1 << (i % 8)); if (((i + 1) % 8) == 0) { headerStream.writeByte(headerbyte); headerbyte = 0; } } if (i % 8 > 0) headerStream.writeByte(headerbyte); // pad the rest of the page with zeroes for (i = 0; i < (npagebytes - (numRecordInCurrPage * nrecbytes + nheaderbytes)); i++) pageStream.writeByte(0); // write header and body to file headerStream.flush(); headerBAOS.writeTo(os); pageStream.flush(); pageBAOS.writeTo(os); // reset header and body for next page headerBAOS = new ByteArrayOutputStream(nheaderbytes); headerStream = new DataOutputStream(headerBAOS); pageBAOS = new ByteArrayOutputStream(npagebytes); pageStream = new DataOutputStream(pageBAOS); numRecordInCurrPage = 0; npages++; } } os.close(); }