package db.modules.metaStructure;

import java.io.IOException;
import java.io.RandomAccessFile;

import db.Utils;
import db.modules.dataStructure.Table;
import db.modules.fs.FileSystem;

public class DataBlockHeader {
	private Table parent;
	private byte container;
	private byte[] blockId = new byte[3];
	private byte blockType = 1;
	private byte[] usedSpace = new byte[3];
	private final int blockBeginning;
	
	public DataBlockHeader(Table parent, int blockId) {
		this.parent = parent;
		this.container = this.parent.getContainer();
		this.blockId = Utils.toByteArray(blockId, 3);
		this.usedSpace = Utils.toByteArray(8, 3);
		this.blockBeginning = FileSystem.pageSize * blockId;
	}
	
	public boolean save() throws IOException {
		RandomAccessFile file = parent.getContainerFile();
		
		file.seek(blockBeginning);
		file.write(container);
		
		file.seek(blockBeginning + 1);
		file.write(blockId);
		
		file.seek(blockBeginning + 4);
		file.write(blockType);
		
		file.seek(blockBeginning + 5);
		file.write(usedSpace);
		
		return true;
	}
	
	public int writeStart() {
		return this.blockBeginning + Utils.toInt(usedSpace, 0);
	}
	
}

@@ -675,25 +675,24 @@
 blockId;%0A%09%7D%0A
-%09
 %0A%09public boo
@@ -777,18 +777,16 @@
 File();%0A
-%09%09
 %0A%09%09file.
@@ -832,18 +832,16 @@
 ainer);%0A
-%09%09
 %0A%09%09file.
@@ -889,18 +889,16 @@
 ockId);%0A
-%09%09
 %0A%09%09file.
@@ -948,18 +948,16 @@
 kType);%0A
-%09%09
 %0A%09%09file.
@@ -1011,119 +1011,779 @@
 e);%0A
+%0A
 %09%09
-%0A%09%09return true;%0A%09%7D%0A%09%0A%09public int writeStart() %7B%0A%09%09return this.blockBeginning + Utils.toInt(usedSpace, 0);%0A%09%7D%0A%09
+return true;%0A%09%7D%0A%0A%09public boolean load() throws IOException %7B%0A%09%09RandomAccessFile file = parent.getContainerFile();%0A%0A%09%09file.seek(blockBeginning);%0A%09%09this.container = file.readByte();%0A%0A%09%09file.seek(blockBeginning + 1);%0A%09%09file.read(this.blockId);%0A%0A%09%09file.seek(blockBeginning + 4);%0A%09%09this.blockType = file.readByte();%0A%0A%09%09file.seek(blockBeginning + 5);%0A%09%09file.read(this.usedSpace);%0A%0A%09%09return true;%0A%09%7D%0A%09%0A%09public int writeStart() %7B%0A%09%09return this.blockBeginning + Utils.toInt(usedSpace, 0);%0A%09%7D%0A%09%0A%09public void print() %7B%0A%09%09System.out.println(%22%5CtCONTAINER: %22 + this.container);%0A%09%09System.out.println(%22%5CtID DO BLOCO: %22 + Utils.toInt(blockId, 0));%0A%09%09System.out.println(%22%5CtTIPO DO BLOCK: %22 + this.blockType);%0A%09%09System.out.println(%22%5CtESPA%C3%87O USADO: %22 + Utils.toInt(this.usedSpace, 0));%0A%09%7D%0A
 %0A%7D%0A

add method load to data block header