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 TestCase2 {

	
	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);
		
		//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= DiskAddress.createAddressByOffset(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");
		}
		
		
		//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(1);
		if(add!=null)
		{
        //we should get pageid=3 and recno=1 (STATE 4)
			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=1 .
		//In the previous steps we put the end position as 20..Now lest change it to 30
		int i=SI.updateEndPosition(1,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=1");
		}
		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 with nodeId=1 (STATE 6)
		 i=SI.setContent(1,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 code tries to get the record in one of the structure pages given an 'PageId' and the 'record number'
		 //when we inserted the record, we got the DiskAddress. Lets use the same object as parameter here and see if we get back the same record
		 StructureIndexRecord rec1=SI.getRecordAddress(ret);
		 
		 
		 System.out.println("The returned record has the node id:"+rec1.nodeId); //if '1' is the nodeID, then we retreived the correct record
		
		 //this moehod is used to delete a record given a node It. lets try to delete the above record
		 i=SI.deleteEntry(rec1.nodeId);
		 
		 rec1=SI.getRecordAddress(ret);
		 if(rec1!=null)//assuming 'null' is returned when no record is found
		 {
			 
			 System.out.println("The record has not been deleted!");//that means there is some problem with the getRecordAddress stub
		 }
		 
		 
//		SI.close();//remove the comments if the file needs to be closed before exiting
		 
	}//end of main() method
	}//end of TestCase2 class