Rebuild package DBs as well when fixing DBs

This commit is contained in:
Martchus 2022-05-15 00:27:51 +02:00
parent 40cfe9883c
commit ae44624989
5 changed files with 44 additions and 1 deletions

View File

@ -62,6 +62,15 @@ void Config::initStorage(const char *path, std::uint32_t maxDbs)
aur.initStorage(*m_storage);
}
void LibPkg::Config::rebuildDb()
{
assert(m_storage != nullptr);
for (auto &db : databases) {
db.rebuildDb();
}
aur.rebuildDb();
}
std::size_t Config::cachedPackages() const
{
return m_storage ? m_storage->packageCache().size() : 0;

View File

@ -121,6 +121,7 @@ struct LIBPKG_EXPORT Config : public Lockable, public ReflectiveRapidJSON::Binar
// storage and caching
void initStorage(const char *path = "libpkg.db", std::uint32_t maxDbs = 0);
void rebuildDb();
std::size_t cachedPackages() const;
void setPackageCacheLimit(std::size_t limit);
std::unique_ptr<StorageDistribution> &storage();

View File

@ -6,6 +6,8 @@
#include <c++utilities/conversion/stringbuilder.h>
#include <iostream>
using namespace std;
using namespace CppUtilities;
@ -69,6 +71,34 @@ void Database::initStorage(StorageDistribution &storage)
m_storage = storage.forDatabase(name % '@' + arch);
}
void LibPkg::Database::rebuildDb()
{
std::cerr << "Rebuilding package database \"" << name << "\"\n";
auto txn = m_storage->packages.getRWTransaction();
auto processed = std::size_t();
auto ok = std::size_t();
txn.rebuild([count = txn.size(), &processed, &ok](StorageID id, Package *package) mutable {
std::cerr << "Processing package " << ++processed << " / " << count << '\n';
if (!package) {
std::cerr << "Deleting package " << id << ": unable to deserialize\n";
return false;
}
if (package->name.empty()) {
std::cerr << "Deleting package " << id << ": name is empty\n";
return false;
}
++ok;
return true;
});
if (ok < processed) {
std::cerr << "Discarding " << (processed - ok) << " invalid packages from \"" << name << "\".\n";
} else {
std::cerr << "All " << ok << " packages from \"" << name << "\" are valid.\n";
}
std::cerr << "Committing changes to package database \"" << name << "\".\n";
txn.commit();
}
void LibPkg::Database::deducePathsFromLocalDirs()
{
if (localDbDir.empty()) {

View File

@ -144,6 +144,7 @@ struct LIBPKG_EXPORT Database : public ReflectiveRapidJSON::JsonSerializable<Dat
Database &operator=(Database &&rhs) = default;
void initStorage(StorageDistribution &storage);
void rebuildDb();
void deducePathsFromLocalDirs();
void resetConfiguration(bool keepLocalPaths = false);
void clearPackages();

View File

@ -362,6 +362,7 @@ std::size_t ServiceSetup::BuildSetup::buildActionCount()
void ServiceSetup::BuildSetup::rebuildDb()
{
std::cerr << "Rebuilding build actions database\n";
auto txn = m_storage->buildActions.getRWTransaction();
auto processed = std::size_t();
auto ok = std::size_t();
@ -391,7 +392,7 @@ void ServiceSetup::BuildSetup::rebuildDb()
} else {
std::cerr << "All " << ok << " build actions are valid.\n";
}
std::cerr << "Committing changes.\n";
std::cerr << "Committing changes to build actions.\n";
txn.commit();
}
@ -833,6 +834,7 @@ int ServiceSetup::fixDb()
loadConfigFiles(true);
building.initStorage(building.dbPath.data());
building.rebuildDb();
config.rebuildDb();
#ifndef CPP_UTILITIES_DEBUG_BUILD
} catch (const std::exception &e) {
cerr << Phrases::ErrorMessage << "Exception occurred when terminating server: " << Phrases::End << " " << e.what() << Phrases::EndFlush;