2021-06-17 20:47:42 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
|
2023-04-23 13:38:57 +03:00
|
|
|
* Copyright (c) 2023, Jelle Raaijmakers <jelle@gmta.nl>
|
2021-06-17 20:47:42 +03:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
#include <AK/ByteString.h>
|
2021-06-17 20:47:42 +03:00
|
|
|
#include <AK/Format.h>
|
|
|
|
#include <AK/QuickSort.h>
|
2022-12-11 19:41:22 +03:00
|
|
|
#include <LibCore/System.h>
|
2021-06-17 20:47:42 +03:00
|
|
|
#include <LibSQL/Heap.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
namespace SQL {
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
ErrorOr<NonnullRefPtr<Heap>> Heap::create(ByteString file_name)
|
2023-08-07 17:57:34 +03:00
|
|
|
{
|
|
|
|
return adopt_nonnull_ref_or_enomem(new (nothrow) Heap(move(file_name)));
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
Heap::Heap(ByteString file_name)
|
2023-08-07 17:57:34 +03:00
|
|
|
: m_name(move(file_name))
|
2021-06-17 20:47:42 +03:00
|
|
|
{
|
2021-11-06 02:05:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Heap::~Heap()
|
|
|
|
{
|
|
|
|
if (m_file && !m_write_ahead_log.is_empty()) {
|
|
|
|
if (auto maybe_error = flush(); maybe_error.is_error())
|
|
|
|
warnln("~Heap({}): {}", name(), maybe_error.error());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<void> Heap::open()
|
|
|
|
{
|
2023-06-13 22:52:58 +03:00
|
|
|
VERIFY(!m_file);
|
|
|
|
|
2021-06-17 20:47:42 +03:00
|
|
|
size_t file_size = 0;
|
|
|
|
struct stat stat_buffer;
|
|
|
|
if (stat(name().characters(), &stat_buffer) != 0) {
|
|
|
|
if (errno != ENOENT) {
|
2021-11-06 02:05:59 +03:00
|
|
|
warnln("Heap::open({}): could not stat: {}"sv, name(), strerror(errno));
|
2022-07-11 20:57:32 +03:00
|
|
|
return Error::from_string_literal("Heap::open(): could not stat file");
|
2021-06-17 20:47:42 +03:00
|
|
|
}
|
2021-11-06 02:05:59 +03:00
|
|
|
} else if (!S_ISREG(stat_buffer.st_mode)) {
|
|
|
|
warnln("Heap::open({}): can only use regular files"sv, name());
|
2022-07-11 20:57:32 +03:00
|
|
|
return Error::from_string_literal("Heap::open(): can only use regular files");
|
2021-06-17 20:47:42 +03:00
|
|
|
} else {
|
|
|
|
file_size = stat_buffer.st_size;
|
|
|
|
}
|
2024-01-10 14:59:54 +03:00
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
if (file_size > 0) {
|
|
|
|
m_next_block = file_size / Block::SIZE;
|
|
|
|
m_highest_block_written = m_next_block - 1;
|
|
|
|
}
|
2021-06-17 20:47:42 +03:00
|
|
|
|
2023-02-09 05:02:46 +03:00
|
|
|
auto file = TRY(Core::File::open(name(), Core::File::OpenMode::ReadWrite));
|
2023-05-04 01:45:18 +03:00
|
|
|
m_file = TRY(Core::InputBufferedFile::create(move(file)));
|
2022-10-29 01:03:10 +03:00
|
|
|
|
2021-11-06 02:05:59 +03:00
|
|
|
if (file_size > 0) {
|
|
|
|
if (auto error_maybe = read_zero_block(); error_maybe.is_error()) {
|
|
|
|
m_file = nullptr;
|
2023-02-09 21:26:53 +03:00
|
|
|
return error_maybe.release_error();
|
2021-11-06 02:05:59 +03:00
|
|
|
}
|
|
|
|
} else {
|
2023-04-23 13:38:57 +03:00
|
|
|
TRY(initialize_zero_block());
|
2021-11-06 02:05:59 +03:00
|
|
|
}
|
2022-10-29 01:03:10 +03:00
|
|
|
|
2022-12-11 19:41:22 +03:00
|
|
|
// FIXME: We should more gracefully handle version incompatibilities. For now, we drop the database.
|
2023-04-19 20:10:31 +03:00
|
|
|
if (m_version != VERSION) {
|
|
|
|
dbgln_if(SQL_DEBUG, "Heap file {} opened has incompatible version {}. Deleting for version {}.", name(), m_version, VERSION);
|
2022-12-11 19:41:22 +03:00
|
|
|
m_file = nullptr;
|
|
|
|
|
|
|
|
TRY(Core::System::unlink(name()));
|
|
|
|
return open();
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:35:35 +03:00
|
|
|
// Perform a heap scan to find all free blocks
|
|
|
|
// FIXME: this is very inefficient; store free blocks in a persistent heap structure
|
|
|
|
for (Block::Index index = 1; index <= m_highest_block_written; ++index) {
|
|
|
|
auto block_data = TRY(read_raw_block(index));
|
|
|
|
auto size_in_bytes = *reinterpret_cast<u32*>(block_data.data());
|
|
|
|
if (size_in_bytes == 0)
|
|
|
|
TRY(m_free_block_indices.try_append(index));
|
|
|
|
}
|
|
|
|
|
|
|
|
dbgln_if(SQL_DEBUG, "Heap file {} opened; number of blocks = {}; free blocks = {}", name(), m_highest_block_written, m_free_block_indices.size());
|
2021-11-06 02:05:59 +03:00
|
|
|
return {};
|
2021-06-17 20:47:42 +03:00
|
|
|
}
|
|
|
|
|
2023-05-24 15:08:31 +03:00
|
|
|
ErrorOr<size_t> Heap::file_size_in_bytes() const
|
|
|
|
{
|
|
|
|
TRY(m_file->seek(0, SeekMode::FromEndPosition));
|
|
|
|
return TRY(m_file->tell());
|
|
|
|
}
|
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
bool Heap::has_block(Block::Index index) const
|
|
|
|
{
|
2023-05-24 15:53:43 +03:00
|
|
|
return (index <= m_highest_block_written || m_write_ahead_log.contains(index))
|
|
|
|
&& !m_free_block_indices.contains_slow(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
Block::Index Heap::request_new_block_index()
|
|
|
|
{
|
|
|
|
if (!m_free_block_indices.is_empty())
|
|
|
|
return m_free_block_indices.take_last();
|
|
|
|
return m_next_block++;
|
2023-04-23 13:38:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<ByteBuffer> Heap::read_storage(Block::Index index)
|
2021-06-17 20:47:42 +03:00
|
|
|
{
|
2023-04-23 13:38:57 +03:00
|
|
|
dbgln_if(SQL_DEBUG, "{}({})", __FUNCTION__, index);
|
|
|
|
|
|
|
|
// Reconstruct the data storage from a potential chain of blocks
|
|
|
|
ByteBuffer data;
|
|
|
|
while (index > 0) {
|
|
|
|
auto block = TRY(read_block(index));
|
|
|
|
dbgln_if(SQL_DEBUG, " -> {} bytes", block.size_in_bytes());
|
|
|
|
TRY(data.try_append(block.data().bytes().slice(0, block.size_in_bytes())));
|
|
|
|
index = block.next_block();
|
2021-11-06 02:05:59 +03:00
|
|
|
}
|
2023-04-23 13:38:57 +03:00
|
|
|
return data;
|
|
|
|
}
|
2022-10-29 01:03:10 +03:00
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
ErrorOr<void> Heap::write_storage(Block::Index index, ReadonlyBytes data)
|
|
|
|
{
|
|
|
|
dbgln_if(SQL_DEBUG, "{}({}, {} bytes)", __FUNCTION__, index, data.size());
|
2023-06-13 22:52:10 +03:00
|
|
|
if (index == 0)
|
|
|
|
return Error::from_string_view("Writing to zero block is not allowed"sv);
|
|
|
|
if (data.is_empty())
|
|
|
|
return Error::from_string_view("Writing empty data is not allowed"sv);
|
|
|
|
if (m_free_block_indices.contains_slow(index))
|
|
|
|
return Error::from_string_view("Invalid write to a free block index"sv);
|
2023-04-23 13:38:57 +03:00
|
|
|
|
|
|
|
// Split up the storage across multiple blocks if necessary, creating a chain
|
|
|
|
u32 remaining_size = static_cast<u32>(data.size());
|
|
|
|
u32 offset_in_data = 0;
|
2023-05-24 15:53:43 +03:00
|
|
|
Block::Index existing_next_block_index = 0;
|
2023-04-23 13:38:57 +03:00
|
|
|
while (remaining_size > 0) {
|
|
|
|
auto block_data_size = AK::min(remaining_size, Block::DATA_SIZE);
|
|
|
|
remaining_size -= block_data_size;
|
|
|
|
|
2023-05-24 15:08:31 +03:00
|
|
|
ByteBuffer block_data;
|
|
|
|
if (has_block(index)) {
|
|
|
|
auto existing_block = TRY(read_block(index));
|
|
|
|
block_data = existing_block.data();
|
|
|
|
TRY(block_data.try_resize(block_data_size));
|
2023-05-24 15:53:43 +03:00
|
|
|
existing_next_block_index = existing_block.next_block();
|
2023-05-24 15:08:31 +03:00
|
|
|
} else {
|
|
|
|
block_data = TRY(ByteBuffer::create_uninitialized(block_data_size));
|
2023-05-24 15:53:43 +03:00
|
|
|
existing_next_block_index = 0;
|
2023-05-24 15:08:31 +03:00
|
|
|
}
|
|
|
|
|
2023-05-24 15:53:43 +03:00
|
|
|
Block::Index next_block_index = existing_next_block_index;
|
2023-05-24 15:08:31 +03:00
|
|
|
if (next_block_index == 0 && remaining_size > 0)
|
|
|
|
next_block_index = request_new_block_index();
|
|
|
|
else if (remaining_size == 0)
|
|
|
|
next_block_index = 0;
|
2021-06-17 20:47:42 +03:00
|
|
|
|
2023-05-24 15:08:31 +03:00
|
|
|
block_data.bytes().overwrite(0, data.offset(offset_in_data), block_data_size);
|
2023-04-23 13:38:57 +03:00
|
|
|
TRY(write_block({ index, block_data_size, next_block_index, move(block_data) }));
|
|
|
|
|
|
|
|
index = next_block_index;
|
|
|
|
offset_in_data += block_data_size;
|
2021-11-06 02:05:59 +03:00
|
|
|
}
|
2023-05-24 15:53:43 +03:00
|
|
|
|
|
|
|
// Free remaining blocks in existing chain, if any
|
2023-05-24 16:48:53 +03:00
|
|
|
if (existing_next_block_index > 0)
|
|
|
|
TRY(free_storage(existing_next_block_index));
|
2023-05-24 15:53:43 +03:00
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
return {};
|
|
|
|
}
|
2022-10-29 01:03:10 +03:00
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
ErrorOr<ByteBuffer> Heap::read_raw_block(Block::Index index)
|
|
|
|
{
|
|
|
|
VERIFY(m_file);
|
|
|
|
VERIFY(index < m_next_block);
|
2022-10-29 01:03:10 +03:00
|
|
|
|
2023-05-24 16:35:35 +03:00
|
|
|
if (auto wal_entry = m_write_ahead_log.get(index); wal_entry.has_value())
|
|
|
|
return wal_entry.value();
|
2023-04-23 13:38:57 +03:00
|
|
|
|
|
|
|
TRY(m_file->seek(index * Block::SIZE, SeekMode::SetPosition));
|
|
|
|
auto buffer = TRY(ByteBuffer::create_uninitialized(Block::SIZE));
|
2023-03-01 19:30:30 +03:00
|
|
|
TRY(m_file->read_until_filled(buffer));
|
2023-04-23 13:38:57 +03:00
|
|
|
return buffer;
|
|
|
|
}
|
2022-10-29 01:03:10 +03:00
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
ErrorOr<Block> Heap::read_block(Block::Index index)
|
|
|
|
{
|
2023-06-13 22:37:11 +03:00
|
|
|
dbgln_if(SQL_DEBUG, "{}({})", __FUNCTION__, index);
|
2022-10-29 01:03:10 +03:00
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
auto buffer = TRY(read_raw_block(index));
|
|
|
|
auto size_in_bytes = *reinterpret_cast<u32*>(buffer.offset_pointer(0));
|
|
|
|
auto next_block = *reinterpret_cast<Block::Index*>(buffer.offset_pointer(sizeof(u32)));
|
|
|
|
auto data = TRY(buffer.slice(Block::HEADER_SIZE, Block::DATA_SIZE));
|
|
|
|
|
|
|
|
return Block { index, size_in_bytes, next_block, move(data) };
|
2021-06-17 20:47:42 +03:00
|
|
|
}
|
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
ErrorOr<void> Heap::write_raw_block(Block::Index index, ReadonlyBytes data)
|
2021-06-17 20:47:42 +03:00
|
|
|
{
|
2023-04-23 13:38:57 +03:00
|
|
|
dbgln_if(SQL_DEBUG, "Write raw block {}", index);
|
2022-10-29 01:03:10 +03:00
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
VERIFY(m_file);
|
|
|
|
VERIFY(data.size() == Block::SIZE);
|
2022-10-29 01:03:10 +03:00
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
TRY(m_file->seek(index * Block::SIZE, SeekMode::SetPosition));
|
|
|
|
TRY(m_file->write_until_depleted(data));
|
2022-10-29 01:03:10 +03:00
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
if (index > m_highest_block_written)
|
|
|
|
m_highest_block_written = index;
|
2022-10-29 01:03:10 +03:00
|
|
|
|
|
|
|
return {};
|
2021-06-17 20:47:42 +03:00
|
|
|
}
|
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
ErrorOr<void> Heap::write_raw_block_to_wal(Block::Index index, ByteBuffer&& data)
|
2021-06-17 20:47:42 +03:00
|
|
|
{
|
2023-06-13 22:37:11 +03:00
|
|
|
dbgln_if(SQL_DEBUG, "{}({})", __FUNCTION__, index);
|
2023-04-23 13:38:57 +03:00
|
|
|
VERIFY(index < m_next_block);
|
|
|
|
VERIFY(data.size() == Block::SIZE);
|
2022-10-29 01:03:10 +03:00
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
TRY(m_write_ahead_log.try_set(index, move(data)));
|
2022-10-29 01:03:10 +03:00
|
|
|
|
2021-11-06 02:05:59 +03:00
|
|
|
return {};
|
2021-06-17 20:47:42 +03:00
|
|
|
}
|
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
ErrorOr<void> Heap::write_block(Block const& block)
|
2021-06-17 20:47:42 +03:00
|
|
|
{
|
2023-06-13 22:37:11 +03:00
|
|
|
dbgln_if(SQL_DEBUG, "{}({})", __FUNCTION__, block.index());
|
2023-04-23 13:38:57 +03:00
|
|
|
VERIFY(block.index() < m_next_block);
|
|
|
|
VERIFY(block.next_block() < m_next_block);
|
2023-05-24 16:35:35 +03:00
|
|
|
VERIFY(block.size_in_bytes() > 0);
|
2023-04-23 13:38:57 +03:00
|
|
|
VERIFY(block.data().size() <= Block::DATA_SIZE);
|
|
|
|
|
|
|
|
auto size_in_bytes = block.size_in_bytes();
|
|
|
|
auto next_block = block.next_block();
|
|
|
|
|
|
|
|
auto heap_data = TRY(ByteBuffer::create_zeroed(Block::SIZE));
|
|
|
|
heap_data.overwrite(0, &size_in_bytes, sizeof(size_in_bytes));
|
|
|
|
heap_data.overwrite(sizeof(size_in_bytes), &next_block, sizeof(next_block));
|
|
|
|
|
|
|
|
block.data().bytes().copy_to(heap_data.bytes().slice(Block::HEADER_SIZE));
|
|
|
|
|
|
|
|
return write_raw_block_to_wal(block.index(), move(heap_data));
|
2021-06-17 20:47:42 +03:00
|
|
|
}
|
|
|
|
|
2023-05-24 16:48:53 +03:00
|
|
|
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 {};
|
|
|
|
}
|
|
|
|
|
2023-05-24 15:53:43 +03:00
|
|
|
ErrorOr<void> Heap::free_block(Block const& block)
|
|
|
|
{
|
|
|
|
auto index = block.index();
|
|
|
|
dbgln_if(SQL_DEBUG, "{}({})", __FUNCTION__, index);
|
|
|
|
|
|
|
|
VERIFY(index > 0);
|
|
|
|
VERIFY(has_block(index));
|
|
|
|
|
|
|
|
// Zero out freed blocks to facilitate a free block scan upon opening the database later
|
|
|
|
auto zeroed_data = TRY(ByteBuffer::create_zeroed(Block::SIZE));
|
|
|
|
TRY(write_raw_block_to_wal(index, move(zeroed_data)));
|
|
|
|
|
|
|
|
return m_free_block_indices.try_append(index);
|
|
|
|
}
|
|
|
|
|
2021-11-06 02:05:59 +03:00
|
|
|
ErrorOr<void> Heap::flush()
|
2021-06-17 20:47:42 +03:00
|
|
|
{
|
2022-10-29 01:03:10 +03:00
|
|
|
VERIFY(m_file);
|
2023-04-23 13:38:57 +03:00
|
|
|
auto indices = m_write_ahead_log.keys();
|
|
|
|
quick_sort(indices);
|
|
|
|
for (auto index : indices) {
|
2023-05-24 15:53:43 +03:00
|
|
|
dbgln_if(SQL_DEBUG, "Flushing block {}", index);
|
2023-04-23 13:38:57 +03:00
|
|
|
auto& data = m_write_ahead_log.get(index).value();
|
|
|
|
TRY(write_raw_block(index, data));
|
2021-06-17 20:47:42 +03:00
|
|
|
}
|
|
|
|
m_write_ahead_log.clear();
|
2023-04-23 13:38:57 +03:00
|
|
|
dbgln_if(SQL_DEBUG, "WAL flushed; new number of blocks = {}", m_highest_block_written);
|
2021-11-06 02:05:59 +03:00
|
|
|
return {};
|
2021-06-17 20:47:42 +03:00
|
|
|
}
|
|
|
|
|
2022-10-29 01:30:43 +03:00
|
|
|
constexpr static auto FILE_ID = "SerenitySQL "sv;
|
|
|
|
constexpr static auto VERSION_OFFSET = FILE_ID.length();
|
|
|
|
constexpr static auto SCHEMAS_ROOT_OFFSET = VERSION_OFFSET + sizeof(u32);
|
|
|
|
constexpr static auto TABLES_ROOT_OFFSET = SCHEMAS_ROOT_OFFSET + sizeof(u32);
|
|
|
|
constexpr static auto TABLE_COLUMNS_ROOT_OFFSET = TABLES_ROOT_OFFSET + sizeof(u32);
|
2023-04-23 13:38:57 +03:00
|
|
|
constexpr static auto USER_VALUES_OFFSET = TABLE_COLUMNS_ROOT_OFFSET + sizeof(u32);
|
2021-06-17 20:47:42 +03:00
|
|
|
|
2021-11-06 02:05:59 +03:00
|
|
|
ErrorOr<void> Heap::read_zero_block()
|
2021-06-17 20:47:42 +03:00
|
|
|
{
|
2023-04-23 13:38:57 +03:00
|
|
|
dbgln_if(SQL_DEBUG, "Read zero block from {}", name());
|
|
|
|
|
|
|
|
auto block = TRY(read_raw_block(0));
|
|
|
|
auto file_id_buffer = TRY(block.slice(0, FILE_ID.length()));
|
2021-11-06 02:05:59 +03:00
|
|
|
auto file_id = StringView(file_id_buffer);
|
|
|
|
if (file_id != FILE_ID) {
|
|
|
|
warnln("{}: Zero page corrupt. This is probably not a {} heap file"sv, name(), FILE_ID);
|
2022-07-11 20:57:32 +03:00
|
|
|
return Error::from_string_literal("Heap()::read_zero_block(): Zero page corrupt. This is probably not a SerenitySQL heap file");
|
2021-06-17 20:47:42 +03:00
|
|
|
}
|
2022-10-29 01:42:04 +03:00
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
memcpy(&m_version, block.offset_pointer(VERSION_OFFSET), sizeof(u32));
|
2021-06-17 20:47:42 +03:00
|
|
|
dbgln_if(SQL_DEBUG, "Version: {}.{}", (m_version & 0xFFFF0000) >> 16, (m_version & 0x0000FFFF));
|
2022-10-29 01:42:04 +03:00
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
memcpy(&m_schemas_root, block.offset_pointer(SCHEMAS_ROOT_OFFSET), sizeof(u32));
|
2022-10-29 01:42:04 +03:00
|
|
|
dbgln_if(SQL_DEBUG, "Schemas root node: {}", m_schemas_root);
|
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
memcpy(&m_tables_root, block.offset_pointer(TABLES_ROOT_OFFSET), sizeof(u32));
|
2021-06-17 20:47:42 +03:00
|
|
|
dbgln_if(SQL_DEBUG, "Tables root node: {}", m_tables_root);
|
2022-10-29 01:42:04 +03:00
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
memcpy(&m_table_columns_root, block.offset_pointer(TABLE_COLUMNS_ROOT_OFFSET), sizeof(u32));
|
2021-06-17 20:47:42 +03:00
|
|
|
dbgln_if(SQL_DEBUG, "Table columns root node: {}", m_table_columns_root);
|
2022-10-29 01:42:04 +03:00
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
memcpy(m_user_values.data(), block.offset_pointer(USER_VALUES_OFFSET), m_user_values.size() * sizeof(u32));
|
2021-06-17 20:47:42 +03:00
|
|
|
for (auto ix = 0u; ix < m_user_values.size(); ix++) {
|
2023-04-19 20:03:00 +03:00
|
|
|
if (m_user_values[ix])
|
2021-06-17 20:47:42 +03:00
|
|
|
dbgln_if(SQL_DEBUG, "User value {}: {}", ix, m_user_values[ix]);
|
|
|
|
}
|
2021-11-06 02:05:59 +03:00
|
|
|
return {};
|
2021-06-17 20:47:42 +03:00
|
|
|
}
|
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
ErrorOr<void> Heap::update_zero_block()
|
2021-06-17 20:47:42 +03:00
|
|
|
{
|
|
|
|
dbgln_if(SQL_DEBUG, "Write zero block to {}", name());
|
|
|
|
dbgln_if(SQL_DEBUG, "Version: {}.{}", (m_version & 0xFFFF0000) >> 16, (m_version & 0x0000FFFF));
|
|
|
|
dbgln_if(SQL_DEBUG, "Schemas root node: {}", m_schemas_root);
|
|
|
|
dbgln_if(SQL_DEBUG, "Tables root node: {}", m_tables_root);
|
|
|
|
dbgln_if(SQL_DEBUG, "Table Columns root node: {}", m_table_columns_root);
|
|
|
|
for (auto ix = 0u; ix < m_user_values.size(); ix++) {
|
2023-04-23 13:38:57 +03:00
|
|
|
if (m_user_values[ix] > 0)
|
2021-06-17 20:47:42 +03:00
|
|
|
dbgln_if(SQL_DEBUG, "User value {}: {}", ix, m_user_values[ix]);
|
|
|
|
}
|
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
auto buffer = TRY(ByteBuffer::create_zeroed(Block::SIZE));
|
|
|
|
auto buffer_bytes = buffer.bytes();
|
|
|
|
buffer_bytes.overwrite(0, FILE_ID.characters_without_null_termination(), FILE_ID.length());
|
|
|
|
buffer_bytes.overwrite(VERSION_OFFSET, &m_version, sizeof(u32));
|
|
|
|
buffer_bytes.overwrite(SCHEMAS_ROOT_OFFSET, &m_schemas_root, sizeof(u32));
|
|
|
|
buffer_bytes.overwrite(TABLES_ROOT_OFFSET, &m_tables_root, sizeof(u32));
|
|
|
|
buffer_bytes.overwrite(TABLE_COLUMNS_ROOT_OFFSET, &m_table_columns_root, sizeof(u32));
|
|
|
|
buffer_bytes.overwrite(USER_VALUES_OFFSET, m_user_values.data(), m_user_values.size() * sizeof(u32));
|
|
|
|
|
|
|
|
return write_raw_block_to_wal(0, move(buffer));
|
2021-06-17 20:47:42 +03:00
|
|
|
}
|
|
|
|
|
2023-04-23 13:38:57 +03:00
|
|
|
ErrorOr<void> Heap::initialize_zero_block()
|
2021-06-17 20:47:42 +03:00
|
|
|
{
|
2023-04-19 20:10:31 +03:00
|
|
|
m_version = VERSION;
|
2021-06-17 20:47:42 +03:00
|
|
|
m_schemas_root = 0;
|
|
|
|
m_tables_root = 0;
|
|
|
|
m_table_columns_root = 0;
|
|
|
|
m_next_block = 1;
|
2024-01-10 14:59:54 +03:00
|
|
|
m_highest_block_written = 0;
|
2023-04-19 20:03:00 +03:00
|
|
|
for (auto& user : m_user_values)
|
2021-06-17 20:47:42 +03:00
|
|
|
user = 0u;
|
2023-04-23 13:38:57 +03:00
|
|
|
return update_zero_block();
|
2021-06-17 20:47:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|