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

📄 ctrl_center.cpp

📁 实现一个精简型单用户SQL引擎(DBMS)MiniSQL
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/****************************************************************** 

** Filename : ZBtree.cpp

** Copyright (c) 2001-2002 Computer Science & Technology Zhejiang University 

** Editor   : zhousen(周森)

** Date     : 2001-12-18 To 2002-01-05

** Version  : 1.00

******************************************************************/

#include "iostream"
#include "Ctrl_Center.h"
#include "Glob_Var.h"
#include "catalog.h"
#include "Record.h"
#include "ZBTree.h"
extern _M_Buffer Buffer;

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

// IncludeMin == 0 --------  exclude min
// IncludeMin == 1 --------  include min
// IncludeMax == 0 --------  exclude max
// IncludeMax == 1 --------  include max
void Select( TB_Select_Info & SelectInfo)
{ 
  Condition_Info ConditionInfo;
  Select_Rec_Info SelectRecInfo;
  HCatalog catalog;
  Record   rec;
  // check the validity of the SelectInfo 
  catalog.Select(SelectInfo,ConditionInfo,SelectRecInfo);
  rec.PrintHead(SelectRecInfo);  // print the head

  ZBTree tree;
  if( tree.IsEmpty() )  // the tree is empty
    throw 1027;         // no record
  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();
      ZBTree_Node node;
      node.ReadNodeFromFile(max.ptr);
      // compare with the biggest key
      if( node.Compare(node.k[node.ItemOnNode-1],ConditionInfo.min) <= 0)
      throw 1027;  // Error 1027: No Reocrd
      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();
      ZBTree_Node node;
      node.ReadNodeFromFile(max.ptr);
      // compare with the biggest key
      if( node.Compare(node.k[node.ItemOnNode-1],ConditionInfo.min) < 0)
      throw 1027;  // Error 1027: No Reocrd
      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();
      ZBTree_Node node;
      node.ReadNodeFromFile(min.ptr);
      // compare with the smallest key
      if( node.Compare(node.k[0],ConditionInfo.max) >= 0)
        throw 1027;  // Error 1027: No Reocrd
      tree.Search(ConditionInfo.max,&max);
         
     /* Key_Location end;
      end = tree.GetEnd();*/
      ZBTree_Node 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();
      ZBTree_Node node;
      node.ReadNodeFromFile(min.ptr);
      if( node.Compare(node.k[0],ConditionInfo.max) > 0)
        throw 1027;// no record
      if( tree.Search(ConditionInfo.max,&max) )  // the max key is existed
        IncludeMax = 1;
      else 
      {
        /*Key_Location end;
        end = tree.GetEnd();*/
        ZBTree_Node 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) ) // the key isn't existed
      {
        min = tree.GetStart();
        max = tree.GetEnd();
        IncludeMin = 1;
        IncludeMax = 1;
        // print all record;
        Print(tree,SelectRecInfo,IncludeMin,min,IncludeMax,max);
        break;
      }
      else  // the key is existed
      {
        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:  // between ... and ...
    {                                        //  **************************************    
      Key_Location temp;                     //  6 conditions
      temp = tree.GetStart();                //    * * | |
      ZBTree_Node StartNode;                 //    | | * *    | -- the boundary of tree
      StartNode.ReadNodeFromFile(temp.ptr);  //    * | | *    * -- the value in SQL
                                             //    * | * |
      temp = tree.GetEnd();                  //    | * | * 
      ZBTree_Node EndNode;                   //    | * * |
      EndNode.ReadNodeFromFile(temp.ptr);    //  **************************************

      // compare with the start key  * * | |
      if( StartNode.Compare(StartNode.k[0],ConditionInfo.max) > 0)
        throw 1027;  // Error 1027: No Reocrd            
              
      // compare with the end key    | | * *
      if( EndNode.Compare(EndNode.k[EndNode.ItemOnNode - 1],ConditionInfo.min) < 0)
        throw 1027;  // Error 1027: No Reocrd

      // * | | *
      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;
        Print(tree,SelectRecInfo,IncludeMin,min,IncludeMax,max);
        break;
      }
      // * | * |  similiar as LE
      if( StartNode.Compare(StartNode.k[0],ConditionInfo.min) >= 0 &&
          EndNode.Compare(EndNode.k[EndNode.ItemOnNode - 1],ConditionInfo.max) > 0)
      {
        min = tree.GetStart();
        ZBTree_Node 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
          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;  // include the max key when printing
        Print(tree,SelectRecInfo,IncludeMin,min,IncludeMax,max); 
        break;
      }
      // | * | *  similiar as BE
      if( StartNode.Compare(StartNode.k[0],ConditionInfo.min) < 0 &&
          EndNode.Compare(EndNode.k[EndNode.ItemOnNode - 1],ConditionInfo.max) <= 0)
      {
        max = tree.GetEnd();
        ZBTree_Node 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;
       }

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

}
void Insert(TB_Insert_Info & InsertInfo)  //TB_Insert_Info
{
  pKey_Attr pKey = new Key_Attr;
  Key_Location KeyLoca;
  Rec_Info RecInfo;
  HCatalog catalog;
  Record   rec;
  ZBTree tree;
  // chech the validity of InsertInfo and produce pKey & RecInfo
  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);  // insert the key in the b+ tree
}

void Update(TB_Update_Info & UpdateInfo)
{
  Condition_Info ConditionInfo;
  Rec_Info RecInfo;
  HCatalog catalog;
  ZBTree tree;
  
  if( tree.IsEmpty() )  // the tree is empty
    throw 1027;         // no record
  // check the validity of UpdateInfo and produce Condition & RecInfo
  catalog.Update(UpdateInfo,ConditionInfo,RecInfo);
  
  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;
      UpdateRec(tree,RecInfo,IncludeMin,min,IncludeMax,max);
      break;
    }
    case B  :  // '>'
    {
      max = tree.GetEnd();
      ZBTree_Node node;
      node.ReadNodeFromFile(max.ptr);
      // compare with the biggest key
      if( node.Compare(node.k[node.ItemOnNode-1],ConditionInfo.min) <= 0)
      throw 1027;  // Error 1027: No Reocrd
      if( tree.Search(ConditionInfo.min,&min) )  // the min key is existed
        min = tree.MoveToNextKey(min);
      if( min.offset == node.MaxItem )

⌨️ 快捷键说明

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