copyfiles() { writefile(TMPFILE); int page_number1 = -1; int page_number2 = -1; int *buf1, *buf2; int fd_inpfile, fd_finalfile; if ((fd_inpfile=PF_OpenFile(TMPFILE))<0){ PF_PrintError("Error in opening file"); exit(1); } if ((fd_finalfile=PF_OpenFile(FILE))<0){ PF_PrintError("Error in opening file"); exit(1); } //transferring contents of finalfile to tmpfile int i, error; for(i = 0 ; i < B_R ; i++) { // get the page page_number of each file if((error=PF_GetNextPage(fd_finalfile,&page_number1,&buf1))!= PFE_OK) { PF_PrintError("Error"); exit(1); } if((error=PF_GetNextPage(fd_inpfile,&page_number2,&buf2))!= PFE_OK) { PF_PrintError("Error"); exit(1); } *((int *)buf2) = *((int *)buf1); //printf("%d ", *((int *)buf2)); if ((error=PF_UnfixPage(fd_finalfile,page_number1,TRUE))!= PFE_OK){ PF_PrintError("unfix buffer\n"); exit(1); } if ((error=PF_UnfixPage(fd_inpfile,page_number2,TRUE))!= PFE_OK){ PF_PrintError("unfix buffer\n"); exit(1); } } if ((error=PF_CloseFile(fd_inpfile))!= PFE_OK){ PF_PrintError("Error in closing input file\n"); exit(1); } if ((error=PF_CloseFile(fd_finalfile))!= PFE_OK){ PF_PrintError("Error in closing temporary file\n"); exit(1); } }
main(){ int fd; /* file descriptor for the index */ char fname[FNAME_LENGTH]; /* file name */ int recnum; /* record number */ int sd; /* scan descriptor */ int numrec; /* # of records retrieved */ int testval; clock_t t; // timeval t1, t2; // double elapsedtime; /* init */ printf("initializing\n"); PF_Init(); /* create index */ printf("creating index\n"); AM_CreateIndex(RELNAME,0,CHAR_TYPE,sizeof(char)); /* open the index */ printf("opening index\n"); sprintf(fname,"%s.0",RELNAME); fd = PF_OpenFile(fname); // gettime for (recnum=1; recnum < MAXRECS; recnum++){ char value; if(recnum%4==0) value=0; else if(recnum%4==1) value=1; else if(recnum%4==2) value=2; else value=3; AM_InsertEntry(fd,CHAR_TYPE,sizeof(char),(char *)&value, recnum); } // gettimeofday(&t2,NULL); t = clock(); numrec= 0; char val = 3; sd = AM_OpenIndexScan(fd,CHAR_TYPE,sizeof(char),LE_OP,(char*)&val); while((recnum=AM_FindNextEntry(sd))>= 0){ numrec++; } AM_CloseIndexScan(sd); t = clock()-t; double time_taken = ((double)t)/CLOCKS_PER_SEC; printf("Scan takes %f seconds to retreive records with repeating attributes\n", time_taken); printf("Number of pages used %d\n",totalNumberOfPages); printf("closing down\n"); PF_CloseFile(fd); AM_DestroyIndex(RELNAME,0); }
/************************************************************ Open the File. allocate as many pages in the file as the buffer manager would allow, and write the page number into the data. then, close file. ******************************************************************/ writefile(char *fname, int n_pages) { struct Record r; strcpy(r.name, "DBInternals"); strcpy(r.phone, "9831929192"); r.dd = 10; r.mm = 11; r.yy = 12; int page_hdr = 4; int n_tuppp = (PF_PAGE_SIZE - page_hdr) / sizeof(r); int i; int fd,pagenum; int *buf; int error; /* open file1, and allocate a few pages in there */ if ((fd=PF_OpenFile(fname))<0){ PF_PrintError("open file1"); exit(1); } //printf("opened %s\n",fname); for (i=0; i < n_pages; i++){ if ((error=PF_AllocPage(fd,&pagenum,&buf))!= PFE_OK){ PF_PrintError("first buffer\n"); exit(1); } buf[0] = n_tuppp; int j; for( j = 0; j < n_tuppp; ++j){ writeRecord(&r, page_hdr + j * sizeof(r), sizeof(r), buf); } //printf("allocated page %d\n",pagenum); if ((error=PF_UnfixPage(fd,i,TRUE))!= PFE_OK){ PF_PrintError("unfix buffer\n"); exit(1); } PF_PrintError("unfixed page successfully\n"); } /* close the file */ if ((error=PF_CloseFile(fd))!= PFE_OK){ PF_PrintError("close file1\n"); exit(1); } }
main() { int fd; /* file descriptor for the index */ char fname[FNAME_LENGTH]; /* file name */ int recnum; /* record number */ int sd; /* scan descriptor */ int numrec; /* # of records retrieved */ int testval; clock_t t; // timeval t1, t2; // double elapsedtime; /* init */ printf("initializing\n"); PF_Init(); /* create index */ printf("creating index\n"); AM_CreateIndex(RELNAME,0,CHAR_TYPE,sizeof(char)); /* open the index */ printf("opening index\n"); sprintf(fname,"%s.0",RELNAME); fd = PF_OpenFile(fname); t = clock(); // gettimeofday(&t1,NULL); for (recnum=1; recnum < MAXRECS; recnum++) { char value; if(recnum%4==0) value='a'; else if(recnum%4==1) value='b'; else if(recnum%4==2) value='c'; else value='d'; AM_InsertEntry(fd,CHAR_TYPE,sizeof(char),(char *)&value, recnum); } t = clock()-t; // gettimeofday(&t2,NULL); double time_taken = ((double)t)/CLOCKS_PER_SEC; // elapsedtime = (t2.tv_sec - t1.tv_sec) * 1000.0; // elapsedtime += (t2.tv_usec - t1.tv_usec) / 1000.0; printf("Insert takes %f seconds to insert records with repeating attributes\n", time_taken); printf("Number of pages used %d\n",totalNumberOfPages); printf("closing down\n"); PF_CloseFile(fd); AM_DestroyIndex(RELNAME,0); }
selectByName(char* fname, char* name) { int page_hdr = 4; int i; int fd,pagenum; int *buf; int error; //printf("opening %s\n",fname); if ((fd=PF_OpenFile(fname))<0){ PF_PrintError("open file"); exit(1); } pagenum = -1; while ((error=PF_GetNextPage(fd,&pagenum,&buf))== PFE_OK){ struct Record r; int n_tuppp = buf[0]; int i = 0; for(; i < n_tuppp; ++i) { readRecord(&r, page_hdr + i * sizeof(r), sizeof(r), buf); if(strcmp(r.name, name)){ // Do something PF_PrintError(":)"); } } if ((error=PF_UnfixPage(fd,pagenum,FALSE))!= PFE_OK){ PF_PrintError("unfix"); exit(1); } } if ((error=PF_CloseFile(fd))!= PFE_OK){ PF_PrintError("close file"); exit(1); } }
main(){ int fd; /* file descriptor for the index */ char fname[FNAME_LENGTH]; /* file name */ int recnum; /* record number */ int sd; /* scan descriptor */ int numrec; /* # of records retrieved */ int testval; clock_t t; // timeval t1, t2; // double elapsedtime; /* init */ printf("initializing\n"); PF_Init(); /* create index */ printf("creating index\n"); AM_CreateIndex(RELNAME,0,INT_TYPE,sizeof(int)); /* open the index */ printf("opening index\n"); sprintf(fname,"%s.0",RELNAME); fd = PF_OpenFile(fname); int value = 1; for (recnum=0; recnum < MAXRECS; recnum++){ AM_InsertEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum, recnum); } t = clock(); for(recnum=0;recnum < MAXRECS; recnum++){ AM_DeleteEntry(fd,INT_TYPE,sizeof(int),(char*)&recnum,recnum); } t = clock()-t; double time_taken = ((double)t)/CLOCKS_PER_SEC; printf("Delete takes %f seconds to delete records\n", time_taken); printf("Number of pages used %d\n",totalNumberOfPages); printf("closing down\n"); PF_CloseFile(fd); AM_DestroyIndex(RELNAME,0); }
main(){ int fd; /* file descriptor for the index */ int recnum; /* record number */ //~ printf("Initialising\n"); PF_Init(); //~ printf("Creating hashing structure\n"); HM_Create("test_hashing"); //~ printf("opening hashmap\n"); fd = PF_OpenFile("test_hashing"); //~ printf("doing insert\n"); loadFromFile(fd); /*printf("printing\n"); HM_Print(fd); printf("Searching\n"); if(HM_Search(fd, 555466)) printf("%d\n", HM_Search(fd, 555466)); printf("deleting\n"); HM_Delete(fd, 0, 555466); printf("deleting\n"); HM_Delete(fd, 1, 555466); printf("printing\n"); HM_Print(fd);*/ PF_CloseFile(fd); HM_Destroy("test_hashing"); }
join(char *f1, char *f2) { int page_hdr1 = 4, page_hdr2 = 4; int fd1, pagenum1, *buf1, error1, fd2, pagenum2, *buf2, error2; //printf("opening %s\n", f1); if ((fd1=PF_OpenFile(f1))<0){ PF_PrintError("open file"); exit(1); } //printf("opening %s\n", f2); if ((fd2=PF_OpenFile(f2))<0) { PF_PrintError("open file"); exit(1); } pagenum1 = -1; while ((error1=PF_GetNextPage(fd1,&pagenum1,&buf1))== PFE_OK) { pagenum2 = -1; while ((error2=PF_GetNextPage(fd2,&pagenum2,&buf2))== PFE_OK) { struct Record r1; struct Record r2; int n_tuppp1 = buf1[0], n_tuppp2 = buf2[0], i1 = 0, i2 = 0; for(; i1 < n_tuppp1; ++ i1) { i2 = 0; for(; i2 < n_tuppp2; ++ i2) { readRecord(&r1, page_hdr1 + i1 * sizeof(r1), sizeof(r1), buf1); readRecord(&r2, page_hdr2 + i2 * sizeof(r2), sizeof(r2), buf2); // Can implement join condition and print output to stdout/file here. Will not affect buffer performance analysis. } } if ((error2=PF_UnfixPage(fd2,pagenum2,FALSE))!= PFE_OK) { PF_PrintError("unfix"); exit(1); } } if ((error1=PF_UnfixPage(fd1,pagenum1,FALSE))!= PFE_OK) { PF_PrintError("unfix"); exit(1); } } if ((error2=PF_CloseFile(fd2))!= PFE_OK){ PF_PrintError("close file"); exit(1); } if ((error1=PF_CloseFile(fd1))!= PFE_OK){ PF_PrintError("close file"); exit(1); } }
main() { int error; int i; int pagenum,*buf; int *buf1,*buf2; int fd1,fd2; /* create a few files */ if ((error=PF_CreateFile(FILE1))!= PFE_OK){ PF_PrintError("file1"); exit(1); } printf("file1 created\n"); if ((error=PF_CreateFile(FILE2))!= PFE_OK){ PF_PrintError("file2"); exit(1); } printf("file2 created\n"); /* write to file1 */ writefile(FILE1); /* print it out */ readfile(FILE1); /* write to file2 */ writefile(FILE2); /* print it out */ readfile(FILE2); /* open both files */ if ((fd1=PF_OpenFile(FILE1))<0){ PF_PrintError("open file1\n"); exit(1); } printf("opened file1\n"); if ((fd2=PF_OpenFile(FILE2))<0 ){ PF_PrintError("open file2\n"); exit(1); } printf("opened file2\n"); /* get rid of records 1, 3, 5, etc from file 1, and 0,2,4,6 from file2 */ for (i=0; i < PF_MAX_BUFS; i++){ if (i & 1){ if ((error=PF_DisposePage(fd1,i))!= PFE_OK){ PF_PrintError("dispose\n"); exit(1); } printf("disposed %d of file1\n",i); } else { if ((error=PF_DisposePage(fd2,i))!= PFE_OK){ PF_PrintError("dispose\n"); exit(1); } printf("disposed %d of file2\n",i); } } if ((error=PF_CloseFile(fd1))!= PFE_OK){ PF_PrintError("close fd1"); exit(1); } printf("closed file1\n"); if ((error=PF_CloseFile(fd2))!= PFE_OK){ PF_PrintError("close fd2"); exit(1); } printf("closed file2\n"); /* print the files */ readfile(FILE1); readfile(FILE2); /* destroy the two files */ if ((error=PF_DestroyFile(FILE1))!= PFE_OK){ PF_PrintError("destroy file1"); exit(1); } if ((error=PF_DestroyFile(FILE2))!= PFE_OK){ PF_PrintError("destroy file2"); exit(1); } /* create them again */ if ((fd1=PF_CreateFile(FILE1))< 0){ PF_PrintError("create file1"); exit(1); } printf("file1 created\n"); if ((fd2=PF_CreateFile(FILE2))< 0){ PF_PrintError("create file2"); exit(1); } printf("file2 created\n"); /* put stuff into the two files */ writefile(FILE1); writefile(FILE2); /* Open the files, and see how the buffer manager handles more insertions, and deletions */ /* open both files */ if ((fd1=PF_OpenFile(FILE1))<0){ PF_PrintError("open file1\n"); exit(1); } printf("opened file1\n"); if ((fd2=PF_OpenFile(FILE2))<0 ){ PF_PrintError("open file2\n"); exit(1); } printf("opened file2\n"); for (i=PF_MAX_BUFS; i < PF_MAX_BUFS*2 ; i++){ if ((error=PF_AllocPage(fd2,&pagenum,&buf))!= PFE_OK){ PF_PrintError("first buffer\n"); exit(1); } *((int *)buf) = i; if ((error=PF_UnfixPage(fd2,pagenum,TRUE))!= PFE_OK){ PF_PrintError("unfix file1"); exit(1); } printf("alloc %d file1\n",i,pagenum); if ((error=PF_AllocPage(fd1,&pagenum,&buf))!= PFE_OK){ PF_PrintError("first buffer\n"); exit(1); } *((int *)buf) = i; if ((error=PF_UnfixPage(fd1,pagenum,TRUE))!= PFE_OK){ PF_PrintError("dispose file1"); exit(1); } printf("alloc %d file2\n",i,pagenum); } for (i= PF_MAX_BUFS; i < PF_MAX_BUFS*2; i++){ if (i & 1){ if ((error=PF_DisposePage(fd1,i))!= PFE_OK){ PF_PrintError("dispose fd1"); exit(1); } printf("dispose fd1 page %d\n",i); } else { if ((error=PF_DisposePage(fd2,i))!= PFE_OK){ PF_PrintError("dispose fd2"); exit(1); } printf("dispose fd2 page %d\n",i); } } printf("getting file2\n"); for (i=PF_MAX_BUFS; i < PF_MAX_BUFS*2; i++){ if (i & 1){ if ((error=PF_GetThisPage(fd2,i,&buf))!=PFE_OK){ PF_PrintError("get this on fd2"); exit(1); } printf("%d %d\n",i,*buf); if ((error=PF_UnfixPage(fd2,i,FALSE))!= PFE_OK){ PF_PrintError("get this on fd2"); exit(1); } } } printf("getting file1\n"); for (i=PF_MAX_BUFS; i < PF_MAX_BUFS*2; i++){ if (!(i & 1)){ if ((error=PF_GetThisPage(fd1,i,&buf))!=PFE_OK){ PF_PrintError("get this on fd2"); exit(1); } printf("%d %d\n",i,*buf); if ((error=PF_UnfixPage(fd1,i,FALSE))!= PFE_OK){ PF_PrintError("get this on fd2"); exit(1); } } } /* print the files */ printfile(fd2); printfile(fd1); /*put some more stuff into file1 */ printf("putting stuff into holes in fd1\n"); for (i=0; i < (PF_MAX_BUFS/2 -1); i++){ if (PF_AllocPage(fd1,&pagenum,&buf)!= PFE_OK){ PF_PrintError("PF_AllocPage"); exit(1); } *buf =pagenum; if (PF_UnfixPage(fd1,pagenum,TRUE)!= PFE_OK){ PF_PrintError("PF_UnfixPage"); exit(1); } } printf("printing fd1"); printfile(fd1); PF_CloseFile(fd1); printf("closed file1\n"); PF_CloseFile(fd2); printf("closed file2\n"); /* open file1 twice */ if ((fd1=PF_OpenFile(FILE1))<0){ PF_PrintError("open file1"); exit(1); } printf("opened file1\n"); /* try to destroy it while it's still open*/ error=PF_DestroyFile(FILE1); PF_PrintError("destroy file1, should not succeed"); /* get rid of some invalid page */ error=PF_DisposePage(fd1,100); PF_PrintError("dispose page 100, should fail"); /* get a valid page, and try to dispose it without unfixing.*/ if ((error=PF_GetThisPage(fd1,1,&buf))!=PFE_OK){ PF_PrintError("get this on fd2"); exit(1); } printf("got page%d\n",*buf); error=PF_DisposePage(fd1,1); PF_PrintError("dispose page1, should fail"); /* Now unfix it */ if ((error=PF_UnfixPage(fd1,1,FALSE))!= PFE_OK){ PF_PrintError("get this on fd2"); exit(1); } error=PF_UnfixPage(fd1,1,FALSE); PF_PrintError("unfix fd1 again, should fail"); if ((fd2=PF_OpenFile(FILE1))<0 ){ PF_PrintError("open file1 again"); exit(1); } printf("opened file1 again\n"); printfile(fd1); printfile(fd2); if (PF_CloseFile(fd1) != PFE_OK){ PF_PrintError("close fd1"); exit(1); } if (PF_CloseFile(fd2)!= PFE_OK){ PF_PrintError("close fd2"); exit(1); } /* print the buffer */ printf("buffer:\n"); PFbufPrint(); /* print the hash table */ printf("hash table:\n"); PFhashPrint(); }
main() { int fd; /* file descriptor for the index */ char fname[FNAME_LENGTH]; /* file name */ int recnum; /* record number */ int sd; /* scan descriptor */ int numrec; /* # of records retrieved */ int testval; /* init */ printf("initializing\n"); PF_Init(); /* create index */ printf("creating index\n"); AM_CreateIndex(RELNAME,0,INT_TYPE,sizeof(int)); /* open the index */ printf("opening index\n"); sprintf(fname,"%s.0",RELNAME); fd = PF_OpenFile(fname); /* first, make sure that simple deletions work */ printf("inserting into index\n"); for (recnum=0; recnum < 20; recnum++){ AM_InsertEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum, recnum); } printf("deleting odd number records\n"); for (recnum=1; recnum < 20; recnum += 2) AM_DeleteEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum, recnum); printf("retrieving even number records\n"); numrec= 0; sd = AM_OpenIndexScan(fd,INT_TYPE,sizeof(int),EQ_OP,NULL); while((recnum=AM_FindNextEntry(sd))>= 0){ printf("%d\n",recnum); numrec++; } printf("retrieved %d records\n",numrec); AM_CloseIndexScan(sd); printf("deleting even number records\n"); for (recnum=0; recnum < 20; recnum += 2) AM_DeleteEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum, recnum); printf("retrieving from empty index\n"); numrec= 0; sd = AM_OpenIndexScan(fd,INT_TYPE,sizeof(int),EQ_OP,NULL); while((recnum=AM_FindNextEntry(sd))>= 0){ printf("%d\n",recnum); numrec++; } printf("retrieved %d records\n",numrec); AM_CloseIndexScan(sd); /* insert into index */ printf("begin test of complex delete\n"); printf("inserting into index\n"); for (recnum=0; recnum < MAXRECS; recnum+=2){ AM_InsertEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum, recnum); } for (recnum=1; recnum < MAXRECS; recnum+=2) AM_InsertEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum, recnum); /* delete everything */ printf("deleting everything\n"); for (recnum=1; recnum < MAXRECS; recnum += 2) AM_DeleteEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum, recnum); for (recnum=0; recnum < MAXRECS; recnum +=2) AM_DeleteEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum, recnum); /* print out what remains */ printf("printing empty index\n"); numrec= 0; sd = AM_OpenIndexScan(fd,INT_TYPE,sizeof(int),EQ_OP,NULL); while((recnum=AM_FindNextEntry(sd))>= 0){ printf("%d\n",recnum); numrec++; } printf("retrieved %d records\n",numrec); AM_CloseIndexScan(sd); /* insert everything back */ printf("inserting everything back\n"); for (recnum=0; recnum < MAXRECS; recnum++){ AM_InsertEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum, recnum); } /* delete records less than 100, using scan!! */ printf("delete records less than 100\n"); testval = 100; sd = AM_OpenIndexScan(fd,INT_TYPE,sizeof(int),LT_OP,(char *)&testval); while((recnum=AM_FindNextEntry(sd))>= 0){ if (recnum >= 100){ printf("invalid recnum %d\n",recnum); exit(1); } AM_DeleteEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum, recnum); } AM_CloseIndexScan(sd); /* delete records greater than 150, using scan */ printf("delete records greater than 150\n"); testval = 150; sd = AM_OpenIndexScan(fd,INT_TYPE,sizeof(int),GT_OP,(char *)&testval); while((recnum=AM_FindNextEntry(sd))>= 0){ if (recnum <= 150){ printf("invalid recnum %d\n",recnum); exit(1); } AM_DeleteEntry(fd,INT_TYPE,sizeof(int),(char *)&recnum, recnum); } AM_CloseIndexScan(sd); /* print out what remains */ printf("printing between 100 and 150\n"); numrec= 0; sd = AM_OpenIndexScan(fd,INT_TYPE,sizeof(int),EQ_OP,NULL); while((recnum=AM_FindNextEntry(sd))>= 0){ printf("%d\n",recnum); numrec++; } printf("retrieved %d records\n",numrec); AM_CloseIndexScan(sd); /* destroy everything */ printf("closing down\n"); PF_CloseFile(fd); AM_DestroyIndex(RELNAME,0); printf("test3 done!\n"); }
main(int argc, char *argv[]) { int error; int i,fd; int pagenum,*buf; int *buf1,*buf2; int fd1,fd2; int nowrites; //int stale_file[2][]; if( argc == 3 ) { printf("output The argument supplied are %s, %s\n", argv[1], argv[2]); //char temp[] = *argv[1]; //nowrites = *argv[2]; sscanf(argv[1], "%d", &filesize); sscanf(argv[2], "%d", &nowrites); filesize = filesize*PF_MAX_BUFS; printf("output final The argument supplied are %d, %d\n", filesize, nowrites); } else { printf("One argument expected.\n"); filesize = 2*PF_MAX_BUFS; nowrites = 20; } /* create a few files */ if ((error=PF_CreateFile(FILE1))!= PFE_OK){ PF_PrintError("file1"); exit(1); } printf("file size - %d", filesize); int stale_file1[20000]; for (i=0; i<filesize; i++) { stale_file1[i] = 0; } printf("file1 created\n"); if ((error=PF_CreateFile(FILE2))!= PFE_OK){ PF_PrintError("file2"); exit(1); } int stale_file2[20000]; for (i=0; i<filesize; i++) { stale_file2[i] = 0; } printf("file2 created\n"); /* write to file1 */ writefile(FILE1,stale_file1); /* print it out */ readfile(FILE1,stale_file1); //Sequential Updates reads = 0; writes = 0; erases = 0; if ((fd=PF_OpenFile(FILE1))<0){ PF_PrintError("open file"); exit(1); } pagenum = -1; int temp = 0; printf("\noutput final Sequential Updates\n"); while (temp<nowrites && (error=PF_GetNextPage(fd,&pagenum,&buf))== PFE_OK) //for(i=0; i<nowrites; i++) { i = pagenum; temp++; if ((error=PF_UnfixPage(fd,pagenum,FALSE))!= PFE_OK){ PF_PrintError("unfix"); exit(1); } updatefile(FILE1,stale_file1,i); } printf("reads - %d\n", reads); printf("erases - %d\n", erases); printf("writes - %d\n", writes); garbagecollect(FILE1,stale_file1); printf("output file size - %d\n", filesize); printf("output Number of updates - %d\n", nowrites); printf("output Number of pages in block - %d\n", PAGEINBLOCK); printf("output After garbage collection\n"); printf("output final reads - %d\n", reads); printf("output final erases(in units of blocks) - %d\n", erases/PAGEINBLOCK); printf("output final writes - %d\n", writes); if ((error=PF_CloseFile(fd))!= PFE_OK){ PF_PrintError("close file"); exit(1); } readfile(FILE1, stale_file1); /* write to file2 */ writefile(FILE2,stale_file2); /* print it out */ readfile(FILE2,stale_file2); //Random Updates reads = 0; writes = 0; erases = 0; if ((fd=PF_OpenFile(FILE2))<0){ PF_PrintError("open file"); exit(1); } pagenum = -1; temp = 0; printf("\noutput final Random Updates\n"); while (temp<nowrites && ((error=PF_GetNextPage(fd,&pagenum,&buf))== PFE_OK || error == PFE_EOF) ) //for(i=0; i<nowrites; i++) { if(error == PFE_EOF) { printf("output EOF reached\n"); pagenum = -1; continue; } i = pagenum; if ((error=PF_UnfixPage(fd,pagenum,FALSE))!= PFE_OK){ printf("outputerror - %d\n", error); PF_PrintError("unfix"); exit(1); } int j = rand(); if(j%2 == 0) continue; temp++; //printf("output i : %d", i); //int j = rand() % filesize; updatefile(FILE2,stale_file2,i); if(temp==nowrites/2) { printf("output Before garbage collection1:-\n"); printf("output reads - %d\n", reads); printf("output erases(in units of blocks) - %d\n", erases/PAGEINBLOCK); printf("output writes - %d\n", writes); numpages(FILE2,stale_file2); printf("output total number of pages - %d, stale pages is - %d\n", numtotal,numstale); garbagecollect(FILE2,stale_file2); printf("output After garbage collection1:-\n"); printf("output reads - %d\n", reads); printf("output erases(in units of blocks) - %d\n", erases/PAGEINBLOCK); printf("output writes - %d\n", writes); numpages(FILE2,stale_file2); printf("output total number of pages - %d, stale pages is - %d\n", numtotal,numstale); } } if(error != PFE_OK) { PF_PrintError("output"); } printf("output Before garbage collection2\n"); printf("output reads - %d\n", reads); printf("output erases(in units of blocks) - %d\n", erases/PAGEINBLOCK); printf("output writes - %d\n", writes); numpages(FILE2,stale_file2); printf("output total number of pages - %d, stale pages is - %d\n", numtotal,numstale); garbagecollect(FILE2,stale_file2); printf("output file size - %d\n", filesize); printf("output Number of updates - %d, %d\n", temp, nowrites); printf("output Number of pages in block - %d\n", PAGEINBLOCK); printf("output After garbage collection2\n"); printf("output final reads - %d\n", reads); printf("output final erases(in units of blocks) - %d\n", erases/PAGEINBLOCK); printf("output final writes - %d\n", writes); numpages(FILE2,stale_file2); printf("output total number of pages - %d, stale pages is - %d\n", numtotal,numstale); if ((error=PF_CloseFile(fd))!= PFE_OK){ PF_PrintError("close file"); exit(1); } readfile(FILE2, stale_file2); //readfile(FILE1,stale_file1); //garbagecollect(FILE1,stale_file1); //readfile(FILE1,stale_file1); }
external_sort() { int i, j, k, error; int * buf; int run_size = 1, run_count = B_R; int MEM = PF_MAX_BUFS - 1, PREVMEM = MEM; int fd_inpfile; if ((fd_inpfile=PF_OpenFile(TMPFILE))<0){ PF_PrintError("Error in opening file"); exit(1); } int fd_finalfile; if ((fd_finalfile=PF_OpenFile(FILE))<0){ PF_PrintError("Error in opening file"); exit(1); } // We assume that inpfile contains all the runs and then we merge them and put them in finalfile // when 1 merge pass is over, we again write back everything to the tmpfile // repeat while(run_count > 1) { int pagenum_finalfile = -1; //printf("Run count: %d\n", run_count); //printf("Run Size: %d\n", run_size); MEM = PREVMEM; int number_of_merges = run_count / MEM; if(run_count % MEM != 0) number_of_merges++; //printf("Number of merges: %d\n", number_of_merges); for(i = 0 ; i < number_of_merges ; i++) { //MEM is the number of runs we merge at once. If say MEM = 4 and run_count = 11, then in the last merge step, we merge just 3 runs. So we set MEM accordingly to 3 if(run_count % MEM != 0 && i == number_of_merges - 1) MEM = run_count % MEM; int be[MEM], en[MEM]; // be[j] stores the beginning of j-th run and en[j] = 1 + ending of j-th run. Idea is that when be[j] == en[j], then that run is finished for(j = 1; j <= MEM; j++) { be[j - 1] = i * PREVMEM * run_size + (j - 1) * run_size; //simple arithmetic en[j - 1] = i * PREVMEM * run_size + j * run_size; } if(i == number_of_merges - 1) //Since the extremely last run can be shorter than other runs, we need to take care of it. This run will come into picture only in the very last merging iteration en[MEM - 1] = en[MEM - 1] < B_R ? en[MEM - 1] : B_R; //the ending of the very last run cannot be more than B_R //for(j = 0; j < MEM; j++) //printf("be: %d, en: %d\n", be[j], en[j]); int blocks_written = run_size * (MEM - 1) + (en[MEM - 1] - be[MEM - 1]); //in 1 merge of iteration, we count the number of blocks we are going to write. Again, take care of the last run separately //printf("blocks written: %d\n", blocks_written); int *arr[MEM]; // the buffers for the runs that will be merged for(j = 0 ; j < MEM ; j++) { //printf("Getting page: %d\n", be[j]); error = PF_GetThisPage(fd_inpfile, be[j], &arr[j]); // get the first page of each run in the buffer if(error != PFE_OK) printf("Error in getting page\n"); //printf("Page value: %d\n", *arr[j]); } //start writing blocks one by one for(k = 0 ; k < blocks_written ; k++) { // here goes the actual merge step int least = (1 << 30), least_index = -1 ; // least_index -> the run whose head is the smallest. least -> the value of the corresponding head // Note -> be[j] also acts as a pointer to the starting of each run. during the merging process, the value of be[j] will increment // if be[j] == en[j], then that particular run is finished //printf("Yo\n"); for(j = 0 ; j < MEM ; j++) { // this for loop finds the run whose head is the smallest and also the smallest value of the head if(en[j] != be[j] && *arr[j] < least) { least = *arr[j]; least_index = j; } //if(en[j] == be[j]) //printf("Ignoring\n"); } //printf("Least: %d\n", least); //get the next page of the finalfile where the least block is to be written //note that pagenum_finalfile will automatically be incremented by 1 after the line below //printf("Getting page number %d of finalfile\n", pagenum_finalfile + 1); if((error=PF_GetNextPage(fd_finalfile,&pagenum_finalfile,&buf))!= PFE_OK) { PF_PrintError("Can't get page"); exit(1); } //printf("Value of pagenum_finalfile now: %d\n", pagenum_finalfile); //printf("Writing it by changing buffer\n"); *((int *)buf) = least; // assign the buffer the least value //printf("unfixing the written page\n"); //unfix the written page if ((error=PF_UnfixPage(fd_finalfile,pagenum_finalfile,TRUE))!= PFE_OK){ PF_PrintError("unfix buffer\n"); exit(1); } //printf("Unfixing the least page\n"); //unfix the least page as well if ((error=PF_UnfixPage(fd_inpfile,be[least_index],TRUE))!= PFE_OK){ PF_PrintError("unfix buffer\n"); exit(1); } be[least_index]++; if(be[least_index] < en[least_index]) { // if the run which had the *least* head isn't finished, get its next page be[least_index]--; if((error=PF_GetNextPage(fd_inpfile , &be[least_index] , &arr[least_index]))!= PFE_OK) { PF_PrintError("Can't get page"); exit(1); } //printf("Fetching next page: %d\n", *arr[least_index]); } } //printf("End of 1 merge iteration\n"); } copyfiles(); //printf("\n"); run_count = number_of_merges; run_size = run_size * PREVMEM; //printf("End of iteration\n"); //readfile(FILE); } //swap files here if ((error=PF_CloseFile(fd_inpfile))!= PFE_OK){ PF_PrintError("Error in closing input file\n"); exit(1); } if ((error=PF_CloseFile(fd_finalfile))!= PFE_OK){ PF_PrintError("Error in closing temporary file\n"); exit(1); } }
/* Creates a secondary idex file called fileName.indexNo */ AM_CreateIndex(char* fileName,int indexNo,char attrType,int attrLength) { char *pageBuf; /* buffer for holding a page */ char indexfName[AM_MAX_FNAME_LENGTH]; /* String to store the indexed files name with extension */ int pageNum; /* page number of the root page(also the first page) */ int fileDesc; /* file Descriptor */ int errVal; int maxKeys;/* Maximum keys that can be held on one internal page */ AM_LEAFHEADER head,*header; /* Check the parameters */ if ((attrType != 'c') && (attrType != 'f') && (attrType != 'i')) { AM_Errno = AME_INVALIDATTRTYPE; return(AME_INVALIDATTRTYPE); } if ((attrLength < 1) || (attrLength > 255)) { AM_Errno = AME_INVALIDATTRLENGTH; return(AME_INVALIDATTRLENGTH); } if (attrLength != 4) if (attrType !='c') { AM_Errno = AME_INVALIDATTRLENGTH; return(AME_INVALIDATTRLENGTH); } header = &head; /* Get the filename with extension and create a paged file by that name*/ sprintf(indexfName,"%s.%d",fileName,indexNo); errVal = PF_CreateFile(indexfName); AM_Check; /* open the new file */ fileDesc = PF_OpenFile(indexfName); if (fileDesc < 0) { AM_Errno = AME_PF; return(AME_PF); } /* allocate a new page for the root */ errVal = PF_AllocPage(fileDesc,&pageNum,&pageBuf); AM_Check; allocsDone++; /* initialise the header */ header->pageType = 'l'; header->nextLeafPage = AM_NULL_PAGE; header->recIdPtr = PF_PAGE_SIZE; header->keyPtr = AM_sl; header->freeListPtr = AM_NULL; header->numinfreeList = 0; header->attrLength = attrLength; header->numKeys = 0; /* the maximum keys in an internal node- has to be even always*/ maxKeys = (PF_PAGE_SIZE - AM_sint - AM_si)/(AM_si + attrLength); if (( maxKeys % 2) != 0) header->maxKeys = maxKeys - 1; else header->maxKeys = maxKeys; /* copy the header onto the page */ bcopy(header,pageBuf,AM_sl); errVal = PF_UnfixPage(fileDesc,pageNum,TRUE); AM_Check; /* Close the file */ errVal = PF_CloseFile(fileDesc); AM_Check; /* initialise the root page and the leftmost page numbers */ AM_RootPageNum = pageNum; return(AME_OK); }
void create_input_pages() { int totalRecsAdded; int error; if ((error=PF_CreateFile(MERGESORTFILE))!= PFE_OK){ PF_PrintError( "Error in creating mergesortfile"); exit(1); } //- printf("mergesortfile created\n"); int fd,pagenum; char *buf; pagenum = -1; if ((fd=PF_OpenFile(MERGESORTFILE))<0){ PF_PrintError("Error in opening mergesortfile"); exit(1); } //-printf("opened %s\n", MERGESORTFILE); if ((error=PF_AllocPage(fd,&pagenum,&buf))!= PFE_OK) { PF_PrintError("ERROR: failed allocating buffers in create_input_pages\n"); exit(1); } //-printf("allocated page %d in create_input_pages\n",pagenum); //printf("f4f42"); FILE *file = fopen ( filename, "r" ); //printf("f4f42"); fflush(file); if ( file != NULL ) { char line[MAXATTRSIZE]; int recsAdded = 0; totalRecsAdded = 0; while ( fgets ( line, ATTRSIZE, file ) != NULL ) { //puts(line); //printf("%d \n", strlen(line)); if(recsAdded == MAXRECSPERPAGE) { if ((error=PF_UnfixPage(fd, pagenum, TRUE))!= PFE_OK) { PF_PrintError("Error in disposing page of mergesortfile"); exit(1); } //-printf("deallocated page %d in create_input_pages\n",pagenum); if ((error=PF_AllocPage(fd,&pagenum,&buf))!= PFE_OK) { PF_PrintError("ERROR: failed allocating buffers in create_input_pages\n"); exit(1); } //-printf("allocated page %d in create_input_pages\n",pagenum); recsAdded = 0; bcopy(&recsAdded, buf, si); } bcopy(line, buf + si + recsAdded * (ATTRSIZE + si), ATTRSIZE); totalRecsAdded++; bcopy((char*)&totalRecsAdded, buf + si + recsAdded * (ATTRSIZE + si) + ATTRSIZE, si); int abc=recsAdded+1; bcopy(&abc, buf, si); // printf("%s ", line); // bcopy(buf + si + recsAdded * (ATTRSIZE + si), line, ATTRSIZE); // bcopy(buf + si + recsAdded * (ATTRSIZE + si) + ATTRSIZE, (char*)&totalRecsAdded, si); // bcopy(buf, &recsAdded, si); recsAdded++; // printf("%s %d %d\n", line, totalRecsAdded, recsAdded); } //- printf("Totally %d records were added\n", totalRecsAdded); fclose ( file ); } else { perror ( filename ); /* why didn't the file open? */ } if ((error=PF_UnfixPage(fd, pagenum, TRUE))!= PFE_OK){ PF_PrintError("Error in disposing page of mergesortfile"); exit(1); } //-printf("deallocated page %d in create_input_pages\n", pagenum); if ((error=PF_CloseFile(fd))!= PFE_OK) { PF_PrintError("Error in closing mergesortfile---"); exit(1); } //-printf("mergesortfile closed\n"); }