1 #ifndef _BTREE_HPP_INCLUDED_ 2 #define _BTREE_HPP_INCLUDED_ 42 template<
typename T, std::
size_t M,
unsigned int BlockSize = BLOCK_SIZE>
49 static constexpr
auto Order = M;
82 bool create(
const char* filepath);
96 bool load(
const char* filepath);
102 void insert(
const T& value);
115 template <
typename U>
116 std::unique_ptr<T>
seek(
const U& key);
186 long children[2 * M + 2];
205 void initialize(
bool isLeaf =
true,
int size = 0);
222 template <
typename U>
223 std::unique_ptr<T>
seek(
const U& key,
BTree& tree)
const;
303 std::unique_ptr<OverflowResult>
insert(BNodeBlock& node, T value,
long rightNodeOffset = -1);
308 #endif // _BTREE_HPP_INCLUDED_ void writeHeader(const FileHeaderBlock &header)
Updates the file header in disk.
Definition: BTree.inl:151
Provides information to deal with an insertion overflow.
Definition: BTree.hpp:275
Block< BNode, BlockSize > BNodeBlock
Node block.
Definition: BTree.hpp:227
bool load(const char *filepath)
Initializes BTree for reading only.
Definition: BTree.inl:76
const Statistics & getStatistics(bool includeFileBlockCount=false) const
Returns the BTree usage statistics so far.
Definition: BTree.inl:97
BTree usage analytics.
Definition: BTree.hpp:119
Statistics m_stats
Where BTree stores its read and write statistics.
Definition: BTree.hpp:231
bool isLeaf
True if the node is a leaf, false if it's an internal node.
Definition: BTree.hpp:169
Block< FileHeader, BlockSize > FileHeaderBlock
File header block.
Definition: BTree.hpp:158
void writeToDisk(BNodeBlock &node)
Writes the node to disk.
Definition: BTree.inl:128
B-tree node.
Definition: BTree.hpp:161
std::FILE * m_file
File pointer to the file where data will be stored.
Definition: BTree.hpp:229
std::size_t size
Quantity of values currently stored in the node.
Definition: BTree.hpp:170
unsigned int blocksInDisk
Quantity of blocks stored in disk.
Definition: BTree.hpp:122
BNodeBlock m_root
Root node of the B-tree.
Definition: BTree.hpp:230
BTree()
Default constructor.
Definition: BTree.inl:40
unsigned int blocksRead
Quantity of blocks read since the tree was initialized.
Definition: BTree.hpp:120
unsigned int blocksCreated
Quantity of blocks created since the tree was initialized.
Definition: BTree.hpp:121
void insert(const T &value)
Inserts a value in the tree.
Definition: BTree.inl:157
std::unique_ptr< T > seek(const U &key)
Seeks a value that's equivalent to the one provided.
Definition: BTree.inl:92
bool create(const char *filepath)
Initializes BTree for writing.
Definition: BTree.inl:51
T ValueType
Type of the stored values.
Definition: BTree.hpp:46
void resetStatistics()
Reset all statistics values to 0.
Definition: BTree.inl:105
FileHeaderBlock readHeader() const
Reads the file header.
Definition: BTree.inl:140
void finishInsertions()
Updates the header with the total blocks in the file.
Definition: BTree.inl:110
~BTree()
Destructor.
Definition: BTree.inl:46
B-tree class.
Definition: BTree.hpp:43
static constexpr auto Order
Tree order.
Definition: BTree.hpp:49
T middle
The value that, after splitting nodes, shall be used as the middle value and must be inserted in the ...
Definition: BTree.hpp:276
long rightNode
Offset of the node that'll have to be the pointer to the right of the OverflowResult::middle value...
Definition: BTree.hpp:277
static constexpr auto BlockSizeInUse
Block size in bytes
Definition: BTree.hpp:52
BNodeBlock readFromDisk(long offset)
Read a node in the block at the provided offset.
Definition: BTree.inl:117
Union for reading and writing blocks containing serialized data.
Definition: Block.hpp:30
long offset
Disk address.
Definition: BTree.hpp:168