indexer/store: avoid completed callback

This cause havoc / race conditions.
This commit is contained in:
Dirk-Jan C. Binnema 2022-05-18 20:16:48 +03:00
parent c3778cd467
commit 9af9d0fa94
4 changed files with 26 additions and 24 deletions

View File

@ -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<time_t> 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<Private>(store)}
{
}
{}
Indexer::~Indexer() = default;
@ -438,3 +438,9 @@ Indexer::progress() const
return priv_->progress_;
}
time_t
Indexer::completed() const
{
return priv_->completed_;
}

View File

@ -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<Private> priv_;

View File

@ -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
{

View File

@ -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<Private> priv_;
};