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 Testcase10 { 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 //depending on what has been spcified in class by Dr.Dekhtyar, you can modify this statement XMLBuffer buf= new XMLBuffer(5,2048); //Those who just pass numPages. uncomment this and comment the previous one /* XMLBuffer buf= new XMLBuffer(5);*/ //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"); //as we assumed only 1 page to hold the element names, so after the page is full, the returned exit code can be something different from failure //these are dummy values. Actually we cant find duplicates in this first page. for(int i=0;i<1000;i++) { id1=EI.insertElementName("root"+ i); //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 and the exit code is:"+id1); } } //The follwing code will return the id of the recently inserted element "root" //Since we inserted many duplicates, this can return any of their id. int id2= EI.getElementId("root"); System.out.println("The 'root' element's id is "+id2); //The follwing code will try to get an element name given an id String ename=EI.getElementName(id2); //this statement should return "root" System.out.println("The element which has an id="+id2+" 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= DiskAddress.createAddressByRN(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= DiskAddress.createAddressByRN(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 //lets insert some thousands of dummy values //this would insert a record for each id assigned till 1000. If the page was full and 1000 element id's were not assigned , then the exit code sould tell that something has gone wrong for(int j=1;j<1000;j++) //would start inserting (1,2,3) to (999,1000,1001) { da3= DiskAddress.createAddressByRN(j+1,j+2); i=EI.insertElementEntryById(j,da3); System.out.println("Exit code="+i+" for insertion of ("+(j)+","+(j+1)+","+(j+2)+")"); } //inserting 1000 records in a single id's(1) bucket. for(int j=1;j<1000;j++) //would start inserting (1,2,3) to (1,1000,1001) { da3= DiskAddress.createAddressByRN(j+1,j+2); i=EI.insertElementEntryById(1,da3); System.out.println("Exit code="+i+" for insertion of ("+(1)+","+(j+1)+","+(j+2)+")"); } //The follwing call would try to get the adress of the 1st entry of each element and we inserted atleast one record for all entries DiskAddress da4=new DiskAddress(-1,-1); for(i=0;i<500;i++) { da4=EI.getElementEntry(i,1);//this should return the address of the 2nd record in the element entries System.out.println("The PageId of the 1st record entry for element with id="+i+" is "+da4.PageId); System.out.println("The record number of the 2nd record entry for element with id="+i+" is "+da4.RecordNumber); } //Next we try to delete one of the entry by name int k; for(i=400;i<800;i++) { da2= DiskAddress.createAddressByRN(i,i+1); k=EI.deleteElementEntryById(1,da2);//This is delete the "root" elements first } da4=EI.getElementEntry(1,450);//this should return the address of the 450thnd record in the element entries System.out.println("The PageId of the 450th record entry for element with id="+i+" is "+da4.PageId); System.out.println("The record number of the 450th record entry for element with id="+i+" is "+da4.RecordNumber); //since we deleted the 400 to 800 range records, we get the different values here, if record sliding has been used // EI.close();//remove the comments if the file needs to be closed before exiting }//End of main() method }//end of TestCase1 class