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


//NOTE: These are just for testing, so the values do not make sense. 
//You can change the test file as you want, ex: the for loop can be run for 10000 times.
//the XML buffer delaration can be added, if you want to use the one which takes in only the numpages.


public class Testcase20 {

	
	public static void main(String[] args)throws IOException {
		
		//all the pages will be stored under this file name
		String filename2="TESTFILE2";
		File file= new File(filename2);
		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. Passews 2 arguments  5=Buffer size and 2048=page size in bytes
		XMLBuffer buf2= new XMLBuffer(5,2048);
		
//		Those who just pass numPages. uncomment this and comment the previous one
		/*	XMLBuffer buf2= new XMLBuffer(5);*/
		
		//Creating an object of StructureIndex class and passing the 'buf2'(buffer) , filename and boolean value 'true' (indicating a new file has to be created with the passed filename)
		//If we do not plan to create a new file for Structure index, then the Constructor of the StructureIndex class should send 'false' to its super class
		
		
		//creating an object of StructureIndex class to test its methods
		StructureIndex SI = new StructureIndex(buf2,filename2,1);
		
		//creating a record of StructureIndexrecord class 
		DiskAddress ptr=new DiskAddress(4,0);//this means the content for this element starts from 0th byte in page with id '4'
		StructureIndexRecord record=new StructureIndexRecord(1,1,0,20,0,0,-1,1,ptr);
		
		//lets try to insert the above created record in the structure page (STATE 5)
		DiskAddress ret= SI.insertEntry(record); 
		if(ret!=null)
		{
			//we should get pageid=3 and recno=1 (STATE 4)
			System.out.println("The adress of the recently inserted record in structure page is : PageId="+ret.PageId+" and Recno:"+ret.RecordNumber);
		}
		else //assuming that 'null' is returned when something goes wrong
		{
			System.out.println("The Structure Index record has not been inserted");
		}
		
		
		for(int i=3;i<1000;i++) //this loop insertes 1000 records and nodeids are assigned
		{

			record=new StructureIndexRecord(i,1,1,1,1,1,1,1,ptr);
			ret= SI.insertEntry(record); 
			if(ret!=null)
			{
				
				System.out.println("The adress of the recently inserted record in structure page is : PageId="+ret.PageId+" and Recno:"+ret.RecordNumber);
			}
			else //assuming that 'null' is returned when something goes wrong
			{
				System.out.println("The Structure Index record has not been inserted");
			}
		}
		
		
		
		
		//This code tries to get the address of the record given an id, since we inserted a record with Nodeid=1, try lets try to get that
		DiskAddress add= SI.getAddress(2);//there might be a record of this id
		if(add!=null)
		{
        
			System.out.println("The record with nodeid=1 is in page="+add.PageId+" and recno="+add.RecordNumber);
		}
		else
		{
			System.out.println("could not retrieve the address given a node id");
		}
		
		
		
		//The following code will try to update the end position of the record having Nodeid=550.
		//In the previous steps we put the end position as 1..Now lest change it to 30
		int i=SI.updateEndPosition(550,30);
		
		if(i>=0)//asumming the exit codes are negatives when something goes wrong
		{
			System.out.println("The end position has been updated to 30 for nodeid=550");
		}
		else
		{
			System.out.println("There was some error in updating the End position");
		}
		
		
		
		//The following method will attempt to update the pointer to the content of the XML node. 
		//Last time we used 'ptr' variable(4,0) to store the pointer. This time we will update the pointer by changing the starting position of content =5
	     ptr=new DiskAddress(4,0);
		
	     //This will atempt to update the pointer of the record 
	     
	     for(int j=3;j<400;j++)
	     {
		 i=SI.setContent(j,ptr);
		 
		 System.out.println("The exit status after updating the pointer using setContent method is:"+i);
	     }
		 
		 //This code will try to get the entire record with ndeid=1 and store it in a object of StructureIndexRecord
		 
		 StructureIndexRecord rec=SI.getRecordById(1);
		 System.out.println("The returned record has the node id:"+rec.nodeId);
		 
		
		 //this moehod is used to delete a record given a node It. lets try to delete the records with node id's 
		 for(int j=450;j<800;j++)
	     {
		 i=SI.deleteEntry(j);
	     }
		 
		 //you need to check if the records were indeed deleted
		 
//		SI.close();//remove the comments if the file needs to be closed before exiting
		 
	}//end of main() method
	}//end of TestCase20 class