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

📄 ctrlcenter.cpp

📁 用c语言实现的一个小型dbms
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//!CtrlCenter.cpp

#include <iostream>
#include "CtrlCenter.h"
#include "Glob_Var.h"
#include "Catalog.h"
#include "Record.h"
#include "BPTree.h"
using namespace std;

extern _M_Buffer  Buffer;

//////////////////////////////////////////////////////////////////////////////////////
// PS:
// IncludeMin == 0 --------  exclude min
// IncludeMin == 1 --------  include min
// IncludeMax == 0 --------  exclude max
// IncludeMax == 1 --------  include max

//////////////////////////////////////////////////////////////////////////////////////
// this method creates the database file and the index file

void Create( TB_Create_Info& CreateInfo) {
	char KeyInfo[256];
	HCatalog catalog; 
	catalog.Create(CreateInfo,KeyInfo);  // create and initialize *.dbf file
	BPTree tree;
	tree.create(KeyInfo);                // create and initialize the *.idx file
}

//////////////////////////////////////////////////////////////////////////////////////
// this method selects the records and prints them out 

void Select( TB_Select_Info& SelectInfo) {
	Condition_Info ConditionInfo;  //for index
	Select_Rec_Info SelectRecInfo; //for record
	HCatalog catalog;
	Record   rec;
	// check the validity of the SelectInfo 
	catalog.Select(SelectInfo, ConditionInfo, SelectRecInfo);
	rec.PrintHead(SelectRecInfo);  // print the head

	BPTree tree;
	if( tree.isEmpty() )
		throw 1027;
	Key_Location min, max;
	bool IncludeMin,IncludeMax;
  
	// set the address of min and max keys in the select operation
	switch(ConditionInfo.OperType) {
	case ALL: {  // select all fields
		  min = tree.getStart();
		  max = tree.getEnd();
		  IncludeMin = 1;
		  IncludeMax = 1;
		  Print(tree, SelectRecInfo, IncludeMin, min, IncludeMax, max);
		  break;
		}
	case B : {
		  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;
		  if( tree.search(ConditionInfo.min, &min) )  // the min key is existed
				min = tree.moveToNextKey(min);
		  if( min.offset == node.MaxItem )
				min = tree.moveToNextKey(min);
		  IncludeMin = 1; IncludeMax =1;  // include the max&min key when printing
		  Print(tree, SelectRecInfo, 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 )  // can't find and reach the end of a node
				min = tree.moveToNextKey(min);
		  IncludeMin = 1; IncludeMax =1;  // include the max&min key when printing
		  Print(tree,SelectRecInfo,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;  // include the min key when printing
		  Print(tree, SelectRecInfo, 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) )  // the max key is existed
			IncludeMax = 1;
		  else 
		  {
			/*Key_Location end;
			end = tree.GetEnd();*/
			BPTreeNode EndNode;
			EndNode.readNodeFromFile(max.ptr);
			// exceed the biggest key in tree
			if( max.offset == EndNode.MaxItem/* && max.ptr == end.ptr*/ ) 
			{
			  IncludeMax = 1;
			  max.offset = EndNode.ItemOnNode - 1;
			}
       
			else 
			  IncludeMax = 0;
		  }
		  IncludeMin = 1;  // include the min key when printing
		  Print(tree,SelectRecInfo,IncludeMin,min,IncludeMax,max); 
		  break;
		}
    case E  :
		  if( !tree.search(ConditionInfo.min,&min) )
				throw 1026;  // Error 1026: the Key name isn't existed    
		  max = min;
		  IncludeMin = 1;  IncludeMax = 1;
		  Print(tree,SelectRecInfo,IncludeMin,min,IncludeMax,max);
		  break;
    case NE :
		  Key_Location temp;
		  if( !tree.search(ConditionInfo.min,&temp) ) {
				min = tree.getStart();
				max = tree.getEnd();
				IncludeMin = 1;
				IncludeMax = 1;
				// print all record;
				Print(tree,SelectRecInfo,IncludeMin,min,IncludeMax,max);
				break;
		  }
		  else {
			min = tree.getStart();
			max = tree.getEnd();
			IncludeMin = 1;
			IncludeMax = 1;                       
			// the key is in the middle of the tree
			IncludeMin = 1;
			IncludeMax = 0;
			Print(tree,SelectRecInfo,IncludeMin,min,IncludeMax,temp);
			IncludeMin = 0;
			IncludeMax = 1;
			Print(tree,SelectRecInfo,IncludeMin,temp,IncludeMax,max);
			break;
		  }
    case BETWEEN: {
		  //the first leaf node
		  Key_Location temp;                     
		  temp = tree.getStart();             
		  BPTreeNode StartNode;                 
		  StartNode.readNodeFromFile(temp.ptr);  
		  //the last leaf node											
		  temp = tree.getEnd();                
		  BPTreeNode EndNode;                 
		  EndNode.readNodeFromFile(temp.ptr);   

		  // two conditions throw error
		  if( StartNode.compare(StartNode.k[0], ConditionInfo.max) > 0)
				throw 1027;              
		  if( EndNode.compare(EndNode.k[EndNode.ItemOnNode - 1], ConditionInfo.min) < 0)
				throw 1027;

		  // print all records
		  if( StartNode.compare(StartNode.k[0], ConditionInfo.min) >= 0 &&
			  EndNode.compare(EndNode.k[EndNode.ItemOnNode - 1], ConditionInfo.max) <= 0) {
			min = tree.getStart();
			max = tree.getEnd();
			IncludeMin = 1;
			IncludeMax = 1;
			Print(tree,SelectRecInfo,IncludeMin,min,IncludeMax,max);
			break;
		  }
		  // print from the leftest to some middle record(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) )  // the max key is existed
					  IncludeMax = 1;
				else {
					  Key_Location end;
					  end = tree.getEnd();
					  // exceed the biggest key in tree
					  // may not need here
					  if( max.offset == node.MaxItem && max.ptr == end.ptr )  
					  {
						IncludeMax = 1;
						max.offset = node.ItemOnNode - 1;
					  }
					  else if( max.offset == node.MaxItem )  // exceed the biggest key in node
					  {
						max = tree.moveToNextKey(max);       // move to the next key
						IncludeMax = 0;
					  }
					  else 
						IncludeMax = 0;
				}      
				IncludeMin = 1;
				Print(tree, SelectRecInfo, IncludeMin, min, IncludeMax, max); 
				break;
		  }
		  // print from min to max
		  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 )  // can't find and reach the end of a node
					min = tree.moveToNextKey(min);
				IncludeMin = 1;   IncludeMax =1;  // include the max&min key when printing
				Print(tree, SelectRecInfo, IncludeMin, min, IncludeMax, max); 
				break;
		   }

		  // print from min to the last record
		  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 )  // can't find and reach the end of a node
				  min = tree.moveToNextKey(min);
				IncludeMin = 1;   // include the min key when printing

				if( tree.search(ConditionInfo.max,&max) )  // the max key is existed
				  IncludeMax = 1;
				else 
				{
				  if( max.offset == node.MaxItem )  // exceed the biggest key in node
				  {
					max = tree.moveToNextKey(max);
					IncludeMax = 0;
				  }
				  else 
					IncludeMax = 0;
				}
				Print(tree,SelectRecInfo,IncludeMin,min,IncludeMax,max); 
				break;     
		  }
		}
	}
	rec.PrintEnd(SelectRecInfo);
}     

