From dfaae412cb507b4a5a024d200a7c7356e916b6ea Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Sun, 12 Apr 2026 20:16:46 -0700 Subject: [PATCH] fix(sqlite-vfs): use delete range for truncate cleanup --- .../packages/sqlite-native/src/vfs.rs | 17 ++++++++--------- .../packages/sqlite-vfs/src/vfs.ts | 13 ++++--------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/rivetkit-typescript/packages/sqlite-native/src/vfs.rs b/rivetkit-typescript/packages/sqlite-native/src/vfs.rs index 2e7f0adb8a..4b8c09d7b2 100644 --- a/rivetkit-typescript/packages/sqlite-native/src/vfs.rs +++ b/rivetkit-typescript/packages/sqlite-native/src/vfs.rs @@ -837,15 +837,14 @@ unsafe extern "C" fn kv_io_truncate(p_file: *mut sqlite3_file, size: sqlite3_int } } - let mut keys_to_delete = Vec::new(); - let mut chunk_idx = last_chunk_to_keep + 1; - while chunk_idx <= last_existing_chunk { - keys_to_delete.push(kv::get_chunk_key(file.file_tag, chunk_idx as u32).to_vec()); - chunk_idx += 1; - } - - for chunk in keys_to_delete.chunks(KV_MAX_BATCH_KEYS) { - if ctx.kv_delete(chunk.to_vec()).is_err() { + if last_chunk_to_keep < last_existing_chunk { + if ctx + .kv_delete_range( + kv::get_chunk_key(file.file_tag, (last_chunk_to_keep + 1) as u32).to_vec(), + kv::get_chunk_key_range_end(file.file_tag).to_vec(), + ) + .is_err() + { return SQLITE_IOERR_TRUNCATE; } } diff --git a/rivetkit-typescript/packages/sqlite-vfs/src/vfs.ts b/rivetkit-typescript/packages/sqlite-vfs/src/vfs.ts index 161adec8a3..b4dbfca8d5 100644 --- a/rivetkit-typescript/packages/sqlite-vfs/src/vfs.ts +++ b/rivetkit-typescript/packages/sqlite-vfs/src/vfs.ts @@ -1324,15 +1324,10 @@ class SqliteSystem implements SqliteVfsRegistration { } } - // Delete chunks beyond the new size - const keysToDelete: Uint8Array[] = []; - for (let i = lastChunkToKeep + 1; i <= lastExistingChunk; i++) { - keysToDelete.push(this.#chunkKey(file, i)); - } - - for (let b = 0; b < keysToDelete.length; b += KV_MAX_BATCH_KEYS) { - await options.deleteBatch( - keysToDelete.slice(b, b + KV_MAX_BATCH_KEYS), + if (lastChunkToKeep < lastExistingChunk) { + await options.deleteRange( + this.#chunkKey(file, lastChunkToKeep + 1), + getChunkKeyRangeEnd(file.fileTag), ); } } catch (error) {