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 struct 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 
229  SlotId BinarySearchOnPage(char *buf,
230  const IndexKey *key,
231  const RecordId &recid);
232 
251  BufferId FindLeafPage(const IndexKey *key,
252  const RecordId &recid,
253  std::vector<PathItem> *p_search_path);
254 
277  const IndexKey *key,
278  const RecordId &recid,
279  std::vector<PathItem> *p_search_path);
280 
290  const RecordId &recid,
291  BufferId bufid);
292 
306  void InsertRecordOnPage(BufferId bufid,
307  SlotId insertion_sid,
308  maxaligned_char_buf &&recbuf,
309  std::vector<PathItem> &&search_path);
310 
318  void CreateNewRoot(BufferId bufid,
319  maxaligned_char_buf &&recbuf);
320 
345  SlotId insertion_sid,
346  maxaligned_char_buf &&recbuf);
347 
369  const RecordId &recid,
370  BufferId &bufid,
371  std::vector<PathItem> &search_path);
372 
378  void DeleteSlotOnPage(BufferId bufid, SlotId sid);
379 
391  void HandleMinPageUsage(BufferId bufid,
392  std::vector<PathItem> &&search_path);
393 
400  BufferId rbufid,
401  BufferId pbufid,
402  SlotId lsid);
403 
404 
416  bool MergePages(BufferId lbufid,
417  BufferId rbufid,
418  BufferId pbufid,
419  SlotId lsid);
420 
433  bool RebalancePages(BufferId lbufid,
434  BufferId rbufid,
435  BufferId pbufid,
436  SlotId lsid);
437 
438  // declare the gtest fixture as a friend class
439  friend class TestBTree;
440  friend class Iterator;
441 };
442 
443 } // namespace taco
444 
445 #endif // INDEX_BTREE_BTREE_H
taco::BTree::CreateNewBTreePage
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
taco::BTree::TryMergeOrRebalancePages
bool TryMergeOrRebalancePages(BufferId lbufid, BufferId rbufid, BufferId pbufid, SlotId lsid)
Try to merge or reblanace two sibling pages.
Definition: BTreeDelete.cpp:262
taco::BTree::GetTreeHeight
uint32_t GetTreeHeight()
Returns the tree height.
Definition: BTree.cpp:123
taco::Index::Iterator::Iterator
Iterator()=default
taco::BTree::m_lt_funcs
std::vector< FunctionInfo > m_lt_funcs
Definition: BTree.h:87
taco::BTree::HandleMinPageUsage
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
taco::BTree::m_eq_funcs
std::vector< FunctionInfo > m_eq_funcs
Definition: BTree.h:89
taco::BTree::~BTree
virtual ~BTree()
Definition: BTree.cpp:105
Index.h
taco::BTree::DeleteSlotOnPage
void DeleteSlotOnPage(BufferId bufid, SlotId sid)
Deletes a slot from a page.
Definition: BTreeDelete.cpp:122
taco::BTree::RebalancePages
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:391
taco::BTree::Iterator::GetCurrentRecordId
RecordId GetCurrentRecordId() override
Returns the record id of the indexed item the iterator is currently at.
Definition: BTreeIterator.cpp:106
taco::BTree::IsEmpty
bool IsEmpty()
Returns whether this tree is empty.
Definition: BTree.cpp:109
taco
Definition: datum.h:28
taco::BTree::Iterator
Definition: BTree.h:115
taco::BTree::BTree
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
taco::BTree::TestBTree
friend class TestBTree
Definition: BTree.h:439
taco::BTree::InsertRecordOnPage
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
taco::SlotId
uint16_t SlotId
Definition: tdb_base.h:223
taco::BufferId
uint64_t BufferId
Definition: tdb_base.h:215
taco::BTree::Iterator::m_upper_isstrict
bool m_upper_isstrict
Definition: BTree.h:146
taco::BTree::FindDeletionSlotIdOnLeaf
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
taco::BTree::Iterator::~Iterator
~Iterator()
Definition: BTree.h:117
taco::BTree::CreateInternalRecord
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
taco::RecordId
The record ID of a record on a page is a pair of ‘(PageNumber, SlotId)’.
Definition: Record.h:17
taco::BTree::Iterator::m_bufid
ScopedBufferId m_bufid
Definition: BTree.h:136
taco::BTree::Create
static std::unique_ptr< BTree > Create(std::shared_ptr< const IndexDesc > idxdesc)
Definition: BTree.cpp:67
taco::BTree::BTreeTupleCompare
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
tdb.h
taco::BTree::Iterator::m_sid
SlotId m_sid
Definition: BTree.h:138
taco::BTree::InsertKey
bool InsertKey(const IndexKey *key, RecordId recid) override
Inserts the (key, rid) pair into the index.
Definition: BTreeInsert.cpp:6
taco::IndexKey
An IndexKey stores references to a few data (Datum objects) that comprise a key tuple to in an index.
Definition: IndexKey.h:35
taco::BTree::Iterator::m_upper_data_buffer
std::vector< Datum > m_upper_data_buffer
Definition: BTree.h:144
taco::BTree::SplitPage
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
taco::BTree::DeleteKey
bool DeleteKey(const IndexKey *key, RecordId &recid) override
Deletes any matching key (if rid is invalid), or the matching (key, rid) pair (if rid is valid) from ...
Definition: BTreeDelete.cpp:13
taco::BTree::BinarySearchOnPage
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
taco::BTree::Iterator::m_upper
UniqueIndexKey m_upper
Definition: BTree.h:142
taco::BTree::Iterator::m_rec
Record m_rec
Definition: BTree.h:140
maxaligned_char_buf
std::vector< char, AlignedAllocImpl::aligned_allocator< 8, char > > maxaligned_char_buf
Definition: tdb_base.h:155
taco::BTree::MergePages
bool MergePages(BufferId lbufid, BufferId rbufid, BufferId pbufid, SlotId lsid)
Merges two sibling pages if there're enough space.
Definition: BTreeDelete.cpp:284
taco::BulkLoadIterator
BulkLoadIterator is an interface for providing (key, RecordId) pairs for index bulk loading.
Definition: BulkLoadIterator.h:17
taco::BTree::m_file
std::unique_ptr< File > m_file
Definition: BTree.h:85
taco::ResourceGuard< BufferId, BufferUnpin, BufferId, INVALID_BUFID >
taco::Record
Definition: Record.h:87
taco::BTree::CreateNewRoot
void CreateNewRoot(BufferId bufid, maxaligned_char_buf &&recbuf)
Creates a new root page and updates the B-Tree meta page.
Definition: BTreeInsert.cpp:141
taco::BTree::BulkLoad
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
taco::UniqueIndexKey
std::unique_ptr< IndexKey, AlignedAllocImpl::FreeMem > UniqueIndexKey
The returned smart pointer of IndexKey::Create().
Definition: IndexKey.h:10
taco::BTree::Iterator::IsAtValidItem
bool IsAtValidItem() override
Returns whether the iterator is currently at a valid indexed item.
Definition: BTreeIterator.cpp:96
IndexDesc.h
taco::BTree::Iterator::Next
bool Next() override
Moves the iterator to the next indexed item if any.
Definition: BTreeIterator.cpp:50
taco::BTree::FindLeafPage
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
taco::Index::Iterator
A forward-iterator for the items in the index.
Definition: Index.h:180
taco::PathItem
RecordId PathItem
Each PathItem is a (PageNumber, SlotId) pair of an B-tree internal record.
Definition: BTree.h:21
taco::BTree::Initialize
static void Initialize(const IndexDesc *idxdesc)
Definition: BTree.cpp:17
taco::BTree::GetBTreeMetaPage
BufferId GetBTreeMetaPage()
Pins the BTree meta page and returns the buffer ID of the buffer frame where it is pinned.
Definition: BTreeUtils.cpp:46
taco::IndexDesc
Definition: IndexDesc.h:11
taco::BTree::StartScan
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
BulkLoadIterator.h
FileManager.h
taco::BTree::Iterator::GetCurrentItem
const Record & GetCurrentItem() override
Returns the current indexed item the iterator is currently at, where the GetData() and GetLength() pa...
Definition: BTreeIterator.cpp:101
taco::BTree
A B-tree stored in the persistent files.
Definition: BTree.h:70
taco::BTree::Iterator::EndScan
void EndScan() override
Ends the index scan.
Definition: BTreeIterator.cpp:111
taco::Index
An interface class for index implementations.
Definition: Index.h:19
taco::BTree::FindInsertionSlotIdOnLeaf
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
Record.h
taco::BTree::CreateLeafRecord
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
IndexKey.h
taco::PageNumber
uint32_t PageNumber
Definition: tdb_base.h:214