From 2086ab09b8d89c27f524d82a68b9a2035ea02436 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 24 Oct 2023 16:29:46 +1100 Subject: Implement incremental updates of database indexes This makes rescanning the library *so* much faster. Yay! --- src/database/include/database.hpp | 6 ++++++ src/database/include/index.hpp | 6 +++++- src/database/include/records.hpp | 3 +++ src/database/include/track.hpp | 6 ++++++ 4 files changed, 20 insertions(+), 1 deletion(-) (limited to 'src/database/include') diff --git a/src/database/include/database.hpp b/src/database/include/database.hpp index 7cb1d09c..cdf69db0 100644 --- a/src/database/include/database.hpp +++ b/src/database/include/database.hpp @@ -7,6 +7,7 @@ #pragma once #include +#include #include #include #include @@ -150,6 +151,11 @@ class Database { auto dbPutHash(const uint64_t& hash, TrackId i) -> void; auto dbGetHash(const uint64_t& hash) -> std::optional; auto dbCreateIndexesForTrack(const Track& track) -> void; + auto dbRemoveIndexes(std::shared_ptr) -> void; + auto dbIngestTagHashes(const TrackTags&, + std::pmr::unordered_map&) -> void; + auto dbRecoverTagsFromHashes(const std::pmr::unordered_map&) + -> std::shared_ptr; template auto dbGetPage(const Continuation& c) -> Result*; diff --git a/src/database/include/index.hpp b/src/database/include/index.hpp index 838eff31..13de952d 100644 --- a/src/database/include/index.hpp +++ b/src/database/include/index.hpp @@ -46,6 +46,8 @@ struct IndexKey { // an index consists of { kArtist, kAlbum, kTitle }, and we are at depth = 2 // then this may contain hash(hash("Jacqueline"), "My Cool Album"). std::uint64_t components_hash; + + bool operator==(const Header&) const = default; }; Header header; @@ -58,7 +60,9 @@ struct IndexKey { std::optional track; }; -auto Index(const IndexInfo&, const Track&, leveldb::WriteBatch*) -> bool; +auto Index(const IndexInfo&, const Track&) + -> std::vector>; + auto ExpandHeader(const IndexKey::Header&, const std::optional&) -> IndexKey::Header; diff --git a/src/database/include/records.hpp b/src/database/include/records.hpp index e13c6568..09764ed0 100644 --- a/src/database/include/records.hpp +++ b/src/database/include/records.hpp @@ -52,6 +52,9 @@ auto EncodeHashKey(const uint64_t& hash) -> std::string; */ auto EncodeHashValue(TrackId id) -> std::string; +/* Encodes a hash key for the specified hash. */ +auto EncodeTagHashKey(const uint64_t& hash) -> std::string; + /* * Parses bytes previously encoded via EncodeHashValue back into a track id. May * return nullopt if parsing fails. diff --git a/src/database/include/track.hpp b/src/database/include/track.hpp index b07da9ba..72296e8d 100644 --- a/src/database/include/track.hpp +++ b/src/database/include/track.hpp @@ -83,6 +83,10 @@ class TrackTags { auto at(const Tag& key) const -> std::optional; auto operator[](const Tag& key) const -> std::optional; + auto tags() const -> const std::pmr::unordered_map& { + return tags_; + } + /* * Returns a hash of the 'identifying' tags of this track. That is, a hash * that can be used to determine if one track is likely the same as another, @@ -119,12 +123,14 @@ struct TrackData { : id(0), filepath(&memory::kSpiRamResource), tags_hash(0), + individual_tag_hashes(&memory::kSpiRamResource), is_tombstoned(false), modified_at() {} TrackId id; std::pmr::string filepath; uint64_t tags_hash; + std::pmr::unordered_map individual_tag_hashes; bool is_tombstoned; std::pair modified_at; -- cgit v1.2.3