예제 #1
0
AM_DumpLeafPages(int fileDesc,int min,int attrType,char attrLength)
{
int pageNum;
char *value;
char *pageBuf;
int index;
int errVal;
AM_LEAFHEADER *header;


value = malloc(AM_si);
bcopy(&min,value,AM_si);
printf("%d PAGE \n",AM_LeftPageNum);
PF_GetThisPage(fileDesc,AM_LeftPageNum,&pageBuf);
header = (AM_LEAFHEADER *) calloc(1,AM_sl);
bcopy(pageBuf,header,AM_sl);
while(header->nextLeafPage != -1)
  {
   printf("PAGENUMBER = %d\n",pageNum);   
   AM_PrintLeafKeys(pageBuf,attrType);
   errVal = PF_UnfixPage(fileDesc,pageNum,FALSE);
   AM_Check;
   pageNum = header->nextLeafPage;   
   errVal = PF_GetThisPage(fileDesc,pageNum,&pageBuf);
   AM_Check;
   bcopy(pageBuf,header,AM_sl);
  }
printf("PAGENUMBER = %d\n",pageNum);
AM_PrintLeafKeys(pageBuf,attrType);
errVal = PF_UnfixPage(fileDesc,pageNum,FALSE);
AM_Check;
}
예제 #2
0
AM_PrintTree(int fileDesc,int pageNum,char attrType)
{
int nextPage;
int errVal;
AM_INTHEADER *header;
char *tempPage;
char *pageBuf;
int recSize;
int i;

printf("GETTING PAGE = %d\n",pageNum);
errVal = PF_GetThisPage(fileDesc,pageNum,&pageBuf);
tempPage = malloc(PF_PAGE_SIZE);
bcopy(pageBuf,tempPage,PF_PAGE_SIZE);
errVal = PF_UnfixPage(fileDesc,pageNum,FALSE);
if (*tempPage == 'l')
  {
   printf("PAGENUM = %d\n",pageNum);
   AM_PrintLeafKeys(tempPage,attrType);
   return;
  }
header = (AM_INTHEADER *)calloc(1,AM_sint);
bcopy(tempPage,header,AM_sint);
recSize = header->attrLength + AM_si;
for(i = 1; i <= (header->numKeys + 1); i++)
  {
   bcopy(tempPage + AM_sint + (i-1)*recSize,&nextPage,AM_si);
   AM_PrintTree(fileDesc,nextPage,attrType);
  }
printf("PAGENUM = %d",pageNum);
AM_PrintIntNode(tempPage,attrType);
}
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();
}
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);
	}
}
예제 #5
0
AM_PrintTreeBulk(int fileDesc, int attrLength, int pageRoot){

	char* pageBuf;
	int pageNum = AM_RootPageNum;
	int errVal = PF_GetThisPage(fileDesc, pageNum, &pageBuf);
	AM_Check;
	AM_LEAFHEADER leafhead;
	AM_INTHEADER inthead;
	AM_LEAFHEADER *leafheader = &leafhead;
	AM_INTHEADER *intheader = &inthead;
	bcopy(pageBuf, leafheader, AM_sl);
	char c = leafheader->pageType;
	printf("Node %d: ", pageNum);
	if(c=='l'){
		printf("Leaf : Value->Record: ");
		int num = leafheader->numKeys;
		int i;
		for(i=0; i<num; i++){
			int recSize = attrLength + AM_ss;
			int offset = AM_sl + recSize*(i);
			int temp;
			int temp2;
			int temp3;
			bcopy(pageBuf + offset, &temp2, attrLength);
			bcopy(pageBuf + offset + attrLength, &temp, AM_ss);
			bcopy(pageBuf + temp, &temp3, attrLength);
			printf("%d->%d", temp2, temp3);
			if(i!=num-1) printf(", ");
		}
		PF_UnfixPage(fileDesc, pageNum, FALSE);
		printf("\n");
		return;
	}
	if(c=='i'){
		printf("Internal: P/V/P/V :");
		bcopy(pageBuf, intheader, AM_sint);
		int num=intheader->numKeys;
		int i;
		int offset = AM_sint;
		int temp, temp2;
		

		bcopy(pageBuf + offset, &temp, AM_si);
		bcopy(pageBuf + offset + AM_si, &temp2, attrLength);
		
		//printf("internal first pointer %d %d\n", temp, temp2);
		printf("%d ",temp);
		for(i=1; i<num; i++){
			int recSize = attrLength + AM_si;
			offset = AM_sint + AM_si + (i-1)*recSize;

			bcopy(pageBuf + offset, &temp, attrLength);
			bcopy(pageBuf + offset + attrLength, &temp2, AM_si);
			printf("%d %d ",temp,temp2);
		}
		printf("\n");




	}




	
	
	
}