Commit Graph

614 Commits

Author SHA1 Message Date
Jussi Saurio
3ba9f2ab97 Small cleanups to pager/wal/vdbe - mostly naming
- Instead of using a confusing CheckpointStatus for many different things,
  introduce the following statuses:
    * PagerCacheflushStatus - cacheflush can result in either:
      - the WAL being written to disk and fsynced
      - but also a checkpoint to the main BD file, and fsyncing the main DB file

      Reflect this in the type.
    * WalFsyncStatus - previously CheckpointStatus was also used for this, even
      though fsyncing the WAL doesn't checkpoint.
    * CheckpointStatus/CheckpointResult is now used only for actual checkpointing.

- Rename HaltState to CommitState (program.halt_state -> program.commit_state)
- Make WAL a non-optional property in Pager
  * This gets rid of a lot of if let Some(...) boilerplate
  * For ephemeral indexes, provide a DummyWAL implementation that does nothing.
- Rename program.halt() to program.commit_txn()
- Add some documentation comments to structs and functions
2025-05-26 10:37:34 +03:00
Jussi Saurio
385c0d8987 clippy stfu part 2: electric boogaloo 2025-05-25 10:32:23 +03:00
Jussi Saurio
cbb56a182e Fix bug: backwards iteration of table btree hangs 2025-05-23 14:23:18 +03:00
Zaid Humayun
4312d371fb addresses comment https://github.com/tursodatabase/limbo/pull/1548#discussion_r2102606810 by @jussisaurio
this commit changes the btree_destroy() signature to return an Option<usize>. This more closely resembles Rust semantics instead of passing a pointer to a usize.
However, I'm unsure if I'm handling the cursor result correctly
2025-05-23 00:46:05 +05:30
Zaid Humayun
4072a41c9c Drop Table now uses an ephemeral table as a scratch table
Now when dropping a table, an ephemeral table is created as a scratch table. If a root page of some other table is moved into the page occupied by the root page of the table being dropped, that row is first written into an ephemeral table. Then on a next pass, it is deleted from the schema table and then re-inserted with the new root page.

This happens during AUTOVACUUM when deleting a root page will force the last root page to move into the slot being vacated by the root page of the table being deleted
2025-05-22 19:39:46 +05:30
Pere Diaz Bou
b135bf449f reduce attempts for fuzz_long overflow 2025-05-21 15:40:42 +02:00
Pere Diaz Bou
7143e43dd4 clippy 2025-05-21 15:27:15 +02:00
Pere Diaz Bou
4704cdd24f validate_btree pin pages 2025-05-21 14:20:11 +02:00
Pere Diaz Bou
b76961ce35 balance mark dirty from start 2025-05-21 14:19:56 +02:00
Pere Diaz Bou
591c674e86 Introduce PageRef wrapper BTreePage.
One problem we have with PageRef, is that this Page reference can be
unloaded, this means if we read the page again instead of loading the
page onto the same reference, we will have split brain of references.

To solve this we wrap PageRef in `BTreePage` so that if a page is seen
as unloaded, we will replace BTreePage::page with the newest version of
the page.
2025-05-21 14:19:41 +02:00
Pere Diaz Bou
15d24bd818 Start transactions in fuzz tests to flush pages
Previously, fuzz tests increase the size of page cache indefinitely,
therefore the was no problem of reaching the capacity of a page cache.
By adding transactions to fuzz tests we allow pages to remove dirty
flags once insert is finished.
2025-05-21 14:11:20 +02:00
Pere Diaz Bou
adf72f2bf8 allow updating a page id in page cache 2025-05-21 14:09:39 +02:00
Pere Diaz Bou
04323f95a5 increase cache size in empty_btree 2025-05-21 14:09:39 +02:00
Pere Diaz Bou
67e260ff71 allow delete of dirty page in cacheflush
Dirty pages can be deleted in `cacheflush`. Furthermore, there could be
multiple live references in the stack of a cursor so let's allow them to
exist while deleting.
2025-05-21 14:09:39 +02:00
Alecco
e808a28c98 WIP (squash) adapt pager and btree to page cache error handling 2025-05-21 14:09:39 +02:00
Alecco
4ef3c1d04d page_cache: fix insert and evict logic
insert() fails if key exists (there shouldn't be two) and panics if
it's different pages, and also fails if it can't make room for the page.

Replaced the limited pop_if_not_dirty() function with make_room_for().
It tries to evict many pages as requested spare capacity. It should come
handy later by resize() and Pager. make_room_for() tries to make room or
fails if it can't evict enough entries.

For make_room_for() I also tried with an all-or-nothing approach, so if
say a query requests a lot more than possible to make room for, it
doesn't evict a bunch of pages from the cache that might be useful. But
implementing this approach got very complicated since it needs to keep
exclusive PageRefs and collecting this caused segfaults. Might be worth
trying again in the future. But beware the rabbit hole.

Updated page cache test logic for new insert rules.

Updated Pager.allocate_page() to handle failure logic but needs further
work. This is to show new cache insert handling. There are many places
to update.

Left comments on callers of pager and page cache needing to update
error handling, for now.
2025-05-21 14:09:39 +02:00
Jussi Saurio
e4334dcfdf Add enum CursorHasRecord to remove assumption that all btrees have rowid 2025-05-20 14:22:17 +03:00
Jussi Saurio
35350a2368 Add IndexKeyInfo to btree 2025-05-20 14:22:17 +03:00
Pekka Enberg
e102cd0be5 Merge 'Add support for DISTINCT aggregate functions' from Jussi Saurio
Reviewable commit by commit. CI failures are not related.
Adds support for e.g. `select first_name, sum(distinct age),
count(distinct age), avg(distinct age) from users group by 1`
Implementation details:
- Creates an ephemeral index per distinct aggregate, and jumps over the
accumulation step if a duplicate is found

Closes #1507
2025-05-20 13:58:57 +03:00
pedrocarlo
52533cab40 only pass collations for index in cursor + adhere to order of columns in index 2025-05-19 15:22:55 -03:00
pedrocarlo
22b6b88f68 fix rebase type errors 2025-05-19 15:22:55 -03:00
pedrocarlo
4a3119786e refactor BtreeCursor and Sorter to accept Vec of collations 2025-05-19 15:22:55 -03:00
pedrocarlo
f28ce2b757 add collations to btree cursor 2025-05-19 15:22:55 -03:00
pedrocarlo
5bd47d7462 post rebase adjustments to accomodate new instructions that were created before the merge conflicts 2025-05-19 15:22:15 -03:00
Pere Diaz Bou
f2d0d61962 copilot nice suggestions :) 2025-05-19 09:59:28 +02:00
Pere Diaz Bou
5eab588115 improve debug build validation speed
Various things:
* remove unnecessary debug_validate_cell calls
* Add SortedVec for keys in fuzz tests
* Validate btree's depth in fuzz test every 1K inserts to not overload
test with validations. We add `VALIDATE_BTREE`  env variable to enable
validation on every insert in case it is needed.
2025-05-19 09:53:15 +02:00
Jussi Saurio
092462fa74 fix build 2025-05-19 07:29:02 +03:00
Jussi Saurio
7c6a4410d2 Merge '(btree): Implement support for handling offset-based payload access with overflow support' from Krishna Vishal
This PR adds a new function `read_write_payload_with_offset` to support
reading and writing payload data at specific offsets, handling both
local content and overflow pages. This is a port of SQLite's
`accessPayload` function in `btree.c` and will be essential for
supporting incremental blob I/O in the coming PRs.
- Added a state machine called `PayloadOverflowWithOffset` to make the
procedure reentrant.
- Correctly processes both local payload data and payload stored in
overflow pages
Testing:
- Reading and writing to a column with no overflow pages.
- Reading and writing at an offset with overflow pages (spanning 10
pages)

