diff --git a/src/libstore/gc.cc b/src/libstore/gc.cc index 0366fe0b0..385215fe0 100644 --- a/src/libstore/gc.cc +++ b/src/libstore/gc.cc @@ -931,7 +931,7 @@ void LocalStore::autoGC(bool sync) std::shared_future future; { - auto state(_state.lock()); + auto state(_state->lock()); if (state->gcRunning) { future = state->gcFuture; @@ -964,7 +964,7 @@ void LocalStore::autoGC(bool sync) /* Wake up any threads waiting for the auto-GC to finish. */ Finally wakeup([&]() { - auto state(_state.lock()); + auto state(_state->lock()); state->gcRunning = false; state->lastGCCheck = std::chrono::steady_clock::now(); promise.set_value(); @@ -979,7 +979,7 @@ void LocalStore::autoGC(bool sync) collectGarbage(options, results); - _state.lock()->availAfterGC = getAvail(); + _state->lock()->availAfterGC = getAvail(); } catch (...) { // FIXME: we could propagate the exception to the diff --git a/src/libstore/include/nix/store/local-store.hh b/src/libstore/include/nix/store/local-store.hh index f7dfcb5ad..444d1b28f 100644 --- a/src/libstore/include/nix/store/local-store.hh +++ b/src/libstore/include/nix/store/local-store.hh @@ -174,7 +174,7 @@ private: std::unique_ptr publicKeys; }; - Sync _state; + ref> _state; public: diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index 112d5b14c..058814f33 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -118,6 +118,7 @@ LocalStore::LocalStore(ref config) : Store{*config} , LocalFSStore{*config} , config{config} + , _state(make_ref>()) , dbDir(config->stateDir + "/db") , linksDir(config->realStoreDir + "/.links") , reservedPath(dbDir + "/reserved") @@ -125,7 +126,7 @@ LocalStore::LocalStore(ref config) , tempRootsDir(config->stateDir + "/temproots") , fnTempRoots(fmt("%s/%d", tempRootsDir, getpid())) { - auto state(_state.lock()); + auto state(_state->lock()); state->stmts = std::make_unique(); /* Create missing state directories if they don't already exist. */ @@ -433,7 +434,7 @@ LocalStore::~LocalStore() std::shared_future future; { - auto state(_state.lock()); + auto state(_state->lock()); if (state->gcRunning) future = state->gcFuture; } @@ -629,7 +630,7 @@ void LocalStore::registerDrvOutput(const Realisation & info) { experimentalFeatureSettings.require(Xp::CaDerivations); retrySQLite([&]() { - auto state(_state.lock()); + auto state(_state->lock()); if (auto oldR = queryRealisation_(*state, info.id)) { if (info.isCompatibleWith(*oldR)) { auto combinedSignatures = oldR->signatures; @@ -736,8 +737,7 @@ void LocalStore::queryPathInfoUncached( { try { callback(retrySQLite>([&]() { - auto state(_state.lock()); - return queryPathInfoInternal(*state, path); + return queryPathInfoInternal(*_state->lock(), path); })); } catch (...) { @@ -819,10 +819,7 @@ bool LocalStore::isValidPath_(State & state, const StorePath & path) bool LocalStore::isValidPathUncached(const StorePath & path) { - return retrySQLite([&]() { - auto state(_state.lock()); - return isValidPath_(*state, path); - }); + return retrySQLite([&]() { return isValidPath_(*_state->lock(), path); }); } StorePathSet LocalStore::queryValidPaths(const StorePathSet & paths, SubstituteFlag maybeSubstitute) @@ -837,7 +834,7 @@ StorePathSet LocalStore::queryValidPaths(const StorePathSet & paths, SubstituteF StorePathSet LocalStore::queryAllValidPaths() { return retrySQLite([&]() { - auto state(_state.lock()); + auto state(_state->lock()); auto use(state->stmts->QueryValidPaths.use()); StorePathSet res; while (use.next()) @@ -856,16 +853,13 @@ void LocalStore::queryReferrers(State & state, const StorePath & path, StorePath void LocalStore::queryReferrers(const StorePath & path, StorePathSet & referrers) { - return retrySQLite([&]() { - auto state(_state.lock()); - queryReferrers(*state, path, referrers); - }); + return retrySQLite([&]() { queryReferrers(*_state->lock(), path, referrers); }); } StorePathSet LocalStore::queryValidDerivers(const StorePath & path) { return retrySQLite([&]() { - auto state(_state.lock()); + auto state(_state->lock()); auto useQueryValidDerivers(state->stmts->QueryValidDerivers.use()(printStorePath(path))); @@ -881,7 +875,7 @@ std::map> LocalStore::queryStaticPartialDerivationOutputMap(const StorePath & path) { return retrySQLite>>([&]() { - auto state(_state.lock()); + auto state(_state->lock()); std::map> outputs; uint64_t drvId; drvId = queryValidPathId(*state, path); @@ -901,7 +895,7 @@ std::optional LocalStore::queryPathFromHashPart(const std::string & h Path prefix = storeDir + "/" + hashPart; return retrySQLite>([&]() -> std::optional { - auto state(_state.lock()); + auto state(_state->lock()); auto useQueryPathFromHashPart(state->stmts->QueryPathFromHashPart.use()(prefix)); @@ -966,7 +960,7 @@ void LocalStore::registerValidPaths(const ValidPathInfos & infos) #endif return retrySQLite([&]() { - auto state(_state.lock()); + auto state(_state->lock()); SQLiteTxn txn(state->db); StorePathSet paths; @@ -1036,7 +1030,7 @@ void LocalStore::invalidatePath(State & state, const StorePath & path) const PublicKeys & LocalStore::getPublicKeys() { - auto state(_state.lock()); + auto state(_state->lock()); if (!state->publicKeys) state->publicKeys = std::make_unique(getDefaultPublicKeys()); return *state->publicKeys; @@ -1359,7 +1353,7 @@ std::pair LocalStore::createTempDirInStore() void LocalStore::invalidatePathChecked(const StorePath & path) { retrySQLite([&]() { - auto state(_state.lock()); + auto state(_state->lock()); SQLiteTxn txn(state->db); @@ -1459,10 +1453,8 @@ bool LocalStore::verifyStore(bool checkContents, RepairFlag repair) update = true; } - if (update) { - auto state(_state.lock()); - updatePathInfo(*state, *info); - } + if (update) + updatePathInfo(*_state->lock(), *info); } } catch (Error & e) { @@ -1549,8 +1541,7 @@ void LocalStore::verifyPath( if (canInvalidate) { printInfo("path '%s' disappeared, removing from database...", pathS); - auto state(_state.lock()); - invalidatePath(*state, path); + invalidatePath(*_state->lock(), path); } else { printError("path '%s' disappeared, but it still has valid referrers!", pathS); if (repair) @@ -1582,14 +1573,13 @@ std::optional LocalStore::isTrustedClient() void LocalStore::vacuumDB() { - auto state(_state.lock()); - state->db.exec("vacuum"); + _state->lock()->db.exec("vacuum"); } void LocalStore::addSignatures(const StorePath & storePath, const StringSet & sigs) { retrySQLite([&]() { - auto state(_state.lock()); + auto state(_state->lock()); SQLiteTxn txn(state->db); @@ -1651,10 +1641,8 @@ void LocalStore::queryRealisationUncached( const DrvOutput & id, Callback> callback) noexcept { try { - auto maybeRealisation = retrySQLite>([&]() { - auto state(_state.lock()); - return queryRealisation_(*state, id); - }); + auto maybeRealisation = + retrySQLite>([&]() { return queryRealisation_(*_state->lock(), id); }); if (maybeRealisation) callback(std::make_shared(maybeRealisation.value())); else