コード例 #1
0
ファイル: createtable.c プロジェクト: devdeepray/ToyDBProject
/************************************************************
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);
	}

}
コード例 #2
0
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();
}
コード例 #3
0
ファイル: amfns.c プロジェクト: sivaprasadsudhir/DBinternals
/* 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);
}
コード例 #4
0
ファイル: amfns.c プロジェクト: sivaprasadsudhir/DBinternals
AM_BulkLoadInternal(int fileDesc, char* fileName,int indexNo,char attrType,int attrLength,int* pages, int* values, int* newPages, int* newValues, int* globalindex, int* leafSize, int earlyExit)
{

	char *pageBuf; /* buffer for holding a page */
	char indexfName[AM_MAX_FNAME_LENGTH]; /* String to store the indexed
					 files name with extension           */
	int pageNum=-1; /* page number of the root page(also the first page) */
	int errVal;
	int maxKeys;/* Maximum keys that can be held on one internal page */
	// vector_init(&pagenNumNextLevel);
	// vector_init(&valuesNextLevel);
	
	maxKeys = (PF_PAGE_SIZE - AM_sint - AM_si)/(AM_si + attrLength);
	if (( maxKeys % 2) != 0) 
		maxKeys--;
	

	int numLeaves = globalindex[0];
	globalindex[0]=0;

	AM_INTHEADER head,*header;
	int recSize = attrLength + AM_si;



	/* 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;
	

	
	/* open the new file */
	
	/* allocate a new page for the root */
	
    
	int haveToAllocate = 1;
	int recNum;
	int index;
	int first=1;
	//printf("Numleaves %d\n", numLeaves);	
	for(recNum=0; recNum<numLeaves; ) {

		//printf("Recnum: %d\n", recNum);
		int value = values[recNum];
		int page = pages[recNum];

		if(haveToAllocate==1 && first!=1){
			if(leafSize[0]==-1){
				if(earlyExit==1){
					leafSize[0]=index-1;
					printf("exiting\n");
					return (AME_OK);
				}
			}
		}
		

		if(haveToAllocate==1){
			//printf("having to allocate\n");
			first=0;
			index=1;

			if(pageNum>0) {
				AM_LEAFHEADER thead,*theader;
				theader=&thead;
				bcopy(pageBuf,theader,AM_sl);
				sizeallocated=sizeallocated+(theader->numKeys)*(AM_si+attrLength) + AM_sint;
				PF_UnfixPage(fileDesc, pageNum, TRUE);
			}
			errVal = PF_AllocPage(fileDesc, &pageNum, &pageBuf);
			
			AM_Check;
			allocsDone++;
			//help("alloced");
			
			//printf("MARKER :%d\n", pageNum);
			
		
			newPages[globalindex[0]]=pageNum;
			newValues[globalindex[0]]=value;
			globalindex[0]++;

			// vector_add(&pagenNumNextLevel, temp1);
			// vector_add(&valuesNextLevel, temp2);

			/* initialise the header */
			header->pageType = 'i';
			header->numKeys=0;
			header->maxKeys=maxKeys;
			header->attrLength=attrLength;

			/* copy the header onto the page */
			bcopy(header,pageBuf,AM_sint);

			haveToAllocate=0;
			

		}
		
		if(earlyExit==0){
			int alreadyDone = index-1;
			int leftInLeaf = leafSize[0]-alreadyDone;
			int leftInFile = numLeaves - recNum;
			if(leftInFile == (leafSize[0]+1)/2){
				if(leftInLeaf < leftInFile){
					haveToAllocate=1;
					continue;
				}
			}
		}

		if(header->numKeys >= header->maxKeys || index>leafSize[0]&&leafSize[0]!=-1){
			haveToAllocate=1;
			continue;
		}

		if(index!=1){
			//bcopy((char*) &values[recNum], pageBuf + AM_sl + (index-1)*recSize + AM_si, attrLength);
			//bcopy((char*) &pages[recNum], pageBuf + AM_sl + (index-1)*recSize + attrLength, AM_si);
			bcopy((char*) &values[recNum], pageBuf + AM_sint + (index-2)*recSize + AM_si, attrLength);
			bcopy((char*) &pages[recNum], pageBuf + AM_sint + (index-2)*recSize + AM_si + attrLength, AM_si);
			bcopy(pageBuf, header, AM_sint);
			header->numKeys++;
			bcopy(header, pageBuf, AM_sint);


		}
		if(index==1){
			bcopy((char*) &pages[recNum], pageBuf + AM_sint, AM_si);
			//bcopy((char*) &pages[recNum], pageBuf + AM_sl, AM_si);
			// sizeallocated=sizeallocated+AM_si;

			bcopy(pageBuf, header, AM_sint);
			header->numKeys++;
			bcopy(header, pageBuf, AM_sint);

		}
		index++;
		recNum++;
	}
	if(leafSize[0]==-1)
		leafSize[0]=numLeaves;

	AM_LEAFHEADER thead1,*theader1;
	theader1=&thead1;
	bcopy(pageBuf,theader1,AM_sl);
	sizeallocated=sizeallocated+(theader1->numKeys)*(AM_si+attrLength) + AM_sint;
	errVal = PF_UnfixPage(fileDesc,pageNum,TRUE);
	//AM_Check;
	
	/* Close the file */
	errVal = PF_CloseFile(fileDesc);
	AM_Check;

	return(AME_OK);

}
コード例 #5
0
ファイル: amfns.c プロジェクト: sivaprasadsudhir/DBinternals
AM_BulkLoadLeaf(int fileDesc, char* fileName,int indexNo,char attrType,int attrLength,int MAXRECS, int* pagenNumNextLevel, int* valuesNextLevel, int* globalindex, int* leafSize, int earlyExit, char *readFilename)
{

	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 errVal;
	int maxKeys;/* Maximum keys that can be held on one internal page */
	// vector_init(&pagenNumNextLevel);
	// vector_init(&valuesNextLevel);
	globalindex[0]=0;
	AM_LEAFHEADER head,*header;
	int recSize = attrLength + AM_ss;

	/* 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);
	


	
	/* allocate a new page for the root */
	
    
	int previousValue;
	int haveToAllocate = 1;
	// int recNum;
	int index;
	int first=1;

	FILE* file = fopen(readFilename, "r");
	int recNum = 0;
	fscanf(file, "%d", &recNum);

	// for(recNum=0; recNum<MAXRECS; ) {
	while (!feof (file))
	{
		// printf("%d\n", recNum);
		int value = recNum;
		
		if(haveToAllocate ==1 && first==1){
			first=0;
			index=1;
			previousValue=-1;
			errVal = PF_AllocPage(fileDesc, &pageNum, &pageBuf);
			AM_Check;
			allocsDone++;
			

			//if(earlyExit != 1)
			//printf("MARKER :%d\n", pageNum);
			
			pagenNumNextLevel[globalindex[0]]=pageNum;
			valuesNextLevel[globalindex[0]]=value;
			globalindex[0]++;

			// vector_add(&pagenNumNextLevel, temp1);
			// vector_add(&valuesNextLevel, temp2);

			/* 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);
			// sizeallocated=sizeallocated+AM_sl;

			AM_LeftPageNum = pageNum;
			haveToAllocate=0;

		}
		else if(haveToAllocate==1 && first==0){

			first=0;
			haveToAllocate=0;
			previousValue=-1;
			// point old guy to new guy, unfix old guy
			AM_LEAFHEADER temp;
			AM_LEAFHEADER *tempheader=&temp;
			
			int newPageNum;
			char* newPageBuf;
			if(leafSize[0]==-1){
				if(earlyExit==1){
					leafSize[0]=index-1;
					return (AME_OK);
				}
			}
			index=1;

			errVal = PF_AllocPage(fileDesc,&newPageNum,&newPageBuf);

			AM_Check;
			allocsDone++;

			//printf("MARKER old, page:%d, %d\n", pageNum, newPageNum);
			bcopy(pageBuf,tempheader,AM_sl);
			tempheader->nextLeafPage = newPageNum;
			bcopy(tempheader,pageBuf,AM_sl);
			
			// sizeallocated=sizeallocated+()AM_ss+attrLength;	
			AM_LEAFHEADER thead1,*theader1;
			theader1=&thead1;
			bcopy(pageBuf,theader1,AM_sl);
			sizeallocated=sizeallocated+(theader1->numKeys)*(AM_ss+attrLength) + AM_sl;		
			errVal = PF_UnfixPage(fileDesc, pageNum, TRUE);
			AM_Check;

			pageNum = newPageNum;
			pageBuf = newPageBuf;
			
			pagenNumNextLevel[globalindex[0]]=pageNum;
			valuesNextLevel[globalindex[0]]=value;
			globalindex[0]++;

			// char temp1[20];
			// char temp2[20];
			// sprintf(temp1, "%d", pageNum);
			// sprintf(temp2, "%d", value);
			// printf("%d\n",myAtoi(temp1) );

			// vector_add(&pagenNumNextLevel, temp1);
			// vector_add(&valuesNextLevel, temp2);
			
			
			/* 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);
			
		}

		if(earlyExit==0){
			int alreadyDone = index-1;
			int leftInLeaf = leafSize[0]-alreadyDone;
			int leftInFile = MAXRECS - recNum;
			if(leftInFile == (leafSize[0]+1)/2){
				if(leftInLeaf < leftInFile){
					haveToAllocate=1;
					continue;
				}

			}
		}

		if(index> leafSize[0] && leafSize[0]!=-1){
			haveToAllocate=1;
			continue;
		}

		if (previousValue == value)
		/* key is already present */ 
		{

			if (header->freeListPtr == 0)
				if ((header->recIdPtr - header->keyPtr) <(AM_si + AM_ss) || index>=leafSize[0] && leafSize[0]!=-1)
				{
					/* no room for one more record */
					haveToAllocate=1;
					continue;
				}
			/* insert into leaf - no need to split */
			AM_InsertToLeafFound(pageBuf,recNum,index,header);
			// sizeallocated=sizeallocated+AM_ss+attrLength;
			help("do not happen yet");	
			bcopy(header,pageBuf,AM_sl);
			haveToAllocate=0;
			// recNum++;

		fscanf (file, "%d", &recNum);  
			index++;

			previousValue=value;
			continue;
		}

		if ((header->freeListPtr) == 0){
    
			/* freelist empty */
			if ((header->recIdPtr - header->keyPtr) < (AM_si + AM_ss + recSize)){
				haveToAllocate=1;
				continue;
			}
				
			else
			{   
				int value2 = value;
    			AM_InsertToLeafNotFound(pageBuf,&value2,recNum,index,header);
				header->numKeys++;
    			bcopy(header,pageBuf,AM_sl);
				haveToAllocate=0;
				// recNum++;

		fscanf (file, "%d", &recNum);  
				index++;
				previousValue=value;				
				continue;
			}
		}

		else{ /* no place in the middle */
			if (((header->numinfreeList)*(AM_si + AM_ss) + header->recIdPtr -  header->keyPtr) > (recSize + AM_si + AM_ss))
			/*there is enough space in the freelist and in the middle put together */
			{
				char tempPage[PF_PAGE_SIZE];
	
				/* Compact the freelist so that we get enough space in the middle so that the new key can be inserted */
				AM_Compact(1,header->numKeys,pageBuf,tempPage,header);
				
				help("We think this should never happen\n");

				bcopy(tempPage,pageBuf,PF_PAGE_SIZE);
				bcopy(pageBuf,header,AM_sl);
				/* Insert into leaf a new key - no need to split */
				AM_InsertToLeafNotFound(pageBuf,&value,recNum,index,header);
				// sizeallocated=sizeallocated+AM_ss+attrLength;
				header->numKeys++;
				bcopy(header,pageBuf,AM_sl);
				haveToAllocate=0;
				// recNum++;
		fscanf (file, "%d", &recNum);  
				previousValue=value;
				continue;

			}
			else{ /* there is not enough room in the page */
				haveToAllocate=1;
				// recNum++;
		fscanf (file, "%d", &recNum);  
				index++;
				previousValue=value;
			}
		}
	}
	
	if(leafSize[0]==-1)
		leafSize[0]=MAXRECS;

	AM_LEAFHEADER thead,*theader;
	theader=&thead;
	bcopy(pageBuf,theader,AM_sl);
	sizeallocated=sizeallocated+(theader->numKeys)*(AM_ss+attrLength) + AM_sl;
	errVal = PF_UnfixPage(fileDesc,pageNum,TRUE);
	//AM_Check;
	
	/* Close the file */
	errVal = PF_CloseFile(fileDesc);
	AM_Check;
  	fclose(file);        


	return(AME_OK);

}
コード例 #6
0
ファイル: mergesort.c プロジェクト: sidutta/toydb
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");
    
    
}