CtrlCenter.cpp
资源名称:minisqlc.rar [点击查看]
上传用户:lkd6667
上传日期:2015-05-13
资源大小:1448k
文件大小:19k
源码类别:
其他数据库
开发平台:
C/C++
- //!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;
- // 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
- }
- //////////////////////////////////////////////////////////////////////////////////////