Skip to content

Commit

Permalink
use read lock in pkg collection (#2341)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <[email protected]>
  • Loading branch information
wagoodman authored Nov 21, 2023
1 parent 4712246 commit 8ee209a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions syft/pkg/catalog.go → syft/pkg/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ func NewCollection(pkgs ...Package) *Collection {

// PackageCount returns the total number of packages that have been added.
func (c *Collection) PackageCount() int {
c.lock.RLock()
defer c.lock.RUnlock()

return len(c.byID)
}

// Package returns the package with the given ID.
func (c *Collection) Package(id artifact.ID) *Package {
c.lock.RLock()
defer c.lock.RUnlock()

v, exists := c.byID[id]
if !exists {
return nil
Expand All @@ -57,16 +63,31 @@ func (c *Collection) Package(id artifact.ID) *Package {

// PackagesByPath returns all packages that were discovered from the given path.
func (c *Collection) PackagesByPath(path string) []Package {
return c.Packages(c.idsByPath[path].slice)
c.lock.RLock()
defer c.lock.RUnlock()

return c.packages(c.idsByPath[path].slice)
}

// PackagesByName returns all packages that were discovered with a matching name.
func (c *Collection) PackagesByName(name string) []Package {
return c.Packages(c.idsByName[name].slice)
c.lock.RLock()
defer c.lock.RUnlock()

return c.packages(c.idsByName[name].slice)
}

// Packages returns all packages for the given ID.
func (c *Collection) Packages(ids []artifact.ID) (result []Package) {
c.lock.RLock()
defer c.lock.RUnlock()

return c.packages(ids)
}

func (c *Collection) packages(ids []artifact.ID) (result []Package) {
// note: read lock must be held by caller

for _, i := range ids {
p, exists := c.byID[i]
if exists {
Expand Down Expand Up @@ -105,25 +126,33 @@ func (c *Collection) Add(pkgs ...Package) {
}

func (c *Collection) addToIndex(p Package) {
// note: write lock must be held by caller

c.byID[p.id] = p
c.addNameToIndex(p)
c.addTypeToIndex(p)
c.addPathsToIndex(p)
}

func (c *Collection) addNameToIndex(p Package) {
// note: write lock must be held by caller

nameIndex := c.idsByName[p.Name]
nameIndex.add(p.id)
c.idsByName[p.Name] = nameIndex
}

func (c *Collection) addTypeToIndex(p Package) {
// note: write lock must be held by caller

typeIndex := c.idsByType[p.Type]
typeIndex.add(p.id)
c.idsByType[p.Type] = typeIndex
}

func (c *Collection) addPathsToIndex(p Package) {
// note: write lock must be held by caller

observedPaths := strset.New()
for _, l := range p.Locations.ToSlice() {
if l.RealPath != "" && !observedPaths.Has(l.RealPath) {
Expand All @@ -138,6 +167,8 @@ func (c *Collection) addPathsToIndex(p Package) {
}

func (c *Collection) addPathToIndex(id artifact.ID, path string) {
// note: write lock must be held by caller

pathIndex := c.idsByPath[path]
pathIndex.add(id)
c.idsByPath[path] = pathIndex
Expand All @@ -161,18 +192,24 @@ func (c *Collection) Delete(ids ...artifact.ID) {
}

func (c *Collection) deleteNameFromIndex(p Package) {
// note: write lock must be held by caller

nameIndex := c.idsByName[p.Name]
nameIndex.delete(p.id)
c.idsByName[p.Name] = nameIndex
}

func (c *Collection) deleteTypeFromIndex(p Package) {
// note: write lock must be held by caller

typeIndex := c.idsByType[p.Type]
typeIndex.delete(p.id)
c.idsByType[p.Type] = typeIndex
}

func (c *Collection) deletePathsFromIndex(p Package) {
// note: write lock must be held by caller

observedPaths := strset.New()
for _, l := range p.Locations.ToSlice() {
if l.RealPath != "" && !observedPaths.Has(l.RealPath) {
Expand All @@ -187,6 +224,8 @@ func (c *Collection) deletePathsFromIndex(p Package) {
}

func (c *Collection) deletePathFromIndex(id artifact.ID, path string) {
// note: write lock must be held by caller

pathIndex := c.idsByPath[path]
pathIndex.delete(id)
if len(pathIndex.slice) == 0 {
Expand All @@ -201,10 +240,15 @@ func (c *Collection) Enumerate(types ...Type) <-chan Package {
channel := make(chan Package)
go func() {
defer close(channel)

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

c.lock.RLock()
defer c.lock.RUnlock()

for ty, ids := range c.idsByType {
if len(types) != 0 {
found := false
Expand Down
File renamed without changes.

0 comments on commit 8ee209a

Please sign in to comment.