Commit graph

661 commits

Author SHA1 Message Date
krishvishal
e0d54f3691 Initialize CursorState properly 2025-03-20 12:49:17 +05:30
krishvishal
7cbd816b2a Add balancing support for delete operation. 2025-03-20 12:44:08 +05:30
krishvishal
bc1fd39a8d Add self.stack.retreat() when balancing is not needed. Fixes: https://github.com/tursodatabase/limbo/issues/1019 2025-03-20 10:47:41 +05:30
Pere Diaz Bou
00ab3d1c0c Fix ordering and implement Deref 2025-03-17 10:22:42 +01:00
Pere Diaz Bou
20f5ade95e Experiment with a custom Lock for database header 2025-03-17 10:21:34 +01:00
Pekka Enberg
9f641f17c6 Disable some B-Tree fuzzers
Fuzz testing is great for finding bugs, but until we fix the bugs,
failing CI runs out of the blue for unrelated PRs is not very
productive. Hopefully we can enable this soon again, but until then,
let's not fail the test suite all the time randomly.
2025-03-13 10:30:34 +02:00
PThorpe92
e22c86c230
Impl Copy on some types in the pager to prevent explicit clones 2025-03-12 21:57:35 -04:00
Pekka Enberg
a2c40dd234 cargo fmt... 2025-03-12 18:59:08 +02:00
Pekka Enberg
b73df180cc Merge 'Fix various issues in insert_into_cell()' from Krishna Vishal
There are two bugs in #1085.
1. `find_free_cell` accesses non-existent free blocks and returns their
size to `allocate_space`. This is out of range access error. The fix is
to add a loop termination condition that stops it when we hit the end of
the blocks
2. This bug is caused by `find_free_cell` some how swallowing the blocks
with size `4 bytes`. So `compute_free_space` consistently undercounts by
`4 bytes`. I've refactored that part of the code to make sure 4 sized
block are not deleted.
I've also added a unit test which proves these fixes work and also added
a function called `debug_print_freelist` which prints all free blocks of
a page.
For now I've silenced the `overflow_page` tests.
Fixes #1085