/////////////////////////////////////////////////////////////////////////////////////////////
// insert a new record and adjust the B+ tree

void Insert( TB_Insert_Info& InsertInfo ) {
	pKey_Attr pKey = new Key_Attr;
	Key_Location KeyLoca;
	Rec_Info RecInfo;
	HCatalog catalog;
	Record   rec;
	BPTree tree;

	catalog.Insert(InsertInfo, (*pKey), RecInfo);
	if( tree.search(pKey, &KeyLoca) )
		throw 1025;  // Error 1025: the Key is existed
	_F_FileAddr pRec = rec.Insert(RecInfo);
	tree.insert(pKey, pRec);
}

//////////////////////////////////////////////////////////////////////////////////////
// update a record 

void Update( TB_Update_Info& UpdateInfo) {
	cout<<"begin update"<<endl; //===//
	Condition_Info ConditionInfo;
	Rec_Info RecInfo;
	HCatalog catalog;
	BPTree tree;
	//error if index tree is empty
	if( tree.isEmpty() )
		throw 1027; 
	cout<<"begin catalog update"<<endl; //====//
	catalog.Update(UpdateInfo, ConditionInfo, RecInfo);
	cout<<"end catalog update"<<endl; //====//
  
	Key_Location min,max;
	bool IncludeMin,IncludeMax;

	switch(ConditionInfo.OperType) {
    case ALL:
    {
		min = tree.getStart();
		max = tree.getEnd();
		IncludeMin = 1;
		IncludeMax = 1;
		UpdateRec(tree, RecInfo, IncludeMin, min, IncludeMax, max);
		break;
    }
    case B :
    {
		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; 

⌨️ 快捷键说明

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