Skip to content

Commit 303ed96

Browse files
authored
Merge pull request #174 from aszady/bestkey/arch
Consider the architecture when choosing the best package variant
2 parents a2033ba + 2f1c06e commit 303ed96

File tree

4 files changed

+51
-15
lines changed

4 files changed

+51
-15
lines changed

pkg/sat/loader.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sat
22

33
import (
4+
"cmp"
45
"fmt"
56
"regexp"
67
"sort"
@@ -22,12 +23,32 @@ type Loader struct {
2223
varsCount int
2324
}
2425

26+
// BestKey groups packages for the purpose of `--nobest` option disabled,
27+
// so that for each set of packages sharing this key, only the "best" package will be preserved
28+
// (best in terms of repo priority, version criteria).
29+
type BestKey struct {
30+
name string
31+
arch string
32+
}
33+
34+
// CompareBestKey provides an arbitrary, deterministic, total order on BestKey
35+
func CompareBestKey(k1 BestKey, k2 BestKey) int {
36+
return cmp.Or(
37+
cmp.Compare(k1.name, k2.name),
38+
cmp.Compare(k1.arch, k2.arch),
39+
)
40+
}
41+
42+
func MakeBestKey(pkg *api.Package) BestKey {
43+
return BestKey{name: pkg.Name, arch: pkg.Arch}
44+
}
45+
2546
func NewLoader() *Loader {
2647
return &Loader{
2748
m: &Model{
2849
packages: map[string][]*Var{},
2950
vars: map[string]*Var{},
30-
bestPackages: map[string]*api.Package{},
51+
bestPackages: map[BestKey]*api.Package{},
3152
forceIgnoreWithDependencies: map[api.PackageKey]*api.Package{},
3253
},
3354
provides: map[string][]*Var{},
@@ -97,17 +118,18 @@ func (loader *Loader) Load(packages []*api.Package, matched, ignoreRegex, allowR
97118

98119
// Create an index to pick the best candidates
99120
for _, pkg := range packages {
100-
if loader.m.bestPackages[pkg.Name] == nil {
101-
loader.m.bestPackages[pkg.Name] = pkg
102-
} else if rpm.ComparePackage(pkg, loader.m.bestPackages[pkg.Name], archOrder) > 0 {
103-
loader.m.bestPackages[pkg.Name] = pkg
121+
key := MakeBestKey(pkg)
122+
if loader.m.bestPackages[key] == nil {
123+
loader.m.bestPackages[key] = pkg
124+
} else if rpm.ComparePackage(pkg, loader.m.bestPackages[key], archOrder) > 0 {
125+
loader.m.bestPackages[key] = pkg
104126
}
105127
}
106128

107129
if !nobest {
108130
packages = nil
109131
bestPackagesKeys := maps.Keys(loader.m.bestPackages)
110-
slices.Sort(bestPackagesKeys)
132+
slices.SortFunc(bestPackagesKeys, CompareBestKey)
111133
for _, v := range bestPackagesKeys {
112134
packages = append(packages, loader.m.bestPackages[v])
113135
}

pkg/sat/loader_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func expectedBest(g *WithT, m *Model, pkgVersions map[string]string) {
3838
g.Expect(m.bestPackages).To(HaveLen(len(pkgVersions)))
3939

4040
for k, version := range pkgVersions {
41-
g.Expect(m.bestPackages[k].Version.String()).To(Equal(version))
41+
g.Expect(m.bestPackages[BestKey{name: k}].Version.String()).To(Equal(version))
4242
}
4343
}
4444

pkg/sat/sat.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type Model struct {
5454
// vars contain as key an exact identifier for a provided resource and the actual SAT variable as value
5555
vars map[string]*Var
5656

57-
bestPackages map[string]*api.Package
57+
bestPackages map[BestKey]*api.Package
5858

5959
ands []bf.Formula
6060
forceIgnoreWithDependencies map[api.PackageKey]*api.Package
@@ -68,8 +68,8 @@ func (m *Model) Var(v string) *Var {
6868
return m.vars[v]
6969
}
7070

71-
func (m *Model) BestPackage(p string) *api.Package {
72-
return m.bestPackages[p]
71+
func (m *Model) BestPackage(k BestKey) *api.Package {
72+
return m.bestPackages[k]
7373
}
7474

7575
func (m *Model) Ands() bf.Formula {
@@ -186,8 +186,10 @@ func Resolve(model *Model) (install []*api.Package, excluded []*api.Package, for
186186
}
187187
}
188188
for _, v := range installMap {
189-
if rpm.Compare(model.BestPackage(v.Name).Version, v.Version) != 0 {
190-
logrus.Infof("Picking %v instead of best candiate %v", v, model.BestPackage(v.Name))
189+
key := MakeBestKey(v)
190+
bestPackage := model.BestPackage(key)
191+
if bestPackage != v {
192+
logrus.Infof("Picking %v instead of best candiate %v", v, bestPackage)
191193
}
192194
install = append(install, v)
193195
}

pkg/sat/sat_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ func TestNewResolver(t *testing.T) {
14291429
"testa",
14301430
},
14311431
install: []string{"testa-0:2.x86_64"},
1432-
exclude: []string{},
1432+
exclude: []string{"testa-0:3.noarch"},
14331433
solvable: true,
14341434
},
14351435
{name: "prioritize dependency: best arch & version", packages: []*api.Package{
@@ -1443,7 +1443,7 @@ func TestNewResolver(t *testing.T) {
14431443
"testa",
14441444
},
14451445
install: []string{"testa-0:1.noarch", "testb-0:2.x86_64"},
1446-
exclude: []string{},
1446+
exclude: []string{"testb-0:3.noarch"},
14471447
solvable: true,
14481448
},
14491449
{name: "cross-arch dependency (by name and by resource)", packages: []*api.Package{
@@ -1478,9 +1478,21 @@ func TestNewResolver(t *testing.T) {
14781478
"testa",
14791479
},
14801480
install: []string{"testa-0:1.x86_64", "testb-0:1.x86_64"},
1481-
exclude: []string{},
1481+
exclude: []string{"testa-0:1.noarch", "testb-0:1.noarch"},
14821482
solvable: true,
14831483
},
1484+
{name: "dependency on non-primary architecture", packages: []*api.Package{
1485+
newPkgAP("testa", "1", "noarch", 1, []string{}, []string{"/usr/lib/libb.so"}, []string{}),
1486+
newPkgAP("testb", "1", "x86_64", 1, []string{"/usr/lib64/libb.so"}, []string{}, []string{}),
1487+
newPkgAP("testb", "1", "i686", 1, []string{"/usr/lib/libb.so"}, []string{}, []string{}),
1488+
}, requires: []string{
1489+
"testa",
1490+
},
1491+
architectures: []string{"x86_64", "i686"},
1492+
install: []string{"testa-0:1.noarch", "testb-0:1.i686"},
1493+
exclude: []string{"testb-0:1.x86_64"},
1494+
solvable: true,
1495+
},
14841496

14851497
// TODO: Add test cases.
14861498
}

0 commit comments

Comments
 (0)