⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ctrlcenter.cpp

📁 用c语言实现的一个小型dbms
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		// the min key is existed
		if( tree.search(ConditionInfo.min,&min) )
			min = tree.moveToNextKey(min);
		if( min.offset == node.MaxItem )
			min = tree.moveToNextKey(min);
		IncludeMin = 1; IncludeMax =1;
		UpdateRec(tree, RecInfo, IncludeMin, min, IncludeMax, max);              
		break;
    }
    case BE : 
    {
		max = tree.getEnd();
		BPTreeNode node;
		node.readNodeFromFile(max.ptr);
		// compare with the biggest key
		if( node.compare(node.k[node.ItemOnNode-1],ConditionInfo.min) < 0)
			throw 1027;
		tree.search(ConditionInfo.min, &min);
		if( min.offset == node.MaxItem )
			min = tree.moveToNextKey(min);
		IncludeMin = 1; IncludeMax =1;
		UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,max);              
		break;
    }
    case L :
    {
		min = tree.getStart();
		BPTreeNode node;
		node.readNodeFromFile(min.ptr);
		// compare with the smallest key
		if( node.compare(node.k[0],ConditionInfo.max) >= 0)
			throw 1027;
		tree.search(ConditionInfo.max, &max);
         
		BPTreeNode EndNode;
		EndNode.readNodeFromFile(max.ptr);
		// exceed the biggest key in tree
		if( max.offset == EndNode.MaxItem ) {
			IncludeMax = 1;
			max.offset = EndNode.ItemOnNode - 1;
		}
		else 
			IncludeMax = 0;

		IncludeMin = 1;
		UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,max);      
		break;
    }
    case LE :
    {
		min = tree.getStart();
		BPTreeNode node;
		node.readNodeFromFile(min.ptr);

		if( node.compare(node.k[0],ConditionInfo.max) > 0)
			throw 1027;
		if( tree.search(ConditionInfo.max, &max) )
			IncludeMax = 1;
		else {
			BPTreeNode EndNode;
			EndNode.readNodeFromFile(max.ptr);
			// exceed the biggest key in tree
			if( max.offset == EndNode.MaxItem ) {
				IncludeMax = 1;
				max.offset = EndNode.ItemOnNode - 1;
			}
			else 
				IncludeMax = 0;
		}
		IncludeMin = 1;
		UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,max); 
		break;
    }
    case E :
    {
		if( !tree.search(ConditionInfo.min,&min) )
			throw 1026;   
		max = min;
		IncludeMin = 1;  IncludeMax = 1;
		UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,max);
		break;
    }
    case NE :
    {
		Key_Location temp;
		//condition update all records
		if( !tree.search(ConditionInfo.min,&temp) ) {
			min = tree.getStart();
			max = tree.getEnd();
			IncludeMin = 1;
			IncludeMax = 1;
			UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,max);
			break;
		}
		else { //condition having some nodes equal the key
			min = tree.getStart();
			max = tree.getEnd();      
			IncludeMin = 1;
			IncludeMax = 0;
			UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,temp);
			IncludeMin = 0;
			IncludeMax = 1;
			UpdateRec(tree,RecInfo,IncludeMin,temp,IncludeMax,max);
			break;
		}
    }
    case BETWEEN:
    {                                        
		Key_Location temp;                     
		temp = tree.getStart();              
		BPTreeNode StartNode;                 
		StartNode.readNodeFromFile(temp.ptr);  
                                           
		temp = tree.getEnd();                
		BPTreeNode EndNode;                  
		EndNode.readNodeFromFile(temp.ptr);

		// max < k[0]
		if( StartNode.compare(StartNode.k[0], ConditionInfo.max) > 0)
			throw 1027;               
		// k[n-1] < min
		if( EndNode.compare(EndNode.k[EndNode.ItemOnNode - 1], ConditionInfo.min) < 0)
			throw 1027;
		// update all records
		if( StartNode.compare(StartNode.k[0],ConditionInfo.min) >= 0 &&
			EndNode.compare(EndNode.k[EndNode.ItemOnNode - 1],ConditionInfo.max) <= 0) {
			// print all record
			min = tree.getStart();
			max = tree.getEnd();
			IncludeMin = 1;
			IncludeMax = 1;
			UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,max);
			break;
		}
		// update from k[0] to max
		if( StartNode.compare(StartNode.k[0],ConditionInfo.min) >= 0 &&
			EndNode.compare(EndNode.k[EndNode.ItemOnNode - 1],ConditionInfo.max) > 0) {
			min = tree.getStart();
			BPTreeNode node;
			node.readNodeFromFile(min.ptr);
			if( tree.search(ConditionInfo.max,&max) )
				IncludeMax = 1;
			else {
				Key_Location end;
				end = tree.getEnd();
				// exceed the biggest key in tree
				if( max.offset == node.MaxItem && max.ptr == end.ptr ) {
					IncludeMax = 1;
					max.offset = node.ItemOnNode - 1;
				}
				else if( max.offset == node.MaxItem ) {
					max = tree.moveToNextKey(max);
					IncludeMax = 0;
				}
				else 
					IncludeMax = 0;
			}
			IncludeMin = 1;  // include the max key when updating
			UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,max); 
			break;
		}
		// update from min to k[n-1]
		if( StartNode.compare(StartNode.k[0],ConditionInfo.min) < 0 &&
			EndNode.compare(EndNode.k[EndNode.ItemOnNode - 1],ConditionInfo.max) <= 0) {
			max = tree.getEnd();
			BPTreeNode node;
			node.readNodeFromFile(max.ptr);
			tree.search(ConditionInfo.min,&min);
			if( min.offset == node.MaxItem )
				min = tree.moveToNextKey(min);
			IncludeMin = 1; IncludeMax =1; 
			UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,max); 
			break;
		}
		// update from min to max
		if( StartNode.compare(StartNode.k[0], ConditionInfo.min) < 0 &&
			EndNode.compare(EndNode.k[EndNode.ItemOnNode - 1], ConditionInfo.max) > 0) {
			Key_Location temp;
			temp = tree.getEnd();
			BPTreeNode node;
			node.readNodeFromFile(temp.ptr);
			tree.search(ConditionInfo.min,&min);
			if( min.offset == node.MaxItem )
				min = tree.moveToNextKey(min);
			IncludeMin = 1; 
			if( tree.search(ConditionInfo.max,&max) )
				IncludeMax = 1;
			else {
				if( max.offset == node.MaxItem ) {
					max = tree.moveToNextKey(max);
					IncludeMax = 0;
				}
				else 
					IncludeMax = 0;
			}
			UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,max); 
			break;     
		}
	}
  }  
  cout<<"end update"<<endl; //===//
}

