Commit Graph

289 Commits

Author SHA1 Message Date
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
Zaid Humayun
e0105398a6 btree: modified the btree_destroy subroutine
modified the btree_destroy subroutine to do an iterative DFS and use the stack cell counters to keep track of whether children have been removed. adds a unit test.
2025-02-19 21:46:26 +05:30
Zaid Humayun
97d87955cc DROP TABLE: renamed BTreeCusor::btree_drop to BTreeCursor::btree_destroy
this more closely matches semantics
2025-02-19 21:46:26 +05:30
Zaid Humayun
76e2d98381 drop table: addresses issue https://github.com/tursodatabase/limbo/issues/894 which requires DROP TABLE to be implemented
this is the initial commit is for the implementation of DROP TABLE. It adds support for the DROP TABLE instruction and adds a DropBTree instruction. It also implements the btree_drop method in btree.rs which makes use of free_page method which will be implemented via PR https://github.com/tursodatabase/limbo/pull/785
2025-02-19 21:46:25 +05:30
Pere Diaz Bou
e25272adc0 fix free_cell_space extend content area 2025-02-17 11:50:15 +01:00
Pere Diaz Bou
ddbfada8bd fix wrong usage of insert in fuzz tests 2025-02-17 08:47:55 +01:00
Pere Diaz Bou
4d6843d1cc clippy 2025-02-16 19:27:34 +01:00
Pere Diaz Bou
d276c22a0c clippy 2025-02-16 19:25:41 +01:00
Pere Diaz Bou
a6f5bcbaf4 fix return find_free_cell 2025-02-16 19:21:37 +01:00
Pere Diaz Bou
286cb8c5bc fix some free_cell_range pointer reads 2025-02-16 19:10:15 +01:00
Pere Diaz Bou
aea4560422 bring back corrupt errors 2025-02-16 18:51:09 +01:00
Pere Diaz Bou
1687072d77 remove dbg 2025-02-16 18:31:19 +01:00
Pere Diaz Bou
b64cc769b6 fix rebase 2025-02-16 18:31:03 +01:00
Pere Diaz Bou
b5ec5186ea fix divider cell on leaf data 2025-02-16 18:30:42 +01:00
Pere Diaz Bou
177dbcd403 simplify balance_root 2025-02-16 18:01:33 +01:00
Nikita Sivukhin
37e27131e3 fix inequality 2025-02-16 12:49:55 +04:00
Nikita Sivukhin
d2a507e458 add unit tests for reading integer serial types 2025-02-16 12:46:45 +04:00
Nikita Sivukhin
279652b271 extend sign for 24/48 bit width serial types 2025-02-16 12:46:18 +04:00
Pere Diaz Bou
f9d979575b fix defragment_page cell payload size calculation 2025-02-15 16:51:52 +01:00
Pere Diaz Bou
d3574f1e55 fix root offfset handling 2025-02-15 16:51:52 +01:00
Pere Diaz Bou
8ba7022e0b remove dbg prints 2025-02-15 16:51:52 +01:00
Pere Diaz Bou
1b96bd8a30 fix free_cell_area offset check 2025-02-15 16:46:22 +01:00
Pere Diaz Bou
7e55f46b26 fix defragment 2025-02-15 16:46:22 +01:00