diff --git a/lib/index/mu-indexer.cc b/lib/index/mu-indexer.cc index 6925e267..339692f1 100644 --- a/lib/index/mu-indexer.cc +++ b/lib/index/mu-indexer.cc @@ -43,7 +43,8 @@ struct IndexState { enum State { Idle, Scanning, Finishing, - Cleaning }; + Cleaning + }; static const char* name(State s) { switch (s) { case Idle: @@ -65,7 +66,6 @@ struct IndexState { bool operator!=(State rhs) const { return state_.load() != rhs; } - void change_to(State new_state) { g_debug("changing indexer state %s->%s", name((State)state_), name((State)new_state)); @@ -128,6 +128,8 @@ struct Indexer::Private { Progress progress_; IndexState state_; std::mutex lock_, w_lock_; + + std::atomic completed_; }; bool @@ -339,7 +341,7 @@ Indexer::Private::scan_worker() g_debug("cleanup finished"); } - store_.index_complete(); + completed_ = ::time({}); state_.change_to(IndexState::Idle); } @@ -380,7 +382,6 @@ Indexer::Private::stop() if (scanner_worker_.joinable()) scanner_worker_.join(); - store_.index_complete(); state_.change_to(IndexState::Idle); for (auto&& w : workers_) if (w.joinable()) @@ -392,8 +393,7 @@ Indexer::Private::stop() Indexer::Indexer(Store& store) : priv_{std::make_unique(store)} -{ -} +{} Indexer::~Indexer() = default; @@ -438,3 +438,9 @@ Indexer::progress() const return priv_->progress_; } + +time_t +Indexer::completed() const +{ + return priv_->completed_; +} diff --git a/lib/index/mu-indexer.hh b/lib/index/mu-indexer.hh index bf318b02..0c678a6a 100644 --- a/lib/index/mu-indexer.hh +++ b/lib/index/mu-indexer.hh @@ -110,6 +110,13 @@ public: */ const Progress& progress() const; + /** + * Last time indexing was completed. + * + * @return the time or 0 + */ + time_t completed() const; + private: struct Private; std::unique_ptr priv_; diff --git a/lib/mu-store.cc b/lib/mu-store.cc index efd40b45..0d368567 100644 --- a/lib/mu-store.cc +++ b/lib/mu-store.cc @@ -103,8 +103,6 @@ struct Store::Private { ~Private() try { - indexer_.reset(); // reset so it cannot call us back - g_debug("closing store @ %s", properties_.database_path.c_str()); if (!read_only_) { transaction_maybe_commit(true /*force*/); @@ -173,6 +171,13 @@ struct Store::Private { contacts_cache_.serialize()); }); } + + if (indexer_) { // save last index time. + if (auto&& t{indexer_->completed()}; t != 0) + writable_db().set_metadata( + IndexedKey, tstamp_to_string(t)); + } + if (transaction_size_ == 0) return; // nothing more to do here. @@ -642,16 +647,6 @@ Store::commit() } -void -Store::index_complete() -{ - std::lock_guard lock{priv_->lock_}; - - g_debug("marking index complete"); - priv_->writable_db().set_metadata(IndexedKey, tstamp_to_string(::time({}))); -} - - std::size_t Store::for_each_term(Field::Id field_id, Store::ForEachTermFunc func) const { diff --git a/lib/mu-store.hh b/lib/mu-store.hh index 8eccbb78..2f5e92ca 100644 --- a/lib/mu-store.hh +++ b/lib/mu-store.hh @@ -456,12 +456,6 @@ private: const Config& conf); - /** - * Call with indexing has completed to update metadata. - */ - friend class Indexer; - void index_complete(); - std::unique_ptr priv_; };