BTreeIterator.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibSQL/BTree.h>
  7. namespace SQL {
  8. BTreeIterator::BTreeIterator(TreeNode* node, int index)
  9. : m_current(node)
  10. , m_index(index)
  11. {
  12. if (!node) {
  13. m_where = Where::End;
  14. } else {
  15. if (index < 0) {
  16. while (!node->is_leaf() && (node->size() != 0)) {
  17. node = node->down_node(0);
  18. }
  19. if (node->size() == 0) {
  20. m_where = Where::End;
  21. m_current = nullptr;
  22. m_index = -1;
  23. } else {
  24. m_where = Where::Valid;
  25. m_current = node;
  26. m_index = 0;
  27. }
  28. } else {
  29. VERIFY(m_index < (int)m_current->size());
  30. }
  31. }
  32. }
  33. int BTreeIterator::cmp(BTreeIterator const& other) const
  34. {
  35. if (is_end())
  36. return (other.is_end()) ? 0 : 1;
  37. if (other.is_end())
  38. return -1;
  39. VERIFY(&other.m_current->tree() == &m_current->tree());
  40. VERIFY((m_current->size() > 0) && (other.m_current->size() > 0));
  41. if (&m_current != &other.m_current)
  42. return (*m_current)[m_current->size() - 1].compare((*(other.m_current))[0]);
  43. return (*m_current)[m_index].compare((*(other.m_current))[other.m_index]);
  44. }
  45. int BTreeIterator::cmp(Key const& other) const
  46. {
  47. if (is_end())
  48. return 1;
  49. if (other.is_null())
  50. return -1;
  51. return key().compare(other);
  52. }
  53. BTreeIterator BTreeIterator::next() const
  54. {
  55. if (is_end())
  56. return end();
  57. auto ix = m_index;
  58. auto node = m_current;
  59. if (ix < (int)(node->size() - 1)) {
  60. if (node->is_leaf()) {
  61. // We're in the middle of a leaf node. Next entry is
  62. // is the next entry of the node:
  63. return BTreeIterator(node, ix + 1);
  64. } else {
  65. /*
  66. * We're in the middle of a non-leaf node. The iterator's
  67. * next value is all the way down to the right, first entry.
  68. *
  69. * |
  70. * +--+--+--+--+
  71. * | |##| | |
  72. * +--+--+--+--+
  73. * / | | | \
  74. * |
  75. * +--+--+--+--+
  76. * | | | | |
  77. * +--+--+--+--+
  78. * /
  79. * +--+--+--+--+
  80. * |++| | | |
  81. * +--+--+--+--+
  82. */
  83. ix++;
  84. while (!node->is_leaf()) {
  85. node = node->down_node(ix);
  86. ix = 0;
  87. }
  88. }
  89. VERIFY(node->is_leaf() && (ix < (int)node->size()));
  90. return BTreeIterator(node, ix);
  91. }
  92. if (node->is_leaf()) {
  93. // We currently at the last entry of a leaf node. We need to check
  94. // one or more levels up until we end up in the "middle" of a node.
  95. // If one level up we're still at the end of the node, we need
  96. // to keep going up until we hit the root node. If we're at the
  97. // end of the root node, we reached the end of the btree.
  98. for (auto up = node->up(); up; up = node->up()) {
  99. for (size_t i = 0; i < up->size(); i++) {
  100. // One level up, try to find the entry with the current
  101. // node's pointer as the left pointer:
  102. if (up->down_pointer(i) == node->block_index())
  103. // Found it. This is the iterator's next value:
  104. return BTreeIterator(up, (int)i);
  105. }
  106. // We didn't find the m_current's pointer as a left node. So
  107. // it must be the right node all the way at the end and we need
  108. // to go one more level up:
  109. node = up;
  110. }
  111. // We reached the root node and we're still at the end of the node.
  112. // That means we're at the end of the btree.
  113. return end();
  114. }
  115. // If we're at the end of a non-leaf node, we need to follow the
  116. // right pointer down until we find a leaf:
  117. TreeNode* down;
  118. for (down = node->down_node(node->size()); !down->is_leaf(); down = down->down_node(0))
  119. ;
  120. return BTreeIterator(down, 0);
  121. }
  122. // FIXME Reverse iterating doesn't quite work; we don't recognize the
  123. // end (which is really the beginning) of the tree.
  124. BTreeIterator BTreeIterator::previous() const
  125. {
  126. if (is_end())
  127. return end();
  128. auto node = m_current;
  129. auto ix = m_index;
  130. if (ix > 0) {
  131. if (node->is_leaf()) {
  132. // We're in the middle of a leaf node. Previous entry is
  133. // is the previous entry of the node:
  134. return BTreeIterator(node, ix - 1);
  135. } else {
  136. /*
  137. * We're in the middle of a non-leaf node. The iterator's
  138. * previous value is all the way down to the left, last entry.
  139. *
  140. * |
  141. * +--+--+--+--+
  142. * | | |##| |
  143. * +--+--+--+--+
  144. * / | | | \
  145. * |
  146. * +--+--+--+--+
  147. * | | | | |
  148. * +--+--+--+--+
  149. * \
  150. * +--+--+--+--+
  151. * | | | |++|
  152. * +--+--+--+--+
  153. */
  154. while (!node->is_leaf()) {
  155. node = node->down_node(ix);
  156. ix = (int)node->size();
  157. }
  158. }
  159. VERIFY(node->is_leaf() && (ix <= (int)node->size()));
  160. return BTreeIterator(node, ix);
  161. }
  162. if (node->is_leaf()) {
  163. // We currently at the first entry of a leaf node. We need to check one
  164. // or more levels up until we end up in the "middle" of a node.
  165. // If one level up we're still at the start of the node, we need
  166. // to keep going up until we hit the root node. If we're at the
  167. // start of the root node, we reached the start of the btree.
  168. auto stash_current = node;
  169. for (auto up = node->up(); up; up = node->up()) {
  170. for (size_t i = up->size(); i > 0; i--) {
  171. // One level up, try to find the entry with the current
  172. // node's pointer as the right pointer:
  173. if (up->down_pointer(i) == node->block_index()) {
  174. // Found it. This is the iterator's next value:
  175. node = up;
  176. ix = (int)i - 1;
  177. return BTreeIterator(node, ix);
  178. }
  179. }
  180. // We didn't find the m_current's pointer as a right node. So
  181. // it must be the left node all the way at the start and we need
  182. // to go one more level up:
  183. node = up;
  184. }
  185. // We reached the root node and we're still at the start of the node.
  186. // That means we're at the start of the btree.
  187. return BTreeIterator(stash_current, 0);
  188. }
  189. // If we're at the start of a non-leaf node, we need to follow the
  190. // left pointer down until we find a leaf:
  191. TreeNode* down = node->down_node(0);
  192. while (!down->is_leaf())
  193. down = down->down_node(down->size());
  194. return BTreeIterator(down, down->size() - 1);
  195. }
  196. Key BTreeIterator::key() const
  197. {
  198. if (is_end())
  199. return {};
  200. return (*m_current)[m_index];
  201. }
  202. bool BTreeIterator::update(Key const& new_value)
  203. {
  204. if (is_end())
  205. return false;
  206. if ((cmp(new_value) == 0) && (key().block_index() == new_value.block_index()))
  207. return true;
  208. auto previous_iter = previous();
  209. auto next_iter = next();
  210. if (!m_current->tree().duplicates_allowed() && ((previous_iter == new_value) || (next_iter == new_value))) {
  211. return false;
  212. }
  213. if ((previous_iter > new_value) || (next_iter < new_value))
  214. return false;
  215. // We are friend of BTree and TreeNode. Don't know how I feel about that.
  216. m_current->m_entries[m_index] = new_value;
  217. m_current->tree().serializer().serialize_and_write(*m_current);
  218. return true;
  219. }
  220. BTreeIterator& BTreeIterator::operator=(BTreeIterator const& other)
  221. {
  222. if (&other != this) {
  223. m_current = other.m_current;
  224. m_index = other.m_index;
  225. m_where = other.m_where;
  226. }
  227. return *this;
  228. }
  229. }