mirror of
https://github.com/Jguer/yay.git
synced 2025-10-10 00:03:58 -04:00
correct nits from metadata package and remove uneeded functionality
This commit is contained in:
parent
5aeb0d696c
commit
d7851223c6
@ -94,6 +94,8 @@ type Grapher struct {
|
|||||||
fullGraph bool // If true, the graph will include all dependencies including already installed ones or repo
|
fullGraph bool // If true, the graph will include all dependencies including already installed ones or repo
|
||||||
noConfirm bool
|
noConfirm bool
|
||||||
w io.Writer // output writer
|
w io.Writer // output writer
|
||||||
|
|
||||||
|
providerCache map[string]*aur.Pkg
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGrapher(dbExecutor db.Executor, aurCache *metadata.AURCache,
|
func NewGrapher(dbExecutor db.Executor, aurCache *metadata.AURCache,
|
||||||
@ -105,6 +107,7 @@ func NewGrapher(dbExecutor db.Executor, aurCache *metadata.AURCache,
|
|||||||
fullGraph: fullGraph,
|
fullGraph: fullGraph,
|
||||||
noConfirm: noConfirm,
|
noConfirm: noConfirm,
|
||||||
w: output,
|
w: output,
|
||||||
|
providerCache: make(map[string]*aurc.Pkg, 5),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -326,11 +329,27 @@ func (g *Grapher) addNodes(
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if aurPkgs, _ := g.aurCache.FindPackage(ctx, depName); len(aurPkgs) != 0 { // Check AUR
|
var aurPkgs []*aur.Pkg
|
||||||
|
if cachedProvidePkg, ok := g.providerCache[depName]; ok {
|
||||||
|
aurPkgs = []*aur.Pkg{cachedProvidePkg}
|
||||||
|
} else {
|
||||||
|
var errMeta error
|
||||||
|
aurPkgs, errMeta = g.aurCache.Get(ctx,
|
||||||
|
&metadata.AURQuery{
|
||||||
|
Needles: []string{depName},
|
||||||
|
By: aurc.None,
|
||||||
|
Contains: false,
|
||||||
|
})
|
||||||
|
if errMeta != nil {
|
||||||
|
text.Warnln("AUR cache error:", errMeta)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(aurPkgs) != 0 { // Check AUR
|
||||||
pkg := aurPkgs[0]
|
pkg := aurPkgs[0]
|
||||||
if len(aurPkgs) > 1 {
|
if len(aurPkgs) > 1 {
|
||||||
pkg = provideMenu(g.w, depName, aurPkgs, g.noConfirm)
|
pkg = provideMenu(g.w, depName, aurPkgs, g.noConfirm)
|
||||||
g.aurCache.SetProvideCache(depName, []*aur.Pkg{pkg})
|
g.providerCache[depName] = pkg
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := graph.DependOn(pkg.Name, parentPkgName); err != nil {
|
if err := graph.DependOn(pkg.Name, parentPkgName); err != nil {
|
||||||
|
@ -2,9 +2,7 @@ package metadata
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -14,17 +12,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
searchCacheCap = 300
|
|
||||||
cacheValidity = 1 * time.Hour
|
cacheValidity = 1 * time.Hour
|
||||||
)
|
)
|
||||||
|
|
||||||
type AURCache struct {
|
type AURCache struct {
|
||||||
cache []byte
|
cache []byte
|
||||||
searchCache map[string][]*aur.Pkg
|
|
||||||
cachePath string
|
cachePath string
|
||||||
unmarshalledCache []interface{}
|
unmarshalledCache []interface{}
|
||||||
cacheHits int
|
|
||||||
gojqCode *gojq.Code
|
|
||||||
DebugLoggerFn func(a ...interface{})
|
DebugLoggerFn func(a ...interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,18 +33,20 @@ func NewAURCache(cachePath string) (*AURCache, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
inputStruct, err := oj.Parse(aurCache)
|
inputStruct, err := oj.Parse(aurCache)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("aur metadata unable to parse cache: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return &AURCache{
|
return &AURCache{
|
||||||
cache: aurCache,
|
cache: aurCache,
|
||||||
cachePath: cachePath,
|
cachePath: cachePath,
|
||||||
searchCache: make(map[string][]*aur.Pkg, searchCacheCap),
|
|
||||||
unmarshalledCache: inputStruct.([]interface{}),
|
unmarshalledCache: inputStruct.([]interface{}),
|
||||||
gojqCode: makeGoJQ(),
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// needsUpdate checks if cachepath is older than 24 hours
|
// needsUpdate checks if cachepath is older than 24 hours.
|
||||||
func (a *AURCache) needsUpdate() (bool, error) {
|
func (a *AURCache) needsUpdate() (bool, error) {
|
||||||
// check if cache is older than 24 hours
|
// check if cache is older than 24 hours
|
||||||
info, err := os.Stat(a.cachePath)
|
info, err := os.Stat(a.cachePath)
|
||||||
@ -61,20 +57,6 @@ func (a *AURCache) needsUpdate() (bool, error) {
|
|||||||
return info.ModTime().Before(time.Now().Add(-cacheValidity)), nil
|
return info.ModTime().Before(time.Now().Add(-cacheValidity)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AURCache) cacheKey(needle string, byProvides, byBase, byName bool) string {
|
|
||||||
return fmt.Sprintf("%s-%v-%v-%v", needle, byProvides, byBase, byName)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *AURCache) DebugInfo() {
|
|
||||||
fmt.Println("Byte Cache", len(a.cache))
|
|
||||||
fmt.Println("Entries Cached", len(a.searchCache))
|
|
||||||
fmt.Println("Cache Hits", a.cacheHits)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *AURCache) SetProvideCache(needle string, pkgs []*aur.Pkg) {
|
|
||||||
a.searchCache[needle] = pkgs
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get returns a list of packages that provide the given search term.
|
// Get returns a list of packages that provide the given search term.
|
||||||
func (a *AURCache) Get(ctx context.Context, query *AURQuery) ([]*aur.Pkg, error) {
|
func (a *AURCache) Get(ctx context.Context, query *AURQuery) ([]*aur.Pkg, error) {
|
||||||
update, err := a.needsUpdate()
|
update, err := a.needsUpdate()
|
||||||
@ -94,7 +76,7 @@ func (a *AURCache) Get(ctx context.Context, query *AURQuery) ([]*aur.Pkg, error)
|
|||||||
|
|
||||||
inputStruct, unmarshallErr := oj.Parse(a.cache)
|
inputStruct, unmarshallErr := oj.Parse(a.cache)
|
||||||
if unmarshallErr != nil {
|
if unmarshallErr != nil {
|
||||||
return nil, unmarshallErr
|
return nil, fmt.Errorf("aur metadata unable to parse cache: %w", unmarshallErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
a.unmarshalledCache = inputStruct.([]interface{})
|
a.unmarshalledCache = inputStruct.([]interface{})
|
||||||
@ -115,30 +97,12 @@ func (a *AURCache) Get(ctx context.Context, query *AURQuery) ([]*aur.Pkg, error)
|
|||||||
return found, nil
|
return found, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns a list of packages that provide the given search term
|
|
||||||
func (a *AURCache) FindPackage(ctx context.Context, needle string) ([]*aur.Pkg, error) {
|
|
||||||
cacheKey := a.cacheKey(needle, true, true, true)
|
|
||||||
if pkgs, ok := a.searchCache[cacheKey]; ok {
|
|
||||||
a.cacheHits++
|
|
||||||
return pkgs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
final, error := a.gojqGet(ctx, needle)
|
|
||||||
if error != nil {
|
|
||||||
return nil, error
|
|
||||||
}
|
|
||||||
|
|
||||||
a.searchCache[cacheKey] = final
|
|
||||||
|
|
||||||
return final, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *AURCache) gojqGetBatch(ctx context.Context, query *AURQuery) ([]*aur.Pkg, error) {
|
func (a *AURCache) gojqGetBatch(ctx context.Context, query *AURQuery) ([]*aur.Pkg, error) {
|
||||||
pattern := ".[] | select("
|
pattern := ".[] | select("
|
||||||
|
|
||||||
for i, searchTerm := range query.Needles {
|
for i, searchTerm := range query.Needles {
|
||||||
if i != 0 {
|
if i != 0 {
|
||||||
pattern += " or "
|
pattern += ","
|
||||||
}
|
}
|
||||||
|
|
||||||
bys := toSearchBy(query.By)
|
bys := toSearchBy(query.By)
|
||||||
@ -163,25 +127,29 @@ func (a *AURCache) gojqGetBatch(ctx context.Context, query *AURQuery) ([]*aur.Pk
|
|||||||
|
|
||||||
parsed, err := gojq.Parse(pattern)
|
parsed, err := gojq.Parse(pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
return nil, fmt.Errorf("unable to parse query: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
final := make([]*aur.Pkg, 0, len(query.Needles))
|
final := make([]*aur.Pkg, 0, len(query.Needles))
|
||||||
|
|
||||||
iter := parsed.RunWithContext(ctx, a.unmarshalledCache) // or query.RunWithContext
|
iter := parsed.RunWithContext(ctx, a.unmarshalledCache) // or query.RunWithContext
|
||||||
|
|
||||||
for v, ok := iter.Next(); ok; v, ok = iter.Next() {
|
for pkgMap, ok := iter.Next(); ok; pkgMap, ok = iter.Next() {
|
||||||
if err, ok := v.(error); ok {
|
if err, ok := pkgMap.(error); ok {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
pkg := new(aur.Pkg)
|
pkg := new(aur.Pkg)
|
||||||
bValue, err := gojq.Marshal(v)
|
|
||||||
|
bValue, err := gojq.Marshal(pkgMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
return nil, fmt.Errorf("unable to marshal aur package: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
errU := oj.Unmarshal(bValue, pkg)
|
||||||
|
if errU != nil {
|
||||||
|
return nil, fmt.Errorf("unable to unmarshal aur package: %w", errU)
|
||||||
}
|
}
|
||||||
|
|
||||||
oj.Unmarshal(bValue, pkg)
|
|
||||||
final = append(final, pkg)
|
final = append(final, pkg)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,45 +160,6 @@ func (a *AURCache) gojqGetBatch(ctx context.Context, query *AURQuery) ([]*aur.Pk
|
|||||||
return final, nil
|
return final, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AURCache) gojqGet(ctx context.Context, searchTerm string) ([]*aur.Pkg, error) {
|
|
||||||
final := make([]*aur.Pkg, 0, 1)
|
|
||||||
|
|
||||||
iter := a.gojqCode.RunWithContext(ctx, a.unmarshalledCache, searchTerm) // or query.RunWithContext
|
|
||||||
|
|
||||||
for v, ok := iter.Next(); ok; v, ok = iter.Next() {
|
|
||||||
if err, ok := v.(error); ok {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
pkg := &aur.Pkg{}
|
|
||||||
bValue, err := gojq.Marshal(v)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
json.Unmarshal(bValue, pkg)
|
|
||||||
final = append(final, pkg)
|
|
||||||
}
|
|
||||||
|
|
||||||
return final, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func makeGoJQ() *gojq.Code {
|
|
||||||
pattern := ".[] | select((.Name == $x) or (.Provides[]? == ($x)))"
|
|
||||||
|
|
||||||
query, err := gojq.Parse(pattern)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
compiled, err := gojq.Compile(query, gojq.WithVariables([]string{"$x"}))
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return compiled
|
|
||||||
}
|
|
||||||
|
|
||||||
func toSearchBy(by aur.By) []string {
|
func toSearchBy(by aur.By) []string {
|
||||||
switch by {
|
switch by {
|
||||||
case aur.Name:
|
case aur.Name:
|
||||||
|
@ -45,7 +45,7 @@ func ReadCache(cachePath string) ([]byte, error) {
|
|||||||
|
|
||||||
// Download the metadata for aur packages.
|
// Download the metadata for aur packages.
|
||||||
// create cache file
|
// create cache file
|
||||||
// write to cache file
|
// write to cache file.
|
||||||
func MakeCache(cachePath string) ([]byte, error) {
|
func MakeCache(cachePath string) ([]byte, error) {
|
||||||
body, err := downloadAURMetadata()
|
body, err := downloadAURMetadata()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user