Do not call solver multiple times, reuse same assertion

Introduce Cut(), it allows to filter in the assertion to achieve the same hashes
This commit is contained in:
Ettore Di Giacinto
2019-12-14 15:00:16 +01:00
parent c4b83605ef
commit fe608469d8
4 changed files with 50 additions and 7 deletions

View File

@@ -442,17 +442,13 @@ func (cs *LuetCompiler) ComputeDepTree(p CompilationSpec) (solver.PackagesAssert
dependencies := solution.Order(cs.Database, p.GetPackage().GetFingerPrint()) dependencies := solution.Order(cs.Database, p.GetPackage().GetFingerPrint())
assertions := solver.PackagesAssertions{} assertions := solver.PackagesAssertions{}
for _, assertion := range dependencies { //highly dependent on the order for _, assertion := range dependencies { //highly dependent on the order
if assertion.Value { if assertion.Value {
nthsolution, err := s.Install([]pkg.Package{assertion.Package}) nthsolution := dependencies.Cut(assertion.Package)
if err != nil {
return nil, errors.Wrap(err, "While computing a solution for "+p.GetPackage().GetName())
}
assertion.Hash = solver.PackageHash{ assertion.Hash = solver.PackageHash{
BuildHash: nthsolution.Order(cs.Database, assertion.Package.GetFingerPrint()).Drop(assertion.Package).AssertionHash(), BuildHash: nthsolution.Drop(assertion.Package).AssertionHash(),
PackageHash: nthsolution.Order(cs.Database, assertion.Package.GetFingerPrint()).AssertionHash(), PackageHash: nthsolution.AssertionHash(),
} }
assertions = append(assertions, assertion) assertions = append(assertions, assertion)
} }

View File

@@ -400,6 +400,11 @@ func (pack *DefaultPackage) BuildFormula(definitiondb PackageDatabase, db Packag
var formulas []bf.Formula var formulas []bf.Formula
for _, requiredDef := range p.GetRequires() { for _, requiredDef := range p.GetRequires() {
// TODO: Stabilize this. We allow any of those version to be selected,
// at the price that they can't be selected alltogether.
// This have the downside that we cannot specify a preference (e.g. The best matching)
// unless we have a user-defined version. It means that the solver could
// give different output between calls, but they are all legit as they respect the constraints.
required, err := definitiondb.FindPackage(requiredDef) required, err := definitiondb.FindPackage(requiredDef)
if err != nil { if err != nil {
packages, err := definitiondb.FindPackages(requiredDef) packages, err := definitiondb.FindPackages(requiredDef)

View File

@@ -144,6 +144,7 @@ func (assertions PackagesAssertions) Search(f string) *PackageAssert {
return nil return nil
} }
func (assertions PackagesAssertions) Order(definitiondb pkg.PackageDatabase, fingerprint string) PackagesAssertions { func (assertions PackagesAssertions) Order(definitiondb pkg.PackageDatabase, fingerprint string) PackagesAssertions {
orderedAssertions := PackagesAssertions{} orderedAssertions := PackagesAssertions{}
@@ -263,3 +264,18 @@ func (assertions PackagesAssertions) Drop(p pkg.Package) PackagesAssertions {
} }
return ass return ass
} }
// Cut returns an assertion list of installed (filter by Value) "cutted" until the package is found (included)
func (assertions PackagesAssertions) Cut(p pkg.Package) PackagesAssertions {
ass := PackagesAssertions{}
for _, a := range assertions {
if a.Value {
ass = append(ass, a)
if a.Package.Matches(p) {
break
}
}
}
return ass
}

View File

@@ -246,5 +246,31 @@ var _ = Describe("Decoder", func() {
Expect(solution.Order(dbDefinitions, Y.GetFingerPrint()).Drop(Y).AssertionHash() == solution2.Order(dbDefinitions, Z.GetFingerPrint()).Drop(Z).AssertionHash()).To(BeTrue()) Expect(solution.Order(dbDefinitions, Y.GetFingerPrint()).Drop(Y).AssertionHash() == solution2.Order(dbDefinitions, Z.GetFingerPrint()).Drop(Z).AssertionHash()).To(BeTrue())
}) })
It("Hashes them, Cuts them and could be used for comparison", func() {
X := pkg.NewPackage("X", "", []*pkg.DefaultPackage{}, []*pkg.DefaultPackage{})
Y := pkg.NewPackage("Y", "", []*pkg.DefaultPackage{X}, []*pkg.DefaultPackage{})
Z := pkg.NewPackage("Z", "", []*pkg.DefaultPackage{X}, []*pkg.DefaultPackage{})
for _, p := range []pkg.Package{X, Y, Z} {
_, err := dbDefinitions.CreatePackage(p)
Expect(err).ToNot(HaveOccurred())
}
for _, p := range []pkg.Package{} {
_, err := dbInstalled.CreatePackage(p)
Expect(err).ToNot(HaveOccurred())
}
solution, err := s.Install([]pkg.Package{Y})
Expect(err).ToNot(HaveOccurred())
solution2, err := s.Install([]pkg.Package{Z})
Expect(err).ToNot(HaveOccurred())
Expect(solution.Order(dbDefinitions, Y.GetFingerPrint()).Cut(Y).Drop(Y)).To(Equal(solution2.Order(dbDefinitions, Z.GetFingerPrint()).Cut(Z).Drop(Z)))
Expect(solution.Order(dbDefinitions, Y.GetFingerPrint()).Cut(Y).Drop(Y).AssertionHash()).To(Equal(solution2.Order(dbDefinitions, Z.GetFingerPrint()).Cut(Z).Drop(Z).AssertionHash()))
})
}) })
}) })