comparison include/library/library.h @ 409:8d06825d96d1

library: refresh in a separate thread this is fugly but it works
author Paper <paper@tflc.us>
date Thu, 02 Apr 2026 00:18:56 -0400
parents e561b7542b7b
children eb554255ea5f
comparison
equal deleted inserted replaced
408:9323153786dc 409:8d06825d96d1
5 #include "library/library.h" 5 #include "library/library.h"
6 6
7 #include <filesystem> 7 #include <filesystem>
8 #include <optional> 8 #include <optional>
9 #include <unordered_map> 9 #include <unordered_map>
10 #include <thread>
11 #include <mutex>
10 12
11 namespace Library { 13 namespace Library {
12 14
13 class Database final { 15 class Database final {
14 public: 16 public:
24 26
25 std::optional<std::filesystem::path> GetAnimeFolder(int id); 27 std::optional<std::filesystem::path> GetAnimeFolder(int id);
26 void Refresh(); 28 void Refresh();
27 void Refresh(int id); 29 void Refresh(int id);
28 30
29 // Anime episodes. Indexed as `folders[id][episode]' 31 std::optional<std::filesystem::path> GetAnimeEpisodePath(int anime, int episode);
30 std::unordered_map<int, std::unordered_map<int, std::filesystem::path>> items;
31 32
32 private: 33 private:
33 void Refresh(std::optional<int> find_id); 34 void Refresh(std::optional<int> find_id);
35 /* refresh_thread entry point */
36 void RefreshThread();
34 37
35 Filesystem::PathMap<std::unique_ptr<Filesystem::IWatcher>> watchers_; 38 Filesystem::PathMap<std::unique_ptr<Filesystem::IWatcher>> watchers_;
36 39
37 /* ID we're looking for */ 40 /* ID we're looking for (This is terrible, but other ways are also terrible in their own unique ways) */
38 std::optional<int> find_id_; 41 std::optional<int> find_id_;
42
43 /* Anime episodes. Indexed as `folders[id][episode]' */
44 std::unordered_map<int, std::unordered_map<int, std::filesystem::path>> items;
45 std::recursive_mutex items_mutex_;
46
47 /* Thread that runs the refreshing.
48 * When Refresh() is called, if this is a valid thread, then it will be
49 * join()'d. This stinks, and a better method would be to have the
50 * thread running in the background waiting on a queue. Oh well. */
51 std::thread refresh_thread_;
39 }; 52 };
40 53
41 extern Database db; 54 extern Database db;
42 55
43 } // namespace Library 56 } // namespace Library