B-Trees
B-tree implementation in C++ to index documents based on numerical id's and title strings.
BTree.hpp
Go to the documentation of this file.
1 #ifndef _BTREE_HPP_INCLUDED_
2 #define _BTREE_HPP_INCLUDED_
3 
4 #include <cstdio>
5 #include <memory>
6 
7 #include "Block.hpp"
8 
10 
42 template<typename T, std::size_t M, unsigned int BlockSize = BLOCK_SIZE>
43 class BTree {
44 public:
46  typedef T ValueType;
47 
49  static constexpr auto Order = M;
50 
52  static constexpr auto BlockSizeInUse = BlockSize;
53 
55 
59  BTree();
60 
62 
63  ~BTree();
64 
66 
82  bool create(const char* filepath);
83 
85 
96  bool load(const char* filepath);
97 
99 
102  void insert(const T& value);
103 
105 
115  template <typename U>
116  std::unique_ptr<T> seek(const U& key);
117 
119  struct Statistics {
120  unsigned int blocksRead;
121  unsigned int blocksCreated;
122  unsigned int blocksInDisk;
123  };
124 
126 
137  const Statistics& getStatistics(bool includeFileBlockCount = false) const;
138 
140 
141  void resetStatistics();
142 
144 
148  void finishInsertions();
149 
150 private:
152  struct FileHeader {
154  unsigned int blockCount;
155  };
156 
159 
161  struct BNode {
163 
168  long offset;
169  bool isLeaf;
170  std::size_t size;
171 
173 
178  T values[2 * M + 1];
179 
181 
186  long children[2 * M + 2];
187 
189 
205  void initialize(bool isLeaf = true, int size = 0);
206 
208 
211  bool isFull() const;
212 
214 
222  template <typename U>
223  std::unique_ptr<T> seek(const U& key, BTree& tree) const;
224  };
225 
228 
229  std::FILE *m_file;
230  BNodeBlock m_root;
231  mutable Statistics m_stats;
232 
234 
242  BNodeBlock readFromDisk(long offset);
243 
245 
255  void writeToDisk(BNodeBlock& node);
256 
258 
266  FileHeaderBlock readHeader() const;
267 
269 
272  void writeHeader(const FileHeaderBlock& header);
273 
275  struct OverflowResult {
276  T middle;
277  long rightNode;
278  };
279 
281 
303  std::unique_ptr<OverflowResult> insert(BNodeBlock& node, T value, long rightNodeOffset = -1);
304 };
305 
306 #include "BTree.inl"
307 
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&#39;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
unsigned int blockCount
Definition: BTree.hpp:154
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
File header data for BTree indexes.
Definition: BTree.hpp:152
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&#39;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 rootAddress
Definition: BTree.hpp:153
long rightNode
Offset of the node that&#39;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