mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 19:07:35 +00:00
LibSQL: Free heap storage when deleting rows
This commit is contained in:
parent
c58c87d7ef
commit
69e09fed39
3 changed files with 33 additions and 1 deletions
|
@ -206,3 +206,32 @@ TEST_CASE(insert_100_into_table)
|
||||||
{
|
{
|
||||||
insert_and_verify(100);
|
insert_and_verify(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE(reuse_row_storage)
|
||||||
|
{
|
||||||
|
ScopeGuard guard([]() { unlink("/tmp/test.db"); });
|
||||||
|
auto db = SQL::Database::construct("/tmp/test.db");
|
||||||
|
MUST(db->open());
|
||||||
|
(void)setup_table(db);
|
||||||
|
auto table = MUST(db->get_table("TestSchema", "TestTable"));
|
||||||
|
|
||||||
|
// Insert row
|
||||||
|
SQL::Row row(*table);
|
||||||
|
row["TextColumn"] = "text value";
|
||||||
|
row["IntColumn"] = 12345;
|
||||||
|
TRY_OR_FAIL(db->insert(row));
|
||||||
|
TRY_OR_FAIL(db->commit());
|
||||||
|
auto original_size_in_bytes = MUST(db->file_size_in_bytes());
|
||||||
|
|
||||||
|
// Remove row
|
||||||
|
TRY_OR_FAIL(db->remove(row));
|
||||||
|
TRY_OR_FAIL(db->commit());
|
||||||
|
auto size_in_bytes_after_removal = MUST(db->file_size_in_bytes());
|
||||||
|
EXPECT(size_in_bytes_after_removal <= original_size_in_bytes);
|
||||||
|
|
||||||
|
// Insert same row again
|
||||||
|
TRY_OR_FAIL(db->insert(row));
|
||||||
|
TRY_OR_FAIL(db->commit());
|
||||||
|
auto size_in_bytes_after_reinsertion = MUST(db->file_size_in_bytes());
|
||||||
|
EXPECT(size_in_bytes_after_reinsertion <= original_size_in_bytes);
|
||||||
|
}
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/DeprecatedString.h>
|
#include <AK/DeprecatedString.h>
|
||||||
#include <AK/NonnullRefPtr.h>
|
|
||||||
#include <LibSQL/BTree.h>
|
#include <LibSQL/BTree.h>
|
||||||
#include <LibSQL/Database.h>
|
#include <LibSQL/Database.h>
|
||||||
#include <LibSQL/Heap.h>
|
#include <LibSQL/Heap.h>
|
||||||
|
@ -217,6 +216,8 @@ ErrorOr<void> Database::remove(Row& row)
|
||||||
auto& table = row.table();
|
auto& table = row.table();
|
||||||
VERIFY(m_table_cache.get(table.key().hash()).has_value());
|
VERIFY(m_table_cache.get(table.key().hash()).has_value());
|
||||||
|
|
||||||
|
TRY(m_heap->free_storage(row.block_index()));
|
||||||
|
|
||||||
if (table.block_index() == row.block_index()) {
|
if (table.block_index() == row.block_index()) {
|
||||||
auto table_key = table.key();
|
auto table_key = table.key();
|
||||||
table_key.set_block_index(row.next_block_index());
|
table_key.set_block_index(row.next_block_index());
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/DeprecatedString.h>
|
#include <AK/DeprecatedString.h>
|
||||||
|
#include <AK/NonnullRefPtr.h>
|
||||||
#include <AK/RefPtr.h>
|
#include <AK/RefPtr.h>
|
||||||
#include <LibCore/Object.h>
|
#include <LibCore/Object.h>
|
||||||
#include <LibSQL/Forward.h>
|
#include <LibSQL/Forward.h>
|
||||||
|
@ -32,6 +33,7 @@ public:
|
||||||
ResultOr<void> open();
|
ResultOr<void> open();
|
||||||
bool is_open() const { return m_open; }
|
bool is_open() const { return m_open; }
|
||||||
ErrorOr<void> commit();
|
ErrorOr<void> commit();
|
||||||
|
ErrorOr<size_t> file_size_in_bytes() const { return m_heap->file_size_in_bytes(); }
|
||||||
|
|
||||||
ResultOr<void> add_schema(SchemaDef const&);
|
ResultOr<void> add_schema(SchemaDef const&);
|
||||||
static Key get_schema_key(DeprecatedString const&);
|
static Key get_schema_key(DeprecatedString const&);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue