From 245d9ff4b9cde1f487beed76085a52f3f2d6d26c Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 23 Jun 2023 15:32:11 +1000 Subject: add indexing to the database idk man i wrote most of this in a fugue state whilst high on the couch with my cat --- src/database/track.cpp | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) (limited to 'src/database/track.cpp') diff --git a/src/database/track.cpp b/src/database/track.cpp index 00acc1f6..dc33701d 100644 --- a/src/database/track.cpp +++ b/src/database/track.cpp @@ -7,11 +7,28 @@ #include "track.hpp" #include +#include "shared_string.h" namespace database { +auto TrackTags::set(const Tag& key, const std::string& val) -> void { + tags_[key] = val; +} + +auto TrackTags::at(const Tag& key) const -> std::optional { + if (tags_.contains(key)) { + return tags_.at(key); + } + return {}; +} + +auto TrackTags::operator[](const Tag& key) const + -> std::optional { + return at(key); +} + /* Helper function to update a komihash stream with a std::string. */ -auto HashString(komihash_stream_t* stream, std::string str) -> void { +auto HashString(komihash_stream_t* stream, const std::string& str) -> void { komihash_stream_update(stream, str.c_str(), str.length()); } @@ -24,9 +41,11 @@ auto TrackTags::Hash() const -> uint64_t { // tags at all. komihash_stream_t stream; komihash_stream_init(&stream, 0); - HashString(&stream, title.value_or("")); - HashString(&stream, artist.value_or("")); - HashString(&stream, album.value_or("")); + + HashString(&stream, at(Tag::kTitle).value_or("")); + HashString(&stream, at(Tag::kArtist).value_or("")); + HashString(&stream, at(Tag::kAlbum).value_or("")); + return komihash_stream_final(&stream); } @@ -48,4 +67,16 @@ void swap(Track& first, Track& second) { second = temp; } +auto Track::TitleOrFilename() const -> shared_string { + auto title = tags().at(Tag::kTitle); + if (title) { + return *title; + } + auto start = data().filepath().find_last_of('/'); + if (start == std::string::npos) { + return data().filepath(); + } + return data().filepath().substr(start); +} + } // namespace database -- cgit v1.2.3