BTree.h 5.8 KB

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