with package id

This commit is contained in:
Alex Goodman
2020-05-29 16:06:31 -04:00
parent dcba50eaa8
commit a0e48468c2
13 changed files with 96 additions and 34 deletions
+55 -11
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),
func NewCatalog() *Catalog {
return &Catalog{
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:
+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)
}
+2 -2
View File
@@ -5,7 +5,7 @@ const (
ApkPkg
DebPkg
JavaPkg
NodePkg
JavaScriptPkg
PacmanPkg
PythonPkg
RpmPkg
@@ -19,7 +19,7 @@ var typeStr = []string{
"apk",
"deb",
"java",
"node",
"javascript",
"pacman",
"python",
"rpm",