From c6bb42cdd21b63accd20012373a8a0e41d8566f5 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 15 Jun 2023 10:42:28 +1000 Subject: song -> track --- src/database/database.cpp | 130 +++++++++++++++++++++++----------------------- 1 file changed, 65 insertions(+), 65 deletions(-) (limited to 'src/database/database.cpp') diff --git a/src/database/database.cpp b/src/database/database.cpp index 71954bbb..9206256f 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -28,16 +28,16 @@ #include "file_gatherer.hpp" #include "records.hpp" #include "result.hpp" -#include "song.hpp" #include "tag_parser.hpp" #include "tasks.hpp" +#include "track.hpp" namespace database { static SingletonEnv sEnv; static const char* kTag = "DB"; -static const char kSongIdKey[] = "next_song_id"; +static const char kTrackIdKey[] = "next_track_id"; static std::atomic sIsDbOpen(false); @@ -128,8 +128,8 @@ Database::~Database() { auto Database::Update() -> std::future { return worker_task_->Dispatch([&]() -> void { - // Stage 1: verify all existing songs are still valid. - ESP_LOGI(kTag, "verifying existing songs"); + // Stage 1: verify all existing tracks are still valid. + ESP_LOGI(kTag, "verifying existing tracks"); const leveldb::Snapshot* snapshot = db_->GetSnapshot(); leveldb::ReadOptions read_options; read_options.fill_cache = false; @@ -138,8 +138,8 @@ auto Database::Update() -> std::future { OwningSlice prefix = CreateDataPrefix(); it->Seek(prefix.slice); while (it->Valid() && it->key().starts_with(prefix.slice)) { - std::optional song = ParseDataValue(it->value()); - if (!song) { + std::optional track = ParseDataValue(it->value()); + if (!track) { // The value was malformed. Drop this record. ESP_LOGW(kTag, "dropping malformed metadata"); db_->Delete(leveldb::WriteOptions(), it->key()); @@ -147,33 +147,33 @@ auto Database::Update() -> std::future { continue; } - if (song->is_tombstoned()) { - ESP_LOGW(kTag, "skipping tombstoned %lx", song->id()); + if (track->is_tombstoned()) { + ESP_LOGW(kTag, "skipping tombstoned %lx", track->id()); it->Next(); continue; } - SongTags tags; - if (!tag_parser_->ReadAndParseTags(song->filepath(), &tags) || + TrackTags tags; + if (!tag_parser_->ReadAndParseTags(track->filepath(), &tags) || tags.encoding == Encoding::kUnsupported) { - // We couldn't read the tags for this song. Either they were + // We couldn't read the tags for this track. Either they were // malformed, or perhaps the file is missing. Either way, tombstone // this record. - ESP_LOGW(kTag, "entombing missing #%lx", song->id()); - dbPutSongData(song->Entomb()); + ESP_LOGW(kTag, "entombing missing #%lx", track->id()); + dbPutTrackData(track->Entomb()); it->Next(); continue; } uint64_t new_hash = tags.Hash(); - if (new_hash != song->tags_hash()) { - // This song's tags have changed. Since the filepath is exactly the + if (new_hash != track->tags_hash()) { + // This track's tags have changed. Since the filepath is exactly the // same, we assume this is a legitimate correction. Update the // database. - ESP_LOGI(kTag, "updating hash (%llx -> %llx)", song->tags_hash(), + ESP_LOGI(kTag, "updating hash (%llx -> %llx)", track->tags_hash(), new_hash); - dbPutSongData(song->UpdateHash(new_hash)); - dbPutHash(new_hash, song->id()); + dbPutTrackData(track->UpdateHash(new_hash)); + dbPutHash(new_hash, track->id()); } it->Next(); @@ -182,9 +182,9 @@ auto Database::Update() -> std::future { db_->ReleaseSnapshot(snapshot); // Stage 2: search for newly added files. - ESP_LOGI(kTag, "scanning for new songs"); + ESP_LOGI(kTag, "scanning for new tracks"); file_gatherer_->FindFiles("", [&](const std::string& path) { - SongTags tags; + TrackTags tags; if (!tag_parser_->ReadAndParseTags(path, &tags) || tags.encoding == Encoding::kUnsupported) { // No parseable tags; skip this fiile. @@ -194,32 +194,32 @@ auto Database::Update() -> std::future { // Check for any existing record with the same hash. uint64_t hash = tags.Hash(); OwningSlice key = CreateHashKey(hash); - std::optional existing_hash; + std::optional existing_hash; std::string raw_entry; if (db_->Get(leveldb::ReadOptions(), key.slice, &raw_entry).ok()) { existing_hash = ParseHashValue(raw_entry); } if (!existing_hash) { - // We've never met this song before! Or we have, but the entry is - // malformed. Either way, record this as a new song. - SongId id = dbMintNewSongId(); + // We've never met this track before! Or we have, but the entry is + // malformed. Either way, record this as a new track. + TrackId id = dbMintNewTrackId(); ESP_LOGI(kTag, "recording new 0x%lx", id); - dbPutSong(id, path, hash); + dbPutTrack(id, path, hash); return; } - std::optional existing_data = dbGetSongData(*existing_hash); + std::optional existing_data = dbGetTrackData(*existing_hash); if (!existing_data) { // We found a hash that matches, but there's no data record? Weird. - SongData new_data(*existing_hash, path, hash); - dbPutSongData(new_data); + TrackData new_data(*existing_hash, path, hash); + dbPutTrackData(new_data); return; } if (existing_data->is_tombstoned()) { - ESP_LOGI(kTag, "exhuming song %lu", existing_data->id()); - dbPutSongData(existing_data->Exhume(path)); + ESP_LOGI(kTag, "exhuming track %lu", existing_data->id()); + dbPutTrackData(existing_data->Exhume(path)); } else if (existing_data->filepath() != path) { ESP_LOGW(kTag, "tag hash collision"); } @@ -227,14 +227,14 @@ auto Database::Update() -> std::future { }); } -auto Database::GetSongs(std::size_t page_size) -> std::future*> { - return worker_task_->Dispatch*>([=, this]() -> Result* { - Continuation c{.iterator = nullptr, - .prefix = CreateDataPrefix().data, - .start_key = CreateDataPrefix().data, - .forward = true, - .was_prev_forward = true, - .page_size = page_size}; +auto Database::GetTracks(std::size_t page_size) -> std::future*> { + return worker_task_->Dispatch*>([=, this]() -> Result* { + Continuation c{.iterator = nullptr, + .prefix = CreateDataPrefix().data, + .start_key = CreateDataPrefix().data, + .forward = true, + .was_prev_forward = true, + .page_size = page_size}; return dbGetPage(c); }); } @@ -260,32 +260,32 @@ auto Database::GetPage(Continuation* c) -> std::future*> { [=, this]() -> Result* { return dbGetPage(copy); }); } -template auto Database::GetPage(Continuation* c) - -> std::future*>; +template auto Database::GetPage(Continuation* c) + -> std::future*>; template auto Database::GetPage(Continuation* c) -> std::future*>; -auto Database::dbMintNewSongId() -> SongId { - SongId next_id = 1; +auto Database::dbMintNewTrackId() -> TrackId { + TrackId next_id = 1; std::string val; - auto status = db_->Get(leveldb::ReadOptions(), kSongIdKey, &val); + auto status = db_->Get(leveldb::ReadOptions(), kTrackIdKey, &val); if (status.ok()) { - next_id = BytesToSongId(val).value_or(next_id); + next_id = BytesToTrackId(val).value_or(next_id); } else if (!status.IsNotFound()) { // TODO(jacqueline): Handle this more. - ESP_LOGE(kTag, "failed to get next song id"); + ESP_LOGE(kTag, "failed to get next track id"); } - if (!db_->Put(leveldb::WriteOptions(), kSongIdKey, - SongIdToBytes(next_id + 1).slice) + if (!db_->Put(leveldb::WriteOptions(), kTrackIdKey, + TrackIdToBytes(next_id + 1).slice) .ok()) { - ESP_LOGE(kTag, "failed to write next song id"); + ESP_LOGE(kTag, "failed to write next track id"); } return next_id; } -auto Database::dbEntomb(SongId id, uint64_t hash) -> void { +auto Database::dbEntomb(TrackId id, uint64_t hash) -> void { OwningSlice key = CreateHashKey(hash); OwningSlice val = CreateHashValue(id); if (!db_->Put(leveldb::WriteOptions(), key.slice, val.slice).ok()) { @@ -293,7 +293,7 @@ auto Database::dbEntomb(SongId id, uint64_t hash) -> void { } } -auto Database::dbPutSongData(const SongData& s) -> void { +auto Database::dbPutTrackData(const TrackData& s) -> void { OwningSlice key = CreateDataKey(s.id()); OwningSlice val = CreateDataValue(s); if (!db_->Put(leveldb::WriteOptions(), key.slice, val.slice).ok()) { @@ -301,7 +301,7 @@ auto Database::dbPutSongData(const SongData& s) -> void { } } -auto Database::dbGetSongData(SongId id) -> std::optional { +auto Database::dbGetTrackData(TrackId id) -> std::optional { OwningSlice key = CreateDataKey(id); std::string raw_val; if (!db_->Get(leveldb::ReadOptions(), key.slice, &raw_val).ok()) { @@ -311,7 +311,7 @@ auto Database::dbGetSongData(SongId id) -> std::optional { return ParseDataValue(raw_val); } -auto Database::dbPutHash(const uint64_t& hash, SongId i) -> void { +auto Database::dbPutHash(const uint64_t& hash, TrackId i) -> void { OwningSlice key = CreateHashKey(hash); OwningSlice val = CreateHashValue(i); if (!db_->Put(leveldb::WriteOptions(), key.slice, val.slice).ok()) { @@ -319,7 +319,7 @@ auto Database::dbPutHash(const uint64_t& hash, SongId i) -> void { } } -auto Database::dbGetHash(const uint64_t& hash) -> std::optional { +auto Database::dbGetHash(const uint64_t& hash) -> std::optional { OwningSlice key = CreateHashKey(hash); std::string raw_val; if (!db_->Get(leveldb::ReadOptions(), key.slice, &raw_val).ok()) { @@ -329,10 +329,10 @@ auto Database::dbGetHash(const uint64_t& hash) -> std::optional { return ParseHashValue(raw_val); } -auto Database::dbPutSong(SongId id, - const std::string& path, - const uint64_t& hash) -> void { - dbPutSongData(SongData(id, path, hash)); +auto Database::dbPutTrack(TrackId id, + const std::string& path, + const uint64_t& hash) -> void { + dbPutTrackData(TrackData(id, path, hash)); dbPutHash(hash, id); } @@ -455,24 +455,24 @@ auto Database::dbGetPage(const Continuation& c) -> Result* { return new Result(std::move(records), next_page, prev_page); } -template auto Database::dbGetPage(const Continuation& c) - -> Result*; +template auto Database::dbGetPage(const Continuation& c) + -> Result*; template auto Database::dbGetPage( const Continuation& c) -> Result*; template <> -auto Database::ParseRecord(const leveldb::Slice& key, - const leveldb::Slice& val) - -> std::optional { - std::optional data = ParseDataValue(val); +auto Database::ParseRecord(const leveldb::Slice& key, + const leveldb::Slice& val) + -> std::optional { + std::optional data = ParseDataValue(val); if (!data || data->is_tombstoned()) { return {}; } - SongTags tags; + TrackTags tags; if (!tag_parser_->ReadAndParseTags(data->filepath(), &tags)) { return {}; } - return Song(*data, tags); + return Track(*data, tags); } template <> -- cgit v1.2.3 From 5b7b88420b169d1dfdeea6d082762fccefbc7d49 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 16 Jun 2023 13:14:45 +1000 Subject: Fix issues with importing my entire library --- src/database/database.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src/database/database.cpp') diff --git a/src/database/database.cpp b/src/database/database.cpp index 9206256f..a96b3eab 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -17,6 +17,7 @@ #include "esp_log.h" #include "ff.h" +#include "freertos/projdefs.h" #include "leveldb/cache.h" #include "leveldb/db.h" #include "leveldb/iterator.h" @@ -68,12 +69,13 @@ auto Database::Open(IFileGatherer* gatherer, ITagParser* parser) return cpp::fail(DatabaseError::ALREADY_OPEN); } + leveldb::sBackgroundThread.reset( + tasks::Worker::Start()); std::shared_ptr worker( tasks::Worker::Start()); - leveldb::sBackgroundThread = std::weak_ptr(worker); return worker ->Dispatch>( - [&]() -> cpp::result { + [=]() -> cpp::result { leveldb::DB* db; leveldb::Cache* cache = leveldb::NewLRUCache(24 * 1024); leveldb::Options options; @@ -121,7 +123,7 @@ Database::~Database() { delete db_; delete cache_; - leveldb::sBackgroundThread = std::weak_ptr(); + leveldb::sBackgroundThread.reset(); sIsDbOpen.store(false); } -- cgit v1.2.3 From 6ff8b5886ef91ed46dba08686900d519f6c9c62d Mon Sep 17 00:00:00 2001 From: jacqueline Date: Mon, 19 Jun 2023 08:51:34 +1000 Subject: Support playing tracks by track id --- src/database/database.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/database/database.cpp') diff --git a/src/database/database.cpp b/src/database/database.cpp index a96b3eab..ac5e4873 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -229,6 +229,18 @@ auto Database::Update() -> std::future { }); } +auto Database::GetTrackPath(TrackId id) + -> std::future> { + return worker_task_->Dispatch>( + [=, this]() -> std::optional { + auto track_data = dbGetTrackData(id); + if (track_data) { + return track_data->filepath(); + } + return {}; + }); +} + auto Database::GetTracks(std::size_t page_size) -> std::future*> { return worker_task_->Dispatch*>([=, this]() -> Result* { Continuation c{.iterator = nullptr, -- cgit v1.2.3