mirror of
https://github.com/aljazceru/turso.git
synced 2026-01-03 08:24:19 +01:00
## What does this fix This PR fixes an issue with BTree upwards traversal logic where we would try to go up to a parent node in `next()` even though we are at the very end of the btree. This behavior can leave the cursor incorrectly positioned at an interior node when it should be at the right edge of the rightmost leaf. ## Why doesn't it cause problems on main This bug is masked on `main` by every table `insert()` (wastefully) calling `find_cell()`: - `op_new_rowid` called, let's say the current max rowid is `666`. Cursor is left pointing at `666`. - `insert()` is called with rowid `667`, cursor is currently pointing at `666`, which is incorrect. - `find_cell()` does a binary search every time, and hence somewhat accidentally positions the cursor correctly _after_ `666` so that the insert goes to the correct place ## Why was this issue found in #1988, I am removing `find_cell()` entirely in favor of always performing a seek to the correct location - and skipping `seek` when it is not required, saving us from wasting a binary search on every insert - but this change means that we need to call `next()` after `op_new_rowid` to have the cursor positioned correctly at the new insertion slot. Doing this surfaces this upwards traversal bug in that PR branch. ## Details of solution - Store `cell_count` together with `cell_idx` in pagestack, so that chlidren can know whether their parents have reached their end without doing IO - To make this foolproof, pin pages on `PageStack` so the page cache cannot evict them during tree traversal - `cell_indices` renamed to `node_states` since it now carries more information (cell index AND count, instead of just index) Reviewed-by: Pere Diaz Bou <pere-altea@homail.com> Closes #2005