Question: Complete the TODO in below java code: package heapdb; import java.util.Iterator; import java.util.ArrayList; import java.nio.ByteBuffer; public class TableHeap implements ITable { BlockedFile bfile; Schema schema;

Complete the TODO in below java code: package heapdb;
import java.util.Iterator;
import java.util.ArrayList;
import java.nio.ByteBuffer;
public class TableHeap implements ITable {
BlockedFile bfile;
Schema schema;
BitMap bitmap; //0=free, 1=row
int rows_per_block;
Index[] indexes;
public TableHeap(String filename, Schema schema){
this.schema = schema;
// create a new file
// write schema to block 0
// write free space bitmap to block 1
// data rows start at block 2
bfile = new BlockedFile(filename, true);
ByteBuffer buffer = ByteBuffer.wrap(new byte[4096]);
buffer.position(8);
schema.serialize(buffer);
// set bytes used in the buffer.
buffer.putInt(0, buffer.position());
bfile.writeBlock(0, buffer);
// write out BitMap of all 0's
bitmap = new BitMap();
bfile.writeBlock(1, bitmap.getBytes());
rows_per_block =(4096-8)/schema.getTupleSizeInBytes();
indexes = new Index[schema.size()];
}
public TableHeap(String filename){
// open existing file
bfile = new BlockedFile(filename, false);
// read the schema from block 0
ByteBuffer buffer = ByteBuffer.wrap(new byte[4096]);
bfile.readBlock(0, buffer);
buffer.limit(buffer.getInt(0));
buffer.position(8);
schema = Schema.deserialize(buffer);
// read the bitmap from block 1
byte[] bytes = new byte[4096];
bfile.readBlock(1, bytes);
bitmap = new BitMap(bytes);
rows_per_block =(4096-8)/schema.getTupleSizeInBytes();
indexes = new Index[schema.size()];
}
@Override
public void close(){
if (bfile.isOpen()){
// write the bitmap
bfile.writeBlock(1, bitmap.getBytes());
bfile.close();
}
}
@Override
public Iterator iterator(){
return new TupleIterator();
}
public class TupleIterator implements Iterator {
private int row_no =-1;
private int current_block_no =0;
private ByteBuffer buffer = ByteBuffer.wrap(new byte[4096]);
@Override
public boolean hasNext(){
for (row_no=row_no+1; row_no<4096*8; row_no++){
if (bitmap.getBit(row_no)){
return true;
}
}
return false;
}
@Override
public Tuple next(){
// read row
int block_no = row_no/rows_per_block +2;
int offset =(row_no%rows_per_block)* schema.getTupleSizeInBytes();
// read the block
if (current_block_no != block_no){
// read the block
bfile.readBlock(block_no, buffer);
current_block_no = block_no;
}
// get the tuple
buffer.position(offset);
Tuple t = Tuple.deserialize(schema, buffer);
return t;
}
private int getRowNo(){
return row_no;
}
}
@Override
public Schema getSchema(){
return schema;
}
/*
* return the number of rows in table
*/
@Override
public int size(){
int size=0;
for (int i=0; i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!