Commit Graph

574 Commits

Author SHA1 Message Date
Pekka Enberg
9a646acead Merge 'Add BTree balancing after delete' from Krishna Vishal
This PR adds balancing after delete.
- [x] Remove linear search for cell in the page
- [x] Change the implementation to state machine approach
- [x] Handle cases when balancing is needed and not needed
- [x] Add unit test to verify that balancing after delete maintains
BTree integrity.
Fixes: https://github.com/tursodatabase/limbo/issues/1019
Closes: https://github.com/tursodatabase/limbo/issues/455

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

Closes #1147
2025-03-28 07:32:15 +02:00
krishvishal
1b77b76d58 Add comments detailing Delete state machine flow and few delete steps. 2025-03-27 01:02:59 +05:30
krishvishal
fe7ff6f53d Add balancing states. Balancing test works. 2025-03-27 00:27:03 +05:30
krishvishal
3e3a0f56a1 Make delete re-entrant.
Setup DeleteState enum
2025-03-26 22:44:49 +05:30
Pere Diaz Bou
63cf86ba36 fix comparison of records 2025-03-26 10:10:19 +01:00
krishvishal
16d77acac6 Add comments describing the test_delete_balancing 2025-03-26 12:29:19 +05:30
Pere Diaz Bou
8642d416c7 Introduce immutable record.
Currently we have a Record, which is a dumb vector of cloned values.
This is incredibly bad for performance as we do not want to clone
objects unless needed. Therefore, let's start by introducing this type
so that any record that has already been serialized will be returned
from btree in the format of a simple payload with reference to payload.
2025-03-25 17:35:41 +01:00
Pekka Enberg
df6af6ed79 core: Rename FileStorage to DatabaseFile 2025-03-25 11:15:16 +02:00
krishvishal
6fe7e419e2 cleanup 2025-03-25 08:58:18 +05:30
krishvishal
ac29563efb Add test to verify balancing after maintains the following:
1. Delete is working properly.
2. Balancing after delete still maintains the validity of btree.
2025-03-25 08:55:13 +05:30
krishvishal
bf1718925e Delete with balancing now works!! 2025-03-25 08:38:18 +05:30
krishvishal
6a8d1deee6 Convert implementation into event loop style. 2025-03-24 20:52:17 +05:30
krishvishal
8c164530fa Remove linear search to obtain cell. current_cell_idx - 1 seems to work reliably. 2025-03-24 16:37:10 +05:30
PThorpe92
676ddd4fb6 Add logic to handle overwrite cell if insert to same rowid to support update 2025-03-23 17:08:14 -04:00
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
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
825907bfac Invalidate cache entry after checkpoint of frame completes 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
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
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
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
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