mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 15:37:43 +00:00
LibSQL: Implement freeing heap storage
This allows us to free entire chains of blocks in one go.
This commit is contained in:
parent
d7bbb8d64a
commit
c58c87d7ef
3 changed files with 40 additions and 5 deletions
|
@ -167,3 +167,27 @@ TEST_CASE(heap_reuse_freed_blocks_after_reopening_file)
|
||||||
EXPECT(heap_size_after_second_storage <= original_heap_size);
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -154,11 +154,8 @@ ErrorOr<void> Heap::write_storage(Block::Index index, ReadonlyBytes data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Free remaining blocks in existing chain, if any
|
// Free remaining blocks in existing chain, if any
|
||||||
while (existing_next_block_index > 0) {
|
if (existing_next_block_index > 0)
|
||||||
auto existing_block = TRY(read_block(existing_next_block_index));
|
TRY(free_storage(existing_next_block_index));
|
||||||
existing_next_block_index = existing_block.next_block();
|
|
||||||
TRY(free_block(existing_block));
|
|
||||||
}
|
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
@ -235,6 +232,19 @@ ErrorOr<void> Heap::write_block(Block const& block)
|
||||||
return write_raw_block_to_wal(block.index(), move(heap_data));
|
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)
|
ErrorOr<void> Heap::free_block(Block const& block)
|
||||||
{
|
{
|
||||||
auto index = block.index();
|
auto index = block.index();
|
||||||
|
|
|
@ -116,6 +116,7 @@ public:
|
||||||
|
|
||||||
ErrorOr<ByteBuffer> read_storage(Block::Index);
|
ErrorOr<ByteBuffer> read_storage(Block::Index);
|
||||||
ErrorOr<void> write_storage(Block::Index, ReadonlyBytes);
|
ErrorOr<void> write_storage(Block::Index, ReadonlyBytes);
|
||||||
|
ErrorOr<void> free_storage(Block::Index);
|
||||||
|
|
||||||
ErrorOr<void> flush();
|
ErrorOr<void> flush();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue