WiscDB
|
00001 00008 #pragma once 00009 00010 #include <iostream> 00011 #include <string> 00012 #include "string.h" 00013 #include <sstream> 00014 00015 #include "include/types.h" 00016 #include "include/page.h" 00017 #include "include/file.h" 00018 #include "include/buffer.h" 00019 00020 namespace wiscdb 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 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 pageNo = p; 00112 key = k; 00113 } 00114 }; 00115 00121 template <class T> 00122 bool operator<( const RIDKeyPair<T>& r1, const RIDKeyPair<T>& r2 ){ 00123 if( r1.key != r2.key ) 00124 return r1.key < r2.key; 00125 else 00126 return r1.rid.page_number < r2.rid.page_number; 00127 } 00128 00137 struct IndexMetaInfo{ 00141 char relationName[20]; 00142 00146 int attrByteOffset; 00147 00151 Datatype attrType; 00152 00156 PageId rootPageNo; 00157 }; 00158 00159 /* 00160 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 00161 These structures basically are the format in which the information is stored in the pages for the index file depending on what kind of 00162 node they are. The level memeber of each non leaf structure seen below is set to 1 if the nodes 00163 at this level are just above the leaf nodes. Otherwise set to 0. 00164 */ 00165 00169 struct NonLeafNodeInt{ 00173 int level; 00174 00178 int keyArray[ INTARRAYNONLEAFSIZE ]; 00179 00183 PageId pageNoArray[ INTARRAYNONLEAFSIZE + 1 ]; 00184 }; 00185 00189 struct NonLeafNodeDouble{ 00193 int level; 00194 00198 double keyArray[ DOUBLEARRAYNONLEAFSIZE ]; 00199 00203 PageId pageNoArray[ DOUBLEARRAYNONLEAFSIZE + 1 ]; 00204 }; 00205 00209 struct NonLeafNodeString{ 00213 int level; 00214 00218 char keyArray[ STRINGARRAYNONLEAFSIZE ][ STRINGSIZE ]; 00219 00223 PageId pageNoArray[ STRINGARRAYNONLEAFSIZE + 1 ]; 00224 }; 00225 00229 struct LeafNodeInt{ 00233 int keyArray[ INTARRAYLEAFSIZE ]; 00234 00238 RecordId ridArray[ INTARRAYLEAFSIZE ]; 00239 00244 PageId rightSibPageNo; 00245 }; 00246 00250 struct LeafNodeDouble{ 00254 double keyArray[ DOUBLEARRAYLEAFSIZE ]; 00255 00259 RecordId ridArray[ DOUBLEARRAYLEAFSIZE ]; 00260 00265 PageId rightSibPageNo; 00266 }; 00267 00271 struct LeafNodeString{ 00275 char keyArray[ STRINGARRAYLEAFSIZE ][ STRINGSIZE ]; 00276 00280 RecordId ridArray[ STRINGARRAYLEAFSIZE ]; 00281 00286 PageId rightSibPageNo; 00287 }; 00288 00293 class BTreeIndex { 00294 00295 private: 00296 00300 File *file; 00301 00305 BufferManager *bufferManager; 00306 00310 PageId headerPageNum; 00311 00315 PageId rootPageNum; 00316 00320 Datatype attributeType; 00321 00325 int attrByteOffset; 00326 00330 int leafOccupancy; 00331 00335 int nodeOccupancy; 00336 00337 00338 // MEMBERS SPECIFIC TO SCANNING 00339 00343 bool scanExecuting; 00344 00348 int nextEntry; 00349 00353 PageId currentPageNum; 00354 00358 Page *currentPageData; 00359 00363 int lowValInt; 00364 00368 double lowValDouble; 00369 00373 std::string lowValString; 00374 00378 int highValInt; 00379 00383 double highValDouble; 00384 00388 std::string highValString; 00389 00393 Operator lowOp; 00394 00398 Operator highOp; 00399 00400 00401 public: 00402 00415 BTreeIndex(const std::string & relationName, std::string & outIndexName, 00416 BufferManager *bufMgrIn, const int attrByteOffset, const Datatype attrType); 00417 00418 00425 ~BTreeIndex(); 00426 00427 00437 const void insertEntry(const void* key, const RecordId rid); 00438 00439 00455 const void startScan(const void* lowVal, const Operator lowOp, const void* highVal, const Operator highOp); 00456 00457 00465 const void scanNext(RecordId& outRid); // returned record id 00466 00467 00472 const void endScan(); 00473 00474 }; 00475 00476 }