package Cases; import java.io.*; import ElementIndex.ElementIndex; import neustore.base.DiskAddress; 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 TestCase1 { 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 2048=page size in bytes LRUBuffer buf= new LRUBuffer (5,4096); //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"); } //The follwing code will return the id of the recently inserted element "root" int id2= EI.getElementId("root"); System.out.println("The 'root' element's id is "+id2); //this code will check if the id1 and id2 are equal, if they are then insertion and retreival are running good if(id1==id2) { System.out.println("Both the Ids are same. The Insertion and retreival works"); } else { System.out.println("There is some problem with the insertion or retrival"); } //When both the ids are equal and everything is running fine, we go ahead to test the next API //The follwing code will try to get an element name given an id String ename=EI.getElementName(id1); //this statement should return "root" System.out.println("The element which has an id="+id1+" is "+ename); if(ename!=null &&ename.equals("root") ) { System.out.println("getElementName method works fine as of now"); } else { System.out.println("The value returned by getElementName is not what we expected."); } //The following code will test the "getAddress(string name)" method DiskAddress da=EI.getAddress("root"); if(da!=null) { //make sure that the PageID and recno(if using dense index) are correct. //Normally , All the elements are stored in 1st page and since 'root' is the first element, its bucket shall start from pageid=2. However it depends on what scheme //you are following in assigning the pageIds. System.out.println("The PageId from which the records of element named 'root' starts is:"+da.PageId); //This statement should be considered by those who used dense index instead of buckets System.out.println("The Record number from which the records of element named 'root' starts in the pageID shown above is:"+da.RecordNumber); } //Following code Will insert a record in the pages(buckets) that hold the records of all the element appearances in the XML file (STATE 1) DiskAddress da2=new DiskAddress(3,1); //should insert a record in page2 , the tuple would be (1,3,1) as the first record in the bucket(or page 2) int i=EI.insertElementEntry("root",da2); System.out.println("An (root)element entry with Pageid=3 and recno=1 has been done and the returned exit status is "+i); //Follwing code will test the insertion of an element entry given an ID (STATE 2) //practically we cant find two 'root' elements. This is just for demo DiskAddress da3=new DiskAddress(3,2); //should insert a record in page2(if the space is still available) , the tuple would be (1,3,2) as the second record in the bucket(or page 2) i=EI.insertElementEntryById(id2,da3);//passing the id of 'root' and the disk adress //Follwing method would tell us if the insertion of element entries went as planned i=EI.getNumberEntries(id2); if(i==2)//because we inserted only 2 record in the elements page { System.out.println("The element entries have been inserted. No of entries for Element with id="+id2+ " is "+i); } else { System.out.println("No of entries is "+id2+ " and we are supposed to get '2'"); } //The follwing call would try to get the adress of the ith entry of an element DiskAddress da4=EI.getElementEntry(id2,2);//this should return the address of the 2nd record in the element entries System.out.println("The PageId of the 2nd record entry for element with id="+id2+" is "+da4.PageId); System.out.println("The record number of the 2nd record entry for element with id="+id2+" is "+da4.RecordNumber); //Next we try to delete one of the entry by name int k=EI.deleteElementEntryById(1,da2);//This is delete the "root" elements first int id6=EI.insertElementName("database"); // The following code will test the "getAddress(string name)" method DiskAddress da10=EI.getAddress("database"); id6=EI.deleteElementEntry("database",da10); // EI.close();//remove the comments if the file needs to be closed before exiting }//End of main() method }//end of TestCase1 class