Browse Source

support contains

jguer 2 years ago
parent
commit
5aeb0d696c
7 changed files with 41 additions and 22 deletions
  1. 2 1
      main.go
  2. 15 5
      pkg/metadata/metadata_aur.go
  3. 1 0
      pkg/query/aur_info.go
  4. 1 1
      pkg/query/mixed_sources.go
  5. 20 13
      pkg/query/source.go
  6. 1 1
      pkg/query/source_test.go
  7. 1 1
      pkg/text/print.go

+ 2 - 1
main.go

@@ -96,7 +96,8 @@ func main() {
 		config.Runtime.QueryBuilder = query.NewSourceQueryBuilder(
 			config.Runtime.AURClient, config.Runtime.AURCache,
 			config.SortBy,
-			config.Runtime.Mode, config.SearchBy, config.BottomUp, config.SingleLineResults)
+			config.Runtime.Mode, config.SearchBy, config.BottomUp,
+			config.SingleLineResults, config.NewInstallEngine)
 	} else {
 		config.Runtime.QueryBuilder = query.NewMixedSourceQueryBuilder(
 			config.Runtime.AURClient, config.SortBy,

+ 15 - 5
pkg/metadata/metadata_aur.go

@@ -29,8 +29,9 @@ type AURCache struct {
 }
 
 type AURQuery struct {
-	Needles []string
-	By      aur.By
+	Needles  []string
+	By       aur.By
+	Contains bool // if true, search for packages containing the needle, not exact matches
 }
 
 func NewAURCache(cachePath string) (*AURCache, error) {
@@ -142,15 +143,24 @@ func (a *AURCache) gojqGetBatch(ctx context.Context, query *AURQuery) ([]*aur.Pk
 
 		bys := toSearchBy(query.By)
 		for j, by := range bys {
-			pattern += fmt.Sprintf("(.%s == \"%s\")", by, searchTerm)
+			if query.Contains {
+				pattern += fmt.Sprintf("(.%s // empty | test(\"%s\"))", by, searchTerm)
+			} else {
+				pattern += fmt.Sprintf("(.%s == \"%s\")", by, searchTerm)
+			}
+
 			if j != len(bys)-1 {
-				pattern += " or "
+				pattern += " , "
 			}
 		}
 	}
 
 	pattern += ")"
 
+	if a.DebugLoggerFn != nil {
+		a.DebugLoggerFn("AUR metadata query", pattern)
+	}
+
 	parsed, err := gojq.Parse(pattern)
 	if err != nil {
 		log.Fatalln(err)
@@ -176,7 +186,7 @@ func (a *AURCache) gojqGetBatch(ctx context.Context, query *AURQuery) ([]*aur.Pk
 	}
 
 	if a.DebugLoggerFn != nil {
-		a.DebugLoggerFn("AUR Query", pattern, "Found", len(final))
+		a.DebugLoggerFn("AUR metadata query found", len(final))
 	}
 
 	return final, nil

+ 1 - 0
pkg/query/aur_info.go

@@ -32,6 +32,7 @@ func AURInfo(ctx context.Context, aurClient aur.ClientInterface, names []string,
 	makeRequest := func(n, max int) {
 		defer wg.Done()
 
+		text.Debugln("AUR RPC:", names[n:max])
 		tempInfo, requestErr := aurClient.Info(ctx, names[n:max])
 		if requestErr != nil {
 			errs.Add(requestErr)

+ 1 - 1
pkg/query/mixed_sources.go

@@ -145,7 +145,7 @@ func (s *MixedSourceQueryBuilder) Execute(ctx context.Context, dbExecutor db.Exe
 
 	if s.targetMode.AtLeastAUR() {
 		var aurResults aurQuery
-		aurResults, aurErr = queryAUR(ctx, s.aurClient, nil, pkgS, s.searchBy)
+		aurResults, aurErr = queryAUR(ctx, s.aurClient, nil, pkgS, s.searchBy, false)
 		dbName := sourceAUR
 
 		for i := range aurResults {

+ 20 - 13
pkg/query/source.go

@@ -30,6 +30,8 @@ const (
 type SourceQueryBuilder struct {
 	repoQuery
 	aurQuery
+
+	useAURCache       bool
 	sortBy            string
 	searchBy          string
 	targetMode        parser.TargetMode
@@ -48,6 +50,7 @@ func NewSourceQueryBuilder(
 	searchBy string,
 	bottomUp,
 	singleLineResults bool,
+	useAURCache bool,
 ) *SourceQueryBuilder {
 	return &SourceQueryBuilder{
 		aurClient:         aurClient,
@@ -59,6 +62,7 @@ func NewSourceQueryBuilder(
 		targetMode:        targetMode,
 		searchBy:          searchBy,
 		singleLineResults: singleLineResults,
+		useAURCache:       useAURCache,
 	}
 }
 
@@ -71,7 +75,7 @@ func (s *SourceQueryBuilder) Execute(ctx context.Context,
 	pkgS = RemoveInvalidTargets(pkgS, s.targetMode)
 
 	if s.targetMode.AtLeastAUR() {
-		s.aurQuery, aurErr = queryAUR(ctx, s.aurClient, s.aurCache, pkgS, s.searchBy)
+		s.aurQuery, aurErr = queryAUR(ctx, s.aurClient, s.aurCache, pkgS, s.searchBy, s.useAURCache)
 		s.aurQuery = filterAURResults(pkgS, s.aurQuery)
 
 		sort.Sort(aurSortable{aurQuery: s.aurQuery, sortBy: s.sortBy, bottomUp: s.bottomUp})
@@ -189,7 +193,7 @@ func filterAURResults(pkgS []string, results []aur.Pkg) []aur.Pkg {
 // queryAUR searches AUR and narrows based on subarguments.
 func queryAUR(ctx context.Context,
 	aurClient aur.ClientInterface, aurMetadata *metadata.AURCache,
-	pkgS []string, searchBy string,
+	pkgS []string, searchBy string, newEngine bool,
 ) ([]aur.Pkg, error) {
 	var (
 		err error
@@ -199,18 +203,11 @@ func queryAUR(ctx context.Context,
 	for _, word := range pkgS {
 		var r []aur.Pkg
 
-		// if one of the search terms returns a result we start filtering by it
-		if aurClient != nil {
-			r, err = aurClient.Search(ctx, word, by)
-			if err == nil {
-				return r, nil
-			}
-		}
-
-		if aurMetadata != nil {
+		if aurMetadata != nil && newEngine {
 			q, err := aurMetadata.Get(ctx, &metadata.AURQuery{
-				Needles: []string{word},
-				By:      by,
+				Needles:  []string{word},
+				By:       by,
+				Contains: true,
 			})
 
 			for _, pkg := range q {
@@ -219,6 +216,16 @@ func queryAUR(ctx context.Context,
 
 			if err == nil {
 				return r, nil
+			} else {
+				text.Warnln("AUR Metadata search failed:", err)
+			}
+		}
+		// if one of the search terms returns a result we start filtering by it
+		if aurClient != nil {
+			text.Debugln("AUR RPC:", by, word)
+
+			if r, err = aurClient.Search(ctx, word, by); err == nil {
+				return r, nil
 			}
 		}
 	}

+ 1 - 1
pkg/query/source_test.go

@@ -108,7 +108,7 @@ func TestSourceQueryBuilder(t *testing.T) {
 			client, err := aur.NewClient(aur.WithHTTPClient(&mockDoer{}))
 			require.NoError(t, err)
 
-			queryBuilder := NewSourceQueryBuilder(client, nil, "votes", parser.ModeAny, "", tc.bottomUp, false)
+			queryBuilder := NewSourceQueryBuilder(client, nil, "votes", parser.ModeAny, "", tc.bottomUp, false, false)
 			search := []string{"linux"}
 			mockStore := &mockDB{}
 

+ 1 - 1
pkg/text/print.go

@@ -28,7 +28,7 @@ func Debugln(a ...interface{}) {
 		return
 	}
 
-	fmt.Fprintln(os.Stdout, append([]interface{}{Bold(yellow("[DEBUG] "))}, a...)...)
+	fmt.Fprintln(os.Stdout, append([]interface{}{Bold(yellow("[DEBUG]"))}, a...)...)
 	fmt.Fprint(os.Stdout, ResetCode)
 }