diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-04-27 12:55:30 +1000 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-04-27 12:55:30 +1000 |
| commit | 5d7cbec34cd5e473d5768b39054d99bc72ddad62 (patch) | |
| tree | f83040e527dc025345b3786a298f11e914b5eb37 /src/database/include | |
| parent | fbe047a35fff100cb5f42d10984bccde137f586e (diff) | |
| download | tangara-fw-5d7cbec34cd5e473d5768b39054d99bc72ddad62.tar.gz | |
Move DB interactions to a background thread
Diffstat (limited to 'src/database/include')
| -rw-r--r-- | src/database/include/database.hpp | 77 | ||||
| -rw-r--r-- | src/database/include/db_task.hpp | 25 | ||||
| -rw-r--r-- | src/database/include/env_esp.hpp | 22 | ||||
| -rw-r--r-- | src/database/include/file_gatherer.hpp | 2 | ||||
| -rw-r--r-- | src/database/include/tag_processor.hpp | 2 |
5 files changed, 88 insertions, 40 deletions
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 <string> +#include <cstdint> +#include <future> #include <memory> #include <optional> +#include <string> +#include <utility> +#include <vector> #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 <typename T> +class DbResult { + public: + DbResult(const std::vector<T>& values, std::unique_ptr<leveldb::Iterator> it) + : values_(values), it_(std::move(it)) {} + auto values() -> std::vector<T> { return values_; } + auto it() -> leveldb::Iterator* { return it_.release(); }; + + private: + std::vector<T> values_; + std::unique_ptr<leveldb::Iterator> it_; +}; class Database { public: enum DatabaseError { + ALREADY_OPEN, FAILED_TO_OPEN, }; static auto Open() -> cpp::result<Database*, DatabaseError>; ~Database(); - auto Initialise() -> void; - auto ByTitle() -> Iterator; + auto Populate() -> std::future<void>; + + auto GetArtists(std::size_t page_size) -> std::future<DbResult<Artist>>; + auto GetMoreArtists(std::size_t page_size, DbResult<Artist> continuation) + -> std::future<DbResult<Artist>>; + + auto GetAlbums(std::size_t page_size, std::optional<Artist> artist) + -> std::future<DbResult<Album>>; + auto GetMoreAlbums(std::size_t page_size, DbResult<Album> continuation) + -> std::future<DbResult<Album>>; + + auto GetSongs(std::size_t page_size) -> std::future<DbResult<Song>>; + auto GetSongs(std::size_t page_size, std::optional<Artist> artist) + -> std::future<DbResult<Song>>; + auto GetSongs(std::size_t page_size, + std::optional<Artist> artist, + std::optional<Album> album) -> std::future<DbResult<Song>>; + auto GetMoreSongs(std::size_t page_size, DbResult<Song> continuation) + -> std::future<DbResult<Song>>; + + auto GetSongIds(std::optional<Artist> artist, std::optional<Album> album) + -> std::future<std::vector<SongId_t>>; + auto GetSongFilePath(SongId_t id) -> std::future<std::optional<std::string>>; + auto GetSongMetadata(SongId_t id) -> std::future<std::optional<SongMetadata>>; 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<std::string>; - - Iterator(const Iterator&) = delete; - Iterator& operator=(const Iterator&) = delete; - - private: - std::unique_ptr<leveldb::Iterator> it_; -}; - } // namespace database diff --git a/src/database/include/db_task.hpp b/src/database/include/db_task.hpp new file mode 100644 index 00000000..39f932b0 --- /dev/null +++ b/src/database/include/db_task.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include <functional> +#include <future> +#include <memory> + +namespace database { + +auto StartDbTask() -> bool; +auto QuitDbTask() -> void; + +auto SendToDbTask(std::function<void(void)> fn) -> void; + +template <typename T> +auto RunOnDbTask(std::function<T(void)> fn) -> std::future<T> { + std::shared_ptr<std::promise<T>> promise = + std::make_shared<std::promise<T>>(); + SendToDbTask([=]() { promise->set_value(std::invoke(fn)); }); + return promise->get_future(); +} + +template <> +auto RunOnDbTask(std::function<void(void)> fn) -> std::future<void>; + +} // namespace database diff --git a/src/database/include/env_esp.hpp b/src/database/include/env_esp.hpp index b8819b05..cf5a20e1 100644 --- a/src/database/include/env_esp.hpp +++ b/src/database/include/env_esp.hpp @@ -2,9 +2,8 @@ #include <mutex> #include <set> +#include <string> -#include "freertos/FreeRTOS.h" -#include "freertos/queue.h" #include "leveldb/env.h" #include "leveldb/status.h" @@ -89,25 +88,6 @@ class EspEnv : public leveldb::Env { void BackgroundThreadMain(); private: - // Stores the work item data in a Schedule() call. - // - // Instances are constructed on the thread calling Schedule() and used on the - // background thread. - // - // This structure is thread-safe beacuse it is immutable. - struct BackgroundWorkItem { - explicit BackgroundWorkItem(void (*f)(void*), void* a) - : function(f), arg(a) {} - - void (*const function)(void*); - void* const arg; - }; - - std::mutex background_work_mutex_; - bool started_background_thread_; - - QueueHandle_t background_work_queue_; - InMemoryLockTable locks_; // Thread-safe. }; diff --git a/src/database/include/file_gatherer.hpp b/src/database/include/file_gatherer.hpp index 7cf00b41..5df5a61b 100644 --- a/src/database/include/file_gatherer.hpp +++ b/src/database/include/file_gatherer.hpp @@ -45,7 +45,7 @@ auto FindFiles(const std::string& root, Callback cb) -> void { to_explore.push_back(full_path.str()); } else { // This is a file! Let the callback know about it. - //std::invoke(cb, full_path.str(), info); + // std::invoke(cb, full_path.str(), info); std::invoke(cb, full_path.str()); } } diff --git a/src/database/include/tag_processor.hpp b/src/database/include/tag_processor.hpp index 88c95b61..fb2201db 100644 --- a/src/database/include/tag_processor.hpp +++ b/src/database/include/tag_processor.hpp @@ -9,6 +9,6 @@ struct FileInfo { std::string title; }; -auto GetInfo(const std::string &path, FileInfo *out) -> bool; +auto GetInfo(const std::string& path, FileInfo* out) -> bool; } // namespace database |
