libstore: extract a real makeGoalCommon

makeDerivationGoalCommon had the right idea, but it didn't quite go far
enough. let's do the rest and remove the remaining factory duplication.

Change-Id: I1fe32446bdfb501e81df56226fd962f85720725b
This commit is contained in:
eldritch horrors 2024-09-25 23:57:46 +02:00
parent 7f4f86795c
commit 47ddd11933
2 changed files with 40 additions and 35 deletions

View file

@ -53,20 +53,24 @@ Worker::~Worker()
} }
std::pair<std::shared_ptr<DerivationGoal>, kj::Promise<void>> Worker::makeDerivationGoalCommon( template<typename ID, std::derived_from<Goal> G>
const StorePath & drvPath, std::pair<std::shared_ptr<G>, kj::Promise<void>> Worker::makeGoalCommon(
const OutputsSpec & wantedOutputs, std::map<ID, CachedGoal<G>> & map,
std::function<std::unique_ptr<DerivationGoal>()> mkDrvGoal) const ID & key,
InvocableR<std::unique_ptr<G>> auto create,
std::invocable<G &> auto modify
)
{ {
auto & goal_weak = derivationGoals[drvPath]; auto [it, _inserted] = map.try_emplace(key);
std::shared_ptr<DerivationGoal> goal = goal_weak.goal.lock(); auto & goal_weak = it->second;
auto goal = goal_weak.goal.lock();
if (!goal) { if (!goal) {
goal = mkDrvGoal(); goal = create();
goal->notify = std::move(goal_weak.fulfiller); goal->notify = std::move(goal_weak.fulfiller);
goal_weak.goal = goal; goal_weak.goal = goal;
wakeUp(goal); wakeUp(goal);
} else { } else {
goal->addWantedOutputs(wantedOutputs); modify(*goal);
} }
return {goal, goal_weak.promise->addBranch()}; return {goal, goal_weak.promise->addBranch()};
} }
@ -76,9 +80,9 @@ std::pair<std::shared_ptr<DerivationGoal>, kj::Promise<void>> Worker::makeDeriva
const StorePath & drvPath, const OutputsSpec & wantedOutputs, BuildMode buildMode const StorePath & drvPath, const OutputsSpec & wantedOutputs, BuildMode buildMode
) )
{ {
return makeDerivationGoalCommon( return makeGoalCommon(
derivationGoals,
drvPath, drvPath,
wantedOutputs,
[&]() -> std::unique_ptr<DerivationGoal> { [&]() -> std::unique_ptr<DerivationGoal> {
return !dynamic_cast<LocalStore *>(&store) return !dynamic_cast<LocalStore *>(&store)
? std::make_unique<DerivationGoal>( ? std::make_unique<DerivationGoal>(
@ -87,7 +91,8 @@ std::pair<std::shared_ptr<DerivationGoal>, kj::Promise<void>> Worker::makeDeriva
: LocalDerivationGoal::makeLocalDerivationGoal( : LocalDerivationGoal::makeLocalDerivationGoal(
drvPath, wantedOutputs, *this, running, buildMode drvPath, wantedOutputs, *this, running, buildMode
); );
} },
[&](DerivationGoal & g) { g.addWantedOutputs(wantedOutputs); }
); );
} }
@ -99,9 +104,9 @@ std::pair<std::shared_ptr<DerivationGoal>, kj::Promise<void>> Worker::makeBasicD
BuildMode buildMode BuildMode buildMode
) )
{ {
return makeDerivationGoalCommon( return makeGoalCommon(
derivationGoals,
drvPath, drvPath,
wantedOutputs,
[&]() -> std::unique_ptr<DerivationGoal> { [&]() -> std::unique_ptr<DerivationGoal> {
return !dynamic_cast<LocalStore *>(&store) return !dynamic_cast<LocalStore *>(&store)
? std::make_unique<DerivationGoal>( ? std::make_unique<DerivationGoal>(
@ -110,7 +115,8 @@ std::pair<std::shared_ptr<DerivationGoal>, kj::Promise<void>> Worker::makeBasicD
: LocalDerivationGoal::makeLocalDerivationGoal( : LocalDerivationGoal::makeLocalDerivationGoal(
drvPath, drv, wantedOutputs, *this, running, buildMode drvPath, drv, wantedOutputs, *this, running, buildMode
); );
} },
[&](DerivationGoal & g) { g.addWantedOutputs(wantedOutputs); }
); );
} }
@ -120,15 +126,12 @@ Worker::makePathSubstitutionGoal(
const StorePath & path, RepairFlag repair, std::optional<ContentAddress> ca const StorePath & path, RepairFlag repair, std::optional<ContentAddress> ca
) )
{ {
auto & goal_weak = substitutionGoals[path]; return makeGoalCommon(
auto goal = goal_weak.goal.lock(); // FIXME substitutionGoals,
if (!goal) { path,
goal = std::make_shared<PathSubstitutionGoal>(path, *this, running, repair, ca); [&] { return std::make_unique<PathSubstitutionGoal>(path, *this, running, repair, ca); },
goal->notify = std::move(goal_weak.fulfiller); [&](auto &) {}
goal_weak.goal = goal; );
wakeUp(goal);
}
return {goal, goal_weak.promise->addBranch()};
} }
@ -137,15 +140,12 @@ Worker::makeDrvOutputSubstitutionGoal(
const DrvOutput & id, RepairFlag repair, std::optional<ContentAddress> ca const DrvOutput & id, RepairFlag repair, std::optional<ContentAddress> ca
) )
{ {
auto & goal_weak = drvOutputSubstitutionGoals[id]; return makeGoalCommon(
auto goal = goal_weak.goal.lock(); // FIXME drvOutputSubstitutionGoals,
if (!goal) { id,
goal = std::make_shared<DrvOutputSubstitutionGoal>(id, *this, running, repair, ca); [&] { return std::make_unique<DrvOutputSubstitutionGoal>(id, *this, running, repair, ca); },
goal->notify = std::move(goal_weak.fulfiller); [&](auto &) {}
goal_weak.goal = goal; );
wakeUp(goal);
}
return {goal, goal_weak.promise->addBranch()};
} }

View file

@ -2,6 +2,7 @@
///@file ///@file
#include "async-semaphore.hh" #include "async-semaphore.hh"
#include "concepts.hh"
#include "notifying-counter.hh" #include "notifying-counter.hh"
#include "types.hh" #include "types.hh"
#include "lock.hh" #include "lock.hh"
@ -240,9 +241,13 @@ public:
* @ref DerivationGoal "derivation goal" * @ref DerivationGoal "derivation goal"
*/ */
private: private:
std::pair<std::shared_ptr<DerivationGoal>, kj::Promise<void>> makeDerivationGoalCommon( template<typename ID, std::derived_from<Goal> G>
const StorePath & drvPath, const OutputsSpec & wantedOutputs, std::pair<std::shared_ptr<G>, kj::Promise<void>> makeGoalCommon(
std::function<std::unique_ptr<DerivationGoal>()> mkDrvGoal); std::map<ID, CachedGoal<G>> & map,
const ID & key,
InvocableR<std::unique_ptr<G>> auto create,
std::invocable<G &> auto modify
);
std::pair<std::shared_ptr<DerivationGoal>, kj::Promise<void>> makeDerivationGoal( std::pair<std::shared_ptr<DerivationGoal>, kj::Promise<void>> makeDerivationGoal(
const StorePath & drvPath, const StorePath & drvPath,
const OutputsSpec & wantedOutputs, BuildMode buildMode = bmNormal) override; const OutputsSpec & wantedOutputs, BuildMode buildMode = bmNormal) override;