From 4d99d22e10a3cb2a421da1618c127128816613c9 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 26 Sep 2023 13:36:07 +1000 Subject: std::string -> std::pmr::string in psram --- src/database/database.cpp | 74 +++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 35 deletions(-) (limited to 'src/database/database.cpp') diff --git a/src/database/database.cpp b/src/database/database.cpp index d73ce771..fd0e50c1 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -31,6 +31,7 @@ #include "env_esp.hpp" #include "event_queue.hpp" #include "file_gatherer.hpp" +#include "memory_resource.hpp" #include "records.hpp" #include "result.hpp" #include "tag_parser.hpp" @@ -195,7 +196,7 @@ auto Database::Update() -> std::future { events::Ui().Dispatch(event::UpdateProgress{ .stage = event::UpdateProgress::Stage::kScanningForNewTracks, }); - file_gatherer_.FindFiles("", [&](const std::string& path) { + file_gatherer_.FindFiles("", [&](const std::pmr::string& path) { TrackTags tags; if (!tag_parser_.ReadAndParseTags(path, &tags) || tags.encoding() == Container::kUnsupported) { @@ -252,9 +253,9 @@ auto Database::Update() -> std::future { } auto Database::GetTrackPath(TrackId id) - -> std::future> { - return worker_task_->Dispatch>( - [=, this]() -> std::optional { + -> std::future> { + return worker_task_->Dispatch>( + [=, this]() -> std::optional { auto track_data = dbGetTrackData(id); if (track_data) { return track_data->filepath(); @@ -362,14 +363,14 @@ auto Database::GetTracks(std::size_t page_size) -> std::future*> { } auto Database::GetDump(std::size_t page_size) - -> std::future*> { - return worker_task_->Dispatch*>( - [=, this]() -> Result* { - Continuation c{.prefix = "", - .start_key = "", - .forward = true, - .was_prev_forward = true, - .page_size = page_size}; + -> std::future*> { + return worker_task_->Dispatch*>( + [=, this]() -> Result* { + Continuation c{.prefix = "", + .start_key = "", + .forward = true, + .was_prev_forward = true, + .page_size = page_size}; return dbGetPage(c); }); } @@ -385,8 +386,9 @@ template auto Database::GetPage(Continuation* c) -> std::future*>; 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::dbMintNewTrackId() -> TrackId { TrackId next_id = 1; @@ -466,7 +468,7 @@ auto Database::dbGetPage(const Continuation& c) -> Result* { // Work out our starting point. Sometimes this will already done. std::unique_ptr it{ db_->NewIterator(leveldb::ReadOptions{})}; - it->Seek(c.start_key); + it->Seek({c.start_key.data(), c.start_key.size()}); // Fix off-by-one if we just changed direction. if (c.forward != c.was_prev_forward) { @@ -478,10 +480,10 @@ auto Database::dbGetPage(const Continuation& c) -> Result* { } // Grab results. - std::optional first_key; + std::optional first_key; std::vector records; while (records.size() < c.page_size && it->Valid()) { - if (!it->key().starts_with(c.prefix)) { + if (!it->key().starts_with({c.prefix.data(), c.prefix.size()})) { break; } if (!first_key) { @@ -498,7 +500,8 @@ auto Database::dbGetPage(const Continuation& c) -> Result* { } } - if (!it->Valid() || !it->key().starts_with(c.prefix)) { + if (!it->Valid() || + !it->key().starts_with({c.prefix.data(), c.prefix.size()})) { it.reset(); } @@ -512,7 +515,8 @@ auto Database::dbGetPage(const Continuation& c) -> Result* { if (c.forward) { if (it != nullptr) { // We were going forward, and now we want the next page. - std::string key = it->key().ToString(); + std::pmr::string key{it->key().data(), it->key().size(), + &memory::kSpiRamResource}; next_page = Continuation{ .prefix = c.prefix, .start_key = key, @@ -549,7 +553,8 @@ auto Database::dbGetPage(const Continuation& c) -> Result* { } else { if (it != nullptr) { // We were going backwards, and we still want to go backwards. - std::string key = it->key().ToString(); + std::pmr::string key{it->key().data(), it->key().size(), + &memory::kSpiRamResource}; prev_page = Continuation{ .prefix = c.prefix, .start_key = key, @@ -566,8 +571,8 @@ 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::dbGetPage( + const Continuation& c) -> Result*; template <> auto Database::ParseRecord(const leveldb::Slice& key, @@ -578,7 +583,7 @@ auto Database::ParseRecord(const leveldb::Slice& key, return {}; } - std::optional title; + std::optional title; if (!val.empty()) { title = val.ToString(); } @@ -602,43 +607,42 @@ auto Database::ParseRecord(const leveldb::Slice& key, } template <> -auto Database::ParseRecord(const leveldb::Slice& key, - const leveldb::Slice& val) - -> std::optional { +auto Database::ParseRecord(const leveldb::Slice& key, + const leveldb::Slice& val) + -> std::optional { std::ostringstream stream; stream << "key: "; if (key.size() < 3 || key.data()[1] != '\0') { stream << key.ToString().c_str(); } else { - std::string str = key.ToString(); - for (size_t i = 0; i < str.size(); i++) { + for (size_t i = 0; i < key.size(); i++) { if (i == 0) { - stream << str[i]; + stream << key.data()[i]; } else if (i == 1) { stream << " / 0x"; } else { stream << std::hex << std::setfill('0') << std::setw(2) - << static_cast(str[i]); + << static_cast(key.data()[i]); } } } if (!val.empty()) { stream << "\tval: 0x"; - std::string str = val.ToString(); for (int i = 0; i < val.size(); i++) { stream << std::hex << std::setfill('0') << std::setw(2) - << static_cast(str[i]); + << static_cast(val.data()[i]); } } - return stream.str(); + std::pmr::string res{stream.str(), &memory::kSpiRamResource}; + return res; } IndexRecord::IndexRecord(const IndexKey& key, - std::optional title, + std::optional title, std::optional track) : key_(key), override_text_(title), track_(track) {} -auto IndexRecord::text() const -> std::optional { +auto IndexRecord::text() const -> std::optional { if (override_text_) { return override_text_; } -- cgit v1.2.3