Skip to content

fix(sqlite-vfs): use delete range for truncate cleanup#4636

Open
NathanFlurry wants to merge 1 commit into04-12-fix_sqlite-native_restore_kv_error_hookfrom
04-12-fix_sqlite-vfs_use_delete_range_for_truncate_cleanup
Open

fix(sqlite-vfs): use delete range for truncate cleanup#4636
NathanFlurry wants to merge 1 commit into04-12-fix_sqlite-native_restore_kv_error_hookfrom
04-12-fix_sqlite-vfs_use_delete_range_for_truncate_cleanup

Conversation

@NathanFlurry
Copy link
Copy Markdown
Member

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copy link
Copy Markdown
Member Author

NathanFlurry commented Apr 13, 2026

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@claude
Copy link
Copy Markdown

claude bot commented Apr 13, 2026

PR Review: fix(sqlite-vfs): use delete range for truncate cleanup

Summary

This PR simplifies the xTruncate path in both the Rust native VFS (sqlite-native/src/vfs.rs) and the TypeScript WASM VFS (sqlite-vfs/src/vfs.ts) by replacing a per-chunk enumeration loop with a single kv_delete_range / deleteRange call. This is a clean, correct optimization that mirrors the existing pattern already used in delete_file.


Correctness

Range bounds are correct. The start key get_chunk_key(file_tag, last_chunk_to_keep + 1) and end key get_chunk_key_range_end(file_tag) correctly cover all surplus chunks. This is the same half-open range pattern already used in the delete_file path (e.g., vfs.rs:311-313).

size == 0 edge case. When size == 0, last_chunk_to_keep = -1, so (last_chunk_to_keep + 1) as u32 = 0 -- the range correctly starts at chunk 0, deleting everything. Same logic holds in TypeScript.

Broader end bound is strictly safer. The old loop terminated at lastExistingChunk, which could miss orphaned chunk keys written past the last-known chunk (e.g., from a partial write before a crash). The new end bound getChunkKeyRangeEnd(fileTag) covers all keys for the file tag up to the key-space boundary, eliminating that class of orphan leak.

VFS parity maintained. Both implementations are updated identically, satisfying the requirement that the native Rust VFS and WASM TypeScript VFS match 1:1, including the delete/truncate strategy.

Guard condition is correct. The if last_chunk_to_keep < last_existing_chunk guard is present in both implementations, correctly skipping the deleteRange call when there is nothing to delete.


Suggestions

Missing test coverage. The PR checklist item for tests is unchecked, and no tests are included. The main regression risk is the >128-chunk case -- the old code issued multiple kv_delete batches, and the new single deleteRange must cover the same key space. Recommend adding tests for:

  • Truncating a file with >128 chunks (>512 KiB), verifying all surplus chunks are gone after truncation.
  • Truncating to size == 0 (all chunks deleted).
  • Truncating when lastChunkToKeep == lastExistingChunk (guard path, no-op).

Missing comment in TypeScript. The old block had // Delete chunks beyond the new size. The new block has no comment. Since the end bound (getChunkKeyRangeEnd) is broader than strictly necessary (it covers the entire file-tag key space rather than just chunks up to lastExistingChunk), a brief comment explaining the intent would help future readers understand why the broader range is deliberate.

PR description is empty. The boilerplate template was left unfilled -- no summary, no test description, no checklist items checked. Worth filling in for reviewability.


No Issues Found

  • No bugs detected.
  • Error handling (is_err() / SQLITE_IOERR_TRUNCATE) is unchanged and correct.
  • KV_MAX_BATCH_KEYS is still used in the write path -- no dead code introduced.
  • No security implications -- internal storage optimization with no change to trust boundaries or key schema.

Overall: clean change, correct semantics, recommend approval pending test coverage.

@NathanFlurry NathanFlurry marked this pull request as ready for review April 13, 2026 05:16
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_restore_kv_error_hook branch from 6eac78f to 4e380c8 Compare April 13, 2026 05:38
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-vfs_use_delete_range_for_truncate_cleanup branch from 4303e83 to 60882a2 Compare April 13, 2026 05:38
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_restore_kv_error_hook branch from 4e380c8 to 7fbbf37 Compare April 13, 2026 05:50
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-vfs_use_delete_range_for_truncate_cleanup branch from 60882a2 to 5f174d0 Compare April 13, 2026 05:50
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-vfs_use_delete_range_for_truncate_cleanup branch from 5f174d0 to c75356d Compare April 13, 2026 07:03
@NathanFlurry NathanFlurry force-pushed the 04-12-fix_sqlite-native_restore_kv_error_hook branch from 7fbbf37 to fe8cf4f Compare April 13, 2026 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant