Skip to content

Commit dfaae41

Browse files
committed
fix(sqlite-vfs): use delete range for truncate cleanup
1 parent 2be63d0 commit dfaae41

File tree

2 files changed

+12
-18
lines changed
  • rivetkit-typescript/packages

2 files changed

+12
-18
lines changed

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

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

840-
let mut keys_to_delete = Vec::new();
841-
let mut chunk_idx = last_chunk_to_keep + 1;
842-
while chunk_idx <= last_existing_chunk {
843-
keys_to_delete.push(kv::get_chunk_key(file.file_tag, chunk_idx as u32).to_vec());
844-
chunk_idx += 1;
845-
}
846-
847-
for chunk in keys_to_delete.chunks(KV_MAX_BATCH_KEYS) {
848-
if ctx.kv_delete(chunk.to_vec()).is_err() {
840+
if last_chunk_to_keep < last_existing_chunk {
841+
if ctx
842+
.kv_delete_range(
843+
kv::get_chunk_key(file.file_tag, (last_chunk_to_keep + 1) as u32).to_vec(),
844+
kv::get_chunk_key_range_end(file.file_tag).to_vec(),
845+
)
846+
.is_err()
847+
{
849848
return SQLITE_IOERR_TRUNCATE;
850849
}
851850
}

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)