1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:18:11 +00:00

LibSQL: Rename Heap constants to match our code style

No functional changes. The constants are moved to constexpr variables
inside `Heap`.
This commit is contained in:
Jelle Raaijmakers 2023-04-19 19:10:31 +02:00 committed by Tim Flynn
parent fcd35e824c
commit 194f846f12
8 changed files with 24 additions and 26 deletions

View file

@ -110,7 +110,7 @@ TEST_CASE(create_heap)
ScopeGuard guard([]() { unlink("/tmp/test.db"); }); ScopeGuard guard([]() { unlink("/tmp/test.db"); });
auto heap = SQL::Heap::construct("/tmp/test.db"); auto heap = SQL::Heap::construct("/tmp/test.db");
EXPECT(!heap->open().is_error()); EXPECT(!heap->open().is_error());
EXPECT_EQ(heap->version(), SQL::Heap::current_version); EXPECT_EQ(heap->version(), SQL::Heap::VERSION);
} }
TEST_CASE(create_from_dev_random) TEST_CASE(create_from_dev_random)

View file

@ -132,7 +132,7 @@ bool HashBucket::insert(Key const& key)
m_hash_index.serializer().deserialize_block_to(pointer(), *this); m_hash_index.serializer().deserialize_block_to(pointer(), *this);
if (find_key_in_bucket(key).has_value()) if (find_key_in_bucket(key).has_value())
return false; return false;
if ((length() + key.length()) > BLOCKSIZE) { if ((length() + key.length()) > Heap::BLOCK_SIZE) {
dbgln_if(SQL_DEBUG, "Adding key {} would make length exceed block size", key.to_deprecated_string()); dbgln_if(SQL_DEBUG, "Adding key {} would make length exceed block size", key.to_deprecated_string());
return false; return false;
} }
@ -247,9 +247,8 @@ HashBucket* HashIndex::get_bucket_for_insert(Key const& key)
do { do {
dbgln_if(SQL_DEBUG, "HashIndex::get_bucket_for_insert({}) bucket {} of {}", key.to_deprecated_string(), key_hash % size(), size()); dbgln_if(SQL_DEBUG, "HashIndex::get_bucket_for_insert({}) bucket {} of {}", key.to_deprecated_string(), key_hash % size(), size());
auto bucket = get_bucket(key_hash % size()); auto bucket = get_bucket(key_hash % size());
if (bucket->length() + key.length() < BLOCKSIZE) { if (bucket->length() + key.length() < Heap::BLOCK_SIZE)
return bucket; return bucket;
}
dbgln_if(SQL_DEBUG, "Bucket is full (bucket size {}/length {} key length {}). Expanding directory", bucket->size(), bucket->length(), key.length()); dbgln_if(SQL_DEBUG, "Bucket is full (bucket size {}/length {} key length {}). Expanding directory", bucket->size(), bucket->length(), key.length());
// We previously doubled the directory but the target bucket is // We previously doubled the directory but the target bucket is
@ -287,7 +286,7 @@ HashBucket* HashIndex::get_bucket_for_insert(Key const& key)
write_directory_to_write_ahead_log(); write_directory_to_write_ahead_log();
auto bucket_after_redistribution = get_bucket(key_hash % size()); auto bucket_after_redistribution = get_bucket(key_hash % size());
if (bucket_after_redistribution->length() + key.length() < BLOCKSIZE) if (bucket_after_redistribution->length() + key.length() < Heap::BLOCK_SIZE)
return bucket_after_redistribution; return bucket_after_redistribution;
} }
expand(); expand();

View file

@ -104,7 +104,7 @@ public:
void serialize(Serializer&) const; void serialize(Serializer&) const;
[[nodiscard]] u32 number_of_pointers() const { return min(max_pointers_in_node(), m_hash_index.size() - m_offset); } [[nodiscard]] u32 number_of_pointers() const { return min(max_pointers_in_node(), m_hash_index.size() - m_offset); }
[[nodiscard]] bool is_last() const { return m_is_last; } [[nodiscard]] bool is_last() const { return m_is_last; }
static constexpr size_t max_pointers_in_node() { return (BLOCKSIZE - 3 * sizeof(u32)) / (2 * sizeof(u32)); } static constexpr size_t max_pointers_in_node() { return (Heap::BLOCK_SIZE - 3 * sizeof(u32)) / (2 * sizeof(u32)); }
private: private:
HashIndex& m_hash_index; HashIndex& m_hash_index;

View file