Closes #1111
2025-03-12 18:51:19 +02:00
Pere Diaz Bou
aa4703c442 Fix read frame setting wrong offset
When I added frame reading support I thought, okay, who cares about the page id of this page it we read it from a frame because we don't need it to compute the offset to read from the file in this case. Fuck me, because it was needed in case we read `page 1` from WAL because it has a differnt `offset`.
2025-03-12 17:24:59 +01:00
Pere Diaz Bou
1af6dccc72 allow arc in tests 2025-03-12 16:02:04 +01:00
Pere Diaz Bou
8127927775 remove modulo by 1 error 2025-03-12 15:48:22 +01:00
Pere Diaz Bou
f9916e8149 update max frame in case we got a read lock with outdated max frame 2025-03-12 15:48:22 +01:00
Pere Diaz Bou
825907bfac Invalidate cache entry after checkpoint of frame completes 2025-03-12 15:48:22 +01:00
Pere Diaz Bou
deaff6c1ec Fix detachment of nodes in lru cache. 2025-03-12 15:48:22 +01:00
Pere Diaz Bou
be3badc1f3 modify a few btree log level and add end_write_txn after checkpoint 2025-03-12 15:48:22 +01:00
krishvishal
6093994bd2 Changed from using raw byte access methods to PageContent read/write methods
Added comments
2025-03-11 22:49:18 +05:30
krishvishal
91fa6a5fa3 Remove debug prints and make clippy happy 2025-03-11 20:30:09 +05:30
krishvishal
a1a63f621e Add a method that can help while debugging freelist blocks 2025-03-11 20:30:09 +05:30
krishvishal
d8210d79aa Add unit test to demonstrate that https://github.com/tursodatabase/limbo/issues/1085 is
fixed.
2025-03-11 20:30:09 +05:30
krishvishal
0f0d56b0e7 Refactor find_free_cell logic to make sure a freeblock with size 4 is not deleted.
Previously any block that has a size 4 is deleted resulting in the issue of computed free space
is less than 4 bytes when compared to expected free space.
2025-03-11 20:30:09 +05:30
krishvishal
77aeb889ae Add loop termination condition when pc = 0 in find_free_cell.
The issue is caused by the function try to read from non-existent free blocks.
2025-03-11 20:30:09 +05:30
Pekka Enberg
051d879db2 core: Fix btree test compilation 2025-03-06 19:14:48 +02:00
Pekka Enberg
b0636e4494 Merge 'Adds Drop Table' from Zaid Humayun
This PR adds support for `DROP TABLE` and addresses issue
https://github.com/tursodatabase/limbo/issues/894
It depends on https://github.com/tursodatabase/limbo/pull/785 being
merged in because it requires the implementation of `free_page`.
EDIT: The PR above has been merged.
It adds the following:
* an implementation for the `DropTable` AST instruction via a method
called `translate_drop_table`
* a couple of new instructions - `Destroy` and `DropTable`. The former
is to modify physical b-tree pages and the latter is to modify in-memory
structures like the schema hash table.
* `btree_destroy` on `BTreeCursor` to walk the tree of pages for this
table and place it in free list.
* state machine traversal for both `btree_destroy` and
`clear_overflow_pages` to ensure performant, correct code.
* unit & tcl tests
* modifies the `Null` instruction to follow SQLite semantics and accept
a second register. It will set all registers in this range to null. This
is required for `DROP TABLE`.
The screenshots below have a comparison of the bytecodes generated via
SQLite & Limbo.
Limbo has the same instruction set except for the subroutines which
involve opening an ephemeral table, copying over the triggers from the
`sqlite_schema` table and then re-inserting them back into the
`sqlite_schema` table.
This is because `OpenEphemeral` is still a WIP and is being tracked at
https://github.com/tursodatabase/limbo/pull/768
![Screenshot 2025-02-09 at 7 05 03 PM](https://github.com/user-
attachments/assets/1d597001-a60c-4a76-89fd-8b90881c77c9)
![Screenshot 2025-02-09 at 7 05 35 PM](https://github.com/user-
attachments/assets/ecfd2a7a-2edc-49cd-a8d1-7b4db8657444)

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

