LibDatabase: Allow creating a memory backed database
This commit is contained in:
parent
32c51774ee
commit
d3624c328f
3 changed files with 23 additions and 9 deletions
|
|
@ -54,15 +54,25 @@ static constexpr StringView sql_error(int error_code)
|
|||
__ENUMERATE_TYPE(unsigned long long) \
|
||||
__ENUMERATE_TYPE(bool)
|
||||
|
||||
ErrorOr<NonnullRefPtr<Database>> Database::create_memory_backed()
|
||||
{
|
||||
sqlite3* sql_database { nullptr };
|
||||
SQL_TRY(sqlite3_open(":memory:", &sql_database));
|
||||
return create(sql_database);
|
||||
}
|
||||
|
||||
ErrorOr<NonnullRefPtr<Database>> Database::create(ByteString const& directory, StringView name)
|
||||
{
|
||||
TRY(Core::Directory::create(directory, Core::Directory::CreateDirectories::Yes));
|
||||
LexicalPath database_path { ByteString::formatted("{}/{}.db", directory, name) };
|
||||
sqlite3* sql_database { nullptr };
|
||||
SQL_TRY(sqlite3_open(database_path.string().characters(), &sql_database));
|
||||
return create(sql_database, database_path);
|
||||
}
|
||||
|
||||
sqlite3* m_database { nullptr };
|
||||
SQL_TRY(sqlite3_open(database_path.string().characters(), &m_database));
|
||||
|
||||
auto database = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Database(move(database_path), m_database)));
|
||||
ErrorOr<NonnullRefPtr<Database>> Database::create(sqlite3* sql_database, Optional<LexicalPath> database_path)
|
||||
{
|
||||
auto database = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Database(sql_database, move(database_path))));
|
||||
|
||||
// Enable the WAL and set the synchronous pragma to normal by default for performance.
|
||||
TRY(database->set_journal_mode_pragma(JournalMode::WriteAheadLog));
|
||||
|
|
@ -71,7 +81,7 @@ ErrorOr<NonnullRefPtr<Database>> Database::create(ByteString const& directory, S
|
|||
return database;
|
||||
}
|
||||
|
||||
Database::Database(LexicalPath database_path, sqlite3* database)
|
||||
Database::Database(sqlite3* database, Optional<LexicalPath> database_path)
|
||||
: m_database_path(move(database_path))
|
||||
, m_database(database)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -23,12 +23,13 @@ namespace Database {
|
|||
|
||||
class DATABASE_API Database : public RefCounted<Database> {
|
||||
public:
|
||||
static ErrorOr<NonnullRefPtr<Database>> create_memory_backed();
|
||||
static ErrorOr<NonnullRefPtr<Database>> create(ByteString const& directory, StringView name);
|
||||
~Database();
|
||||
|
||||
using OnResult = Function<void(StatementID)>;
|
||||
|
||||
LexicalPath const& database_path() const { return m_database_path; }
|
||||
Optional<LexicalPath> const& database_path() const { return m_database_path; }
|
||||
|
||||
ErrorOr<StatementID> prepare_statement(StringView statement);
|
||||
|
||||
|
|
@ -72,7 +73,8 @@ public:
|
|||
ErrorOr<void> set_synchronous_pragma(Synchronous);
|
||||
|
||||
private:
|
||||
Database(LexicalPath, sqlite3*);
|
||||
static ErrorOr<NonnullRefPtr<Database>> create(sqlite3*, Optional<LexicalPath> database_path = {});
|
||||
Database(sqlite3*, Optional<LexicalPath> database_path);
|
||||
|
||||
void execute_statement_internal(StatementID, OnResult);
|
||||
|
||||
|
|
@ -87,7 +89,7 @@ private:
|
|||
return m_prepared_statements[statement_id];
|
||||
}
|
||||
|
||||
LexicalPath m_database_path;
|
||||
Optional<LexicalPath> m_database_path;
|
||||
sqlite3* m_database { nullptr };
|
||||
Vector<sqlite3_stmt*> m_prepared_statements;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -129,7 +129,9 @@ ErrorOr<CacheIndex> CacheIndex::create(Database::Database& database)
|
|||
WHERE last_access_time >= ?;
|
||||
)#"sv));
|
||||
|
||||
auto disk_space = TRY(FileSystem::compute_disk_space(database.database_path().parent()));
|
||||
auto database_path = database.database_path();
|
||||
VERIFY(database_path.has_value()); // We assume a disk backed database for CacheIndex.
|
||||
auto disk_space = TRY(FileSystem::compute_disk_space(database.database_path().value().parent()));
|
||||
auto maximum_disk_cache_size = compute_maximum_disk_cache_size(disk_space.free_bytes);
|
||||
|
||||
Limits limits {
|
||||
|
|
|
|||
Loading…
Reference in a new issue