From ee8e5234562c2b9ee1bb261785135abd4f718f83 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 4 Oct 2023 15:38:18 +1100 Subject: Add a basic database reindex screen --- src/database/database.cpp | 20 ++++++++++++++------ src/database/file_gatherer.cpp | 12 ++++++++++-- src/database/include/db_events.hpp | 2 ++ 3 files changed, 26 insertions(+), 8 deletions(-) (limited to 'src/database') diff --git a/src/database/database.cpp b/src/database/database.cpp index 1ecd72e0..ad81cfcf 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -136,14 +136,18 @@ auto Database::Update() -> std::future { // Stage 1: verify all existing tracks are still valid. ESP_LOGI(kTag, "verifying existing tracks"); - events::Ui().Dispatch(event::UpdateProgress{ - .stage = event::UpdateProgress::Stage::kVerifyingExistingTracks, - }); { + uint64_t num_processed = 0; std::unique_ptr it{db_->NewIterator(read_options)}; OwningSlice prefix = EncodeDataPrefix(); it->Seek(prefix.slice); while (it->Valid() && it->key().starts_with(prefix.slice)) { + num_processed++; + events::Ui().Dispatch(event::UpdateProgress{ + .stage = event::UpdateProgress::Stage::kVerifyingExistingTracks, + .val = num_processed, + }); + std::shared_ptr track = ParseDataValue(it->value()); if (!track) { // The value was malformed. Drop this record. @@ -195,10 +199,14 @@ auto Database::Update() -> std::future { // Stage 2: search for newly added files. ESP_LOGI(kTag, "scanning for new tracks"); - events::Ui().Dispatch(event::UpdateProgress{ - .stage = event::UpdateProgress::Stage::kScanningForNewTracks, - }); + uint64_t num_processed = 0; file_gatherer_.FindFiles("", [&](const std::pmr::string& path) { + num_processed++; + events::Ui().Dispatch(event::UpdateProgress{ + .stage = event::UpdateProgress::Stage::kScanningForNewTracks, + .val = num_processed, + }); + std::shared_ptr tags = tag_parser_.ReadAndParseTags(path); if (!tags || tags->encoding() == Container::kUnsupported) { // No parseable tags; skip this fiile. diff --git a/src/database/file_gatherer.cpp b/src/database/file_gatherer.cpp index d8c80b0d..b4e87acb 100644 --- a/src/database/file_gatherer.cpp +++ b/src/database/file_gatherer.cpp @@ -14,6 +14,7 @@ #include "ff.h" #include "memory_resource.hpp" +#include "spi.hpp" namespace database { @@ -30,7 +31,11 @@ auto FileGathererImpl::FindFiles( const TCHAR* next_path = static_cast(next_path_str.c_str()); FF_DIR dir; - FRESULT res = f_opendir(&dir, next_path); + FRESULT res; + { + auto lock = drivers::acquire_spi(); + res = f_opendir(&dir, next_path); + } if (res != FR_OK) { // TODO: log. continue; @@ -38,7 +43,10 @@ auto FileGathererImpl::FindFiles( for (;;) { FILINFO info; - res = f_readdir(&dir, &info); + { + auto lock = drivers::acquire_spi(); + res = f_readdir(&dir, &info); + } if (res != FR_OK || info.fname[0] == 0) { // No more files in the directory. break; diff --git a/src/database/include/db_events.hpp b/src/database/include/db_events.hpp index c071c6d4..a1aefc27 100644 --- a/src/database/include/db_events.hpp +++ b/src/database/include/db_events.hpp @@ -6,6 +6,7 @@ #pragma once +#include #include "tinyfsm.hpp" namespace database { @@ -21,6 +22,7 @@ struct UpdateProgress : tinyfsm::Event { kScanningForNewTracks, }; Stage stage; + uint64_t val; }; } // namespace event -- cgit v1.2.3