package db.modules.dataStructure;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import db.Utils;
import db.modules.descriptors.ColumnDescriptor;
import db.modules.fs.FileSystem;
import db.modules.metaStructure.DataBlockHeader;

public class DataBlock {
	private Table parent;
	private DataBlockHeader header;
	private Iterator<ColumnDescriptor> columns;
	private List<Tuple> tuples;
	
	public DataBlock(Table parent, int blockId) {
		this.parent = parent;
		this.header = new DataBlockHeader(parent, blockId);
		this.columns = this.parent.getColumns().iterator();
		this.tuples = new ArrayList<Tuple>();
	}
	
	public boolean availableSpaceFor(int tupleSize) {
		return this.header.getUsedSpace() + 8 + tupleSize <= FileSystem.pageSize;
	}

	public boolean save() throws IOException {
		return header.save();
	}
	
	public boolean load() throws IOException {
		return header.load();
	}
	
	public void printTuples() {
		for (Tuple tuple : tuples) {
			tuple.print();
		}
	}
	
	public boolean loadTuples() throws IOException {
		this.tuples = new ArrayList<Tuple>();
		
		int zero = this.header.readStart();
		int spaceToRead = this.header.getUsedSpace() + zero;
		int cursor = zero;

		while (cursor < spaceToRead) {
			cursor += readTuple(cursor);
		}
		
		return true;
	}
	
	private int readTuple(int cursor) throws IOException {
		Tuple tp = new Tuple();
		cursor = tp.load(parent, cursor);;
		tuples.add(tp);
		return cursor;
	}
	
	public boolean insert(Tuple tuple) throws IOException {
		int currentUsedSpace = this.header.writeStart();
		byte[] tpBytes = tuple.inBytes();
		
		RandomAccessFile file =  this.parent.getContainerFile();
		
		file.seek(currentUsedSpace);
		file.write(tpBytes);
		
		this.header.updateUsedSpace(this.header.getUsedSpace() + tuple.size());
		
		return true;
	}
}

@@ -138,106 +138,14 @@
 til.
-Iterator;%0Aimport java.util.List;%0A%0Aimport db.Utils;%0Aimport db.modules.descriptors.ColumnDescriptor;
+List;%0A
 %0Aimp
@@ -309,53 +309,8 @@
 er;%0A
-%09private Iterator%3CColumnDescriptor%3E columns;%0A
 %09pri
@@ -465,62 +465,8 @@
 d);%0A
-%09%09this.columns = this.parent.getColumns().iterator();%0A
 %09%09th

remove unused imports