Esempio n. 1
0
char* bufferManager::readInfo(CString table_name, int& size)
{	
	int num;
	CString a;
	address addr;
	char* info;

    addr.block_position = 0;
	addr.index_position = 1;
	num = readBlockFromDisk(addr.block_position);
    buffer[num].getNextLink(addr.index_position,addr);
	
    while (addr.index_position != -1)
	{
	   num = readBlockFromDisk(addr.block_position);
	   size = buffer[num].getDataSize(addr.index_position);
       info = new char[size];
       buffer[num].getData(addr.index_position, info);
	   a = info;
       if (a == table_name)
		   break;
	   else
		   delete [] info;
       buffer[num].getNextLink(addr.index_position,addr);
	}

	if (addr.index_position == -1)
	{
	    size = -1;
     	info = NULL;
	}
	return info;
}
Esempio n. 2
0
void bufferManager::rmTableEntry(CString table_name)
{
	address add, oldAdd, nextAdd;
	oldAdd.block_position = 0;
	oldAdd.index_position = 0;
	int i = readBlockFromDisk(0);
	buffer[i].getNextLink(0,add);
	CString name;
	while(add.block_position != -1 && add.index_position != -1)
	{
		char* content;
		int size;
		content = readData(add, size);
		name = content;
		nextAdd.block_position = ((int *)(content + size - 8))[0];
		nextAdd.index_position = ((int *)(content + size - 8))[1];
		if(name == table_name)
		{
			int j = readBlockFromDisk(oldAdd.block_position);
			buffer[j].setNextLink(oldAdd.index_position, nextAdd);
			int m = readBlockFromDisk(add.block_position);
			buffer[m].rmData(add.index_position);
			break;
		}
		delete [] content;
		oldAdd = add;
		add = nextAdd;
	}
}
Esempio n. 3
0
void bufferManager::rmInfo(CString table_name)
{
	int i = readBlockFromDisk(0);
	address add;
	buffer[i].getNextLink(1,add);

	address oldAdd, nextAdd;
	CString name;
	int size;
	oldAdd.block_position = 0;
	oldAdd.index_position = 1;
	int j;
	while(add.block_position != -1 && add.index_position != -1)
	{
		j = readBlockFromDisk(add.block_position);
		size = buffer[j].getDataSize(add.index_position);
		char* TableInfo = new char[size];
		buffer[j].getData(add.index_position,TableInfo);
		buffer[j].getNextLink(add.index_position,nextAdd);
		name = TableInfo;
		if(name == table_name)
		{
			int front = readBlockFromDisk(oldAdd.block_position);
			buffer[front].setNextLink(oldAdd.index_position, nextAdd);
			rmData(add);
			delete [] TableInfo;
			break;
		}
		delete [] TableInfo;
		oldAdd.block_position = add.block_position;
		oldAdd.index_position = add.index_position;
		add.block_position = nextAdd.block_position;
		add.index_position = nextAdd.index_position;
	}
}
Esempio n. 4
0
int join(int r, int s) {
    int addr_r = r, addr_s = s;
    int ret = BLKNUM;
    unsigned char * blkr, *blks;
    while(addr_r != 0) {
        blkr = readBlockFromDisk(addr_r, &buf);
        for(int ii = 0; ii < 7; ii++) {
            int a = *(blkr+ii*8+0);
            addr_s = s;
            while(addr_s != 0) {
                blks = readBlockFromDisk(addr_s, &buf);
                blk = getNewBlockInBuffer(&buf);
                for(int jj = 0; jj < 7; jj++) {
                    int c = *(blks+jj*8+0);
                    *(blk+jj*8+0) = a;
                    *(blk+jj*8+4) = c;
                }
                addr_s = *(blks+BLKSIZE-4);
                //printf(" addr = %d\n", addr_s);
                *(blk+BLKSIZE-4) = BLKNUM+1;
                writeBlockToDisk(blk,BLKNUM,&buf);
                BLKNUM++;
                freeBlockInBuffer(blks, &buf);
            }
        }
        addr_r = *(blkr+BLKSIZE-4);

        freeBlockInBuffer(blkr,&buf);
    }
    blk = readBlockFromDisk(BLKNUM-1, &buf);
    *(blk+BLKSIZE-4) = 0;
    writeBlockToDisk(blk, BLKNUM-1,&buf);
    return ret;
}
Esempio n. 5
0
int binaryselect(int addr, int val)
{
	int barray[16];
	Buffer buf;
	unsigned int *blkr = NULL, *blkw = NULL;
	int j = 0, i = 0;
	int waddr = 1300;
	//先把磁盘上之前的select结果块们删掉
	for (i = 0; i < 48; i++)
	{
		if (dropBlockOnDisk(waddr + i * 100) == -1) break;
	}
	if (!initBuffer(520, 64, &buf))
	{
		perror("Buffer Initialization Failed!\n");
		return -1;
	}
	//----------------------------------------------
	blkw = (unsigned int *)getNewBlockInBuffer(&buf);//要一块缓冲区块,用来暂存要写回磁盘的块
	for (i = 0; i < 16; i++) barray[i] = 0;
	i = 0;
	while (addr != 0)//读下一个块
	{
		if ((blkr = (unsigned int *)readBlockFromDisk(addr, &buf)) == NULL)
		{
			perror("Reading Block Failed!\n");
			return -1;
		}
		barray[i * 2] = *(blkr);
		barray[i * 2 + 1] = *(blkr + 12);
		addr = *(blkr + 15);
		i++;
	}
	addr = 8000+(binarysort(barray,val)/2);
	if ((blkr = (unsigned int *)readBlockFromDisk(addr, &buf)) == NULL)
	{
		perror("Reading Block Failed!\n");
		return -1;
	} 
	int flag = 0;
	for (i = 0; i < 8; i++)
	{
		if (*(blkr + i * 2) == val)
		{
			flag = 1; 
			break;
		}
	}
	if (flag = 1)
	{
		*blkw = *(blkr + i * 2);
		*(blkw + 1) = *(blkr + i * 2 + 1);
		writeBlockToDisk((unsigned char *)blkw, waddr, &buf);
		return waddr;
	}
	else return 0;
}
Esempio n. 6
0
address bufferManager::nextRecordPosition(address currentAdd)
{
	address nextAdd;
	int i = readBlockFromDisk(currentAdd.block_position);
	buffer[i].getRecordNextLink(currentAdd.index_position, nextAdd);
	return nextAdd;
}
Esempio n. 7
0
int main ()
{
	testResult_t *result = initializeTestResult();

	fileSystem_t *FS = getFS();

	FS->superBlock->blockBitmap[0] = 0b11111111;
	FS->superBlock->blockBitmap[1] = 0b11111111;
	FS->superBlock->blockBitmap[2] = 0b00001101;
	
	assert(findEmptyBlock() == 17, "empty block find test.", result);

	assert(FS->activeDir->entryCount == 0, "add entry test begin", result);

	activeFile_t * aF = openFileInternal(FS->activeDir, "TestFile");

	assert(FS->activeDir->entryCount == 1, "add entry test begin", result);

	directoryEntry_t dE = FS->activeDir->entries[0];
	fprintf(stdout, "%s, %d, %d\n", dE.name, dE.blockType, dE.startingBlockNum);
	fprintf(stdout, "%s, %d\n", aF->name, aF->curContentsPtr);

	fileBlock_t *fB;
	fB = malloc(sizeof(fileBlock_t));
	readBlockFromDisk(17, fB);
	fprintf(stdout, "%d\n", fB->blockType);

	assert(fB->blockType == 3, "read blockType", result);

	fprintf(stdout, "%s\n", fB->name);

	assert(findEmptyBlock() == 20, "bitmap affect test.", result);

	markBlockFree(17);
	assert(findEmptyBlock() == 17, "bitmap affect test.", result);
	memset(fB, 0, sizeof(fileBlock_t));
	readBlockFromDisk(17, fB);
	fprintf(stdout, "%d\n", fB->blockType);

	assert(fB->blockType == 0, "free block zero set test.", result);

	printTestResult(result);
	freeTestResult(result);
	return 0;
}
Esempio n. 8
0
char* bufferManager::readData(address position, int& size)
{
	int i = readBlockFromDisk(position.block_position);
	char* head;
	size = buffer[i].getDataSize(position.index_position);
	head = new char[size];
	buffer[i].getData(position.index_position, head);
	return head;
}
Esempio n. 9
0
// ***********************  tableInfo functions *****************************
void bufferManager::addInfo(char* head, int size)
{
	address store_add;
	int j = storeData(head,size,store_add);
	int i = readBlockFromDisk(0);
	address head_add;
	buffer[i].getData(1,(char *)&head_add);
	buffer[j].setNextLink(store_add.index_position, head_add);
	buffer[i].setNextLink(1,store_add);
}
Esempio n. 10
0
int findfirst(int start, int value) {
    int ans = BLKNUM;
    int addr = start;
    unsigned char *tarblk;
    tarblk = getNewBlockInBuffer(&buf);
    int i = 0;
    while(addr != 0) {
        blk = readBlockFromDisk(addr, &buf);
        for(int j = 0; j < group_in_blk; j++) {
            int a = *(blk+j*8+0);
            int b = *(blk+j*8+4);
            if(value == a) {
                *(tarblk+i*8+0)=a;
                *(tarblk+i*8+4)=b;
                i++;
                if(i >= 7) {
                    *(tarblk+BLKSIZE-4) = BLKNUM+1;
                    writeBlockToDisk(tarblk, BLKNUM, &buf);
                    tarblk = getNewBlockInBuffer(&buf);
                    BLKNUM++;
                    i = 0;
                }
            }
        }
        addr = *(blk+BLKSIZE-4);
        freeBlockInBuffer(blk, &buf);
    }
    if(i != 0) {
        for(; i<=7; i++) {
            *(tarblk+i*8+0)=0;
            *(tarblk+i*8+4)=0;
        }
        writeBlockToDisk(tarblk, BLKNUM, &buf);
        BLKNUM++;
    } else {
        blk = readBlockFromDisk(BLKNUM-1, &buf);
        *(blk+BLKSIZE-4) = 0;
        writeBlockToDisk(blk, BLKNUM, &buf);
    }
    return ans;
}
Esempio n. 11
0
void bufferManager::addRecord(CString table_name, char* head, int size)
{
	address add, dataAdd, nextAdd;
	findTableEntry(table_name, add);
	if(add.block_position == -1 && add.index_position == -1)
	{
		int i = addTableEntry(table_name,add);
		int j = storeData(head,size,dataAdd);
		buffer[i].setRecordFrontLink(add.index_position, dataAdd);
		buffer[i].setRecordNextLink(add.index_position, dataAdd);
		buffer[j].setRecordFrontLink(dataAdd.index_position, add);
		buffer[j].setRecordNextLink(dataAdd.index_position, add);
	}
	else
	{
		int i = readBlockFromDisk(add.block_position);
		buffer[i].getRecordNextLink(add.index_position, nextAdd);
		if(nextAdd.block_position != -1 && nextAdd.index_position != -1)
		{
			int j = storeData(head, size, dataAdd);
			buffer[i].getRecordNextLink(add.index_position, nextAdd);
			int m = readBlockFromDisk(nextAdd.block_position);

			buffer[j].setRecordFrontLink(dataAdd.index_position, add);
			buffer[j].setRecordNextLink(dataAdd.index_position, nextAdd);
			buffer[i].setRecordNextLink(add.index_position, dataAdd);
			buffer[m].setRecordFrontLink(nextAdd.index_position, dataAdd);
		}
		else
		{
			int j = storeData(head, size, dataAdd);
			buffer[j].setRecordFrontLink(dataAdd.index_position, add);
			buffer[j].setRecordNextLink(dataAdd.index_position, add);
			buffer[i].setRecordFrontLink(add.index_position, dataAdd);
			buffer[i].setRecordNextLink(add.index_position, dataAdd);
		}
	}
}
Esempio n. 12
0
void bufferManager::rmRecord(address add)
{
	int i = readBlockFromDisk(add.block_position);
	address frontAdd, nextAdd, end;
	end.block_position = end.index_position = -1;
	buffer[i].getRecordFrontLink(add.index_position, frontAdd);
	buffer[i].getRecordNextLink(add.index_position, nextAdd);
	if(frontAdd.block_position == nextAdd.block_position && 
		frontAdd.index_position == nextAdd.index_position)
	{
		buffer[i].rmData(add.index_position);
		i = readBlockFromDisk(frontAdd.block_position);
		buffer[i].setRecordFrontLink(frontAdd.index_position, end);
		buffer[i].setRecordNextLink(frontAdd.index_position, end);
	}
	else
	{
		buffer[i].rmData(add.index_position);
		i = readBlockFromDisk(frontAdd.block_position);
		buffer[i].setRecordNextLink(frontAdd.index_position, nextAdd);
		int j = readBlockFromDisk(nextAdd.block_position);	
		buffer[j].setRecordFrontLink(nextAdd.index_position, frontAdd);
	}
}
Esempio n. 13
0
int projection(int start) {
    int ans = BLKNUM;
    int i = 0;
    unsigned char *blks[6];
    for(int k = 0; k < 6; k++) {
        blks[k] = getNewBlockInBuffer(&buf);;
        for(int j = 0; j < 64; j++)
            *(blks[k]+j) = 0;
    }
    int addr = start;
    while(addr != 0) {
        blk = readBlockFromDisk(addr, &buf);
        for(int j = 0; j < group_in_blk; j++) {
            int a = *(blk+j*8+0);
            bool flag = false;
            for(int k = 0; k < 6; k++) {
                if(issaved(a, blks[k]))
                    flag = true;
            }
            if(!flag) {
                int ii = i%7;
                int jj = i/7;
                *(blks[jj]+ii*8+0) = a;
                i++;
            }
        }
        addr = *(blk+BLKSIZE-4);
        freeBlockInBuffer(blk, &buf);
    }
    int jj = i/7;
    for(int k = 0; k <= jj; k++) {
        if(k == jj)
            *(blks[k]+BLKSIZE-4) = 0;
        else
            *(blks[k]+BLKSIZE-4) = BLKNUM+1;
        writeBlockToDisk(blks[k],BLKNUM,&buf);
        BLKNUM++;
    }
    for(int k = jj+1; k < 6; k++) {
        freeBlockInBuffer(blks[k], &buf);
    }
    return ans;
}
Esempio n. 14
0
void printData(int start) {
    int addr = start;

    printf("Data of %d:\n", addr);
    while(addr != 0) {

        blk = readBlockFromDisk(addr, &buf);
        for(int j = 0; j < group_in_blk; j++) {
            int a = *(blk+j*8+0);
            int b = *(blk+j*8+4);
            if(a != 0)
                printf("%d",a);
            if(b != 0)
                printf("\t%d",b);
            printf("\n");
        }
        addr = *(blk+BLKSIZE-4);
        freeBlockInBuffer(blk, &buf);
    }
    printf("\n");
}
Esempio n. 15
0
int bufferManager::addTableEntry(CString table_name, address &return_value)
{
	int size = 0;
	size = table_name.GetLength() + 1 + 6*sizeof(int);
	char* tableEntryHead = new char[size];
	for(int i = 0; i < table_name.GetLength(); i++)
		tableEntryHead[i] = table_name.GetAt(i);
	tableEntryHead[i++] = '\0';
	((int *)(tableEntryHead + i))[0] = ((int *)(tableEntryHead + i))[1]
		= ((int *)(tableEntryHead + i))[2] = ((int *)(tableEntryHead + i))[3]
		= ((int *)(tableEntryHead + i))[4] = ((int *)(tableEntryHead + i))[5]
		= -1;

	int j = storeData(tableEntryHead, size, return_value);
	i = readBlockFromDisk(0);
	address head_add;
	buffer[i].getData(0,(char *)&head_add);
	buffer[j].setNextLink(return_value.index_position, head_add);
	buffer[i].setNextLink(0,return_value);
	delete [] tableEntryHead;
	return j;
}
Esempio n. 16
0
void bufferManager::findTableEntry(CString table_name, address& return_value)
{
	int i = readBlockFromDisk(0);
	address add;
	buffer[i].getData(0, (char *)&add);

	CString table;
	int size;
	char* tableEntry;
	while(add.block_position != -1 && add.index_position != -1)
	{
		tableEntry = readData(add, size);	
		table = tableEntry;
		if(table == table_name)
			break;
		add.block_position = ((int *)(tableEntry + size - 8))[0];
		add.index_position = ((int *)(tableEntry + size - 8))[1];
		delete [] tableEntry;
	}
	return_value.block_position = add.block_position;
	return_value.index_position = add.index_position;
}
Esempio n. 17
0
int main()
{
    struct A array_a[N_a];
    struct B array_b[N_b];
    struct A a;
    struct B b;
    Buffer buf; /* A buffer */
    unsigned char *blk; /* A pointer to a block */
    int * iblk;/*A input blk*/
    int iblk_count;/*Record the position of the last data in the iblk*/
    int * iblk2;/*A input blk*/
    int iblk2_count;/*Record the position of the last data in the iblk2*/
    int * oblk;/*A output blk*/
    int oblk_count;/*Record the position of the last data input in the oblk*/
    int i = 0;
    int j=0;
    int seq_a=1;/*The start sequance number of the relation R*/
    int seq_b=101;/*The start sequance number of the realtion S*/
    int seq_1=201;/*The start sequance number of the 1st operation*/
    int seq_2=301;/*The start sequance number of the 2nd operation*/
    int seq_3=401;/*The start sequance number of the 3nd operation*/
    srand((int)time(0));
    //Generate A and B
    for (i=0;i<N_a;i++)
    {
        array_a[i].A=i % 40+1;
        array_a[i].B=i%1000+1;
    }
    for (i=0;i<N_b;i++)
    {
        array_b[i].C=i%41+20;
        array_b[i].D=i%1000+1;
    }
    array_b[N_b-1].C=60;
    //Write data into disk


    /* Initialize the buffer */
    if (!initBuffer(520, 64, &buf))
    {
        perror("Buffer Initialization Failed!\n");
        return -1;
    }
    /*
        Fill data into the block
        And write the block into disk
    */
    /*Relation R*/
    j=0;
    while(j<N_a)
    {
         /* Get a new block in the buffer */
        blk = getNewBlockInBuffer(&buf);
        iblk=(int *)blk;
        for (i = 0; i < NumOFBlk-2; i=i+2)
        {
            *(iblk + i) = array_a[j].A;
            *(iblk+i+1)=array_a[j].B;
            j=j+1;
            //处理块未存满的情况
            if (j>=N_a&&i-1<NumOFBlk-2)
            {
                *(iblk+NumOFBlk-1)=0;
            }
        }
        if (j==N_a)
        {
            *(iblk+NumOFBlk-1)=0;
        }
        else
        {
            *(iblk+i+1)=seq_a+1;
        }
        /* Write the block to the hard disk */
        if (writeBlockToDisk(blk, seq_a, &buf) != 0)
        {
            perror("Writing Block Failed!\n");
            return -1;
        }
        seq_a++;
    }
    /*Relation S*/
    j=0;
    while(j<N_b)
    {
         /* Get a new block in the buffer */
        blk = getNewBlockInBuffer(&buf);
        iblk=(int *)blk;
        for (i = 0; i < NumOFBlk-2; i=i+2)
        {
            *(iblk + i) = array_b[j].C;
            *(iblk+i+1)=array_b[j].D;
            j=j+1;
            //处理块未存满的情况
            if (j>=N_b&&i-1<NumOFBlk-2)
            {
                *(iblk+NumOFBlk-1)=0;
            }
        }
        if (j==N_b)
        {
            *(iblk+NumOFBlk-1)=0;
        }
        else
        {
            *(iblk+i+1)=seq_b+1;
        }
        /* Write the block to the hard disk */
        if (writeBlockToDisk(blk, seq_b, &buf) != 0)
        {
            perror("Writing Block Failed!\n");
            return -1;
        }
        seq_b++;
    }
    //output the data to test the process is correct
    printf("***********Input Data***************\n");
    for (i=0;i<N_a;i++)
    {
        printf("%d:%d %d\n",i,array_a[i].A,array_a[i].B);
    }
    for (i=0;i<N_b;i++)
    {
        printf("%d:%d %d\n",i,array_b[i].C,array_b[i].D);
    }
    //read from the disk and to print
    seq_a=1;
    seq_b=101;

    //Read relation R from disk
    j=0;
    while(seq_a!=0)
    {

        if ((blk = readBlockFromDisk(seq_a, &buf)) == NULL)
        {
            perror("Reading Block Failed!\n");
            return -1;
        }
        iblk=(int *)blk;
        for (i=0;i<NumOFBlk-2;i=i+2)
        {
            a.A=*(iblk+i);
            a.B=*(iblk+i+1);
            printf("%d: %d %d\n",j,a.A,a.B);
            j++;
        }
        seq_a=*(iblk+NumOFBlk-1);
        freeBlockInBuffer(blk,&buf);
    }
    //Read relation S from disk
    j=0;
    while(seq_b!=0)
    {

        if ((blk = readBlockFromDisk(seq_b, &buf)) == NULL)
        {
            perror("Reading Block Failed!\n");
            return -1;
        }
        iblk=(int *)blk;
        for (i=0;i<NumOFBlk-2;i=i+2)
        {
            b.C=*(iblk+i);
            b.D=*(iblk+i+1);
            printf("%d: %d %d\n",j,b.C,b.D);
            j++;
        }
        seq_b=*(iblk+NumOFBlk-1);
        freeBlockInBuffer(blk,&buf);
    }

    /*
    1. select operation
    R.A=40 or S.C=60
    */
    oblk=(int *)getNewBlockInBuffer(&buf);
    oblk_count=0;
    seq_a=1;
    while(seq_a!=0)
    {
        if ((iblk=(int*)readBlockFromDisk(seq_a,&buf))==NULL)
        {
            perror("Reading Block Failed!\n");
            return -1;
        }
        for (i=0;i<NumOFBlk-2;i=i+2)
        {
            a.A=*(iblk+i);
            a.B=*(iblk+i+1);
            /*Put the data in the oblk*/
            if (a.A==40)
            {
                /*If the block is full then write into the disk*/
                if (oblk_count==NumOFBlk-2)
                {
                    *(oblk+NumOFBlk-2)=NumOFBlk-2;
                    *(oblk+NumOFBlk-1)=seq_1+1;
                    if (writeBlockToDisk((unsigned char*)oblk, seq_1, &buf) != 0)
                    {
                        perror("Writing Block Failed!\n");
                        return -1;
                    }
                    memset(oblk,0,16*sizeof(int));
                    oblk_count=0;
                    seq_1++;
                }
                if (oblk_count<=NumOFBlk-2-num_int_A)//the rest room is enough
                {
                    *(oblk+oblk_count)=a.A;
                    oblk_count++;
                    *(oblk+oblk_count)=a.B;
                    oblk_count++;
                }

            }
        }
        seq_a=*(iblk+NumOFBlk-1);
        freeBlockInBuffer((unsigned char*)iblk,&buf);
    }
    seq_b=101;
    while(seq_b!=0)
    {
        if ((iblk=(int*)readBlockFromDisk(seq_b,&buf))==NULL)
        {
            perror("Reading Block Failed!\n");
            return -1;
        }
        for (i=0;i<NumOFBlk-2;i=i+2)
        {
            b.C=*(iblk+i);
            b.D=*(iblk+i+1);
            /*Put the data in the oblk*/
            if (b.C==60)
            {
                /*If the block is full then write into the disk*/
                if (oblk_count==NumOFBlk-2)
                {
                    *(oblk+NumOFBlk-2)=NumOFBlk-2;
                    *(oblk+NumOFBlk-1)=seq_1+1;
                    if (writeBlockToDisk((unsigned char*)oblk, seq_1, &buf) != 0)
                    {
                        perror("Writing Block Failed!\n");
                        return -1;
                    }
                    oblk=(int *)getNewBlockInBuffer(&buf);
                    memset(oblk,0,16*sizeof(int));
                    oblk_count=0;
                    seq_1++;
                }
                if (oblk_count<=NumOFBlk-2-num_int_B)//the rest room is enough
                {
                    *(oblk+oblk_count)=b.C;
                    oblk_count++;
                    *(oblk+oblk_count)=b.D;
                    oblk_count++;
                }

            }
        }
        seq_b=*(iblk+NumOFBlk-1);
        freeBlockInBuffer((unsigned char*)iblk,&buf);
    }
    /*Output the last block*/
    *(oblk+NumOFBlk-2)=oblk_count;
    *(oblk+NumOFBlk-1)=0;
    if (writeBlockToDisk((unsigned char*)oblk, seq_1, &buf) != 0)
    {
        perror("Writing Block Failed!\n");
        return -1;
    }
    oblk = (int*)getNewBlockInBuffer(&buf);
    memset(oblk,0,16*sizeof(int));
    oblk_count=0;

    /*
    *Second Operation
    */

    seq_a=1;
    oblk_count=0;
    while(seq_a!=0)
    {
        if ((iblk=(int*)readBlockFromDisk(seq_a,&buf))==NULL)
        {
            perror("Reading Block Failed!\n");
            return -1;
        }
        for (i=0;i<NumOFBlk-2;i=i+2)
        {
            a.A=*(iblk+i);
            a.B=*(iblk+i+1);
            /*Put the data in the oblk*/
            /*If the block is full then write into the disk*/
            if (oblk_count==NumOFBlk-2)
            {
                *(oblk+NumOFBlk-2)=NumOFBlk-2;
                *(oblk+NumOFBlk-1)=seq_2+1;
                if (writeBlockToDisk((unsigned char*)oblk, seq_2, &buf) != 0)
                {
                    perror("Writing Block Failed!\n");
                    return -1;
                }
                oblk = (int*)getNewBlockInBuffer(&buf);
                memset(oblk,0,16*sizeof(int));
                oblk_count=0;
                seq_2++;
            }
            if (oblk_count<=NumOFBlk-2-1)//the rest room is enough
            {
                *(oblk+oblk_count)=a.A;
                oblk_count++;
            }


        }
        seq_a=*(iblk+NumOFBlk-1);
        freeBlockInBuffer((unsigned char*)iblk,&buf);
    }
    /*Output the last block*/
    *(oblk+NumOFBlk-2)=oblk_count;
    *(oblk+NumOFBlk-1)=0;
    if (writeBlockToDisk((unsigned char*)oblk, seq_2, &buf) != 0)
    {
        perror("Writing Block Failed!\n");
        return -1;
    }
    memset(oblk,0,16*sizeof(int));
    oblk_count=0;

    /*
        3rd Operation
    */
    /*set the sequence number of the relation R and S*/
    seq_a=1;
    seq_b=101;
    /*Apply for a new block for output block*/
    oblk=(int*)getNewBlockInBuffer(&buf);
    oblk_count=0;
    while(seq_a!=0)
    {
        /*Read a block of the R*/
        if ((iblk=(int*)readBlockFromDisk(seq_a,&buf))==NULL)
        {
            perror("Reading Block Failed!\n");
            return -1;
        }
        for (i=0;i<NumOFBlk-2;i=i+2)
        {
            a.A=*(iblk+i);
            a.B=*(iblk+i+1);
            /*Compared with every in S*/
            seq_b=101;
            while(seq_b!=0)
            {
                /*Read a block of the S*/
                if ((iblk2=(int*)readBlockFromDisk(seq_b,&buf))==NULL)
                {
                    perror("Reading Block Failed!\n");
                    return -1;
                }
                for (j=0;j<NumOFBlk-2;j=j+2)
                {
                    b.C=*(iblk2+j);
                    b.D=*(iblk2+j+1);
                    if (a.A==b.C)
                    {
                        /*Deal with the full output block*/
                        if (oblk_count>NumOFBlk-2-num_int_B-num_int_A)
                        {
                            *(oblk+NumOFBlk-2)=oblk_count;
                            *(oblk+NumOFBlk-1)=seq_3+1;
                            if (writeBlockToDisk((unsigned char*)oblk, seq_3, &buf) != 0)
                            {
                                perror("Writing Block Failed!\n");
                                return -1;
                            }
                            seq_3++;
                            /*Apply for a new output block*/
                            oblk=(int*)getNewBlockInBuffer(&buf);
                            oblk_count=0;
                        }
                        *(oblk+oblk_count)=a.A;
                        *(oblk+oblk_count+1)=a.B;
                        *(oblk+oblk_count+2)=b.C;
                        *(oblk+oblk_count+3)=b.D;
                        oblk_count=oblk_count+4;
                    }
                }
                seq_b=*(iblk2+NumOFBlk-1);
                freeBlockInBuffer((unsigned char*)iblk2,&buf);
            }
        }
        seq_a=*(iblk+NumOFBlk-1);
        freeBlockInBuffer((unsigned char*)iblk,&buf);
    }
    return 0;
}
Esempio n. 18
0
void bufferManager::rmData(address position)
{
	int i = readBlockFromDisk(position.block_position);
	buffer[i].rmData(position.index_position);
}
Esempio n. 19
0
int linearselect(int addr, int val)
{
	Buffer buf;
	unsigned int *blkr = NULL, *blkw = NULL;
	int j = 0, i = 0;
	int waddr = 1200;
	FILE *fo;
	//先把磁盘上之前的select结果块们删掉
	for (i = 0; i < 48; i++)
	{
		if (dropBlockOnDisk(waddr + i * 100) == -1)
			break;
	}

	if ((fo = fopen("OUTwhenSelect.txt", "wb")) == NULL)
	{
		printf("打不开文件whenSelect.txt\n");
		return 0;
	}

	if (!initBuffer(520, 64, &buf))
	{
		perror("Buffer Initialization Failed!\n");
		return -1;
	}
	//----------------------------------------------
	if ((blkr = (unsigned int *)readBlockFromDisk(addr, &buf)) == NULL)
	{
		perror("Reading Block Failed!\n");
		return -1;
	}

	blkw = (unsigned int *)getNewBlockInBuffer(&buf);//要一块缓冲区块,用来暂存要写回磁盘的块

	while (addr != 0)//读下一个块
	{
		fprintf(fo, "现在是第%d块\n", addr);
		for (i = 0; i < 7; i++)//遍历块中的元组
		{//处理读取块中的单个元组
			fprintf(fo, "A %d, B %d\n", *(blkr + 2 * i), *(blkr + 2 * i + 1));
			if (*(blkr + 2 * i) == val)
			{
				//把当前的查询到的复制到申请的缓存块中
				*(blkw + j) = *(blkr + 2 * i);
				*(blkw + j + 1) = *(blkr + 2 * i + 1);
				j = (j + 2) % 16;
				fprintf(fo, "^\n");

				if (j == 0)
				{
					*(blkw + 15) = waddr;
					waddr += 100;
					writeBlockToDisk((unsigned char *)blkw, waddr, &buf);
					//freeBlockInBuffer((unsigned char *)blkw,&buf);
					//blkw = (unsigned int *)getNewBlockInBuffer(&buf);//要一块缓冲区块,用来暂存要写回磁盘的块
				}
			}
		}

		addr = *(blkr + 15);
		freeBlockInBuffer((unsigned char *)blkr, &buf);
		printf("SELECT next: %d\n", addr);
		if (addr != 0)
		{
			if ((blkr = (unsigned int *)readBlockFromDisk(addr, &buf)) == NULL)
			{
				perror("Reading Block Failed!\n");
				return -1;
			}
		}
	}
	//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	//最后判断一下是否有往磁盘上写过块,若没有,则写一次。因为如果未写过,说明选择到的记录不到7条(不满一个blk)
	if (j != 0)
	{
		writeBlockToDisk((unsigned char *)blkw, waddr, &buf);
	}
	fclose(fo);
	freeBuffer(&buf);
	return waddr;
}