package Cases;

import java.io.*;
import java.io.File;

import neustore.base.DBIndex;//because we create a object of index.java
import neustore.base.LRUBuffer;//because we create an object of it




public class Test_EI_full {

	
	public static void main(String[] args)throws IOException {
		
		//all the pages will be stored under this file name
		String filename1="TESTFILE";
		File file= new File(filename1);
		if(file.delete())
		{
			 System.out.println("The old file with that name has been deleted");
			 
		}
		else
		{
			System.out.println("The file was not found");
		}
		
		//'buf' is an object of XMLBuffer class. Passes 2 arguments  5=Buffer size and 120=page size in bytes
		XMLBuffer buf= new XMLBuffer(5,120); //since the lookup page holding the element names has a record of size=40 (Element name=32, 2 ints=8bytes , totals 40 bytes)
		//so the above page can store 3 record(maximum) but some space is reserved for the page header also. I assume that we hasve some space for page header , so we will be able to store only 2 recs
		
		//Creating an object of ElementIndex class and passing the 'buf'(buffer) , filename and boolean value 'true' (indicating a new file has to be created with the passed filename)
		
		ElementIndex EI = new ElementIndex(buf,filename1,1);
		
		
		//inserting an element named "root" and displaying the element id on console
		int id1=EI.insertElementName("root");
		
		//asuuming all id's are given numbers starting from 0 and negative numbers means something has gone wrong
		if(id1>=0)
		{
		System.out.println("The 'root' element has been assigned an id:"+id1);
		}
		else
		{
			System.out.println("The 'root' element has not been inserted");
		}
		
		
		int id12=EI.insertElementName("database");
		int id13=EI.insertElementName("overflow"); 
		
		System.out.println("The element overflow's id="+id13); //This should not be inserted as we assumed that the first page which holds the element names should be only one page
		
		System.out.println("the element having id=2 is "+EI.getElementName(2));//should display "database"
		System.out.println("the element having id=4 is "+EI.getElementName(4));//This element is not present at all.
		
		
		//The page that holds the Element entries has a size of 12 bytes(3 ints). So 120/12=10 record at max(if there is no page header)
		
		DiskAddress addr1=new DiskAddress(4,1);
		DiskAddress addr2=new DiskAddress(4,2);
		DiskAddress addr3=new DiskAddress(5,1);
		DiskAddress addr4=new DiskAddress(5,2);
		DiskAddress addr5=new DiskAddress(6,1);
		DiskAddress addr6=new DiskAddress(6,2);
		DiskAddress addr7=new DiskAddress(7,1);
		DiskAddress addr8=new DiskAddress(7,2);
		DiskAddress addr9=new DiskAddress(8,1);
		DiskAddress addr10=new DiskAddress(8,2);
		DiskAddress addr11=new DiskAddress(9,1);
		DiskAddress addr12=new DiskAddress(9,2);
		
		int exit=EI.insertElementEntry("root",addr1);
		exit=EI.insertElementEntry("root",addr2);//no overflow for this page
		
		
		exit=EI.insertElementEntry("database",addr3);
		exit=EI.insertElementEntry("database",addr4);
		exit=EI.insertElementEntry("database",addr5);
		exit=EI.insertElementEntry("database",addr6);
		exit=EI.insertElementEntry("database",addr7);
		exit=EI.insertElementEntryById(2,addr8);
		exit=EI.insertElementEntryById(2,addr9);
		exit=EI.insertElementEntryById(2,addr10);
		exit=EI.insertElementEntryById(2,addr11);
		exit=EI.insertElementEntryById(2,addr12); //This might cause an overflow, in case you allocated atleast 1 byte for page header
		
		//The following step is upto you to check if this causes an error
		//exit=EI.insertElementEntry("root",addr3);//This is should cause as error, because addr contains an element "database" 
		
		
		System.out.println("The number of entries for element 'database'= "+EI.getNumberEntries(EI.getElementId("database")));
		
		exit=EI.deleteElementEntry("root",addr1);
		exit=EI.deleteElementEntry("root",addr2); //this statement would remove all the elements of type "root".
		
	}//End of main() method

}//end of Test_EI_full class