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

LibSQL: Improve error handling

The handling of filesystem level errors was basically non-existing or
consisting of `VERIFY_NOT_REACHED` assertions. Addressed this by
* Adding `open` methods to `Heap` and `Database` which return errors.
* Changing the interface of methods of these classes and clients
downstream to propagate these errors.

The constructors of `Heap` and `Database` don't open the underlying
filesystem file anymore.

The SQL statement handlers return an `SQLErrorCode::InternalError`
error code if an error comes back from the lower levels. Note that some
of these errors are things like duplicate index entry errors that should
be caught before the SQL layer attempts to actually update the database.

Added tests to catch attempts to open weird or non-existent files as
databases.

Finally, in between me writing this patch and submitting the PR the
AK::Result<Foo, Bar> template got deprecated in favour of ErrorOr<Foo>.
This resulted in more busywork.
This commit is contained in:
Jan de Visser 2021-11-05 19:05:59 -04:00 committed by Ali Mohammad Pur
parent 108de5dea0
commit 001949d77a
15 changed files with 355 additions and 140 deletions

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/Array.h>
#include <AK/Debug.h>
#include <AK/HashMap.h>
#include <AK/String.h>
@ -32,13 +33,14 @@ class Heap : public Core::Object {
C_OBJECT(Heap);
public:
virtual ~Heap() override { flush(); }
virtual ~Heap() override;
ErrorOr<void> open();
u32 size() const { return m_end_of_file; }
ErrorOr<ByteBuffer> read_block(u32);
bool write_block(u32, ByteBuffer&);
u32 new_record_pointer();
[[nodiscard]] u32 new_record_pointer();
[[nodiscard]] bool has_block(u32 block) const { return block < size(); }
[[nodiscard]] bool valid() const { return m_file != nullptr; }
u32 schemas_root() const { return m_schemas_root; }
@ -89,17 +91,18 @@ public:
m_write_ahead_log.set(block, buffer);
}
void flush();
ErrorOr<void> flush();
private:
explicit Heap(String);
bool seek_block(u32);
void read_zero_block();
ErrorOr<void> write_block(u32, ByteBuffer&);
ErrorOr<void> seek_block(u32);
ErrorOr<void> read_zero_block();
void initialize_zero_block();
void update_zero_block();
RefPtr<Core::File> m_file;
RefPtr<Core::File> m_file { nullptr };
u32 m_free_list { 0 };
u32 m_next_block { 1 };
u32 m_end_of_file { 1 };
@ -107,7 +110,7 @@ private:
u32 m_tables_root { 0 };
u32 m_table_columns_root { 0 };
u32 m_version { 0x00000001 };
Array<u32, 16> m_user_values;
Array<u32, 16> m_user_values { 0 };
HashMap<u32, ByteBuffer> m_write_ahead_log;
};