2022-09-04 23:45:40 +02:00
|
|
|
package metadata
|
|
|
|
|
|
|
|
import (
|
2022-11-08 01:32:21 +01:00
|
|
|
"context"
|
2022-09-04 23:45:40 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2022-11-08 01:32:21 +01:00
|
|
|
"os"
|
|
|
|
"time"
|
2022-09-04 23:45:40 +02:00
|
|
|
|
|
|
|
"github.com/Jguer/aur"
|
|
|
|
"github.com/itchyny/gojq"
|
|
|
|
"github.com/ohler55/ojg/oj"
|
2022-11-08 01:32:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
searchCacheCap = 300
|
|
|
|
cacheValidity = 1 * time.Hour
|
2022-09-04 23:45:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type AURCache struct {
|
|
|
|
cache []byte
|
2022-11-08 01:32:21 +01:00
|
|
|
searchCache map[string][]*aur.Pkg
|
|
|
|
cachePath string
|
2022-09-04 23:45:40 +02:00
|
|
|
unmarshalledCache []interface{}
|
|
|
|
cacheHits int
|
|
|
|
gojqCode *gojq.Code
|
2022-11-08 01:32:21 +01:00
|
|
|
DebugLoggerFn func(a ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type AURQuery struct {
|
2022-11-13 23:53:37 +01:00
|
|
|
Needles []string
|
|
|
|
By aur.By
|
|
|
|
Contains bool // if true, search for packages containing the needle, not exact matches
|
2022-09-04 23:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewAURCache(cachePath string) (*AURCache, error) {
|
|
|
|
aurCache, err := MakeOrReadCache(cachePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
inputStruct, err := oj.Parse(aurCache)
|
|
|
|
|
|
|
|
return &AURCache{
|
|
|
|
cache: aurCache,
|
2022-11-08 01:32:21 +01:00
|
|
|
cachePath: cachePath,
|
|
|
|
searchCache: make(map[string][]*aur.Pkg, searchCacheCap),
|
2022-09-04 23:45:40 +02:00
|
|
|
unmarshalledCache: inputStruct.([]interface{}),
|
|
|
|
gojqCode: makeGoJQ(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-11-08 01:32:21 +01:00
|
|
|
// needsUpdate checks if cachepath is older than 24 hours
|
|
|
|
func (a *AURCache) needsUpdate() (bool, error) {
|
|
|
|
// check if cache is older than 24 hours
|
|
|
|
info, err := os.Stat(a.cachePath)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("unable to read cache: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-09-04 23:45:40 +02:00
|
|
|
func (a *AURCache) DebugInfo() {
|
|
|
|
fmt.Println("Byte Cache", len(a.cache))
|
2022-11-08 01:32:21 +01:00
|
|
|
fmt.Println("Entries Cached", len(a.searchCache))
|
2022-09-04 23:45:40 +02:00
|
|
|
fmt.Println("Cache Hits", a.cacheHits)
|
|
|
|
}
|
|
|
|
|
2022-09-06 23:25:44 +02:00
|
|
|
func (a *AURCache) SetProvideCache(needle string, pkgs []*aur.Pkg) {
|
2022-11-08 01:32:21 +01:00
|
|
|
a.searchCache[needle] = pkgs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns a list of packages that provide the given search term.
|
|
|
|
func (a *AURCache) Get(ctx context.Context, query *AURQuery) ([]*aur.Pkg, error) {
|
|
|
|
update, err := a.needsUpdate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if update {
|
|
|
|
if a.DebugLoggerFn != nil {
|
|
|
|
a.DebugLoggerFn("AUR Cache is out of date, updating")
|
|
|
|
}
|
|
|
|
|
|
|
|
var makeErr error
|
|
|
|
if a.cache, makeErr = MakeCache(a.cachePath); makeErr != nil {
|
|
|
|
return nil, makeErr
|
|
|
|
}
|
|
|
|
|
|
|
|
inputStruct, unmarshallErr := oj.Parse(a.cache)
|
|
|
|
if unmarshallErr != nil {
|
|
|
|
return nil, unmarshallErr
|
|
|
|
}
|
|
|
|
|
|
|
|
a.unmarshalledCache = inputStruct.([]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
found := make([]*aur.Pkg, 0, len(query.Needles))
|
|
|
|
if len(query.Needles) == 0 {
|
|
|
|
return found, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
iterFound, errNeedle := a.gojqGetBatch(ctx, query)
|
|
|
|
if errNeedle != nil {
|
|
|
|
return nil, errNeedle
|
|
|
|
}
|
|
|
|
|
|
|
|
found = append(found, iterFound...)
|
|
|
|
|
|
|
|
return found, nil
|
2022-09-06 23:25:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns a list of packages that provide the given search term
|
2022-11-08 01:32:21 +01:00
|
|
|
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 {
|
2022-09-04 23:45:40 +02:00
|
|
|
a.cacheHits++
|
|
|
|
return pkgs, nil
|
|
|
|
}
|
|
|
|
|
2022-11-08 01:32:21 +01:00
|
|
|
final, error := a.gojqGet(ctx, needle)
|
2022-09-04 23:45:40 +02:00
|
|
|
if error != nil {
|
|
|
|
return nil, error
|
|
|
|
}
|
|
|
|
|
2022-11-08 01:32:21 +01:00
|
|
|
a.searchCache[cacheKey] = final
|
2022-09-04 23:45:40 +02:00
|
|
|
|
|
|
|
return final, nil
|
|
|
|
}
|
|
|
|
|
2022-11-08 01:32:21 +01:00
|
|
|
func (a *AURCache) gojqGetBatch(ctx context.Context, query *AURQuery) ([]*aur.Pkg, error) {
|
|
|
|
pattern := ".[] | select("
|
2022-11-13 17:47:19 +01:00
|
|
|
|
2022-11-08 01:32:21 +01:00
|
|
|
for i, searchTerm := range query.Needles {
|
|
|
|
if i != 0 {
|
|
|
|
pattern += " or "
|
|
|
|
}
|
|
|
|
|
2022-11-13 17:47:19 +01:00
|
|
|
bys := toSearchBy(query.By)
|
|
|
|
for j, by := range bys {
|
2022-11-13 23:53:37 +01:00
|
|
|
if query.Contains {
|
|
|
|
pattern += fmt.Sprintf("(.%s // empty | test(\"%s\"))", by, searchTerm)
|
|
|
|
} else {
|
|
|
|
pattern += fmt.Sprintf("(.%s == \"%s\")", by, searchTerm)
|
|
|
|
}
|
|
|
|
|
2022-11-13 17:47:19 +01:00
|
|
|
if j != len(bys)-1 {
|
2022-11-13 23:53:37 +01:00
|
|
|
pattern += " , "
|
2022-11-08 01:32:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-04 23:45:40 +02:00
|
|
|
|
2022-11-08 01:32:21 +01:00
|
|
|
pattern += ")"
|
2022-09-04 23:45:40 +02:00
|
|
|
|
2022-11-13 23:53:37 +01:00
|
|
|
if a.DebugLoggerFn != nil {
|
|
|
|
a.DebugLoggerFn("AUR metadata query", pattern)
|
|
|
|
}
|
|
|
|
|
2022-11-08 01:32:21 +01:00
|
|
|
parsed, err := gojq.Parse(pattern)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2022-09-04 23:45:40 +02:00
|
|
|
|
2022-11-08 01:32:21 +01:00
|
|
|
final := make([]*aur.Pkg, 0, len(query.Needles))
|
2022-09-04 23:45:40 +02:00
|
|
|
|
2022-11-08 01:32:21 +01:00
|
|
|
iter := parsed.RunWithContext(ctx, a.unmarshalledCache) // or query.RunWithContext
|
2022-09-04 23:45:40 +02:00
|
|
|
|
2022-11-08 01:32:21 +01:00
|
|
|
for v, ok := iter.Next(); ok; v, ok = iter.Next() {
|
|
|
|
if err, ok := v.(error); ok {
|
|
|
|
return nil, err
|
2022-09-04 23:45:40 +02:00
|
|
|
}
|
2022-11-08 01:32:21 +01:00
|
|
|
|
|
|
|
pkg := new(aur.Pkg)
|
|
|
|
bValue, err := gojq.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
oj.Unmarshal(bValue, pkg)
|
|
|
|
final = append(final, pkg)
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.DebugLoggerFn != nil {
|
2022-11-13 23:53:37 +01:00
|
|
|
a.DebugLoggerFn("AUR metadata query found", len(final))
|
2022-09-04 23:45:40 +02:00
|
|
|
}
|
2022-11-08 01:32:21 +01:00
|
|
|
|
2022-09-04 23:45:40 +02:00
|
|
|
return final, nil
|
|
|
|
}
|
|
|
|
|
2022-11-08 01:32:21 +01:00
|
|
|
func (a *AURCache) gojqGet(ctx context.Context, searchTerm string) ([]*aur.Pkg, error) {
|
2022-09-04 23:45:40 +02:00
|
|
|
final := make([]*aur.Pkg, 0, 1)
|
|
|
|
|
2022-11-08 01:32:21 +01:00
|
|
|
iter := a.gojqCode.RunWithContext(ctx, a.unmarshalledCache, searchTerm) // or query.RunWithContext
|
|
|
|
|
|
|
|
for v, ok := iter.Next(); ok; v, ok = iter.Next() {
|
2022-09-04 23:45:40 +02:00
|
|
|
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 {
|
2022-09-20 00:01:19 +02:00
|
|
|
pattern := ".[] | select((.Name == $x) or (.Provides[]? == ($x)))"
|
|
|
|
|
2022-09-04 23:45:40 +02:00
|
|
|
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
|
|
|
|
}
|
2022-11-13 17:47:19 +01:00
|
|
|
|
|
|
|
func toSearchBy(by aur.By) []string {
|
|
|
|
switch by {
|
|
|
|
case aur.Name:
|
|
|
|
return []string{"Name"}
|
|
|
|
case aur.NameDesc:
|
|
|
|
return []string{"Name", "Description"}
|
|
|
|
case aur.Maintainer:
|
|
|
|
return []string{"Maintainer"}
|
|
|
|
case aur.Depends:
|
|
|
|
return []string{"Depends[]?"}
|
|
|
|
case aur.MakeDepends:
|
|
|
|
return []string{"MakeDepends[]?"}
|
|
|
|
case aur.OptDepends:
|
|
|
|
return []string{"OptDepends[]?"}
|
|
|
|
case aur.CheckDepends:
|
|
|
|
return []string{"CheckDepends[]?"}
|
|
|
|
case aur.None:
|
|
|
|
return []string{"Name", "Provides[]?"}
|
|
|
|
default:
|
|
|
|
panic("invalid By")
|
|
|
|
}
|
|
|
|
}
|