//package Cases;


// CSC 468. Project Stage 1. 
// Test File for the StructureIndex structure.
// Alex Dekhtyar

// Note: the organization of your project may require somewhat different import statements.
//       Please, edit this test file accordingly.
//

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

import neustore.base.DBIndex;
import neustore.base.LRUBuffer;
import neustore.base.DiskAddress;  // change as necessary

import StructureIndex.*;

public class StructureIndexTest.java {

	
	public static void main(String[] args)throws IOException {
		

                int pageSize = 4096;  // Size of a single buffer/disk page. Insert your size HERE!

		//all the pages will be stored under this file name
		String filename1="TESTFILE2";
		File file= new File(filename1);
		if(file.delete())
		{
			 System.out.println("File already exists: deleted");
			 
		}
		else
		{
			System.out.println("File not found");
		}
		
		//create a buffer with 5 slots. 
		LRUBuffer buf= new LRUBuffer (5, pageSize);

		
		//Create a new StructureIndex instance/file
		StructureIndex SI = new StructureIndex(buf,filename1,1);
		
		//Create a disk address for content
                DiskAddress ptr = DiskAddress.createAddressByOffset(4,0);
                Systen.out.println("Disk Address created: "+ ptr.toString());

                 // Create a new StructureIndexRecord
                 // (NodeId, elementId, PreOrder, PostOrder, Ordinal, Layer, Parent, isLeaf, ptrContent)
                int nodeId = 1;
		StructureIndexRecord record=new StructureIndexRecord(nodeId,2,3,0,1,1,5,1,ptr);
		

		// Insert record
		DiskAddress ret= SI.insertEntry(record); 
		if(ret!=null)
		{
                   System.out.println("InsertEntry("+record.toString()+") at address:"+ ret.toString();
 
		}
		else //assuming that 'null' is returned when something goes wrong
		{
			System.out.println("Record not inserted: FAIL");
		}
		
                

		
		//Retrieve the address via getAddress
		DiskAddress add= SI.getAddress(nodeId);
		if(add!=null)
		{
 		    System.out.println("getAddress("+nodeId)+") :"+ add.toString();
                    StructureIndexRecord r = SI.getRecordByAddress(add);
                    System.out.println("getRecordByAddress("+add.toString()+"): "+ r.toString());
		}
		else
		{
			System.out.println("getAddress(): FAIL");
		}
		
		
		
		
		// Update postorder
		int status =SI.updatePostOrder(nodeId,30);
                System.out.println("updatePostOrder("+nodeId+",30): ", status);		
		StructureIndexRecord entry = SI.getRecordById(nodeId);
                if (entry != null) {
                  System.out.println("getRecordById("+nodeId+"): "+ entry.toString());
                }
                else {
                   System.out.println("getRecordById("+nodeId+"): FAIL");
                }		
		
                // Set Content pointer	
 
                 DiskAddress ptrNew = DiskAddress.createAddressByOffset(5,20);  
		 status=SI.setContent(nodeId,ptrNew);
                 entry = SI.getRecordById(nodeId);
                 if (entry != null) {
                  System.out.println("getRecordById("+nodeId+"): "+ entry.toString());
                 }
                 else {
                    System.out.println("getRecordById("+nodeId+"): FAIL");
                 }		
		

 
                 // Insert a bunch of records
                 for (int k=2; k<= 100; k++) {

                  DiskAddress addy = DiskAddress.createAddressByOffset(k, k*10 - k);
                   // (NodeId, elementId, PreOrder, PostOrder, Ordinal, Layer, Parent, isLeaf, ptrContent)
                  StructureIndexRecord newRecord = new StructureIndexRecord(k, k+1, k, k+10, k-1, k-2, k-1, 0, addy);
                  DiskAddress rr = SI.insertEntry(newRecord);
                  System.out.println("reccord: "+ newRecord.toString() + "@ address " + rr.toString());

                  }
 
                  // Delete some

                 for (int k=2; k<=30; k = k+2) {
 		     int deleteStatus = SI.deleteEntry(k);
                     System.out.println("deleteEntry("+k+"): "+ status);
                 }
		 
                  // retrieve, observe deletions

                 for (int k =1; k<=30; k++) {
                     StructureIndexRecord r = SI.getRecordByID(k);
                     System.out.println("getRecordById("+k+"): "+ r.toString());
                  }
			 
		 
//		SI.close();//remove the comments if the file needs to be closed before exiting
		 
	}//end of main() method
	}