use read lock in pkg collection (#2341)

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
Alex Goodman 2023-11-21 13:48:25 -05:00 committed by GitHub
parent 4712246897
commit 8ee209a5ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 2 deletions

View File

@ -37,11 +37,17 @@ func NewCollection(pkgs ...Package) *Collection {
// PackageCount returns the total number of packages that have been added. // PackageCount returns the total number of packages that have been added.
func (c *Collection) PackageCount() int { func (c *Collection) PackageCount() int {
c.lock.RLock()
defer c.lock.RUnlock()
return len(c.byID) return len(c.byID)
} }
// Package returns the package with the given ID. // Package returns the package with the given ID.
func (c *Collection) Package(id artifact.ID) *Package { func (c *Collection) Package(id artifact.ID) *Package {
c.lock.RLock()
defer c.lock.RUnlock()
v, exists := c.byID[id] v, exists := c.byID[id]
if !exists { if !exists {
return nil return nil
@ -57,16 +63,31 @@ func (c *Collection) Package(id artifact.ID) *Package {
// PackagesByPath returns all packages that were discovered from the given path. // PackagesByPath returns all packages that were discovered from the given path.
func (c *Collection) PackagesByPath(path string) []Package { 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. // PackagesByName returns all packages that were discovered with a matching name.
func (c *Collection) PackagesByName(name string) []Package { 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. // Packages returns all packages for the given ID.
func (c *Collection) Packages(ids []artifact.ID) (result []Package) { 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 { for _, i := range ids {
p, exists := c.byID[i] p, exists := c.byID[i]
if exists { if exists {
@ -105,6 +126,8 @@ func (c *Collection) Add(pkgs ...Package) {
} }
func (c *Collection) addToIndex(p Package) { func (c *Collection) addToIndex(p Package) {
// note: write lock must be held by caller
c.byID[p.id] = p c.byID[p.id] = p
c.addNameToIndex(p) c.addNameToIndex(p)
c.addTypeToIndex(p) c.addTypeToIndex(p)
@ -112,18 +135,24 @@ func (c *Collection) addToIndex(p Package) {
} }
func (c *Collection) addNameToIndex(p Package) { func (c *Collection) addNameToIndex(p Package) {
// note: write lock must be held by caller
nameIndex := c.idsByName[p.Name] nameIndex := c.idsByName[p.Name]
nameIndex.add(p.id) nameIndex.add(p.id)
c.idsByName[p.Name] = nameIndex c.idsByName[p.Name] = nameIndex
} }
func (c *Collection) addTypeToIndex(p Package) { func (c *Collection) addTypeToIndex(p Package) {
// note: write lock must be held by caller
typeIndex := c.idsByType[p.Type] typeIndex := c.idsByType[p.Type]
typeIndex.add(p.id) typeIndex.add(p.id)
c.idsByType[p.Type] = typeIndex c.idsByType[p.Type] = typeIndex
} }
func (c *Collection) addPathsToIndex(p Package) { func (c *Collection) addPathsToIndex(p Package) {
// note: write lock must be held by caller
observedPaths := strset.New() observedPaths := strset.New()
for _, l := range p.Locations.ToSlice() { for _, l := range p.Locations.ToSlice() {
if l.RealPath != "" && !observedPaths.Has(l.RealPath) { if l.RealPath != "" && !observedPaths.Has(l.RealPath) {
@ -138,6 +167,8 @@ func (c *Collection) addPathsToIndex(p Package) {
} }
func (c *Collection) addPathToIndex(id artifact.ID, path string) { func (c *Collection) addPathToIndex(id artifact.ID, path string) {
// note: write lock must be held by caller
pathIndex := c.idsByPath[path] pathIndex := c.idsByPath[path]
pathIndex.add(id) pathIndex.add(id)
c.idsByPath[path] = pathIndex c.idsByPath[path] = pathIndex
@ -161,18 +192,24 @@ func (c *Collection) Delete(ids ...artifact.ID) {
} }
func (c *Collection) deleteNameFromIndex(p Package) { func (c *Collection) deleteNameFromIndex(p Package) {
// note: write lock must be held by caller
nameIndex := c.idsByName[p.Name] nameIndex := c.idsByName[p.Name]
nameIndex.delete(p.id) nameIndex.delete(p.id)
c.idsByName[p.Name] = nameIndex c.idsByName[p.Name] = nameIndex
} }
func (c *Collection) deleteTypeFromIndex(p Package) { func (c *Collection) deleteTypeFromIndex(p Package) {
// note: write lock must be held by caller
typeIndex := c.idsByType[p.Type] typeIndex := c.idsByType[p.Type]
typeIndex.delete(p.id) typeIndex.delete(p.id)
c.idsByType[p.Type] = typeIndex c.idsByType[p.Type] = typeIndex
} }
func (c *Collection) deletePathsFromIndex(p Package) { func (c *Collection) deletePathsFromIndex(p Package) {
// note: write lock must be held by caller
observedPaths := strset.New() observedPaths := strset.New()
for _, l := range p.Locations.ToSlice() { for _, l := range p.Locations.ToSlice() {
if l.RealPath != "" && !observedPaths.Has(l.RealPath) { if l.RealPath != "" && !observedPaths.Has(l.RealPath) {
@ -187,6 +224,8 @@ func (c *Collection) deletePathsFromIndex(p Package) {
} }
func (c *Collection) deletePathFromIndex(id artifact.ID, path string) { func (c *Collection) deletePathFromIndex(id artifact.ID, path string) {
// note: write lock must be held by caller
pathIndex := c.idsByPath[path] pathIndex := c.idsByPath[path]
pathIndex.delete(id) pathIndex.delete(id)
if len(pathIndex.slice) == 0 { if len(pathIndex.slice) == 0 {
@ -201,10 +240,15 @@ func (c *Collection) Enumerate(types ...Type) <-chan Package {
channel := make(chan Package) channel := make(chan Package)
go func() { go func() {
defer close(channel) defer close(channel)
if c == nil { 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 catalog that was never created (which will result in no packages enumerated)
return return
} }
c.lock.RLock()
defer c.lock.RUnlock()
for ty, ids := range c.idsByType { for ty, ids := range c.idsByType {
if len(types) != 0 { if len(types) != 0 {
found := false found := false