diff --git a/src/database/database.cpp b/src/database/database.cpp index c8dd49be..d73ce771 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -46,19 +46,6 @@ static const char kTrackIdKey[] = "next_track_id"; static std::atomic sIsDbOpen(false); -template -auto IterateAndParse(leveldb::Iterator* it, std::size_t limit, Parser p) - -> void { - for (int i = 0; i < limit; i++) { - if (!it->Valid()) { - delete it; - break; - } - std::invoke(p, it->key(), it->value()); - it->Next(); - } -} - auto Database::Open(IFileGatherer& gatherer, ITagParser& parser) -> cpp::result { // TODO(jacqueline): Why isn't compare_and_exchange_* available? @@ -66,8 +53,11 @@ auto Database::Open(IFileGatherer& gatherer, ITagParser& parser) return cpp::fail(DatabaseError::ALREADY_OPEN); } - leveldb::sBackgroundThread.reset( - tasks::Worker::Start()); + if (!leveldb::sBackgroundThread) { + leveldb::sBackgroundThread.reset( + tasks::Worker::Start()); + } + std::shared_ptr worker( tasks::Worker::Start()); return worker @@ -120,8 +110,6 @@ Database::~Database() { delete db_; delete cache_; - leveldb::sBackgroundThread.reset(); - sIsDbOpen.store(false); } @@ -136,7 +124,7 @@ auto Database::Update() -> std::future { // indexes, but my brain hurts. ESP_LOGI(kTag, "dropping stale indexes"); { - leveldb::Iterator* it = db_->NewIterator(read_options); + std::unique_ptr it{db_->NewIterator(read_options)}; OwningSlice prefix = EncodeAllIndexesPrefix(); it->Seek(prefix.slice); while (it->Valid() && it->key().starts_with(prefix.slice)) { @@ -151,7 +139,7 @@ auto Database::Update() -> std::future { .stage = event::UpdateProgress::Stage::kVerifyingExistingTracks, }); { - leveldb::Iterator* it = db_->NewIterator(read_options); + std::unique_ptr it{db_->NewIterator(read_options)}; OwningSlice prefix = EncodeDataPrefix(); it->Seek(prefix.slice); while (it->Valid() && it->key().starts_with(prefix.slice)) { @@ -200,7 +188,6 @@ auto Database::Update() -> std::future { it->Next(); } - delete it; } // Stage 2: search for newly added files. @@ -302,7 +289,8 @@ auto Database::GetBulkTracks(std::vector ids) std::vector sorted_ids = ids; std::sort(sorted_ids.begin(), sorted_ids.end()); - leveldb::Iterator* it = db_->NewIterator(leveldb::ReadOptions{}); + std::unique_ptr it{ + db_->NewIterator(leveldb::ReadOptions{})}; for (const TrackId& id : sorted_ids) { OwningSlice key = EncodeDataKey(id); it->Seek(key.slice); @@ -476,7 +464,8 @@ auto Database::dbCreateIndexesForTrack(Track track) -> void { template auto Database::dbGetPage(const Continuation& c) -> Result* { // Work out our starting point. Sometimes this will already done. - leveldb::Iterator* it = db_->NewIterator(leveldb::ReadOptions()); + std::unique_ptr it{ + db_->NewIterator(leveldb::ReadOptions{})}; it->Seek(c.start_key); // Fix off-by-one if we just changed direction. @@ -509,11 +498,8 @@ auto Database::dbGetPage(const Continuation& c) -> Result* { } } - std::unique_ptr iterator(it); - if (iterator != nullptr) { - if (!iterator->Valid() || !it->key().starts_with(c.prefix)) { - iterator.reset(); - } + if (!it->Valid() || !it->key().starts_with(c.prefix)) { + it.reset(); } // Put results into canonical order if we were iterating backwards. @@ -524,9 +510,9 @@ auto Database::dbGetPage(const Continuation& c) -> Result* { // Work out the new continuations. std::optional> next_page; if (c.forward) { - if (iterator != nullptr) { + if (it != nullptr) { // We were going forward, and now we want the next page. - std::string key = iterator->key().ToString(); + std::string key = it->key().ToString(); next_page = Continuation{ .prefix = c.prefix, .start_key = key, @@ -561,9 +547,9 @@ auto Database::dbGetPage(const Continuation& c) -> Result* { .page_size = c.page_size, }; } else { - if (iterator != nullptr) { + if (it != nullptr) { // We were going backwards, and we still want to go backwards. - std::string key = iterator->key().ToString(); + std::string key = it->key().ToString(); prev_page = Continuation{ .prefix = c.prefix, .start_key = key,