summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJelle Raaijmakers <jelle@gmta.nl>2023-04-19 19:10:31 +0200
committerTim Flynn <trflynn89@pm.me>2023-04-23 18:08:17 -0400
commit194f846f1297fd919a1aa96d7c6f003da7b4a430 (patch)
tree29491d9df4d85fc6a19d09576c9daf19ff9e7453
parentfcd35e824c70c825aebb0d3a0882cbfba0e1bdc7 (diff)
downloadserenity-194f846f1297fd919a1aa96d7c6f003da7b4a430.zip
LibSQL: Rename `Heap` constants to match our code style
No functional changes. The constants are moved to constexpr variables inside `Heap`.
-rw-r--r--Tests/LibSQL/TestSqlDatabase.cpp2
-rw-r--r--Userland/Libraries/LibSQL/HashIndex.cpp7
-rw-r--r--Userland/Libraries/LibSQL/HashIndex.h2
-rw-r--r--Userland/Libraries/LibSQL/Heap.cpp24
-rw-r--r--Userland/Libraries/LibSQL/Heap.h7
-rw-r--r--Userland/Libraries/LibSQL/TreeNode.cpp4
-rw-r--r--Userland/Libraries/LibSQL/Type.h2
-rw-r--r--Userland/Libraries/LibSQL/Value.cpp2
8 files changed, 24 insertions, 26 deletions
diff --git a/Tests/LibSQL/TestSqlDatabase.cpp b/Tests/LibSQL/TestSqlDatabase.cpp
index 9677b446ec..90008f8073 100644
--- a/Tests/LibSQL/TestSqlDatabase.cpp
+++ b/Tests/LibSQL/TestSqlDatabase.cpp
@@ -110,7 +110,7 @@ TEST_CASE(create_heap)
ScopeGuard guard([]() { unlink("/tmp/test.db"); });
auto heap = SQL::Heap::construct("/tmp/test.db");
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)
diff --git a/Userland/Libraries/LibSQL/HashIndex.cpp b/Userland/Libraries/LibSQL/HashIndex.cpp
index 25a8b5c272..f9998cd055 100644
--- a/Userland/Libraries/LibSQL/HashIndex.cpp
+++ b/Userland/Libraries/LibSQL/HashIndex.cpp
@@ -132,7 +132,7 @@ bool HashBucket::insert(Key const& key)
m_hash_index.serializer().deserialize_block_to(pointer(), *this);
if (find_key_in_bucket(key).has_value())
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());
return false;
}
@@ -247,9 +247,8 @@ HashBucket* HashIndex::get_bucket_for_insert(Key const& key)
do {
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());
- if (bucket->length() + key.length() < BLOCKSIZE) {
+ if (bucket->length() + key.length() < Heap::BLOCK_SIZE)
return bucket;
- }
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
@@ -287,7 +286,7 @@ HashBucket* HashIndex::get_bucket_for_insert(Key const& key)
write_directory_to_write_ahead_log();
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;
}
expand();
diff --git a/Userland/Libraries/LibSQL/HashIndex.h b/Userland/Libraries/LibSQL/HashIndex.h
index 37caaaae1e..e320a6f5d9 100644
--- a/Userland/Libraries/LibSQL/HashIndex.h
+++ b/Userland/Libraries/LibSQL/HashIndex.h
@@ -104,7 +104,7 @@ public:
void serialize(Serializer&) const;
[[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; }
- 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:
HashIndex& m_hash_index;
diff --git a/Userland/Libraries/LibSQL/Heap.cpp b/Userland/Libraries/LibSQL/Heap.cpp
index 93c466b977..10e79d3f5f 100644
--- a/Userland/Libraries/LibSQL/Heap.cpp
+++ b/Userland/Libraries/LibSQL/Heap.cpp
@@ -43,7 +43,7 @@ ErrorOr<void> Heap::open()
file_size = stat_buffer.st_size;
}
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));
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.
- if (m_version != current_version) {
- dbgln_if(SQL_DEBUG, "Heap file {} opened has incompatible version {}. Deleting for version {}.", name(), m_version, current_version);
+ if (m_version != VERSION) {
+ dbgln_if(SQL_DEBUG, "Heap file {} opened has incompatible version {}. Deleting for version {}.", name(), m_version, VERSION);
m_file = nullptr;
TRY(Core::System::unlink(name()));
@@ -88,7 +88,7 @@ ErrorOr<ByteBuffer> Heap::read_block(u32 block)
dbgln_if(SQL_DEBUG, "Read heap 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));
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);
return Error::from_string_literal("Heap()::write_block(): block # out of range");
}
- if (buffer.size() > BLOCKSIZE) {
- warnln("Heap({})::write_block({}): Oversized block ({} > {})"sv, name(), block, buffer.size(), BLOCKSIZE);
+ if (buffer.size() > BLOCK_SIZE) {
+ warnln("Heap({})::write_block({}): Oversized block ({} > {})"sv, name(), block, buffer.size(), BLOCK_SIZE);
return Error::from_string_literal("Heap()::write_block(): Oversized block");
}
dbgln_if(SQL_DEBUG, "Write heap block {} size {}", block, buffer.size());
TRY(seek_block(block));
- if (auto current_size = buffer.size(); current_size < BLOCKSIZE) {
- TRY(buffer.try_resize(BLOCKSIZE));
- memset(buffer.offset_pointer(current_size), 0, BLOCKSIZE - current_size);
+ if (auto current_size = buffer.size(); current_size < BLOCK_SIZE) {
+ TRY(buffer.try_resize(BLOCK_SIZE));
+ memset(buffer.offset_pointer(current_size), 0, BLOCK_SIZE - current_size);
}
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)
TRY(m_file->seek(0, SeekMode::FromEndPosition));
else
- TRY(m_file->seek(block * BLOCKSIZE, SeekMode::SetPosition));
+ TRY(m_file->seek(block * BLOCK_SIZE, SeekMode::SetPosition));
return {};
}
@@ -238,7 +238,7 @@ void Heap::update_zero_block()
}
// 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(VERSION_OFFSET, &m_version, 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()
{
- m_version = current_version;
+ m_version = VERSION;
m_schemas_root = 0;
m_tables_root = 0;
m_table_columns_root = 0;
diff --git a/Userland/Libraries/LibSQL/Heap.h b/Userland/Libraries/LibSQL/Heap.h
index 6add15fba6..00ca39f7a9 100644
--- a/Userland/Libraries/LibSQL/Heap.h
+++ b/Userland/Libraries/LibSQL/Heap.h
@@ -15,8 +15,6 @@
namespace SQL {
-constexpr static u32 BLOCKSIZE = 1024;
-
/**
* 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.
@@ -32,7 +30,8 @@ class Heap : public Core::Object {
C_OBJECT(Heap);
public:
- static constexpr inline u32 current_version = 3;
+ static constexpr u32 VERSION = 3;
+ static constexpr u32 BLOCK_SIZE = 1024;
virtual ~Heap() override;
@@ -103,7 +102,7 @@ private:
u32 m_schemas_root { 0 };
u32 m_tables_root { 0 };
u32 m_table_columns_root { 0 };
- u32 m_version { current_version };
+ u32 m_version { VERSION };
Array<u32, 16> m_user_values { 0 };
HashMap<u32, ByteBuffer> m_write_ahead_log;
};
diff --git a/Userland/Libraries/LibSQL/TreeNode.cpp b/Userland/Libraries/LibSQL/TreeNode.cpp
index b4dd4c37a7..d25676203c 100644
--- a/Userland/Libraries/LibSQL/TreeNode.cpp
+++ b/Userland/Libraries/LibSQL/TreeNode.cpp
@@ -271,7 +271,7 @@ void TreeNode::just_insert(Key const& key, TreeNode* right)
m_entries.insert(ix, key);
VERIFY(is_leaf() == (right == nullptr));
m_down.insert(ix + 1, DownPointer(this, right));
- if (length() > BLOCKSIZE) {
+ if (length() > Heap::BLOCK_SIZE) {
split();
} else {
dump_if(SQL_DEBUG, "To WAL");
@@ -283,7 +283,7 @@ void TreeNode::just_insert(Key const& key, TreeNode* right)
m_entries.append(key);
m_down.empend(this, right);
- if (length() > BLOCKSIZE) {
+ if (length() > Heap::BLOCK_SIZE) {
split();
} else {
dump_if(SQL_DEBUG, "To WAL");
diff --git a/Userland/Libraries/LibSQL/Type.h b/Userland/Libraries/LibSQL/Type.h
index bc44126ccd..c487c2aee8 100644
--- a/Userland/Libraries/LibSQL/Type.h
+++ b/Userland/Libraries/LibSQL/Type.h
@@ -13,7 +13,7 @@ namespace SQL {
// 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
-// to bump Heap::current_version.
+// to bump Heap::VERSION.
#define ENUMERATE_SQL_TYPES(S) \
S("null", Null) \
S("text", Text) \
diff --git a/Userland/Libraries/LibSQL/Value.cpp b/Userland/Libraries/LibSQL/Value.cpp
index 8fb9dacd9c..d4b495615b 100644
--- a/Userland/Libraries/LibSQL/Value.cpp
+++ b/Userland/Libraries/LibSQL/Value.cpp
@@ -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
// 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 {
Null = 1 << 4,
Int8 = 2 << 4,