1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:47:44 +00:00

LibSQL: Redesign heap storage to support arbitrary amounts of data

Previously, `Heap` would store serialized data in blocks of 1024 bytes
regardless of the actual length. Data longer than 1024 bytes was
silently truncated causing database corruption.

This changes the heap storage to prefix every block with two new fields:
the total data size in bytes, and the next block to retrieve if the data
is longer than what can be stored inside a single block. By chaining
blocks together, we can store arbitrary amounts of data without needing
to change anything of the logic in the rest of LibSQL.

As part of these changes, the "free list" is also removed from the heap
awaiting an actual implementation: it was never used.

Note that this bumps the database version from 3 to 4, and as such
invalidates (deletes) any database opened with LibSQL that is not
version 4.
This commit is contained in:
Jelle Raaijmakers 2023-04-23 12:38:57 +02:00 committed by Tim Flynn
parent 194f846f12
commit 6601ff9d65
13 changed files with 246 additions and 180 deletions

View file

@ -12,7 +12,6 @@
#include <AK/Format.h>
#include <LibSQL/Forward.h>
#include <LibSQL/Heap.h>
#include <string.h>
namespace SQL {
@ -25,12 +24,9 @@ public:
{
}
void get_block(u32 pointer)
void read_storage(Block::Index block_index)
{
auto buffer_or_error = m_heap->read_block(pointer);
if (buffer_or_error.is_error())
VERIFY_NOT_REACHED();
m_buffer = buffer_or_error.value();
m_buffer = m_heap->read_storage(block_index).release_value_but_fixme_should_propagate_errors();
m_current_offset = 0;
}
@ -48,14 +44,14 @@ public:
template<typename T, typename... Args>
T deserialize_block(u32 pointer, Args&&... args)
{
get_block(pointer);
read_storage(pointer);
return deserialize<T>(forward<Args>(args)...);
}
template<typename T>
void deserialize_block_to(u32 pointer, T& t)
{
get_block(pointer);
read_storage(pointer);
return deserialize_to<T>(t);
}
@ -111,19 +107,19 @@ public:
VERIFY(!m_heap.is_null());
reset();
serialize<T>(t);
m_heap->add_to_wal(t.pointer(), m_buffer);
m_heap->write_storage(t.pointer(), m_buffer).release_value_but_fixme_should_propagate_errors();
return true;
}
[[nodiscard]] size_t offset() const { return m_current_offset; }
u32 new_record_pointer()
u32 request_new_block_index()
{
return m_heap->new_record_pointer();
return m_heap->request_new_block_index();
}
bool has_block(u32 pointer) const
{
return pointer < m_heap->size();
return m_heap->has_block(pointer);
}
Heap& heap()