taco-db  0.1.0
BTreeInternal.h
Go to the documentation of this file.
1 #ifndef INDEX_BTREE_BTREEINTERNAL_H
2 #define INDEX_BTREE_BTREEINTERNAL_H
3 
4 #include "tdb.h"
5 
6 #include "index/btree/BTree.h"
7 #include "storage/Record.h"
9 
10 namespace taco {
11 
17 
23 
29 
33  static void Initialize(char *btmeta_buf, PageNumber root_pid);
34 };
35 
36 constexpr uint16_t BTREE_PAGE_ISROOT = 1u;
37 constexpr uint16_t BTREE_PAGE_ISLEAF = 2u;
38 
43 
47  uint16_t m_flags;
48 
54 
60 
66 
70  uint32_t m_reserved;
71 
72  constexpr bool
73  IsRootPage() const {
74  return m_flags & BTREE_PAGE_ISROOT;
75  }
76 
77  constexpr bool
78  IsLeafPage() const {
79  return m_flags & BTREE_PAGE_ISLEAF;
80  }
81 
86  static void Initialize(char *pagebuf,
87  uint16_t flags,
88  PageNumber prev_pid,
89  PageNumber next_pid);
90 };
91 
92 constexpr size_t BTreePageHeaderSize = sizeof(BTreePageHeaderData);
93 
102 
106  uint32_t m_reserved;
107 
112 
113  char*
114  GetPayload() const {
115  return const_cast<char*>(reinterpret_cast<const char*>(this)) +
117  }
118 };
119 
122 
131 
132  char*
133  GetPayload() const {
134  return const_cast<char*>(reinterpret_cast<const char*>(this)) +
136  }
137 };
138 
139 constexpr size_t BTreeLeafRecordHeaderSize =
141 
142 static inline char*
143 BTreeRecordGetPayload(char* recbuf, bool isleaf) {
144  if (isleaf) {
145  return ((BTreeLeafRecordHeaderData*) recbuf)->GetPayload();
146  } else {
147  return ((BTreeInternalRecordHeaderData*) recbuf)->GetPayload();
148  }
149 }
150 
151 static inline const char*
152 BTreeRecordGetPayload(const char* recbuf, bool isleaf) {
153  return BTreeRecordGetPayload(const_cast<char*>(recbuf), isleaf);
154 }
155 
156 static inline const RecordId&
157 BTreeRecordGetHeapRecordId(const char *recbuf, bool isleaf) {
158  if (isleaf) {
159  return ((const BTreeLeafRecordHeaderData*) recbuf)->m_recid;
160  } else {
161  return ((const BTreeInternalRecordHeaderData*) recbuf)->m_heap_recid;
162  }
163 }
164 
169 static inline FieldOffset
171  return ((FieldOffset) PAGE_SIZE) -
173  num_recs, totrlen);
174 }
175 
176 // This should be enough for fitting at least 3 internal records on an internal
177 // pages, or 2 leaf records on a leaf page, for 4KB pages.
178 //
179 // XXX This is not computed from VarlenDataPage impl because it's inconvenient
180 // to mark VarlenDataPage::ComputeFreeSpace() as constexpr in C++11 where
181 // constexpr function can only have one return statement (plus a few
182 // declarations).
183 // This should be computed as VarlenDataPage::BTreeComputePageUsage(3, 3 *
184 // BTreeInternalRecordHeaderData::HeaderSize) / 2, if we switch to C++14 or
185 // later C++ standard. (Note we do not need to store the first key on an
186 // internal page, hence / 2).
188 
190 
191 } // namespace taco
192 
193 #endif // INDEX_BTREE_BTREEINTERNAL_H
PageHeaderData defines the header of every virtual file data page.
Definition: FileManager.h:23
static FieldOffset ComputeFreeSpace(FieldOffset usr_data_sz, SlotId num_recs, FieldOffset total_reclen)
Computes the size of the free space on the page if there are num_recs records inserted to an empty Va...
Definition: VarlenDataPage.cpp:526
Definition: datum.h:28
constexpr FieldOffset BTREE_MAX_RECORD_SIZE
Definition: BTreeInternal.h:187
constexpr uint16_t BTREE_PAGE_ISLEAF
Definition: BTreeInternal.h:37
constexpr size_t BTreeLeafRecordHeaderSize
Definition: BTreeInternal.h:139
static FieldOffset BTreeComputePageUsage(SlotId num_recs, FieldOffset totrlen)
Returns the page usage of a B-Tree page given its number of records and its total length of all the r...
Definition: BTreeInternal.h:170
constexpr size_t BTreeInternalRecordHeaderSize
Definition: BTreeInternal.h:120
constexpr size_t BTreePageHeaderSize
Definition: BTreeInternal.h:92
constexpr SlotId MaxSlotId
The maximum valid slot ID.
Definition: tdb_base.h:264
uint16_t SlotId
Definition: tdb_base.h:222
int16_t FieldOffset
Definition: tdb_base.h:211
static const RecordId & BTreeRecordGetHeapRecordId(const char *recbuf, bool isleaf)
Definition: BTreeInternal.h:157
uint32_t PageNumber
Definition: tdb_base.h:213
constexpr FieldOffset BTREE_MIN_PAGE_USAGE
Definition: BTreeInternal.h:189
constexpr PageNumber RESERVED_PID
An invalid page number reserved for file manager internal use.
Definition: tdb_base.h:242
static const RecordId INFINITY_RECORDID
This is the record number that is guaranteed to be larger than any valid record ID.
Definition: BTreeInternal.h:16
constexpr uint16_t BTREE_PAGE_ISROOT
Definition: BTreeInternal.h:36
static char * BTreeRecordGetPayload(char *recbuf, bool isleaf)
Definition: BTreeInternal.h:143
The header of a B-Tree internal page record.
Definition: BTreeInternal.h:97
RecordId m_heap_recid
The record ID for making keys distinct.
Definition: BTreeInternal.h:111
char * GetPayload() const
Definition: BTreeInternal.h:114
uint32_t m_reserved
Currently unused.
Definition: BTreeInternal.h:106
PageNumber m_child_pid
The page number of the page this internal record points to.
Definition: BTreeInternal.h:101
The header of a B-Tree leaf page record.
Definition: BTreeInternal.h:126
RecordId m_recid
The record ID of the record this leaf record points to.
Definition: BTreeInternal.h:130
char * GetPayload() const
Definition: BTreeInternal.h:133
The B-tree meta page.
Definition: BTreeInternal.h:21
PageHeaderData m_ph
Definition: BTreeInternal.h:22
static void Initialize(char *btmeta_buf, PageNumber root_pid)
Initializes the B-tree meta page.
Definition: BTreeUtils.cpp:8
PageNumber m_root_pid
The page number of the B-Tree root page.
Definition: BTreeInternal.h:28
The B-Tree page header for both internal pages and the leaf pages.
Definition: BTreeInternal.h:42
uint16_t m_flags
Flags, see BTREE_PAGE_xxx above.
Definition: BTreeInternal.h:47
FieldOffset m_totrlen
The total length of index records on this page.
Definition: BTreeInternal.h:53
static void Initialize(char *pagebuf, uint16_t flags, PageNumber prev_pid, PageNumber next_pid)
Initializes the B-tree internal/leaf page as a data page, and then initializes the BTreePageHeaderDat...
Definition: BTreeUtils.cpp:14
uint32_t m_reserved
Currently unused.
Definition: BTreeInternal.h:70
PageNumber m_prev_pid
The previous page's page number on this level, or INVALID_PID if this page is the first page on this ...
Definition: BTreeInternal.h:59
PageNumber m_next_pid
The next page's page number on this level, or INVALID_PID if this page is the last page on this level...
Definition: BTreeInternal.h:65
constexpr bool IsRootPage() const
Definition: BTreeInternal.h:73
constexpr bool IsLeafPage() const
Definition: BTreeInternal.h:78
The record ID of a record on a page is a pair of ‘(PageNumber, SlotId)’.
Definition: Record.h:17
#define MAXALIGN(LEN)
Definition: tdb_base.h:327
constexpr const size_t PAGE_SIZE
Definition: tdb_base.h:159