BadgerDB
/afs/cs.wisc.edu/u/m/o/mohan/private/cs564/btree/btree/src/btree.h
00001 
00008 #pragma once
00009 
00010 #include <iostream>
00011 #include <string>
00012 #include "string.h"
00013 #include <sstream>
00014 
00015 #include "types.h"
00016 #include "page.h"
00017 #include "file.h"
00018 #include "buffer.h"
00019 
00020 namespace badgerdb
00021 {
00022 
00026 enum Datatype
00027 {
00028   INTEGER = 0,
00029   DOUBLE = 1,
00030   STRING = 2
00031 };
00032 
00036 enum Operator
00037 { 
00038   LT,   /* Less Than */
00039   LTE,  /* Less Than or Equal to */
00040   GTE,  /* Greater Than or Equal to */
00041   GT    /* Greater Than */
00042 };
00043 
00047 const  int STRINGSIZE = 10;
00048 
00052 //                                                  sibling ptr             key               rid
00053 const  int INTARRAYLEAFSIZE = ( Page::SIZE - sizeof( PageId ) ) / ( sizeof( int ) + sizeof( RecordId ) );
00054 
00058 //                                                     sibling ptr               key               rid
00059 const  int DOUBLEARRAYLEAFSIZE = ( Page::SIZE - sizeof( PageId ) ) / ( sizeof( double ) + sizeof( RecordId ) );
00060 
00064 //                                                    sibling ptr           key                      rid
00065 const  int STRINGARRAYLEAFSIZE = ( Page::SIZE - sizeof( PageId ) ) / ( 10 * sizeof(char) + sizeof( RecordId ) );
00066 
00070 //                                                     level     extra pageNo                  key       pageNo
00071 const  int INTARRAYNONLEAFSIZE = ( Page::SIZE - sizeof( int ) - sizeof( PageId ) ) / ( sizeof( int ) + sizeof( PageId ) );
00072 
00076 //                                                        level        extra pageNo                 key            pageNo   -1 due to structure padding
00077 const  int DOUBLEARRAYNONLEAFSIZE = (( Page::SIZE - sizeof( int ) - sizeof( PageId ) ) / ( sizeof( double ) + sizeof( PageId ) )) - 1;
00078 
00082 //                                                        level        extra pageNo             key                   pageNo
00083 const  int STRINGARRAYNONLEAFSIZE = ( Page::SIZE - sizeof( int ) - sizeof( PageId ) ) / ( 10 * sizeof(char) + sizeof( PageId ) );
00084 
00089 template <class T>
00090 class RIDKeyPair{
00091 public:
00092   RecordId rid;
00093   T key;
00094   void set( RecordId r, T k)
00095   {
00096     rid = r;
00097     key = k;
00098   }
00099 };
00100 
00105 template <class T>
00106 class PageKeyPair{
00107 public:
00108   PageId pageNo;
00109   T key;
00110   void set( int p, T k)
00111   {
00112     pageNo = p;
00113     key = k;
00114   }
00115 };
00116 
00122 template <class T>
00123 bool operator<( const RIDKeyPair<T>& r1, const RIDKeyPair<T>& r2 )
00124 {
00125   if( r1.key != r2.key )
00126     return r1.key < r2.key;
00127   else
00128     return r1.rid.page_number < r2.rid.page_number;
00129 }
00130 
00139 struct IndexMetaInfo{
00143   char relationName[20];
00144 
00148   int attrByteOffset;
00149 
00153   Datatype attrType;
00154 
00158   PageId rootPageNo;
00159 };
00160 
00161 /*
00162 Each node is a page, so once we read the page in we just cast the pointer to the page to this struct and use it to access the parts
00163 These structures basically are the format in which the information is stored in the pages for the index file depending on what kind of 
00164 node they are. The level memeber of each non leaf structure seen below is set to 1 if the nodes 
00165 at this level are just above the leaf nodes. Otherwise set to 0.
00166 */
00167 
00171 struct NonLeafNodeInt{
00175   int level;
00176 
00180   int keyArray[ INTARRAYNONLEAFSIZE ];
00181 
00185   PageId pageNoArray[ INTARRAYNONLEAFSIZE + 1 ];
00186 };
00187 
00191 struct NonLeafNodeDouble{
00195   int level;
00196 
00200   double keyArray[ DOUBLEARRAYNONLEAFSIZE ];
00201 
00205   PageId pageNoArray[ DOUBLEARRAYNONLEAFSIZE + 1 ];
00206 };
00207 
00211 struct NonLeafNodeString{
00215   int level;
00216 
00220   char keyArray[ STRINGARRAYNONLEAFSIZE ][ STRINGSIZE ];
00221 
00225   PageId pageNoArray[ STRINGARRAYNONLEAFSIZE + 1 ];
00226 };
00227 
00231 struct LeafNodeInt{
00235   int keyArray[ INTARRAYLEAFSIZE ];
00236 
00240   RecordId ridArray[ INTARRAYLEAFSIZE ];
00241 
00246   PageId rightSibPageNo;
00247 };
00248 
00252 struct LeafNodeDouble{
00256   double keyArray[ DOUBLEARRAYLEAFSIZE ];
00257 
00261   RecordId ridArray[ DOUBLEARRAYLEAFSIZE ];
00262 
00267   PageId rightSibPageNo;
00268 };
00269 
00273 struct LeafNodeString{
00277   char keyArray[ STRINGARRAYLEAFSIZE ][ STRINGSIZE ];
00278 
00282   RecordId ridArray[ STRINGARRAYLEAFSIZE ];
00283 
00288   PageId rightSibPageNo;
00289 };
00290 
00295 class BTreeIndex {
00296 
00297  private:
00298 
00302   File    *file;
00303 
00307   BufMgr  *bufMgr;
00308 
00312   PageId  headerPageNum;
00313 
00317   PageId  rootPageNum;
00318 
00322   Datatype  attributeType;
00323 
00327   int     attrByteOffset;
00328 
00332   int     leafOccupancy;
00333 
00337   int     nodeOccupancy;
00338 
00339 
00340   // MEMBERS SPECIFIC TO SCANNING
00341 
00345   bool    scanExecuting;
00346 
00350   int     nextEntry;
00351 
00355   PageId  currentPageNum;
00356 
00360   Page    *currentPageData;
00361 
00365   int     lowValInt;
00366 
00370   double  lowValDouble;
00371 
00375   std::string lowValString;
00376 
00380   int     highValInt;
00381 
00385   double  highValDouble;
00386 
00390   std::string highValString;
00391   
00395   Operator  lowOp;
00396 
00400   Operator  highOp;
00401 
00402   
00403  public:
00404 
00417   BTreeIndex(const std::string & relationName, std::string & outIndexName,
00418             BufMgr *bufMgrIn, const int attrByteOffset, const Datatype attrType);
00419   
00420 
00427   ~BTreeIndex();
00428 
00429 
00439   const void insertEntry(const void* key, const RecordId rid);
00440 
00441 
00457   const void startScan(const void* lowVal, const Operator lowOp, const void* highVal, const Operator highOp);
00458 
00459 
00467   const void scanNext(RecordId& outRid);  // returned record id
00468 
00469 
00474   const void endScan();
00475   
00476 };
00477 
00478 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends