Commit Graph

7247 Commits

Author SHA1 Message Date
PThorpe92
bcadcb2014 Remove RefCell from copy_to method in io trait 2025-08-07 17:07:53 -04:00
PThorpe92
98f4e5cd2d Add comment/TODO about method we use to copy the db file 2025-08-07 16:27:08 -04:00
PThorpe92
b131331673 Add shell .py tests for .clone cli command 2025-08-07 16:27:08 -04:00
PThorpe92
e32d04ea97 Use ephemeral PlatformIO for clone method to support memory io 2025-08-07 16:27:08 -04:00
PThorpe92
04b40b4cf5 Impl copy_to for Database impl in JS bindings 2025-08-07 16:27:08 -04:00
PThorpe92
f75eaee886 Rename .copy -> .clone 2025-08-07 16:27:07 -04:00
PThorpe92
7e42b97b93 Add .help output for copying db file cmd 2025-08-07 16:27:07 -04:00
PThorpe92
837278c2d0 Add .help output for copying db file cmd 2025-08-07 16:27:07 -04:00
PThorpe92
736f78de64 Add .copy CLI command 2025-08-07 16:27:07 -04:00
PThorpe92
039fe22405 Add copy_to to io::File trait to support copying DB files 2025-08-07 16:27:02 -04:00
Preston Thorpe
e23637b6ad Merge 'only allow multiples of 64 for performance in arena bitmap' from Preston Thorpe
Trying to support this is unnecessary and just adds branches and bit ops
when we could just round the allocation up or down

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

Closes #2497
2025-08-07 14:25:11 -04:00
PThorpe92
c03ca5701a Dont accept values that are not multiples of 64 for performance in page bitmap 2025-08-07 13:45:07 -04:00
Jussi Saurio
5cafa9b223 Merge 'PageContent: make read_x/write_x methods private and add dedicated methods' from Jussi Saurio
# Note: this pull request is built on top of #2491 so let's merge that
one first.
## Problem:
A very easy source of bugs is to mistakenly use e.g.
`PageContent::read_u16()` instead of
`PageContent::read_u16_no_offset()`. The difference between the two is
that `read_u16()` adds 100 bytes to the requested byte offset if and
only if the page in question is page 1, which contains a 100-byte
database header.
Case in point about this being easy to misuse: see #2491.
## Observation:
In all of the cases where we want to read from or write to a page
"header-sensitively" (taking the possible db header into account), those
reads/writes are to statically known offsets, e.g. specific known bytes
in a btree page header.
In all other cases, the "no-offset" versions, i.e. the ones taking the
absolute byte offset as parameter, should be used - the common use cases
for these are reading/writing to some absolute offset that you just read
from another location on a page, for example. Another use case is
writing to specific offsets on overflow pages and freelist pages, which
we can in the future expose more methods for, but right now they can
always use the "no offset" versions safely, because neither of those
page types can ever be page 1.
## Solution:
1. Make all the offset-sensitive versions (`read_u16()` and friends)
private methods of `PageContent`.
2. Expose dedicated public methods for things like updating rightmost
pointer, updating fragmented bytes count and so on, and use them instead
of the plain read/write methods universally.
## Follow-up:
I will perhaps follow this up with a renaming PR that renames
`read_xxx_no_offset()` to just `read_xxx()` and gives the private
header-sensitive methods new names.

Reviewed-by: Preston Thorpe <preston@turso.tech>

Closes #2493
2025-08-07 17:58:49 +03:00
Pekka Enberg
ec69f40675 Merge 'SQLite C API improvements: add basic bind and column functions' from Danawan Bimantoro
Add support for more of the SQLite C API.
Bind functions:
* bind_parameter_count
* bind_parameter_name
* bind_null
* bind_int
* bind_int64
* bind_double
Column functions:
* column_count
* column_name
* column_int
* column_double
* last_insert_rowid

Closes #2494
2025-08-07 17:32:19 +03:00
Jussi Saurio
1fe32dadf3 PageContent: make read_x/write_x methods private and add dedicated methods
Problem:

A very easy source of bugs is to mistakenly use e.g. PageContent::read_u16()
instead of PageContent::read_u16_no_offset(). The difference between the two
is that `read_u16()` adds 100 bytes to the requested byte offset if and only if
the page in question is page 1, which contains a 100-byte database header.

Case in point: see #2491.

Observation:

In all of the cases where we want to read from or write to a page  "header-sensitively",
those reads/writes are to so-called "well known offsets", e.g. specific bytes in a btree
page header.

In all other cases, the "no-offset" versions, i.e. the ones taking the absolute byte offset
as parameter, should be used.

Solution:

1. Make all the offset-sensitive versions (read_u16() and friends) private methods of
`PageContent`.
2. Expose dedicated methods for things like updating rightmost pointer, updating fragmented
bytes count and so on, and use them instead of the plain read/write methods universally.
2025-08-07 17:00:06 +03:00
Pekka Enberg
3f181c9145 Merge 'btree: Use correct byte offsets for page 1 in defragmentation ' from Jussi Saurio
## Beef
`defragment_page_fast()` incorrectly didn't use the version of
read/write methods on `PageContent` that does NOT add the 100 byte
database header into the requested byte offset.
this resulted in defragment of page 1 in reading 2nd/3rd freeblocks
from the wrong offset and reading/writing freeblock sizes and cell
offsets to the wrong location.
## Testing
Adds fuzz test for CREATE TABLE / DROP TABLE / ALTER TABLE, which I was
able to reproduce this with.

Closes #2491
2025-08-07 16:52:11 +03:00
danawan
ea7cad1188 fix clippy warnings in tests 2025-08-07 20:49:39 +07:00
danawan
0ab0c2e0c7 fix clippy 2025-08-07 20:36:52 +07:00
danawan
eaa614aa51 add basic bind and column functions 2025-08-07 20:20:23 +07:00
Jussi Saurio
6cd7334afc btree/fix: use correct byte offsets for page1 in defragmentation
`defragment_page_fast()` incorrectly didn't use the version of
read/write methods on `PageContent` that does NOT add the 100 byte
database header into the requested byte offset.

this resulted in defragment of page 1 in reading 2nd/3rd freeblocks
from the wrong offset and writing cell offsets to the wrong location.
2025-08-07 15:42:06 +03:00
Jussi Saurio
aca02c9969 tests/fuzz: add schema operations fuzz test 2025-08-07 15:35:39 +03:00
Pekka Enberg
a1df9365a2 Merge 'javascript: Implement Statement.iterate()' from Pekka Enberg
Closes #2486
2025-08-07 15:05:56 +03:00
Pekka Enberg
1358e0be50 Merge 'bench/insert: use PRAGMA synchronous=full' from Jussi Saurio
synchronous=FULL means WAL is fsynced on every commit, which is what we
also do.
however we do not do the padding to sector alignment that sqlite does
(see #2450), which makes sqlite do more work than we do.

Closes #2485
2025-08-07 14:30:42 +03:00
Pekka Enberg
bae4406e32 testing/javascript: Enable iterate() test cases 2025-08-07 14:28:34 +03:00
Pekka Enberg
ab7b0dd1aa bindings/javascript: Implement Statement.iterate() 2025-08-07 14:28:34 +03:00
Pekka Enberg
b603ee7062 Merge 'JavaScript improvements' from Pekka Enberg
Closes #2467
2025-08-07 14:01:07 +03:00
Jussi Saurio
95c6c7581b bench/insert: use PRAGMA synchronous=full
synchronous=FULL means WAL is fsynced on every commit,
which is what we also do.

however we do not do the padding to sector alignment that
sqlite does (see #2450), which makes sqlite do more work
than we do.
2025-08-07 13:40:14 +03:00
Jussi Saurio
edd7c45c22 Merge 'Fix segfault on schema update for virtual tables' from Preston Thorpe
Closes #2478
```console
turso>create table t(a,b);
turso>insert into t select 1,2 from generate_series(1,20);
turso>create index idxa on t(a);
turso>create index idxb on t(b);
# segfault
```
The issue was the `turso_ext::Conn` pointer was stored on the
`VirtualTable`, which lives longer than necessary and the underlying
core `Connection` is not guaranteed pinned. Moving the `turso_ext::Conn`
on to the cursor fixes this issue because it's independent of the
Schema.
This also fixes the issue that comes up when that issue is fixed, and
some of the relevant code for this hadn't been updated when other
surrounding changes had been made.

Closes #2479
2025-08-07 09:02:11 +03:00
Jussi Saurio
eb7fa9693d Merge 'Return error on attempting to drop index associated with PRIMARY KEY and UNIQUE constraints' from
Closes issue #2455. Also includes tests.

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

Closes #2461
2025-08-07 09:00:02 +03:00
Pekka Enberg
be8e8ff7c0 Merge 'turso-sync: rewrite' from Nikita Sivukhin
This PR rewrites `turso-sync` package introduced in the #2334 and
renames it to the `turso-sync-engine` (anyway the diff will be
unreadable).
The purpose of rewrite is to get rid of Tokio because this makes things
harder when we wants to export bindings to WASM.
In order to achieve "runtime"-agnostic sync core but still be able to
use async/await machiner - this PR introduce usage of `genawaiter` crate
which allows to transfer async/await Rust state machines to the
generators. So, sync operations are just generators which can yield `IO`
command in case where there is a need for it.
Also, this PR introduces separate `ProtocolIo` in the `turso-sync-
engine` which defines extra IO methods:
1. HTTP interaction
2. Atomic read/writes to the file. This is not strictly necessary and
`turso_core::IO` methods can be extended to support few more things
(like `delete`/`rename`) - but I decided that it will be simpler to just
expose 2 more methods for sync protocol for the sake of atomic metadata
update (which is very small - dozens of bytes).
    * As a bonus, we can store metadata for browser in the
`LocalStorage` which may be more natural thing to do(?) (user can reset
everything by just clearing local storage)
The `ProtocolIo` works similarly to the `IO` in a sense that it gives
the caller `Completion` which it can check periodically for new data.

Closes #2457
2025-08-07 07:58:02 +03:00
Pekka Enberg
fa6c925751 bindings/javascript: Switch from RefCell to Cell 2025-08-07 07:47:10 +03:00
Preston Thorpe
f55d34a3db Merge 'Fix panic on loading extension on brand new connection' from Preston Thorpe
Closes #2476

Closes #2477
2025-08-06 23:44:38 -04:00
Preston Thorpe
0777fd9082 Merge 'implement the MaxPgCount opcode' from Glauber Costa
It is used by the pragma max_page_count, which is also implemented.

Reviewed-by: Preston Thorpe <preston@turso.tech>

Closes #2472
2025-08-06 23:44:05 -04:00
PThorpe92
df2c39b98e Use load_insn macro for op_journal_mode 2025-08-06 23:42:47 -04:00
Glauber Costa
071330a739 implement the JournalMode vdbe instruction
We do this already, but not through any opcode.
Move it to an opcode for compatibility reasons.
2025-08-06 19:30:19 -05:00
rajajisai
44ba3caaaa Remove miscellaneous characters generated due varying CLI size 2025-08-06 16:02:15 -07:00
PThorpe92
ae99edf4a6 Adjust test to test for behavior of loading extension on brand new connection 2025-08-06 16:55:54 -04:00
PThorpe92
6cc5c66964 Remove useless close method on extension Connection 2025-08-06 16:46:44 -04:00
PThorpe92
e9838daa28 Update macro to use const conn pointer 2025-08-06 16:27:26 -04:00
PThorpe92
273c12b2b3 Remove extension Conn from VirtualTable to survive schema changes 2025-08-06 16:27:26 -04:00
PThorpe92
657f3f3095 Fix panic on loading extension on brand new connection 2025-08-06 15:51:49 -04:00
Jussi Saurio
7b4703eba4 Merge 'bench/insert: use locking_mode EXCLUSIVE and journal_mode=WAL for sqlite' from Jussi Saurio
Reviewed-by: Pere Diaz Bou <pere-altea@homail.com>

Closes #2475
2025-08-06 21:56:05 +03:00
Jussi Saurio
ff128e2f20 bench/insert: use locking_mode EXCLUSIVE and journal_mode=WAL for sqlite 2025-08-06 21:40:43 +03:00
Jussi Saurio
64c8587f27 Merge 'IO More State Machine' from Pedro Muniz
I swear we just need one more state machine. Just more state machine
until we achieve IO tracking. (PS: this is a meme)

Closes #2462
2025-08-06 21:26:19 +03:00
Glauber Costa
f36974f086 implement the MaxPgCount opcode
It is used by the pragma max_page_count, which is also implemented.
2025-08-06 13:20:15 -05:00
Nikita Sivukhin
bfacc141b0 remove unnecessary change 2025-08-06 22:09:29 +04:00
Nikita Sivukhin
ae0dff6a55 cargo clippy --tests --fix 2025-08-06 22:09:29 +04:00
Nikita Sivukhin
09daa97150 format fixes 2025-08-06 21:53:03 +04:00
Nikita Sivukhin
b1f1526673 add DB file for tests 2025-08-06 21:47:18 +04:00
Jussi Saurio
cc98f9f88b Merge 'Direct schema mutation – add instruction' from Levy A.
**86%** performance improvement. We are **25x** faster than SQLite.
<img width="953" height="511" alt="image" src="https://github.com/user-
attachments/assets/fd717d1e-bbbe-4959-ae48-41afc73e5e9f" />
```
ALTER TABLE _ DROP COLUMN _`/limbo_drop_column/
                        time:   [1.8821 ms 1.8929 ms 1.9047 ms]
                        change: [-86.850% -86.733% -86.614%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  3 (3.00%) high mild
  2 (2.00%) high severe
Benchmarking `ALTER TABLE _ DROP COLUMN _`/sqlite_drop_column/: Warming up for 3.0000 s

`ALTER TABLE _ DROP COLUMN _`/sqlite_drop_column/
                        time:   [46.227 ms 46.258 ms 46.291 ms]
                        change: [-1.3202% -1.0505% -0.8109%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 15 outliers among 100 measurements (15.00%)
  10 (10.00%) high mild
  5 (5.00%) high severe
  ```

Closes #2452
2025-08-06 20:32:22 +03:00