From 5d7cbec34cd5e473d5768b39054d99bc72ddad62 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 27 Apr 2023 12:55:30 +1000 Subject: Move DB interactions to a background thread --- src/database/include/database.hpp | 77 ++++++++++++++++++++++++++++++--------- 1 file changed, 60 insertions(+), 17 deletions(-) (limited to 'src/database/include/database.hpp') diff --git a/src/database/include/database.hpp b/src/database/include/database.hpp index cb1437df..bc102ca8 100644 --- a/src/database/include/database.hpp +++ b/src/database/include/database.hpp @@ -1,29 +1,85 @@ #pragma once -#include +#include +#include #include #include +#include +#include +#include #include "leveldb/cache.h" #include "leveldb/db.h" #include "leveldb/iterator.h" +#include "leveldb/slice.h" #include "result.hpp" namespace database { -class Iterator; +struct Artist { + std::string name; +}; + +struct Album { + std::string name; +}; + +typedef uint64_t SongId_t; + +struct Song { + std::string title; + uint64_t id; +}; + +struct SongMetadata {}; + +template +class DbResult { + public: + DbResult(const std::vector& values, std::unique_ptr it) + : values_(values), it_(std::move(it)) {} + auto values() -> std::vector { return values_; } + auto it() -> leveldb::Iterator* { return it_.release(); }; + + private: + std::vector values_; + std::unique_ptr it_; +}; class Database { public: enum DatabaseError { + ALREADY_OPEN, FAILED_TO_OPEN, }; static auto Open() -> cpp::result; ~Database(); - auto Initialise() -> void; - auto ByTitle() -> Iterator; + auto Populate() -> std::future; + + auto GetArtists(std::size_t page_size) -> std::future>; + auto GetMoreArtists(std::size_t page_size, DbResult continuation) + -> std::future>; + + auto GetAlbums(std::size_t page_size, std::optional artist) + -> std::future>; + auto GetMoreAlbums(std::size_t page_size, DbResult continuation) + -> std::future>; + + auto GetSongs(std::size_t page_size) -> std::future>; + auto GetSongs(std::size_t page_size, std::optional artist) + -> std::future>; + auto GetSongs(std::size_t page_size, + std::optional artist, + std::optional album) -> std::future>; + auto GetMoreSongs(std::size_t page_size, DbResult continuation) + -> std::future>; + + auto GetSongIds(std::optional artist, std::optional album) + -> std::future>; + auto GetSongFilePath(SongId_t id) -> std::future>; + auto GetSongMetadata(SongId_t id) -> std::future>; Database(const Database&) = delete; Database& operator=(const Database&) = delete; @@ -35,17 +91,4 @@ class Database { Database(leveldb::DB* db, leveldb::Cache* cache); }; -class Iterator { - public: - explicit Iterator(leveldb::Iterator *it) : it_(it) {} - - auto Next() -> std::optional; - - Iterator(const Iterator&) = delete; - Iterator& operator=(const Iterator&) = delete; - - private: - std::unique_ptr it_; -}; - } // namespace database -- cgit v1.2.3