github.com/quay/claircore@v1.5.28/linux/packagesearcher.go (about) 1 package linux 2 3 import ( 4 "github.com/quay/claircore" 5 "github.com/quay/claircore/indexer" 6 ) 7 8 // PackageSearcher tracks the layer's hash and index a package 9 // was introduced in. 10 type PackageSearcher struct { 11 m map[string]entry 12 } 13 14 // an entry mapped to a package's key. 15 // tracks package hash and index pkg was introduced in. 16 type entry struct { 17 digest *claircore.Digest 18 index int 19 } 20 21 // creates a unique key in the package searcher's map 22 func keyify(pkg *claircore.Package) string { 23 return pkg.Name + pkg.PackageDB + pkg.Version 24 } 25 26 // NewPackageSearcher contructs a PackageSearcher ready for its Search method 27 // to be called 28 func NewPackageSearcher(layerArtifacts []*indexer.LayerArtifacts) PackageSearcher { 29 m := make(map[string]entry, 0) 30 for i, artifacts := range layerArtifacts { 31 if len(artifacts.Pkgs) == 0 { 32 continue 33 } 34 35 for _, pkg := range artifacts.Pkgs { 36 key := keyify(pkg) 37 if _, ok := m[key]; !ok { 38 m[key] = entry{ 39 &artifacts.Hash, 40 i, 41 } 42 } 43 } 44 45 } 46 return PackageSearcher{m} 47 } 48 49 // Search returns the layer hash and index a package was introduced in. 50 func (pi *PackageSearcher) Search(pkg *claircore.Package) (*claircore.Digest, int, error) { 51 key := keyify(pkg) 52 entry, ok := pi.m[key] 53 if !ok { 54 return nil, 0, nil 55 } 56 57 return entry.digest, entry.index, nil 58 }