From 19f83eaa34e95a25373437728c8ab3316a3f273c Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Sun, 11 May 2025 16:38:32 +0100 Subject: Fix loading databases in db_json. --- modules/database/db_json.cpp | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'modules') diff --git a/modules/database/db_json.cpp b/modules/database/db_json.cpp index 9796ea7f3..a43acc36b 100644 --- a/modules/database/db_json.cpp +++ b/modules/database/db_json.cpp @@ -201,6 +201,22 @@ private: CreateBackup(backupdir, dbpath, monthly_backups, "%Y-%m", "\?\?\?\?-\?\?"); } + void LoadType(Serialize::Type *s_type, yyjson_mut_val *data) + { + auto *entries = yyjson_mut_obj_get(data, s_type->GetName().c_str()); + if (!entries || !yyjson_mut_is_arr(entries)) + return; + + Log(LOG_DEBUG) << "Loading " << yyjson_mut_arr_size(entries) << " " << s_type->GetName() << " records"; + size_t idx, max; + yyjson_mut_val *elem; + yyjson_mut_arr_foreach(entries, idx, max, elem) + { + Data ld(elem); + s_type->Unserialize(nullptr, ld); + } + } + DBPair ReadDatabase(const Anope::string &dbname) { yyjson_read_err errmsg; @@ -269,19 +285,11 @@ public: for (const auto &type : Serialize::Type::GetTypeOrder()) { auto *s_type = Serialize::Type::Find(type); - if (!s_type || !s_type->GetOwner()) - continue; - - size_t idx, max; - yyjson_mut_val *elem; - yyjson_mut_arr_foreach(data, idx, max, elem) - { - Data ld(elem); - s_type->Unserialize(nullptr, ld); - } + if (s_type && !s_type->GetOwner()) + LoadType(s_type, data); } - loaded = false; + loaded = true; return EVENT_STOP; } @@ -413,15 +421,8 @@ public: it = databases.emplace(s_type->GetOwner(), db).first; } - auto &[doc, data] = it->second; - - size_t idx, max; - yyjson_mut_val *elem; - yyjson_mut_arr_foreach(data, idx, max, elem) - { - Data ld(elem); - s_type->Unserialize(nullptr, ld); - } + auto &[_, data] = it->second; + LoadType(s_type, data); } }; -- cgit