app/FtsIndexCoordinator.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <QElapsedTimer> | ||
| 4 | #include <QList> | ||
| 5 | #include <QObject> | ||
| 6 | |||
| 7 | #include <functional> | ||
| 8 | |||
| 9 | class QThread; | ||
| 10 | |||
| 11 | // Owns the single global FTS maintenance lane. Repeated rebuild requests are | ||
| 12 | // coalesced into the active job, and shutdown joins the database-owning worker | ||
| 13 | // before the main cache connection is closed. | ||
| 14 | class FtsIndexCoordinator final : public QObject { | ||
| 15 | ✗ | Q_OBJECT | |
| 16 | |||
| 17 | public: | ||
| 18 | enum class State { Ready, Queued, Running, Failed, ShuttingDown }; | ||
| 19 | ✗ | Q_ENUM(State) | |
| 20 | |||
| 21 | using RebuildJob = std::function<bool()>; | ||
| 22 | |||
| 23 | explicit FtsIndexCoordinator(RebuildJob rebuildJob, | ||
| 24 | QObject *parent = nullptr, | ||
| 25 | QList<int> retryDelaysMs = {1000, 5000, 30000}); | ||
| 26 | ~FtsIndexCoordinator() override; | ||
| 27 | |||
| 28 | void requestRebuild(); | ||
| 29 | void shutdown(); | ||
| 30 | |||
| 31 | State state() const { return m_state; } | ||
| 32 | 627 | bool isSearchReady() const { return m_state == State::Ready; } | |
| 33 | int queueDepth() const; | ||
| 34 | qint64 oldestAgeMs() const; | ||
| 35 | 460 | quint64 completionGeneration() const { return m_completionGeneration; } | |
| 36 | quint64 coalescedRequests() const { return m_coalescedRequests; } | ||
| 37 | QString terminalError() const { return m_terminalError; } | ||
| 38 | |||
| 39 | signals: | ||
| 40 | void stateChanged(FtsIndexCoordinator::State state); | ||
| 41 | void rebuildFinished(bool success, quint64 completionGeneration); | ||
| 42 | void rebuildRetryScheduled(int nextAttempt, int maximumAttempts, | ||
| 43 | int delayMs); | ||
| 44 | |||
| 45 | private: | ||
| 46 | void setState(State state); | ||
| 47 | void startQueuedRebuild(); | ||
| 48 | |||
| 49 | RebuildJob m_rebuildJob; | ||
| 50 | QThread *m_worker = nullptr; | ||
| 51 | State m_state = State::Ready; | ||
| 52 | QElapsedTimer m_oldestRequest; | ||
| 53 | quint64 m_completionGeneration = 0; | ||
| 54 | quint64 m_coalescedRequests = 0; | ||
| 55 | QList<int> m_retryDelaysMs; | ||
| 56 | qsizetype m_retriesScheduled = 0; | ||
| 57 | QString m_terminalError; | ||
| 58 | }; | ||
| 59 |