BTreeIterator.cpp 8.3 KB

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