@ -43,7 +43,7 @@ ErrorOr<void> Heap::open()
file_size = stat_buffer.st_size; file_size = stat_buffer.st_size;
} }
if (file_size > 0) if (file_size > 0)
m_next_block = m_end_of_file = file_size / BLOCKSIZE; m_next_block = m_end_of_file = file_size / BLOCK_SIZE;
auto file = TRY(Core::File::open(name(), Core::File::OpenMode::ReadWrite)); auto file = TRY(Core::File::open(name(), Core::File::OpenMode::ReadWrite));
m_file = TRY(Core::BufferedFile::create(move(file))); m_file = TRY(Core::BufferedFile::create(move(file)));
@ -58,8 +58,8 @@ ErrorOr<void> Heap::open()
} }
// FIXME: We should more gracefully handle version incompatibilities. For now, we drop the database. // FIXME: We should more gracefully handle version incompatibilities. For now, we drop the database.
if (m_version != current_version) { if (m_version != VERSION) {
dbgln_if(SQL_DEBUG, "Heap file {} opened has incompatible version {}. Deleting for version {}.", name(), m_version, current_version); dbgln_if(SQL_DEBUG, "Heap file {} opened has incompatible version {}. Deleting for version {}.", name(), m_version, VERSION);
m_file = nullptr; m_file = nullptr;
TRY(Core::System::unlink(name())); TRY(Core::System::unlink(name()));
@ -88,7 +88,7 @@ ErrorOr<ByteBuffer> Heap::read_block(u32 block)
dbgln_if(SQL_DEBUG, "Read heap block {}", block); dbgln_if(SQL_DEBUG, "Read heap block {}", block);
TRY(seek_block(block)); TRY(seek_block(block));
auto buffer = TRY(ByteBuffer::create_uninitialized(BLOCKSIZE)); auto buffer = TRY(ByteBuffer::create_uninitialized(BLOCK_SIZE));
TRY(m_file->read_until_filled(buffer)); TRY(m_file->read_until_filled(buffer));
dbgln_if(SQL_DEBUG, "{:hex-dump}", buffer.bytes().trim(8)); dbgln_if(SQL_DEBUG, "{:hex-dump}", buffer.bytes().trim(8));
@ -106,17 +106,17 @@ ErrorOr<void> Heap::write_block(u32 block, ByteBuffer& buffer)
warnln("Heap({})::write_block({}): block # out of range (> {})"sv, name(), block, m_next_block); warnln("Heap({})::write_block({}): block # out of range (> {})"sv, name(), block, m_next_block);
return Error::from_string_literal("Heap()::write_block(): block # out of range"); return Error::from_string_literal("Heap()::write_block(): block # out of range");
} }
if (buffer.size() > BLOCKSIZE) { if (buffer.size() > BLOCK_SIZE) {
warnln("Heap({})::write_block({}): Oversized block ({} > {})"sv, name(), block, buffer.size(), BLOCKSIZE); warnln("Heap({})::write_block({}): Oversized block ({} > {})"sv, name(), block, buffer.size(), BLOCK_SIZE);
return Error::from_string_literal("Heap()::write_block(): Oversized block"); return Error::from_string_literal("Heap()::write_block(): Oversized block");
} }
dbgln_if(SQL_DEBUG, "Write heap block {} size {}", block, buffer.size()); dbgln_if(SQL_DEBUG, "Write heap block {} size {}", block, buffer.size());
TRY(seek_block(block)); TRY(seek_block(block));
if (auto current_size = buffer.size(); current_size < BLOCKSIZE) { if (auto current_size = buffer.size(); current_size < BLOCK_SIZE) {
TRY(buffer.try_resize(BLOCKSIZE)); TRY(buffer.try_resize(BLOCK_SIZE));
memset(buffer.offset_pointer(current_size), 0, BLOCKSIZE - current_size); memset(buffer.offset_pointer(current_size), 0, BLOCK_SIZE - current_size);
} }
dbgln_if(SQL_DEBUG, "{:hex-dump}", buffer.bytes().trim(8)); dbgln_if(SQL_DEBUG, "{:hex-dump}", buffer.bytes().trim(8));
@ -141,7 +141,7 @@ ErrorOr<void> Heap::seek_block(u32 block)
if (block == m_end_of_file) if (block == m_end_of_file)
TRY(m_file->seek(0, SeekMode::FromEndPosition)); TRY(m_file->seek(0, SeekMode::FromEndPosition));
else else
TRY(m_file->seek(block * BLOCKSIZE, SeekMode::SetPosition)); TRY(m_file->seek(block * BLOCK_SIZE, SeekMode::SetPosition));
return {}; return {};
} }
@ -238,7 +238,7 @@ void Heap::update_zero_block()
} }
// FIXME: Handle an OOM failure here. // FIXME: Handle an OOM failure here.
auto buffer = ByteBuffer::create_zeroed(BLOCKSIZE).release_value_but_fixme_should_propagate_errors(); auto buffer = ByteBuffer::create_zeroed(BLOCK_SIZE).release_value_but_fixme_should_propagate_errors();
buffer.overwrite(0, FILE_ID.characters_without_null_termination(), FILE_ID.length()); buffer.overwrite(0, FILE_ID.characters_without_null_termination(), FILE_ID.length());
buffer.overwrite(VERSION_OFFSET, &m_version, sizeof(u32)); buffer.overwrite(VERSION_OFFSET, &m_version, sizeof(u32));
buffer.overwrite(SCHEMAS_ROOT_OFFSET, &m_schemas_root, sizeof(u32)); buffer.overwrite(SCHEMAS_ROOT_OFFSET, &m_schemas_root, sizeof(u32));
@ -252,7 +252,7 @@ void Heap::update_zero_block()
void Heap::initialize_zero_block() void Heap::initialize_zero_block()
{ {
m_version = current_version; m_version = VERSION;
m_schemas_root = 0; m_schemas_root = 0;
m_tables_root = 0; m_tables_root = 0;
m_table_columns_root = 0; m_table_columns_root = 0;

View file

@ -15,8 +15,6 @@
namespace SQL { namespace SQL {
constexpr static u32 BLOCKSIZE = 1024;
/** /**
* A Heap is a logical container for database (SQL) data. Conceptually a * A Heap is a logical container for database (SQL) data. Conceptually a
* Heap can be a database file, or a memory block, or another storage medium. * Heap can be a database file, or a memory block, or another storage medium.
@ -32,7 +30,8 @@ class Heap : public Core::Object {
C_OBJECT(Heap); C_OBJECT(Heap);
public: public:
static constexpr inline u32 current_version = 3; static constexpr u32 VERSION = 3;
static constexpr u32 BLOCK_SIZE = 1024;
virtual ~Heap() override; virtual ~Heap() override;
@ -103,7 +102,7 @@ private:
u32 m_schemas_root { 0 }; u32 m_schemas_root { 0 };
u32 m_tables_root { 0 }; u32 m_tables_root { 0 };
u32 m_table_columns_root { 0 }; u32 m_table_columns_root { 0 };
u32 m_version { current_version }; u32 m_version { VERSION };
Array<u32, 16> m_user_values { 0 }; Array<u32, 16> m_user_values { 0 };
HashMap<u32, ByteBuffer> m_write_ahead_log; HashMap<u32, ByteBuffer> m_write_ahead_log;
}; };

View file

@ -271,7 +271,7 @@ void TreeNode::just_insert(Key const& key, TreeNode* right)
m_entries.insert(ix, key); m_entries.insert(ix, key);
VERIFY(is_leaf() == (right == nullptr)); VERIFY(is_leaf() == (right == nullptr));
m_down.insert(ix + 1, DownPointer(this, right)); m_down.insert(ix + 1, DownPointer(this, right));
if (length() > BLOCKSIZE) { if (length() > Heap::BLOCK_SIZE) {
split(); split();
} else { } else {
dump_if(SQL_DEBUG, "To WAL"); dump_if(SQL_DEBUG, "To WAL");
@ -283,7 +283,7 @@ void TreeNode::just_insert(Key const& key, TreeNode* right)
m_entries.append(key); m_entries.append(key);
m_down.empend(this, right); m_down.empend(this, right);
if (length() > BLOCKSIZE) { if (length() > Heap::BLOCK_SIZE) {
split(); split();
} else { } else {
dump_if(SQL_DEBUG, "To WAL"); dump_if(SQL_DEBUG, "To WAL");

View file

@ -13,7 +13,7 @@ namespace SQL {
// Adding to this list is fine, but changing the order of any value here will result in LibSQL // Adding to this list is fine, but changing the order of any value here will result in LibSQL
// becoming unable to read existing .db files. If the order must absolutely be changed, be sure // becoming unable to read existing .db files. If the order must absolutely be changed, be sure
// to bump Heap::current_version. // to bump Heap::VERSION.
#define ENUMERATE_SQL_TYPES(S) \ #define ENUMERATE_SQL_TYPES(S) \
S("null", Null) \ S("null", Null) \
S("text", Text) \ S("text", Text) \

View file

@ -30,7 +30,7 @@ static_assert(to_underlying(SQLTypeWithCount::Count) <= 0x0f, "Too many SQL type
// Adding to this list is fine, but changing the order of any value here will result in LibSQL // Adding to this list is fine, but changing the order of any value here will result in LibSQL
// becoming unable to read existing .db files. If the order must absolutely be changed, be sure // becoming unable to read existing .db files. If the order must absolutely be changed, be sure
// to bump Heap::current_version. // to bump Heap::VERSION.
enum class TypeData : u8 { enum class TypeData : u8 {
Null = 1 << 4, Null = 1 << 4,
Int8 = 2 << 4, Int8 = 2 << 4,