mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
add bleve index
This commit is contained in:
parent
1f08caa9ea
commit
ddd4a81bfa
34
cmd/root.go
34
cmd/root.go
@ -3,9 +3,10 @@ package cmd
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/anchore/imgbom/imgbom"
|
||||
"github.com/anchore/imgbom/imgbom/presenter"
|
||||
"github.com/anchore/imgbom/imgbom/pkg"
|
||||
"github.com/anchore/imgbom/internal"
|
||||
"github.com/anchore/stereoscope"
|
||||
"github.com/spf13/cobra"
|
||||
@ -63,11 +64,36 @@ func doRunCmd(cmd *cobra.Command, args []string) int {
|
||||
}
|
||||
|
||||
log.Info("Complete!")
|
||||
err = presenter.GetPresenter(appConfig.PresenterOpt).Present(os.Stdout, img, catalog)
|
||||
// err = presenter.GetPresenter(appConfig.PresenterOpt).Present(os.Stdout, img, catalog)
|
||||
// if err != nil {
|
||||
// log.Errorf("could not format catalog results: %w", err)
|
||||
// return 1
|
||||
// }
|
||||
result := catalog.SearchName("libselinux")
|
||||
fmt.Println(result)
|
||||
if result != nil {
|
||||
for _, hit := range result.Hits {
|
||||
pkgId, err := strconv.Atoi(hit.ID)
|
||||
if err != nil {
|
||||
log.Errorf("could not format catalog results: %w", err)
|
||||
return 1
|
||||
// TODO: just no...
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(pkgId, catalog.Package(pkg.ID(pkgId)), hit.Score)
|
||||
}
|
||||
}
|
||||
fmt.Println("------------------------------------------")
|
||||
|
||||
result = catalog.SearchMetadata("libselinux")
|
||||
fmt.Println(result)
|
||||
if result != nil {
|
||||
for _, hit := range result.Hits {
|
||||
pkgId, err := strconv.Atoi(hit.ID)
|
||||
if err != nil {
|
||||
// TODO: just no...
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(pkgId, catalog.Package(pkg.ID(pkgId)), hit.Score)
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
@ -1,10 +1,13 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/anchore/imgbom/internal/log"
|
||||
"github.com/anchore/stereoscope/pkg/file"
|
||||
"github.com/blevesearch/bleve"
|
||||
"github.com/blevesearch/bleve/mapping"
|
||||
)
|
||||
|
||||
// TODO: add reader methods (by type, id, fuzzy search, etc)
|
||||
@ -15,14 +18,31 @@ type Catalog struct {
|
||||
byID map[ID]*Package
|
||||
byType map[Type][]*Package
|
||||
byFile map[file.Reference][]*Package
|
||||
searchSpace mapping.IndexMapping
|
||||
nameSearchIndex bleve.Index
|
||||
metadataSearchIndex bleve.Index
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func NewCatalog() Catalog {
|
||||
searchMap := bleve.NewIndexMapping()
|
||||
nameIndex, err := bleve.NewMemOnly(searchMap)
|
||||
if err != nil {
|
||||
// TODO: log
|
||||
panic(err)
|
||||
}
|
||||
metadataIndex, err := bleve.NewMemOnly(searchMap)
|
||||
if err != nil {
|
||||
// TODO: log
|
||||
panic(err)
|
||||
}
|
||||
return Catalog{
|
||||
byID: make(map[ID]*Package),
|
||||
byType: make(map[Type][]*Package),
|
||||
byFile: make(map[file.Reference][]*Package),
|
||||
searchSpace: searchMap,
|
||||
nameSearchIndex: nameIndex,
|
||||
metadataSearchIndex: metadataIndex,
|
||||
}
|
||||
}
|
||||
|
||||
@ -63,6 +83,38 @@ func (c *Catalog) Add(p Package) {
|
||||
}
|
||||
c.byFile[s] = append(c.byFile[s], &p)
|
||||
}
|
||||
|
||||
// index the package findings
|
||||
err := c.nameSearchIndex.Index(fmt.Sprintf("%d", p.id), p.Name)
|
||||
if err != nil {
|
||||
// TODO: just no...
|
||||
panic(err)
|
||||
}
|
||||
err = c.metadataSearchIndex.Index(fmt.Sprintf("%d", p.id), p.Metadata)
|
||||
if err != nil {
|
||||
// TODO: just no...
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Catalog) SearchMetadata(query string) *bleve.SearchResult {
|
||||
request := bleve.NewSearchRequest(bleve.NewMatchQuery(query))
|
||||
result, err := c.metadataSearchIndex.Search(request)
|
||||
if err != nil {
|
||||
// TODO: just no...
|
||||
panic(err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (c *Catalog) SearchName(query string) *bleve.SearchResult {
|
||||
request := bleve.NewSearchRequest(bleve.NewMatchQuery(query))
|
||||
result, err := c.nameSearchIndex.Search(request)
|
||||
if err != nil {
|
||||
// TODO: just no...
|
||||
panic(err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (c *Catalog) Enumerate(types ...Type) <-chan *Package {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user