Closes #897
2025-03-06 18:27:41 +02:00
Pekka Enberg
d6c514c8d1 core: Integrate MVCC to B-Tree cursor 2025-03-06 10:16:42 +02:00
Pekka Enberg
96175cccf7 cli: Add --experimental-mvcc option to enable MVCC 2025-03-06 10:16:42 +02:00
Pere Diaz Bou
7aae885af4 add logs to wal locks 2025-03-05 22:32:59 +01:00
Pere Diaz Bou
9a01e32c01 add tracing_subscriber and test_log::test
I don't know when and why we dropped log::* in favor of tracing but when it was done, it made relevant logs not appear any more while debugging so... I added test_log::test which helps by automatically adding info logs from trace package.
2025-03-05 15:36:06 +01:00
Pere Diaz Bou
3d3cdbeb0c fix generic and windows sync compilation 2025-03-05 14:07:48 +01:00
Pere Diaz Bou
ab2fc3fb13 fix clippy arc warnings for now 2025-03-05 14:07:48 +01:00
Pere Diaz Bou
e4a8ee5402 move load extensions to Connection
Extensions are loaded per connection and not per database as per SQLite
behaviour. This also helps with removing locks.
2025-03-05 14:07:48 +01:00
Pere Diaz Bou
8daf7666d1 Make database Sync + Send 2025-03-05 14:07:48 +01:00
Pekka Enberg
f57d2b32af core: Clean up B-Tree creation code
Move page allocation to pager so that we don't need to instantiate a
cursor to create a B-Tree.
2025-03-04 18:38:06 +02:00
Pere Diaz Bou
1de4861414 fix balance_non_root should trigger balance again 2025-03-02 23:16:32 +01:00
Pere Diaz Bou
10824e3eb3 remove new_pages, remove extra divider_cells and cells capacity 2025-03-02 22:49:20 +01:00
Pere Diaz Bou
0da12df67c Introduce BalanceInfo to hold all balance procedure information 2025-03-02 22:42:48 +01:00
PThorpe92
588e43c5aa
Minor improvements and cleanups in btree 2025-03-01 15:48:42 -05:00
Zaid Humayun
23a904f38d Merge branch 'main' of https://github.com/tursodatabase/limbo 2025-03-01 01:18:45 +05:30
Pekka Enberg
1de73b389e Merge ' fix usable_space calculation and wrong old pages cell count usage ' from Pere Diaz Bou
Closes #1067
2025-02-28 19:45:07 +02:00
Pere Diaz Bou
e545cc7057 fix btree_insert_fuzz_ex implementation 2025-02-28 18:21:38 +01:00
Pere Diaz Bou
bbb3252ab6 fix usable_space calculation and wrong old pages cell count usage 2025-02-28 18:19:27 +01:00
Pekka Enberg
20d618f35c Disable some failing b-tree tests until we've fixed them 2025-02-28 19:17:29 +02:00
Pekka Enberg
13750e9255 Human Rust programmers exist to keep Clippy happy 2025-02-28 19:12:12 +02:00
Pekka Enberg
b4e8afa3c7 Merge 'Implement SQLite balancing algorithm' from Pere Diaz Bou
Beep boop.
What happened you ask? I removed the dumb balancing algorithm I
implemented in favor of SQLite's implementation based on B*Tree[1] where
a page is 2/3 full instead of 1/2. It also tries to balance a page by
taking a maximum 3 pages and distributing cells evenly between them.
I've made some changes that are somewhat related:
* Moved most operations on pages out of BTreeCursor because those
operations are based on a page, not on a cursor, and it makes it easier
to test.
* Fixed `write_u16` and `read_u16` cases that didn't need a implicit
offset calculation. Added: `write_u16_no_offset` and
`read_u16_no_offset` to counter this.
* Added some tests with fuzz testing too.
* Fixed some important actions like: `compute_free_space`,
`defragment_page` and `drop_cell`.
[1] https://dl.acm.org/doi/10.1145/356770.356776

Closes #968
2025-02-28 19:10:52 +02:00
Pekka Enberg
6d44ad22fd core: Optimize read_record() function
The SerialType::try_from() was pretty high up in CPU profiles so I asked
my dear friend Claude to optimize it away by using the serial type
integer value directly instead of constructing a fancy enumeration.
2025-02-26 13:31:38 +02:00
Zaid Humayun
662b34c8b6 fix: addresses comments https://github.com/tursodatabase/limbo/pull/897#discussion_r1962100891 and https://github.com/tursodatabase/limbo/pull/897#discussion_r1962100299 by @pereman2
this commit introduces the ability of the state machine traversal to handle index interior cell overflow pages. it also makes the state machine states more explicit with match arms.
2025-02-23 16:12:28 +05:30
Zaid Humayun
fdf4166a7c fix: addresses comment https://github.com/tursodatabase/limbo/pull/897#discussion_r1962102119 by @pereman2
this commit changes the contents of the state machine state for ClearOverflowPages to contain the BTreeCell instead of the cell index.
this avoids having to repeatedly do an operation to get the BTreeCell from the cell index each time the state is reached
2025-02-23 12:41:17 +05:30
Zaid Humayun
77815b20d7 docs: added documentation for btree_destroy and clear_overflow_pages 2025-02-19 21:46:26 +05:30
Zaid Humayun
2ff27834ff fix: addresses comment https://github.com/tursodatabase/limbo/pull/897#discussion_r1948154297 by @pereman2
this commit adds the final touches to the state machine for btree_destroy and also introduces a state machine for clear_overflow_pages
this addresses the issue of IO interrupting an operation in progress and solves the following problems:
* performance issues with repeating an operation
* missing page issues with clear overflow pages
2025-02-19 21:46:26 +05:30
Zaid Humayun
f7dc0d42df btree_destroy: implemented functionality with state machine 2025-02-19 21:46:26 +05:30