Skip to content

Commit 60882a2

Browse files
committed
fix(sqlite-vfs): use delete range for truncate cleanup
1 parent 4e380c8 commit 60882a2

2 files changed

Lines changed: 12 additions & 18 deletions

File tree

  • rivetkit-typescript/packages

rivetkit-typescript/packages/sqlite-native/src/vfs.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -820,15 +820,14 @@ unsafe extern "C" fn kv_io_truncate(p_file: *mut sqlite3_file, size: sqlite3_int
820820
}
821821
}
822822

823-
let mut keys_to_delete = Vec::new();
824-
let mut chunk_idx = last_chunk_to_keep + 1;
825-
while chunk_idx <= last_existing_chunk {
826-
keys_to_delete.push(kv::get_chunk_key(file.file_tag, chunk_idx as u32).to_vec());
827-
chunk_idx += 1;
828-
}
829-
830-
for chunk in keys_to_delete.chunks(KV_MAX_BATCH_KEYS) {
831-
if ctx.kv_delete(chunk.to_vec()).is_err() {
823+
if last_chunk_to_keep < last_existing_chunk {
824+
if ctx
825+
.kv_delete_range(
826+
kv::get_chunk_key(file.file_tag, (last_chunk_to_keep + 1) as u32).to_vec(),
827+
kv::get_chunk_key_range_end(file.file_tag).to_vec(),
828+
)
829+
.is_err()
830+
{
832831
return SQLITE_IOERR_TRUNCATE;
833832
}
834833
}

rivetkit-typescript/packages/sqlite-vfs/src/vfs.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,15 +1324,10 @@ class SqliteSystem implements SqliteVfsRegistration {
13241324
}
13251325
}
13261326

1327-
// Delete chunks beyond the new size
1328-
const keysToDelete: Uint8Array[] = [];
1329-
for (let i = lastChunkToKeep + 1; i <= lastExistingChunk; i++) {
1330-
keysToDelete.push(this.#chunkKey(file, i));
1331-
}
1332-
1333-
for (let b = 0; b < keysToDelete.length; b += KV_MAX_BATCH_KEYS) {
1334-
await options.deleteBatch(
1335-
keysToDelete.slice(b, b + KV_MAX_BATCH_KEYS),
1327+
if (lastChunkToKeep < lastExistingChunk) {
1328+
await options.deleteRange(
1329+
this.#chunkKey(file, lastChunkToKeep + 1),
1330+
getChunkKeyRangeEnd(file.fileTag),
13361331
);
13371332
}
13381333
} catch (error) {

0 commit comments

Comments
 (0)