with package id

This commit is contained in:
Alex Goodman
2020-05-29 11:30:26 -04:00
parent dcba50eaa8
commit 1f08caa9ea
7 changed files with 139 additions and 14 deletions
+53 -9
View File
@@ -1,31 +1,75 @@
package pkg
import (
"sync"
"github.com/anchore/imgbom/internal/log"
"github.com/anchore/stereoscope/pkg/file"
)
// TODO: add reader methods (by type, id, fuzzy search, etc)
var nextPackageID int64
type Catalog struct {
// TODO: catalog by package ID for potential indexing
packages map[Type][]Package
byID map[ID]*Package
byType map[Type][]*Package
byFile map[file.Reference][]*Package
lock sync.RWMutex
}
func NewCatalog() Catalog {
return Catalog{
packages: make(map[Type][]Package),
byID: make(map[ID]*Package),
byType: make(map[Type][]*Package),
byFile: make(map[file.Reference][]*Package),
}
}
func (c *Catalog) Package(id ID) *Package {
return c.byID[id]
}
func (c *Catalog) PackagesByFile(ref file.Reference) []*Package {
return c.byFile[ref]
}
func (c *Catalog) Add(p Package) {
_, ok := c.packages[p.Type]
if p.id != 0 {
log.Errorf("package already added to catalog: %s", p)
return
}
c.lock.Lock()
defer c.lock.Unlock()
p.id = ID(nextPackageID)
nextPackageID++
// store by package ID
c.byID[p.id] = &p
// store by package type
_, ok := c.byType[p.Type]
if !ok {
c.packages[p.Type] = make([]Package, 0)
c.byType[p.Type] = make([]*Package, 0)
}
c.byType[p.Type] = append(c.byType[p.Type], &p)
// store by file references
for _, s := range p.Source {
_, ok := c.byFile[s]
if !ok {
c.byFile[s] = make([]*Package, 0)
}
c.byFile[s] = append(c.byFile[s], &p)
}
c.packages[p.Type] = append(c.packages[p.Type], p)
}
func (c *Catalog) Enumerate(types ...Type) <-chan Package {
channel := make(chan Package)
func (c *Catalog) Enumerate(types ...Type) <-chan *Package {
channel := make(chan *Package)
go func() {
defer close(channel)
for ty, packages := range c.packages {
for ty, packages := range c.byType {
if len(types) != 0 {
found := false
typeCheck:
+1
View File
@@ -17,4 +17,5 @@ type DpkgMetadata struct {
SuggestsPkgs string `mapstructure:"Suggests"`
Version string `mapstructure:"Version"`
ConfigFiles string `mapstructure:"Conffiles"`
Description string `mapstructure:"Description"`
}
+14 -3
View File
@@ -1,11 +1,16 @@
package pkg
import "github.com/anchore/stereoscope/pkg/file"
import (
"fmt"
// TODO: add package ID (random/incremental)
"github.com/anchore/stereoscope/pkg/file"
)
type ID int64
// TODO: add field to trace which analyzer detected this
type Package struct {
id ID
Name string
Version string
Source []file.Reference
@@ -14,4 +19,10 @@ type Package struct {
Metadata interface{}
}
// TODO: stringer...
func (p Package) ID() ID {
return p.id
}
func (p Package) String() string {
return fmt.Sprintf("Pkg(type=%s, name=%s, version=%s)", p.Type, p.Name, p.Version)
}
+1 -1
View File
@@ -5,7 +5,7 @@ const (
ApkPkg
DebPkg
JavaPkg
NodePkg
JavaScriptPkg
PacmanPkg
PythonPkg
RpmPkg