//////////////////////////////////////////////////////////////////////////////////////
// delete a record and adjust the B+ tree
void Delete( TB_Delete_Info& DeleteInfo ) {
	Condition_Info ConditionInfo;
	Key_Location KeyLoca;
	Rec_Info RecInfo;
	HCatalog catalog;
	Record   rec;
	BPTree tree;

	catalog.Delete(DeleteInfo,ConditionInfo);
	if( !tree.search(ConditionInfo.min, &KeyLoca) )
		throw 1026;  // Error 1026: the Key name isn't existed
	_F_FileAddr ptr;
	tree.myDelete(ConditionInfo.min, ptr); 
	rec.Delete(ptr);
}

//////////////////////////////////////////////////////////////////////////////////////
// print the record

void Print(BPTree & tree, Select_Rec_Info & SelectRecInfo,      
           bool IncludeMin, Key_Location min, bool IncludeMax, Key_Location max) {
	
	Record rec;
	Key_Location temp;

	if( (min == max) && ((0==IncludeMin) || (0==IncludeMax)) ) {
		return;
	}
	if( IncludeMin == 0 )
		min = tree.moveToNextKey(min);
	//not include the max
	if(IncludeMax == 0 ) {
		temp = min;
		_F_FileAddr ptr;
		if(temp == max )
			throw 1027;  // no record
		while( temp != max ) {
			ptr = tree.getCurRecAddr(temp);
			rec.Print(ptr, SelectRecInfo);     // print the record
			temp = tree.moveToNextKey(temp);  // move to next
		}
	}
	else { // include max
		temp = min;
		_F_FileAddr ptr;
		do {
			ptr = tree.getCurRecAddr(temp);
			rec.Print(ptr, SelectRecInfo);     // print the record
			if(temp == max)
				break;
			temp = tree.moveToNextKey(temp);  // move to next
		}while(1);
	}
}

//////////////////////////////////////////////////////////////////////////////////////
// update the record

void UpdateRec(BPTree & tree,Rec_Info & RecInfo,              
			   bool IncludeMin,Key_Location min,bool IncludeMax,Key_Location max) {
	cout<<"begin updateRec"<<endl; //====//
	Record rec;
	Key_Location temp;

	if( (min == max) && ((0==IncludeMin) || (0==IncludeMax)) )
		return;
	if( IncludeMin == 0 ) 
		min = tree.moveToNextKey(min);
	cout<<"in updaterec"<<endl; //===//
	if(IncludeMax == 0 ) {
		temp = min;
		_F_FileAddr ptr;
		if(temp == max )
			throw 1027;
		while( temp != max ) {
			ptr = tree.getCurRecAddr(temp);
			rec.Update(ptr,RecInfo); 
			temp = tree.moveToNextKey(temp);
		}
	}
	else { //( IncludeMax == 1)
		temp = min;
		_F_FileAddr ptr;
		do {
			ptr = tree.getCurRecAddr(temp);
			rec.Update(ptr,RecInfo); 
			if(temp == max)
				break;
			temp = tree.moveToNextKey(temp);
		} while(1);
	}
	cout<<"end updateRec"<<endl; //==//
}

//////////////////////////////////////////////////////////////////////////////////////
// drop the table

void Ctrl_DropTB() {
	char address[256];
	strcpy(address,CurLocation);
	strcat(address,CurRelationName);
	strcat(address,".dbf");
	_M_File table1 = Buffer[address];
	table1.Close();  // close the file whose path is 'address'

	strcpy(address,CurLocation);
	strcat(address,CurRelationName);
	DelTB(address);  // delete the file
}

//////////////////////////////////////////////////////////////////////////////////////
// drop the database

void Ctrl_DropDB() {
	Buffer.End();   // close the buffer,write all page from memory to file
	char address[256];
	strcpy(address,CurLocation);
	DelDB(address); // delete datebase 
	Buffer.Start(); // initialize a new buffer
}

//////////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -