[CardDB] Reload after the first run enables all sets without freezing startup (#7470)

* [CardDB] Reload when the first run enables all sets

checkUnknownSets() enabled every set when none was enabled, but the
database had already been parsed under that empty enablement, so the
parsers had stripped every printing. It only notified the models and never
reloaded, leaving the deck editor's card database display empty until the
next launch.

Route the all-sets-enabled case through the same reload the new-sets path
uses, and skip caching a snapshot whose sets are all disabled.

* [CardDB] Own checkUnknownSets after startup and fix the cache skip

The loader's initialLoadComplete guard skipped checkUnknownSets() on the
first successful load of the session, so a download that landed after
startupConfigCheck() and the onboarding wizard had already run (e.g.
skipping the wizard download and fetching later from the menu) left every
set disabled and the deck editor blank until the next launch. The guard
also let the check run on the worker before the queued snapshot swap, and
alongside reloadCardDatabasesAndNotify()'s own check, double-emitting
cardDatabaseNewSetsFound on manual reloads.

Move the check to the callers that load after startup: the post-update,
custom-import, and spoiler paths run it via a QFutureWatcher once the
finished snapshot has been swapped in, and the loader's synchronous
path-change reload runs it directly.

Judge the cache skip by the parsed sets' own enablement, what the parsers
actually saw, instead of getEnabledSetNames(), which reports empty for the
noop controller even though it enables every set. Wait for the first-run
reparse before opening Manage Sets so the dialog cannot bind to the
pre-reload set instances.

* [CardDB] Queue post-reload check and unblock first-run Manage Sets

reloadCardDatabasesAndNotify() no longer emits cardDatabaseEnabledSetsChanged after the swap's Reset already rebuilt the models (second 36k rebuild froze startup), and its follow-up checkUnknownSets() is queued to the GUI thread so it sees swapped-in data. cardDatabaseAllNewSetsEnabled() chains Welcome/Manage Sets off a QFutureWatcher instead of waitForFinished() blocking the event loop during the reparse.

* [CardDB] Batch the first-run enable-all into a single settings flush

checkUnknownSets() enabled every set with ~3 QSettings rewrite cycles per set (sort key, known, enabled), and each setValue re-opens and re-syncs the whole cardDatabase.ini. With a full database (874 sets) that froze the GUI thread for ~9s after the database download (LagMonitor: event loop stalled for 9129 ms, measured offscreen with a real 59MB cards.xml), reading as a total application hang that recovers on its own.

Persist the enable-all through the controller's batched saveSets() in a single flush instead, mirroring what SetsModel::save() already does: CardSetList::enableAllBatched() assigns fresh sort keys and marks every set known+enabled in memory, then writes all records at once. SetSaveData gains the isKnown flag so the batch covers all three options, and SetsModel::save() passes the sets' known state through (unchanged behavior there).

Also drop the cardDatabaseEnabledSetsChanged emission from the all-sets-enabled branch: the reload chained off cardDatabaseAllNewSetsEnabled rebuilds the models from the re-parsed snapshot via cardDatabaseReset, so emitting both rebuilt the 36k-card models twice on the GUI thread.

Adds a cache-invalidation test driving the real checkUnknownSets() first-run path.

* [CardDB] Batch the new-sets-found enable-all the same way

The first-run batching fixed the 874-set case, but the new-sets-found dialog path (enableAllUnknownSets/markAllSetsAsKnown) still wrote per set and per option, each setValue re-opening and re-syncing the whole cardDatabase.ini. Users with hundreds of custom sets hit the same multi-second GUI stall when answering the dialog.

Persist both through the controller's batched saveSets() in a single flush, writing only changed sets (so the common no-op check skips the flush entirely). Removes the now-unused write-through CardSetList methods (enableAll, enableAllUnknown, markAllAsKnown, guessSortKeys) so the batched flush is the only way to persist bulk set changes.

Extends the test controller mock with per-write/batched-flush counters and adds tests asserting both dialog paths persist with exactly one flush and zero individual writes.

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
BruebachL 2026-10-13 06:29:16 +02:00 • committed by GitHub
parent bac2d285e3
commit a712553c81
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 364 additions and 112 deletions

View file

@ -13,6 +13,7 @@
#include <QDebug>
#include <QList>
#include <QMessageLogger>
#include <QMetaObject>
#include <QMetaType>
#include <QMutexLocker>
#include <QSharedPointer>
@ -80,15 +81,17 @@ void CardDatabase::reloadCardDatabasesAndNotify()
{
// An explicit reload bypasses the binary cache so the files on disk are
// always re-read, even when their size/mtime hash still matches the cache.
// Models refresh from the swap's cardDatabaseReset in a single batch, so
// no cardDatabaseEnabledSetsChanged is emitted here: that rebuilt the
// 36k-card models a second time on the GUI thread, which froze startup
// after enabling new/custom sets. The follow-up unknown-sets check is
// queued to the GUI thread so it sees the swapped-in data (this may run
// on a worker via QtConcurrent::run); it is silent unless new sets
// appeared mid-reload.
loadStatus = loader->loadCardDatabases(/* ignoreCache */ true);
if (loadStatus == Ok) {
checkUnknownSets();
// A reload reconstructs the exact same enabled-set state, so the cached
// set-name / alt-name data computed during construction is still valid.
// Skipping the per-card refresh avoids a full 36k-card recompute that
// would otherwise duplicate the work already done while building cards.
notifyEnabledSetsChanged(false);
QMetaObject::invokeMethod(this, &CardDatabase::checkUnknownSets, Qt::AutoConnection);
}
}
@ -207,15 +210,19 @@ void CardDatabase::checkUnknownSets()
if (numUnknownSets > 0) {
emit cardDatabaseNewSetsFound(numUnknownSets, unknownSetNames);
} else {
_sets.markAllAsKnown();
_sets.markAllAsKnownBatched(setPriorityController);
}
} else {
// No set enabled. Probably this is the first time running trice
_sets.guessSortKeys();
_sets.enableAllBatched(setPriorityController);
_sets.sortByKey();
_sets.enableAll();
notifyEnabledSetsChanged();
// No cardDatabaseEnabledSetsChanged here: the reload chained off
// cardDatabaseAllNewSetsEnabled re-parses with the sets enabled and its
// swap's cardDatabaseReset rebuilds the models in one batch. Emitting
// both back-to-back rebuilt the 36k-card models twice on the GUI thread,
// stalling startup. (When the card list is empty the slot returns early
// without reloading, but there is nothing to display either.)
emit cardDatabaseAllNewSetsEnabled();
}
}
@ -223,13 +230,13 @@ void CardDatabase::checkUnknownSets()
void CardDatabase::enableAllUnknownSets()
{
auto _sets = getSetList();
_sets.enableAllUnknown();
_sets.enableAllUnknownBatched(setPriorityController);
}
void CardDatabase::markAllSetsAsKnown()
{
auto _sets = getSetList();
_sets.markAllAsKnown();
_sets.markAllAsKnownBatched(setPriorityController);
}
void CardDatabase::notifyEnabledSetsChanged(bool recomputeCachedSets)

View file

@ -44,7 +44,13 @@ CardDatabaseLoader::CardDatabaseLoader(QObject *parent,
// the finished snapshot is swapped into the live database on the GUI thread.
// when SettingsCache's path changes, trigger reloads
connect(pathProvider, &ICardDatabasePathProvider::cardDatabasePathChanged, this, [this] { loadCardDatabases(); });
connect(pathProvider, &ICardDatabasePathProvider::cardDatabasePathChanged, this, [this] {
loadCardDatabases();
// This reload runs synchronously on the emitting (GUI) thread, so the
// finished snapshot is already swapped in when it returns: enable any
// sets the newly pointed-at database introduced.
database->checkUnknownSets();
});
}
CardDatabaseLoader::~CardDatabaseLoader()
@ -136,7 +142,23 @@ LoadStatus CardDatabaseLoader::doLoadCardDatabases(bool ignoreCache)
loadCardDatabase(p, data);
}
if (!saveToCache(data, sourceHash)) {
// A snapshot parsed while no set is enabled has had every printing
// stripped by the parsers, and the cache reader re-derives enablement
// from the priority controller, so such an entry can never be reused
// meaningfully. Skip writing it so a first-run (blank config) load
// cannot poison the cache with an empty snapshot. The check mirrors
// what the parsers saw: any set still reporting enabled means the
// snapshot is complete for the current enablement.
bool anySetEnabled = false;
for (const CardSetPtr &set : data.sets) {
if (set && set->getEnabled()) {
anySetEnabled = true;
break;
}
}
if (!data.sets.isEmpty() && !anySetEnabled) {
qCInfo(CardDatabaseLoadingLog) << "Skipping binary cache: no set is enabled yet, snapshot was stripped";
} else if (!saveToCache(data, sourceHash)) {
qCWarning(CardDatabaseLoadingLog) << "Failed to write binary cache to" << cachePath();
}
}
@ -148,18 +170,15 @@ LoadStatus CardDatabaseLoader::doLoadCardDatabases(bool ignoreCache)
database->refreshCachedReverseRelatedCards(data.cards);
qCInfo(CardDatabaseLoadingSuccessOrFailureLog) << "Card Database Loading Success";
// NOTE: checkUnknownSets() is intentionally NOT called here. During the
// front-loaded parse in main() MainWindow does not exist yet, so
// cardDatabaseNewSetsFound / cardDatabaseAllNewSetsEnabled would be
// emitted with no receivers, and enabling the sets up-front would
// suppress the first-run experience MainWindow::startupConfigCheck()
// and the onboarding wizard are designed around. Callers that load
// after startup own the check (and any reload it triggers).
emit databaseDataReady(std::move(data));
emit loadingFinished();
// During the front-loaded parse in main() this runs before MainWindow
// exists, so cardDatabaseNewSetsFound / cardDatabaseAllNewSetsEnabled
// would be emitted with no receivers. Skip the check on the first load;
// MainWindow::startupConfigCheck() calls checkUnknownSets() once its
// signal connections are live. On subsequent reloads (e.g. path change)
// MainWindow exists and signals have live receivers.
if (initialLoadComplete) {
database->checkUnknownSets();
}
initialLoadComplete = true;
} else {
qCInfo(CardDatabaseLoadingSuccessOrFailureLog) << "Card Database Loading Failed";
emit loadingFailed(); // bring up the settings dialog

View file

@ -179,7 +179,6 @@ private:
QBasicMutex *loadFromFileMutex = new QBasicMutex(); /**< Mutex for single-file loading. */
QBasicMutex *reloadDatabaseMutex = new QBasicMutex(); /**< Mutex for reloading entire database. */
bool initialLoadComplete = false; /**< Set after the first successful load. */
};
#endif // COCKATRICE_CARD_DATABASE_LOADER_H

View file

@ -227,6 +227,10 @@ public:
* @param _isknown True if known, false if unknown.
*/
void setIsKnown(bool _isknown);
void setIsKnownInMemory(bool _isknown)
{
isknown = _isknown;
}
/**
* @brief Determines whether the set has incomplete metadata and should be ignored.

View file

@ -1,9 +1,11 @@
#include "card_set_list.h"
#include "libcockatrice/card/set/card_set.h"
#include "libcockatrice/interfaces/interface_card_set_priority_controller.h"
#include <QDate>
#include <QString>
#include <QVector>
#include <algorithm>
#include <compare>
@ -62,60 +64,87 @@ QStringList CardSetList::getUnknownSetsNames()
return sets;
}
void CardSetList::enableAllUnknown()
void CardSetList::enableAllUnknownBatched(ICardSetPriorityController *controller)
{
for (int i = 0; i < size(); ++i) {
CardSetPtr set = at(i);
if (set && !set->getIsKnown() && !set->getIsKnownIgnored()) {
set->setIsKnown(true);
set->setEnabled(true);
} else if (set && set->getIsKnownIgnored() && !set->getEnabled()) {
set->setEnabled(true);
}
}
}
void CardSetList::enableAll()
{
for (int i = 0; i < size(); ++i) {
CardSetPtr set = at(i);
if (set == nullptr) {
// qCWarning(CardInfoLog) << "enabledAll has null";
continue;
}
if (!set->getIsKnownIgnored()) {
set->setIsKnown(true);
}
set->setEnabled(true);
}
}
void CardSetList::markAllAsKnown()
{
for (int i = 0; i < size(); ++i) {
CardSetPtr set = at(i);
if (set && !set->getIsKnown() && !set->getIsKnownIgnored()) {
set->setIsKnown(true);
set->setEnabled(false);
} else if (set && set->getIsKnownIgnored() && !set->getEnabled()) {
set->setEnabled(true);
}
}
}
void CardSetList::guessSortKeys()
{
defaultSort();
QVector<ICardSetPriorityController::SetSaveData> saveData;
for (int i = 0; i < size(); ++i) {
CardSetPtr set = at(i);
if (set.isNull()) {
// qCWarning(CardInfoLog) << "guessSortKeys set is null";
continue;
}
set->setSortKey(i);
bool changed = false;
if (!set->getIsKnown() && !set->getIsKnownIgnored()) {
set->setIsKnownInMemory(true);
set->setEnabledInMemory(true);
changed = true;
} else if (set->getIsKnownIgnored() && !set->getEnabled()) {
set->setEnabledInMemory(true);
changed = true;
}
if (changed) {
saveData.append(
{set->getShortName(), static_cast<unsigned int>(set->getSortKey()), true, set->getIsKnown()});
}
}
if (!saveData.isEmpty()) {
controller->saveSets(saveData);
}
}
void CardSetList::enableAllBatched(ICardSetPriorityController *controller)
{
defaultSort();
QVector<ICardSetPriorityController::SetSaveData> saveData;
saveData.reserve(size());
for (int i = 0; i < size(); ++i) {
CardSetPtr set = at(i);
if (set.isNull()) {
continue;
}
const auto sortKey = static_cast<unsigned int>(i);
set->setSortKeyInMemory(sortKey);
if (!set->getIsKnownIgnored()) {
set->setIsKnownInMemory(true);
}
set->setEnabledInMemory(true);
saveData.append({set->getShortName(), sortKey, true, set->getIsKnown()});
}
if (!saveData.isEmpty()) {
controller->saveSets(saveData);
}
}
void CardSetList::markAllAsKnownBatched(ICardSetPriorityController *controller)
{
QVector<ICardSetPriorityController::SetSaveData> saveData;
for (int i = 0; i < size(); ++i) {
CardSetPtr set = at(i);
if (set.isNull()) {
continue;
}
bool changed = false;
if (!set->getIsKnown() && !set->getIsKnownIgnored()) {
set->setIsKnownInMemory(true);
set->setEnabledInMemory(false);
changed = true;
} else if (set->getIsKnownIgnored() && !set->getEnabled()) {
set->setEnabledInMemory(true);
changed = true;
}
if (changed) {
saveData.append({set->getShortName(), static_cast<unsigned int>(set->getSortKey()), set->getEnabled(),
set->getIsKnown()});
}
}
if (!saveData.isEmpty()) {
controller->saveSets(saveData);
}
}

View file

@ -40,36 +40,37 @@ public:
void sortByKey();
/**
* @brief Reassigns sort keys based on the current order.
* @brief Enables every set with fresh sort keys, persisting in one flush.
*
* Calls defaultSort() and then assigns sequential sort keys
* to all sets according to their resulting positions, replacing
* any existing sort keys to ensure consistent ordering.
* Assigns sequential sort keys in default-sorted order and marks every set
* known and enabled, writing all records through the controller's batched
* saveSets() in a single settings flush instead of ~3 QSettings rewrite
* cycles per set, which froze the GUI thread for seconds on a first run
* with a full database.
* @param controller The set priority controller to persist the records with.
*/
void guessSortKeys();
void enableAllBatched(ICardSetPriorityController *controller);
/**
* @brief Enables all sets that are unknown or ignored.
* @brief Enables all sets that are unknown or ignored, persisting in one flush.
*
* Sets that are not marked as known and not ignored are marked as known
* and enabled. Ignored-known sets are also enabled, but remain ignored.
* Only changed sets are written, so the common case of a few new sets costs
* a single settings flush no matter how large the database is.
* @param controller The set priority controller to persist the records with.
*/
void enableAllUnknown();
void enableAllUnknownBatched(ICardSetPriorityController *controller);
/**
* @brief Enables all sets in the list.
*
* Equivalent to calling `setEnabled(true)` on each entry.
*/
void enableAll();
/**
* @brief Marks all sets as known and adjusts their enabled state.
* @brief Marks all sets as known and adjusts their enabled state, persisting in one flush.
*
* Unknown, non-ignored sets become known and disabled.
* Ignored-known sets are enabled if they were previously disabled.
* Only changed sets are written; when nothing is unknown no flush happens at all.
* @param controller The set priority controller to persist the records with.
*/
void markAllAsKnown();
void markAllAsKnownBatched(ICardSetPriorityController *controller);
/**
* @brief Counts the number of sets that are currently enabled.