(fix): delete collection name/type key entries when empty (#3797)

Signed-off-by: Adam McClenaghan <adam@mcclenaghan.co.uk>
This commit is contained in:
Adam McClenaghan 2025-04-21 18:41:39 +01:00 committed by GitHub
parent 0bcf2881c4
commit 1f15361ecf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 2 deletions

View File

@ -201,7 +201,11 @@ func (c *Collection) deleteNameFromIndex(p Package) {
nameIndex := c.idsByName[p.Name]
nameIndex.delete(p.id)
c.idsByName[p.Name] = nameIndex
if len(nameIndex.slice) == 0 {
delete(c.idsByName, p.Name)
} else {
c.idsByName[p.Name] = nameIndex
}
}
func (c *Collection) deleteTypeFromIndex(p Package) {
@ -209,7 +213,11 @@ func (c *Collection) deleteTypeFromIndex(p Package) {
typeIndex := c.idsByType[p.Type]
typeIndex.delete(p.id)
c.idsByType[p.Type] = typeIndex
if len(typeIndex.slice) == 0 {
delete(c.idsByType, p.Type)
} else {
c.idsByType[p.Type] = typeIndex
}
}
func (c *Collection) deletePathsFromIndex(p Package) {

View File

@ -184,6 +184,24 @@ func TestCatalogDeleteRemovesPackages(t *testing.T) {
},
},
},
{
name: "delete idsBy key entries when all deleted",
pkgs: []Package{
{
id: artifact.ID("pkg:deb/debian/1"),
Name: "debian",
Version: "1",
Type: DebPkg,
Locations: file.NewLocationSet(
file.NewVirtualLocation("/c/path", "/another/path1"),
),
},
},
deleteIDs: []artifact.ID{
artifact.ID("pkg:deb/debian/1"),
},
expectedIndexes: expectedIndexes{},
},
}
for _, test := range tests {