BadgerDB
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends Pages
btree.h
1 
8 #pragma once
9 
10 #include <iostream>
11 #include <string>
12 #include "string.h"
13 #include <sstream>
14 
15 #include "types.h"
16 #include "page.h"
17 #include "file.h"
18 #include "buffer.h"
19 
20 namespace badgerdb
21 {
22 
27 {
28  INTEGER = 0,
29  DOUBLE = 1,
30  STRING = 2
31 };
32 
37 {
38  LT, /* Less Than */
39  LTE, /* Less Than or Equal to */
40  GTE, /* Greater Than or Equal to */
41  GT /* Greater Than */
42 };
43 
47 const int STRINGSIZE = 10;
48 
52 // sibling ptr key rid
53 const int INTARRAYLEAFSIZE = ( Page::SIZE - sizeof( PageId ) ) / ( sizeof( int ) + sizeof( RecordId ) );
54 
58 // sibling ptr key rid
59 const int DOUBLEARRAYLEAFSIZE = ( Page::SIZE - sizeof( PageId ) ) / ( sizeof( double ) + sizeof( RecordId ) );
60 
64 // sibling ptr key rid
65 const int STRINGARRAYLEAFSIZE = ( Page::SIZE - sizeof( PageId ) ) / ( 10 * sizeof(char) + sizeof( RecordId ) );
66 
70 // level extra pageNo key pageNo
71 const int INTARRAYNONLEAFSIZE = ( Page::SIZE - sizeof( int ) - sizeof( PageId ) ) / ( sizeof( int ) + sizeof( PageId ) );
72 
76 // level extra pageNo key pageNo -1 due to structure padding
77 const int DOUBLEARRAYNONLEAFSIZE = (( Page::SIZE - sizeof( int ) - sizeof( PageId ) ) / ( sizeof( double ) + sizeof( PageId ) )) - 1;
78 
82 // level extra pageNo key pageNo
83 const int STRINGARRAYNONLEAFSIZE = ( Page::SIZE - sizeof( int ) - sizeof( PageId ) ) / ( 10 * sizeof(char) + sizeof( PageId ) );
84 
89 template <class T>
90 class RIDKeyPair{
91 public:
92  RecordId rid;
93  T key;
94  void set( RecordId r, T k)
95  {
96  rid = r;
97  key = k;
98  }
99 };
100 
105 template <class T>
107 public:
108  PageId pageNo;
109  T key;
110  void set( int p, T k)
111  {
112  pageNo = p;
113  key = k;
114  }
115 };
116 
122 template <class T>
123 bool operator<( const RIDKeyPair<T>& r1, const RIDKeyPair<T>& r2 )
124 {
125  if( r1.key != r2.key )
126  return r1.key < r2.key;
127  else
128  return r1.rid.page_number < r2.rid.page_number;
129 }
130 
143  char relationName[20];
144 
149 
154 
159 };
160 
161 /*
162 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
163 These structures basically are the format in which the information is stored in the pages for the index file depending on what kind of
164 node they are. The level memeber of each non leaf structure seen below is set to 1 if the nodes
165 at this level are just above the leaf nodes. Otherwise set to 0.
166 */
167 
175  int level;
176 
181 
186 };
187 
195  int level;
196 
201 
206 };
207 
215  int level;
216 
221 
226 };
227 
231 struct LeafNodeInt{
236 
241 
247 };
248 
257 
262 
268 };
269 
278 
283 
289 };
290 
295 class BTreeIndex {
296 
297  private:
298 
302  File *file;
303 
307  BufMgr *bufMgr;
308 
312  PageId headerPageNum;
313 
317  PageId rootPageNum;
318 
322  Datatype attributeType;
323 
327  int attrByteOffset;
328 
332  int leafOccupancy;
333 
337  int nodeOccupancy;
338 
339 
340  // MEMBERS SPECIFIC TO SCANNING
341 
345  bool scanExecuting;
346 
350  int nextEntry;
351 
355  PageId currentPageNum;
356 
360  Page *currentPageData;
361 
365  int lowValInt;
366 
370  double lowValDouble;
371 
375  std::string lowValString;
376 
380  int highValInt;
381 
385  double highValDouble;
386 
390  std::string highValString;
391 
395  Operator lowOp;
396 
400  Operator highOp;
401 
402 
403  public:
404 
417  BTreeIndex(const std::string & relationName, std::string & outIndexName,
418  BufMgr *bufMgrIn, const int attrByteOffset, const Datatype attrType);
419 
420 
427  ~BTreeIndex();
428 
429 
439  const void insertEntry(const void* key, const RecordId rid);
440 
441 
457  const void startScan(const void* lowVal, const Operator lowOp, const void* highVal, const Operator highOp);
458 
459 
467  const void scanNext(RecordId& outRid); // returned record id
468 
469 
474  const void endScan();
475 
476 };
477 
478 }
Datatype
Datatype enumeration type.
Definition: btree.h:26
The central class which manages the buffer pool including frame allocation and deallocation to pages ...
Definition: buffer.h:161
const int INTARRAYNONLEAFSIZE
Number of key slots in B+Tree non-leaf for INTEGER key.
Definition: btree.h:71
RecordId ridArray[INTARRAYLEAFSIZE]
Definition: btree.h:240
const int DOUBLEARRAYLEAFSIZE
Number of key slots in B+Tree leaf for DOUBLE key.
Definition: btree.h:59
const int DOUBLEARRAYNONLEAFSIZE
Number of key slots in B+Tree leaf for DOUBLE key.
Definition: btree.h:77
int keyArray[INTARRAYLEAFSIZE]
Definition: btree.h:235
PageId pageNoArray[STRINGARRAYNONLEAFSIZE+1]
Definition: btree.h:225
double keyArray[DOUBLEARRAYLEAFSIZE]
Definition: btree.h:256
const int INTARRAYLEAFSIZE
Number of key slots in B+Tree leaf for INTEGER key.
Definition: btree.h:53
char keyArray[STRINGARRAYNONLEAFSIZE][STRINGSIZE]
Definition: btree.h:220
Class which represents a file in the filesystem containing database pages.
Definition: file.h:75
Operator
Scan operations enumeration. Passed to BTreeIndex::startScan() method.
Definition: btree.h:36
RecordId ridArray[DOUBLEARRAYLEAFSIZE]
Definition: btree.h:261
std::uint32_t PageId
Identifier for a page in a file.
Definition: types.h:15
static const std::size_t SIZE
Definition: page.h:114
PageId pageNoArray[DOUBLEARRAYNONLEAFSIZE+1]
Definition: btree.h:205
Structure for all non-leaf nodes when the key is of INTEGER type.
Definition: btree.h:171
char relationName[20]
Definition: btree.h:143
Class which represents a fixed-size database page containing records.
Definition: page.h:108
const void endScan()
Definition: btree.cpp:81
BTreeIndex(const std::string &relationName, std::string &outIndexName, BufMgr *bufMgrIn, const int attrByteOffset, const Datatype attrType)
Definition: btree.cpp:29
Structure for all non-leaf nodes when the key is of DOUBLE type.
Definition: btree.h:191
BTreeIndex class. It implements a B+ Tree index on a single attribute of a relation. This index supports only one scan at a time.
Definition: btree.h:295
const void scanNext(RecordId &outRid)
Definition: btree.cpp:72
const int STRINGARRAYLEAFSIZE
Number of key slots in B+Tree leaf for STRING key.
Definition: btree.h:65
PageId rightSibPageNo
Definition: btree.h:246
RecordId ridArray[STRINGARRAYLEAFSIZE]
Definition: btree.h:282
double keyArray[DOUBLEARRAYNONLEAFSIZE]
Definition: btree.h:200
Structure for all leaf nodes when the key is of INTEGER type.
Definition: btree.h:231
Identifier for a record in a page.
Definition: types.h:30
char keyArray[STRINGARRAYLEAFSIZE][STRINGSIZE]
Definition: btree.h:277
const void insertEntry(const void *key, const RecordId rid)
Definition: btree.cpp:51
Structure for all leaf nodes when the key is of STRING type.
Definition: btree.h:273
Structure to store a key page pair which is used to pass the key and page to functions that make any ...
Definition: btree.h:106
const int STRINGSIZE
Size of String key.
Definition: btree.h:47
Structure for all leaf nodes when the key is of DOUBLE type.
Definition: btree.h:252
Structure to store a key-rid pair. It is used to pass the pair to functions that add to or make chang...
Definition: btree.h:90
The meta page, which holds metadata for Index file, is always first page of the btree index file and ...
Definition: btree.h:139
int keyArray[INTARRAYNONLEAFSIZE]
Definition: btree.h:180
PageId pageNoArray[INTARRAYNONLEAFSIZE+1]
Definition: btree.h:185
const int STRINGARRAYNONLEAFSIZE
Number of key slots in B+Tree leaf for STRING key.
Definition: btree.h:83
Structure for all non-leaf nodes when the key is of STRING type.
Definition: btree.h:211
const void startScan(const void *lowVal, const Operator lowOp, const void *highVal, const Operator highOp)
Definition: btree.cpp:60