taco-db  0.1.0
BTree.h
Go to the documentation of this file.
1 #ifndef INDEX_BTREE_BTREE_H
2 #define INDEX_BTREE_BTREE_H
3 
4 #include "tdb.h"
5 
6 #include "catalog/IndexDesc.h"
8 #include "index/Index.h"
9 #include "index/IndexKey.h"
10 #include "storage/Record.h"
11 #include "storage/FileManager.h"
12 
13 namespace taco {
14 
22 
70 class BTree: public Index {
71 public:
72  class Iterator;
73 
74  static void Initialize(const IndexDesc *idxdesc);
75 
76  static std::unique_ptr<BTree> Create(
77  std::shared_ptr<const IndexDesc> idxdesc);
78 
79 private:
80  BTree(std::shared_ptr<const IndexDesc> idxdesc,
81  std::unique_ptr<File> file,
82  std::vector<FunctionInfo> lt_funcs,
83  std::vector<FunctionInfo> eq_funcs);
84 
85  std::unique_ptr<File> m_file;
86 
87  std::vector<FunctionInfo> m_lt_funcs;
88 
89  std::vector<FunctionInfo> m_eq_funcs;
90 
91 public:
92  virtual ~BTree();
93 
94  void BulkLoad(BulkLoadIterator &iter) override;
95 
96  bool InsertKey(const IndexKey *key, RecordId recid) override;
97 
98  bool DeleteKey(const IndexKey *key, RecordId &recid) override;
99 
100  std::unique_ptr<Index::Iterator> StartScan(const IndexKey *lower,
101  bool lower_isstrict,
102  const IndexKey *upper,
103  bool upper_isstrict) override;
104 
108  bool IsEmpty();
109 
113  uint32_t GetTreeHeight();
114 
115  class Iterator: public Index::Iterator {
116  public:
118  EndScan();
119  }
120 
121  bool Next() override;
122 
123  bool IsAtValidItem() override;
124 
125  const Record& GetCurrentItem() override;
126 
127  RecordId GetCurrentRecordId() override;
128 
129  void EndScan() override;
130 
131  private:
132  Iterator(BTree *btree,
133  ScopedBufferId bufid, SlotId one_before_matching_sid,
134  const IndexKey *key, bool upper_isstrict);
135 
137 
139 
141 
143 
144  std::vector<Datum> m_upper_data_buffer;
145 
147 
148  friend class BTree;
149  };
150 
151 private:
152  // in index/btree/BTreeUtils.cpp
153 
158  BufferId CreateNewBTreePage(bool isroot,
159  bool isleaf,
160  PageNumber prev_pid,
161  PageNumber next_pid);
162 
168 
174  void CreateLeafRecord(const IndexKey *key,
175  const RecordId &recid,
176  maxaligned_char_buf &recbuf);
177 
186  void CreateInternalRecord(const Record &child_recbuf,
187  PageNumber child_pid,
188  bool child_isleaf,
189  maxaligned_char_buf &recbuf);
190 
209  int BTreeTupleCompare(const IndexKey *key,
210  const RecordId &recid,
211  const char *tuplebuf,
212  bool isleaf) const;
213 
226  SlotId BinarySearchOnPage(char *buf,
227  const IndexKey *key,
228  const RecordId &recid);
229 
248  BufferId FindLeafPage(const IndexKey *key,
249  const RecordId &recid,
250  std::vector<PathItem> *p_search_path);
251 
274  const IndexKey *key,
275  const RecordId &recid,
276  std::vector<PathItem> *p_search_path);
277 
287  const RecordId &recid,
288  BufferId bufid);
289 
303  void InsertRecordOnPage(BufferId bufid,
304  SlotId insertion_sid,
305  maxaligned_char_buf &&recbuf,
306  std::vector<PathItem> &&search_path);
307 
315  void CreateNewRoot(BufferId bufid,
316  maxaligned_char_buf &&recbuf);
317 
343  SlotId insertion_sid,
344  maxaligned_char_buf &&recbuf);
345 
367  const RecordId &recid,
368  BufferId &bufid,
369  std::vector<PathItem> &search_path);
370 
376  void DeleteSlotOnPage(BufferId bufid, SlotId sid);
377 
389  void HandleMinPageUsage(BufferId bufid,
390  std::vector<PathItem> &&search_path);
391 
398  BufferId rbufid,
399  BufferId pbufid,
400  SlotId lsid);
401 
402 
414  bool MergePages(BufferId lbufid,
415  BufferId rbufid,
416  BufferId pbufid,
417  SlotId lsid);
418 
431  bool RebalancePages(BufferId lbufid,
432  BufferId rbufid,
433  BufferId pbufid,
434  SlotId lsid);
435 
436  // declare the gtest fixture as a friend class
437  friend class TestBTree;
438  friend class Iterator;
439 };
440 
441 } // namespace taco
442 
443 #endif // INDEX_BTREE_BTREE_H
Definition: BTree.h:115
bool Next() override
Moves the iterator to the next indexed item if any.
Definition: BTreeIterator.cpp:53
SlotId m_sid
Definition: BTree.h:138
bool m_upper_isstrict
Definition: BTree.h:146
RecordId GetCurrentRecordId() override
Returns the record id of the indexed item the iterator is currently at.
Definition: BTreeIterator.cpp:109
void EndScan() override
Ends the index scan.
Definition: BTreeIterator.cpp:114
const Record & GetCurrentItem() override
Returns the current indexed item the iterator is currently at, where the GetData() and GetLength() pa...
Definition: BTreeIterator.cpp:104
ScopedBufferId m_bufid
Definition: BTree.h:136
~Iterator()
Definition: BTree.h:117
std::vector< Datum > m_upper_data_buffer
Definition: BTree.h:144
Record m_rec
Definition: BTree.h:140
UniqueIndexKey m_upper
Definition: BTree.h:142
bool IsAtValidItem() override
Returns whether the iterator is currently at a valid indexed item.
Definition: BTreeIterator.cpp:99
A B-tree stored in the persistent files.
Definition: BTree.h:70
bool IsEmpty()
Returns whether this tree is empty.
Definition: BTree.cpp:109
BufferId GetBTreeMetaPage()
Pins the BTree meta page and returns the buffer ID of the buffer frame where it is pinned.
Definition: BTreeUtils.cpp:46
bool MergePages(BufferId lbufid, BufferId rbufid, BufferId pbufid, SlotId lsid)
Merges two sibling pages if there're enough space.
Definition: BTreeDelete.cpp:288
void HandleMinPageUsage(BufferId bufid, std::vector< PathItem > &&search_path)
Checks if a page pinned in buffer frame bufid needs to be merged or rebalanced with a sibling page sh...
Definition: BTreeDelete.cpp:139
friend class TestBTree
Definition: BTree.h:437
void CreateInternalRecord(const Record &child_recbuf, PageNumber child_pid, bool child_isleaf, maxaligned_char_buf &recbuf)
Creates a new internal record with separator key and heap record id from child_recbuf,...
Definition: BTreeUtils.cpp:67
int BTreeTupleCompare(const IndexKey *key, const RecordId &recid, const char *tuplebuf, bool isleaf) const
Compares a (key, recid) pair to a leaf or internal record in the B-tree.
Definition: BTreeSearch.cpp:8
bool RebalancePages(BufferId lbufid, BufferId rbufid, BufferId pbufid, SlotId lsid)
Rebalances two sibling pages if it is possible and it should choose a way that minimizes the differen...
Definition: BTreeDelete.cpp:395
void InsertRecordOnPage(BufferId bufid, SlotId insertion_sid, maxaligned_char_buf &&recbuf, std::vector< PathItem > &&search_path)
Inserts a record in recbuf onto a leaf page already pinned in the buffer frame bufid whose key range ...
Definition: BTreeInsert.cpp:95
SlotId BinarySearchOnPage(char *buf, const IndexKey *key, const RecordId &recid)
Uses binary search to find the last slot whose key-recid pair is smaller than or equal to the (key,...
Definition: BTreeSearch.cpp:46
static std::unique_ptr< BTree > Create(std::shared_ptr< const IndexDesc > idxdesc)
Definition: BTree.cpp:67
void CreateLeafRecord(const IndexKey *key, const RecordId &recid, maxaligned_char_buf &recbuf)
Creates a new leaf record with the given key and heap \recid in recbuf.
Definition: BTreeUtils.cpp:53
static void Initialize(const IndexDesc *idxdesc)
Definition: BTree.cpp:17
std::unique_ptr< File > m_file
Definition: BTree.h:85
bool InsertKey(const IndexKey *key, RecordId recid) override
Inserts the (key, rid) pair into the index.
Definition: BTreeInsert.cpp:6
std::unique_ptr< Index::Iterator > StartScan(const IndexKey *lower, bool lower_isstrict, const IndexKey *upper, bool upper_isstrict) override
Returns a forward-iterator for all the indexed items in the specified range defined by the arguments.
Definition: BTreeIterator.cpp:7
BufferId FindLeafPage(const IndexKey *key, const RecordId &recid, std::vector< PathItem > *p_search_path)
Finds a leaf page from the root such that:
Definition: BTreeSearch.cpp:83
bool TryMergeOrRebalancePages(BufferId lbufid, BufferId rbufid, BufferId pbufid, SlotId lsid)
Try to merge or reblanace two sibling pages.
Definition: BTreeDelete.cpp:266
BTree(std::shared_ptr< const IndexDesc > idxdesc, std::unique_ptr< File > file, std::vector< FunctionInfo > lt_funcs, std::vector< FunctionInfo > eq_funcs)
Definition: BTree.cpp:95
BufferId CreateNewBTreePage(bool isroot, bool isleaf, PageNumber prev_pid, PageNumber next_pid)
Allocates and initializes a new BTree internal or leaf page.
Definition: BTreeUtils.cpp:28
void BulkLoad(BulkLoadIterator &iter) override
Loads all the (key, record id) pairs provided by the iterator iter into an empty index.
Definition: BTreeBulkLoad.cpp:6
virtual ~BTree()
Definition: BTree.cpp:105
SlotId FindDeletionSlotIdOnLeaf(const IndexKey *key, const RecordId &recid, BufferId &bufid, std::vector< PathItem > &search_path)
Finds the slot to delete for the (key, recid) pair.
Definition: BTreeDelete.cpp:46
maxaligned_char_buf SplitPage(BufferId bufid, SlotId insertion_sid, maxaligned_char_buf &&recbuf)
Splits a page pinned in bufid that is too full to insert an additional record in recbuf at slot inser...
Definition: BTreeInsert.cpp:190
bool DeleteKey(const IndexKey *key, RecordId &recid) override
Deletes an arbitrary data entry with matching key if rid is invalid.
Definition: BTreeDelete.cpp:13
void DeleteSlotOnPage(BufferId bufid, SlotId sid)
Deletes a slot from a page.
Definition: BTreeDelete.cpp:122
uint32_t GetTreeHeight()
Returns the tree height.
Definition: BTree.cpp:123
SlotId FindInsertionSlotIdOnLeaf(const IndexKey *key, const RecordId &recid, BufferId bufid)
Finds the slot id where the inserting (key, recid) pair at that slot will keep the page's key and rec...
Definition: BTreeInsert.cpp:47
std::vector< FunctionInfo > m_lt_funcs
Definition: BTree.h:87
std::vector< FunctionInfo > m_eq_funcs
Definition: BTree.h:89
void CreateNewRoot(BufferId bufid, maxaligned_char_buf &&recbuf)
Creates a new root page and updates the B-Tree meta page.
Definition: BTreeInsert.cpp:141
BulkLoadIterator is an interface for providing (key, RecordId) pairs for index bulk loading.
Definition: BulkLoadIterator.h:17
Definition: IndexDesc.h:11
A forward-iterator for the items in the index.
Definition: Index.h:181
An interface class for index implementations.
Definition: Index.h:19
Definition: Record.h:87
Definition: datum.h:28
uint64_t BufferId
Definition: tdb_base.h:214
uint16_t SlotId
Definition: tdb_base.h:222
std::unique_ptr< IndexKey, AlignedAllocImpl::FreeMem > UniqueIndexKey
The returned smart pointer of IndexKey::Create().
Definition: IndexKey.h:10
uint32_t PageNumber
Definition: tdb_base.h:213
RecordId PathItem
Each PathItem is a (PageNumber, SlotId) pair of an B-tree internal record.
Definition: BTree.h:21
An IndexKey stores references to a few data (Datum objects) that comprise a key tuple to in an index.
Definition: IndexKey.h:35
The record ID of a record on a page is a pair of ‘(PageNumber, SlotId)’.
Definition: Record.h:17
std::vector< char, AlignedAllocImpl::aligned_allocator< 8, char > > maxaligned_char_buf
Definition: tdb_base.h:155