BTree.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Function.h>
  8. #include <AK/NonnullRefPtr.h>
  9. #include <AK/NonnullRefPtrVector.h>
  10. #include <AK/Optional.h>
  11. #include <AK/RefPtr.h>
  12. #include <AK/String.h>
  13. #include <AK/Vector.h>
  14. #include <LibCore/File.h>
  15. #include <LibCore/Object.h>
  16. #include <LibSQL/Forward.h>
  17. #include <LibSQL/Heap.h>
  18. #include <LibSQL/Index.h>
  19. #include <LibSQL/Key.h>
  20. namespace SQL {
  21. /**
  22. * The BTree class models a B-Tree index. It contains a collection of
  23. * Key objects organized in TreeNode objects. Keys can be inserted,
  24. * located, deleted, and the set can be traversed in sort order. All keys in
  25. * a tree have the same underlying structure. A BTree's TreeNodes and
  26. * the keys it includes are lazily loaded from the Heap when needed.
  27. *
  28. * The classes implementing the B-Tree functionality are BTree, TreeNode,
  29. * BTreeIterator, and DownPointer (a smart pointer-like helper class).
  30. */
  31. class DownPointer {
  32. public:
  33. explicit DownPointer(TreeNode*, u32 = 0);
  34. DownPointer(TreeNode*, TreeNode*);
  35. DownPointer(DownPointer const&);
  36. DownPointer(TreeNode*, DownPointer&);
  37. ~DownPointer() = default;
  38. [[nodiscard]] u32 pointer() const { return m_pointer; }
  39. TreeNode* node();
  40. private:
  41. void inflate();
  42. TreeNode* m_owner;
  43. u32 m_pointer { 0 };
  44. OwnPtr<TreeNode> m_node { nullptr };
  45. friend TreeNode;
  46. };
  47. class TreeNode : public IndexNode {
  48. public:
  49. TreeNode(BTree&, TreeNode*, u32 = 0);
  50. TreeNode(BTree&, TreeNode*, TreeNode*, u32 = 0);
  51. TreeNode(BTree&, TreeNode*, u32 pointer, ByteBuffer&, size_t&);
  52. ~TreeNode() override = default;
  53. [[nodiscard]] BTree& tree() const { return m_tree; }
  54. [[nodiscard]] TreeNode* up() const { return m_up; }
  55. [[nodiscard]] size_t size() const { return m_entries.size(); }
  56. [[nodiscard]] Vector<Key> entries() const { return m_entries; }
  57. [[nodiscard]] u32 down_pointer(size_t) const;
  58. [[nodiscard]] TreeNode* down_node(size_t);
  59. [[nodiscard]] bool is_leaf() const { return m_is_leaf; }
  60. [[nodiscard]] size_t max_keys_in_node();
  61. Key const& operator[](size_t) const;
  62. bool insert(Key const&);
  63. bool update_key_pointer(Key const&);
  64. TreeNode* node_for(Key const&);
  65. Optional<u32> get(Key&);
  66. void serialize(ByteBuffer&) const override;
  67. IndexNode* as_index_node() override { return dynamic_cast<IndexNode*>(this); }
  68. private:
  69. TreeNode(BTree&, TreeNode*, DownPointer&, u32 = 0);
  70. void dump_if(int, String&& = "");
  71. bool insert_in_leaf(Key const&);
  72. void just_insert(Key const&, TreeNode* = nullptr);
  73. void split();
  74. void list_node(int);
  75. BTree& m_tree;
  76. TreeNode* m_up;
  77. Vector<Key> m_entries;
  78. bool m_is_leaf { true };
  79. Vector<DownPointer> m_down;
  80. friend BTree;
  81. friend BTreeIterator;
  82. };
  83. class BTree : public Index {
  84. C_OBJECT(BTree);
  85. public:
  86. ~BTree() override = default;
  87. u32 root() const { return (m_root) ? m_root->pointer() : 0; }
  88. bool insert(Key const&);
  89. bool update_key_pointer(Key const&);
  90. Optional<u32> get(Key&);
  91. BTreeIterator find(Key const& key);
  92. BTreeIterator begin();
  93. static BTreeIterator end();
  94. void list_tree();
  95. Function<void(void)> on_new_root;
  96. private:
  97. BTree(Heap& heap, NonnullRefPtr<TupleDescriptor> const&, bool unique, u32 pointer);
  98. BTree(Heap& heap, NonnullRefPtr<TupleDescriptor> const&, u32 pointer);
  99. void initialize_root();
  100. TreeNode* new_root();
  101. OwnPtr<TreeNode> m_root { nullptr };
  102. friend BTreeIterator;
  103. friend DownPointer;
  104. friend TreeNode;
  105. };
  106. class BTreeIterator {
  107. public:
  108. [[nodiscard]] bool is_end() const { return m_where == Where::End; }
  109. [[nodiscard]] size_t index() const { return m_index; }
  110. bool update(Key const&);
  111. bool operator==(BTreeIterator const& other) const { return cmp(other) == 0; }
  112. bool operator!=(BTreeIterator const& other) const { return cmp(other) != 0; }
  113. bool operator<(BTreeIterator const& other) const { return cmp(other) < 0; }
  114. bool operator>(BTreeIterator const& other) const { return cmp(other) > 0; }
  115. bool operator<=(BTreeIterator const& other) const { return cmp(other) <= 0; }
  116. bool operator>=(BTreeIterator const& other) const { return cmp(other) >= 0; }
  117. bool operator==(Key const& other) const { return cmp(other) == 0; }
  118. bool operator!=(Key const& other) const { return cmp(other) != 0; }
  119. bool operator<(Key const& other) const { return cmp(other) < 0; }
  120. bool operator>(Key const& other) const { return cmp(other) > 0; }
  121. bool operator<=(Key const& other) const { return cmp(other) <= 0; }
  122. bool operator>=(Key const& other) const { return cmp(other) >= 0; }
  123. BTreeIterator operator++()
  124. {
  125. *this = next();
  126. return *this;
  127. }
  128. BTreeIterator operator++(int)
  129. {
  130. *this = next();
  131. return *this;
  132. }
  133. BTreeIterator operator--()
  134. {
  135. *this = previous();
  136. return *this;
  137. }
  138. BTreeIterator const operator--(int)
  139. {
  140. *this = previous();
  141. return *this;
  142. }
  143. Key const& operator*() const
  144. {
  145. VERIFY(!is_end());
  146. return (*m_current)[m_index];
  147. }
  148. Key const& operator->() const
  149. {
  150. VERIFY(!is_end());
  151. return (*m_current)[m_index];
  152. }
  153. BTreeIterator& operator=(BTreeIterator const&);
  154. BTreeIterator(BTreeIterator const&) = default;
  155. private:
  156. BTreeIterator(TreeNode*, int index);
  157. static BTreeIterator end() { return BTreeIterator(nullptr, -1); }
  158. [[nodiscard]] int cmp(BTreeIterator const&) const;
  159. [[nodiscard]] int cmp(Key const&) const;
  160. [[nodiscard]] BTreeIterator next() const;
  161. [[nodiscard]] BTreeIterator previous() const;
  162. [[nodiscard]] Key key() const;
  163. enum class Where {
  164. Valid,
  165. End
  166. };
  167. Where m_where { Where::Valid };
  168. TreeNode* m_current { nullptr };
  169. int m_index { -1 };
  170. friend BTree;
  171. };
  172. }