From 39f7545cd5ef7a30bbd482f3579df7744c6b688d Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 7 Jul 2023 15:29:47 +1000 Subject: wire up the playing screen with some real data Includes implementing song duration calculation for CBR MP3 files --- src/database/database.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'src/database/database.cpp') diff --git a/src/database/database.cpp b/src/database/database.cpp index 1ac5d729..0d1c43e2 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -268,6 +268,62 @@ auto Database::GetTrackPath(TrackId id) }); } +auto Database::GetTrack(TrackId id) -> std::future> { + return worker_task_->Dispatch>( + [=, this]() -> std::optional { + std::optional data = dbGetTrackData(id); + if (!data || data->is_tombstoned()) { + return {}; + } + TrackTags tags; + if (!tag_parser_->ReadAndParseTags(data->filepath(), &tags)) { + return {}; + } + return Track(*data, tags); + }); +} + +auto Database::GetBulkTracks(std::vector ids) + -> std::future>> { + return worker_task_->Dispatch>>( + [=, this]() -> std::vector> { + std::map id_to_track{}; + + // Sort the list of ids so that we can retrieve them all in a single + // iteration through the database, without re-seeking. + std::vector sorted_ids = ids; + std::sort(sorted_ids.begin(), sorted_ids.end()); + + leveldb::Iterator* it = db_->NewIterator(leveldb::ReadOptions{}); + for (const TrackId& id : sorted_ids) { + OwningSlice key = EncodeDataKey(id); + it->Seek(key.slice); + if (!it->Valid() || it->key() != key.slice) { + // This id wasn't found at all. Skip it. + continue; + } + std::optional track = + ParseRecord(it->key(), it->value()); + if (track) { + id_to_track.insert({id, *track}); + } + } + + // We've fetched all of the ids in the request, so now just put them + // back into the order they were asked for in. + std::vector> results; + for (const TrackId& id : ids) { + if (id_to_track.contains(id)) { + results.push_back(id_to_track.at(id)); + } else { + // This lookup failed. + results.push_back({}); + } + } + return results; + }); +} + auto Database::GetIndexes() -> std::vector { // TODO(jacqueline): This probably needs to be async? When we have runtime // configurable indexes, they will need to come from somewhere. -- cgit v1.2.3