Reviewed-by: Pere Diaz Bou <pere-altea@homail.com>

Closes #1476
2025-05-18 22:58:10 +03:00
pedrocarlo
fd51c0a970 invalidate records not necessary for fix 2025-05-18 16:43:25 -03:00
pedrocarlo
af1f9492ef fix updating single value 2025-05-17 19:43:24 -03:00
Jussi Saurio
02d8e329ba Fix debug_validate_cells_core() not accepting cells below 4 bytes 2025-05-17 15:33:55 +03:00
Pere Diaz Bou
82e5597b00 long fuzz tests ci on btree changes
The idea is simple, if you modify the btree, we should verify fuzz tests
with long number of iterations to decrease the chance of a regression
2025-05-16 11:26:00 +02:00
Pekka Enberg
2127de3422 cargo fmt 2025-05-15 12:00:55 +03:00
Pekka Enberg
9303262dfd OwnedValue -> Value 2025-05-15 11:43:23 +03:00
Pere Diaz Bou
ef65275bda Merge 'test page_free_array' from Pere Diaz Bou
Simply add a fuzz test to test free_array works as intended

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #1480
2025-05-15 10:19:09 +02:00
Pekka Enberg
e3f71259d8 Rename OwnedValue -> Value
We have not had enough merge conflicts for a while so let's do a
tree-wide rename.
2025-05-15 09:59:46 +03:00
pedrocarlo
9fc9415b20 use Jussi's code to avoid cloning immutable record 2025-05-14 13:30:39 -03:00
pedrocarlo
72cc0fcdcb fixes and comments 2025-05-14 13:30:39 -03:00
pedrocarlo
b2615d7739 add CursorValidState and only save context in delete when rebalancing 2025-05-14 13:30:39 -03:00
pedrocarlo
814508981c fixing more rebase issues and cleaning up code. Save cursor context when calling delete for later use when needed 2025-05-14 13:30:39 -03:00
pedrocarlo
05f4ca28cc btree rewind and next fix. Keep track of rowids seen to avoid infinite loop 2025-05-14 13:30:39 -03:00
pedrocarlo
c146877344 add sqlite debug cli for nix. Fix cursor delete panic. Add tracing for cell indices in btree 2025-05-14 13:30:39 -03:00
Pere Diaz Bou
5025655f75 debug assertions 2025-05-14 13:53:51 +02:00
Pere Diaz Bou
c3432e1cc3 fmt 2025-05-14 13:23:12 +02:00
Pere Diaz Bou
13a120530e comment shift_cells_left
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-14 13:20:11 +02:00
Pere Diaz Bou
0d8a94d49d expect parse seed in rng_from_time_or_env
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-14 13:19:20 +02:00
Pere Diaz Bou
34c74e34d9 test page_free_array
Simply add a fuzz test to test free_array works as intended
2025-05-14 12:48:59 +02:00
krishvishal
f6e814c113 Refactor read_write_payload_to_page into separate read and write
functions
2025-05-14 13:03:23 +05:30
krishvishal
fb9fcb7887 Remove prints and change overflow test blob to be 10 pages long. 2025-05-13 21:19:49 +05:30
krishvishal
c424e830cd Add safety documentation 2025-05-13 16:57:44 +05:30