snapshot: explicity call FilterLatestRefs() where needed rather than calling from Merge()

This commit is contained in:
Ryan Uber
2014-04-22 22:21:38 -07:00
parent d9f8673286
commit 385ac1afd0
3 changed files with 26 additions and 32 deletions

View File

@@ -27,7 +27,7 @@ func aptlyDbCleanup(cmd *commander.Command, args []string) error {
return err
}
if repo.RefList() != nil {
existingPackageRefs = existingPackageRefs.Merge(repo.RefList(), false, false)
existingPackageRefs = existingPackageRefs.Merge(repo.RefList(), false)
}
return nil
})
@@ -41,7 +41,7 @@ func aptlyDbCleanup(cmd *commander.Command, args []string) error {
return err
}
if repo.RefList() != nil {
existingPackageRefs = existingPackageRefs.Merge(repo.RefList(), false, false)
existingPackageRefs = existingPackageRefs.Merge(repo.RefList(), false)
}
return nil
})
@@ -54,7 +54,7 @@ func aptlyDbCleanup(cmd *commander.Command, args []string) error {
if err != nil {
return err
}
existingPackageRefs = existingPackageRefs.Merge(snapshot.RefList(), false, false)
existingPackageRefs = existingPackageRefs.Merge(snapshot.RefList(), false)
return nil
})
if err != nil {

View File

@@ -29,15 +29,16 @@ func aptlySnapshotMerge(cmd *commander.Command, args []string) error {
}
latest := context.flags.Lookup("latest").Value.Get().(bool)
overrideMatching := !latest
result := sources[0].RefList()
for i := 1; i < len(sources); i++ {
if latest {
result = result.Merge(sources[i].RefList(), false, true)
} else {
result = result.Merge(sources[i].RefList(), true, false)
}
result = result.Merge(sources[i].RefList(), overrideMatching)
}
if latest {
result = deb.FilterLatestRefs(result)
}
sourceDescription := make([]string, len(sources))

View File

@@ -220,12 +220,9 @@ func (l *PackageRefList) Diff(r *PackageRefList, packageCollection *PackageColle
}
// Merge merges reflist r into current reflist. If overrideMatching, merge
// replaces matching packages (by architecture/name) with reference from r. If
// latestWins, compare versions between common packages and take the latest from
// the set. Otherwise, all packages are saved.
func (l *PackageRefList) Merge(r *PackageRefList, overrideMatching bool,
latestWins bool) (result *PackageRefList) {
// replaces matching packages (by architecture/name) with reference from r.
// Otherwise, all packages are saved.
func (l *PackageRefList) Merge(r *PackageRefList, overrideMatching bool) (result *PackageRefList) {
// pointer to left and right reflists
il, ir := 0, 0
// length of reflists
@@ -285,24 +282,18 @@ func (l *PackageRefList) Merge(r *PackageRefList, overrideMatching bool,
}
}
// Filter results down to the latest packages only if requested
if latestWins {
result = FilterLatestPackages(result)
}
return
}
// FilterLatestPackages takes in a reflist with potentially multiples of the
// same packages and returns a reflist containing only the latest of each
// package. This implements a "latest wins" approach which can be used while
// merging two or more snapshots together.
func FilterLatestPackages(r *PackageRefList) *PackageRefList {
// FilterLatestRefs takes in a reflist with potentially multiples of the same
// packages and returns a reflist containing only the latest of each package.
// This implements a "latest wins" approach which can be used while merging two
// or more snapshots together.
func FilterLatestRefs(r *PackageRefList) *PackageRefList {
// A running tab of latest seen package refs.
latestRefs := make(map[string]int)
i := 0
for _ = range r.Refs {
for i := 0; i < len(r.Refs); i++ {
partsL := bytes.Split(r.Refs[i], []byte(" "))
archL, nameL, verL := partsL[0][1:], partsL[1], partsL[2]
pkgId := string(nameL) + "." + string(archL)
@@ -310,7 +301,6 @@ func FilterLatestPackages(r *PackageRefList) *PackageRefList {
// If the package hasn't been seen before, add it and advance.
if _, ok := latestRefs[pkgId]; !ok {
latestRefs[pkgId] = i
i++
continue
}
@@ -320,13 +310,16 @@ func FilterLatestPackages(r *PackageRefList) *PackageRefList {
vres := CompareVersions(string(verL), string(verR))
// Remove the older or duplicate refs from the result
if vres <= 0 {
r.Refs = append(r.Refs[0:i], r.Refs[i+1:]...)
latestRefs[pkgId] = i
if vres > 0 {
old := latestRefs[pkgId]
r.Refs = append(r.Refs[0:old], r.Refs[old+1:]...)
latestRefs[pkgId] = i - 1
} else {
oi := latestRefs[pkgId]
r.Refs = append(r.Refs[0:oi], r.Refs[oi+1:]...)
r.Refs = append(r.Refs[0:i], r.Refs[i+1:]...)
}
// Compensate for the reduced set
i -= 1
}
return r