LibSQL: Implement freeing heap storage

This allows us to free entire chains of blocks in one go.
This commit is contained in:
Jelle Raaijmakers 2023-05-24 15:48:53 +02:00 committed by Tim Flynn
parent d7bbb8d64a
commit c58c87d7ef
Notes: sideshowbarker 2024-07-17 02:35:27 +09:00
3 changed files with 40 additions and 5 deletions

View file

@ -167,3 +167,27 @@ TEST_CASE(heap_reuse_freed_blocks_after_reopening_file)
EXPECT(heap_size_after_second_storage <= original_heap_size);
}
}
TEST_CASE(heap_free_storage)
{
ScopeGuard guard([]() { MUST(Core::System::unlink(db_path)); });
auto heap = create_heap();
auto storage_block_id = heap->request_new_block_index();
// Write large storage spanning multiple blocks
StringBuilder builder;
MUST(builder.try_append_repeated('x', SQL::Block::DATA_SIZE * 4));
auto long_string = builder.string_view();
TRY_OR_FAIL(heap->write_storage(storage_block_id, long_string.bytes()));
MUST(heap->flush());
auto heap_size = MUST(heap->file_size_in_bytes());
// Free the storage
TRY_OR_FAIL(heap->free_storage(storage_block_id));
// Again, write some large storage spanning multiple blocks
TRY_OR_FAIL(heap->write_storage(storage_block_id, long_string.bytes()));
MUST(heap->flush());
auto new_heap_size = MUST(heap->file_size_in_bytes());
EXPECT(new_heap_size <= heap_size);
}

View file

@ -154,11 +154,8 @@ ErrorOr<void> Heap::write_storage(Block::Index index, ReadonlyBytes data)
}
// Free remaining blocks in existing chain, if any
while (existing_next_block_index > 0) {
auto existing_block = TRY(read_block(existing_next_block_index));
existing_next_block_index = existing_block.next_block();
TRY(free_block(existing_block));
}
if (existing_next_block_index > 0)
TRY(free_storage(existing_next_block_index));
return {};
}
@ -235,6 +232,19 @@ ErrorOr<void> Heap::write_block(Block const& block)
return write_raw_block_to_wal(block.index(), move(heap_data));
}
ErrorOr<void> Heap::free_storage(Block::Index index)
{
dbgln_if(SQL_DEBUG, "{}({})", __FUNCTION__, index);
VERIFY(index > 0);
while (index > 0) {
auto block = TRY(read_block(index));
TRY(free_block(block));
index = block.next_block();
}
return {};
}
ErrorOr<void> Heap::free_block(Block const& block)
{
auto index = block.index();

View file

@ -116,6 +116,7 @@ public:
ErrorOr<ByteBuffer> read_storage(Block::Index);
ErrorOr<void> write_storage(Block::Index, ReadonlyBytes);
ErrorOr<void> free_storage(Block::Index);
ErrorOr<void> flush();