Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 28 additions & 23 deletions syft/pkg/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ type Collection struct {

// NewCollection returns a new empty Collection
func NewCollection(pkgs ...Package) *Collection {
catalog := Collection{
c := Collection{
byID: make(map[artifact.ID]Package),
idsByName: make(map[string]orderedIDSet),
idsByType: make(map[Type]orderedIDSet),
idsByPath: make(map[string]orderedIDSet),
}

for _, p := range pkgs {
catalog.Add(p)
c.Add(p)
}

return &catalog
return &c
}

// PackageCount returns the total number of packages that have been added.
Expand Down Expand Up @@ -97,32 +97,37 @@ func (c *Collection) packages(ids []artifact.ID) (result []Package) {
return result
}

// Add n packages to the catalog.
// Add n packages to the collection.
func (c *Collection) Add(pkgs ...Package) {
for _, p := range pkgs {
c.add(p)
}
}

// Add a package to the Collection.
func (c *Collection) add(p Package) {
c.lock.Lock()
defer c.lock.Unlock()

for _, p := range pkgs {
id := p.ID()
if id == "" {
log.Warnf("found package with empty ID while adding to the catalog: %+v", p)
p.SetID()
id = p.ID()
}
id := p.ID()
if id == "" {
log.Warnf("found package with empty ID while adding to the collection: %+v", p)
p.SetID()
id = p.ID()
}

if existing, exists := c.byID[id]; exists {
// there is already a package with this fingerprint merge the existing record with the new one
if err := existing.merge(p); err != nil {
log.Warnf("failed to merge packages: %+v", err)
} else {
c.byID[id] = existing
c.addPathsToIndex(p)
}
return
if existing, exists := c.byID[id]; exists {
// there is already a package with this fingerprint merge the existing record with the new one
if err := existing.merge(p); err != nil {
log.Warnf("failed to merge packages: %+v", err)
} else {
c.byID[id] = existing
c.addPathsToIndex(p)
}

c.addToIndex(p)
return
}

c.addToIndex(p)
}

func (c *Collection) addToIndex(p Package) {
Expand Down Expand Up @@ -242,7 +247,7 @@ func (c *Collection) Enumerate(types ...Type) <-chan Package {
defer close(channel)

if c == nil {
// we should allow enumerating from a catalog that was never created (which will result in no packages enumerated)
// we should allow enumerating from a collection that was never created (which will result in no packages enumerated)
return
}

Expand Down