mixed_sources.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package query
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "sort"
  7. "strconv"
  8. "strings"
  9. "github.com/Jguer/aur"
  10. "github.com/Jguer/go-alpm/v2"
  11. "github.com/adrg/strutil"
  12. "github.com/adrg/strutil/metrics"
  13. "github.com/leonelquinteros/gotext"
  14. "github.com/Jguer/yay/v11/pkg/db"
  15. "github.com/Jguer/yay/v11/pkg/intrange"
  16. "github.com/Jguer/yay/v11/pkg/settings/parser"
  17. "github.com/Jguer/yay/v11/pkg/stringset"
  18. "github.com/Jguer/yay/v11/pkg/text"
  19. )
  20. const sourceAUR = "aur"
  21. type Builder interface {
  22. Len() int
  23. Execute(ctx context.Context, dbExecutor db.Executor, pkgS []string)
  24. Results(w io.Writer, dbExecutor db.Executor, verboseSearch SearchVerbosity) error
  25. GetTargets(include, exclude intrange.IntRanges, otherExclude stringset.StringSet) ([]string, error)
  26. }
  27. type MixedSourceQueryBuilder struct {
  28. results []abstractResult
  29. sortBy string
  30. searchBy string
  31. targetMode parser.TargetMode
  32. queryMap map[string]map[string]interface{}
  33. bottomUp bool
  34. singleLineResults bool
  35. aurClient aur.ClientInterface
  36. }
  37. func NewMixedSourceQueryBuilder(
  38. aurClient aur.ClientInterface,
  39. sortBy string,
  40. targetMode parser.TargetMode,
  41. searchBy string,
  42. bottomUp,
  43. singleLineResults bool,
  44. ) *MixedSourceQueryBuilder {
  45. return &MixedSourceQueryBuilder{
  46. aurClient: aurClient,
  47. bottomUp: bottomUp,
  48. sortBy: sortBy,
  49. targetMode: targetMode,
  50. searchBy: searchBy,
  51. singleLineResults: singleLineResults,
  52. queryMap: map[string]map[string]interface{}{},
  53. results: make([]abstractResult, 0, 100),
  54. }
  55. }
  56. type abstractResult struct {
  57. source string
  58. name string
  59. description string
  60. votes int
  61. provides []string
  62. }
  63. type abstractResults struct {
  64. results []abstractResult
  65. search string
  66. distanceCache map[string]float64
  67. bottomUp bool
  68. metric strutil.StringMetric
  69. }
  70. func (a *abstractResults) Len() int { return len(a.results) }
  71. func (a *abstractResults) Swap(i, j int) { a.results[i], a.results[j] = a.results[j], a.results[i] }
  72. func (a *abstractResults) GetMetric(pkg *abstractResult) float64 {
  73. if v, ok := a.distanceCache[pkg.name]; ok {
  74. return v
  75. }
  76. sim := strutil.Similarity(pkg.name, a.search, a.metric)
  77. for _, prov := range pkg.provides {
  78. // If the package provides search, it's a perfect match
  79. // AUR packages don't populate provides
  80. candidate := strutil.Similarity(prov, a.search, a.metric)
  81. if candidate > sim {
  82. sim = candidate
  83. }
  84. }
  85. simDesc := strutil.Similarity(pkg.description, a.search, a.metric)
  86. // slightly overweight sync sources by always giving them max popularity
  87. popularity := 1.0
  88. if pkg.source == sourceAUR {
  89. popularity = float64(pkg.votes) / float64(pkg.votes+60)
  90. }
  91. sim = sim*0.6 + simDesc*0.2 + popularity*0.2
  92. a.distanceCache[pkg.name] = sim
  93. return sim
  94. }
  95. func (a *abstractResults) Less(i, j int) bool {
  96. pkgA := a.results[i]
  97. pkgB := a.results[j]
  98. simA := a.GetMetric(&pkgA)
  99. simB := a.GetMetric(&pkgB)
  100. if a.bottomUp {
  101. return simA < simB
  102. }
  103. return simA > simB
  104. }
  105. func (s *MixedSourceQueryBuilder) Execute(ctx context.Context, dbExecutor db.Executor, pkgS []string) {
  106. var aurErr error
  107. pkgS = RemoveInvalidTargets(pkgS, s.targetMode)
  108. metric := &metrics.JaroWinkler{
  109. CaseSensitive: false,
  110. }
  111. sortableResults := &abstractResults{
  112. results: []abstractResult{},
  113. search: strings.Join(pkgS, ""),
  114. distanceCache: map[string]float64{},
  115. bottomUp: s.bottomUp,
  116. metric: metric,
  117. }
  118. if s.targetMode.AtLeastAUR() {
  119. var aurResults aurQuery
  120. aurResults, aurErr = queryAUR(ctx, s.aurClient, nil, pkgS, s.searchBy, false)
  121. dbName := sourceAUR
  122. for i := range aurResults {
  123. if s.queryMap[dbName] == nil {
  124. s.queryMap[dbName] = map[string]interface{}{}
  125. }
  126. s.queryMap[dbName][aurResults[i].Name] = aurResults[i]
  127. sortableResults.results = append(sortableResults.results, abstractResult{
  128. source: dbName,
  129. name: aurResults[i].Name,
  130. description: aurResults[i].Description,
  131. provides: aurResults[i].Provides,
  132. votes: aurResults[i].NumVotes,
  133. })
  134. }
  135. }
  136. var repoResults []alpm.IPackage
  137. if s.targetMode.AtLeastRepo() {
  138. repoResults = dbExecutor.SyncPackages(pkgS...)
  139. for i := range repoResults {
  140. dbName := repoResults[i].DB().Name()
  141. if s.queryMap[dbName] == nil {
  142. s.queryMap[dbName] = map[string]interface{}{}
  143. }
  144. s.queryMap[dbName][repoResults[i].Name()] = repoResults[i]
  145. rawProvides := repoResults[i].Provides().Slice()
  146. provides := make([]string, len(rawProvides))
  147. for j := range rawProvides {
  148. provides[j] = rawProvides[j].Name
  149. }
  150. sortableResults.results = append(sortableResults.results, abstractResult{
  151. source: repoResults[i].DB().Name(),
  152. name: repoResults[i].Name(),
  153. description: repoResults[i].Description(),
  154. provides: provides,
  155. votes: -1,
  156. })
  157. }
  158. }
  159. sort.Sort(sortableResults)
  160. s.results = sortableResults.results
  161. if aurErr != nil {
  162. text.Errorln(ErrAURSearch{inner: aurErr})
  163. if len(repoResults) != 0 {
  164. text.Warnln(gotext.Get("Showing repo packages only"))
  165. }
  166. }
  167. }
  168. func (s *MixedSourceQueryBuilder) Results(w io.Writer, dbExecutor db.Executor, verboseSearch SearchVerbosity) error {
  169. for i := range s.results {
  170. if verboseSearch == Minimal {
  171. _, _ = fmt.Fprintln(w, s.results[i].name)
  172. continue
  173. }
  174. var toPrint string
  175. if verboseSearch == NumberMenu {
  176. if s.bottomUp {
  177. toPrint += text.Magenta(strconv.Itoa(len(s.results)-i)) + " "
  178. } else {
  179. toPrint += text.Magenta(strconv.Itoa(i+1)) + " "
  180. }
  181. }
  182. pkg := s.queryMap[s.results[i].source][s.results[i].name]
  183. if s.results[i].source == sourceAUR {
  184. aurPkg := pkg.(aur.Pkg)
  185. toPrint += aurPkgSearchString(&aurPkg, dbExecutor, s.singleLineResults)
  186. } else {
  187. syncPkg := pkg.(alpm.IPackage)
  188. toPrint += syncPkgSearchString(syncPkg, dbExecutor, s.singleLineResults)
  189. }
  190. fmt.Fprintln(w, toPrint)
  191. }
  192. return nil
  193. }
  194. func (s *MixedSourceQueryBuilder) Len() int {
  195. return len(s.results)
  196. }
  197. func (s *MixedSourceQueryBuilder) GetTargets(include, exclude intrange.IntRanges,
  198. otherExclude stringset.StringSet,
  199. ) ([]string, error) {
  200. var (
  201. isInclude = len(exclude) == 0 && len(otherExclude) == 0
  202. targets []string
  203. lenRes = len(s.results)
  204. )
  205. for i := 0; i <= s.Len(); i++ {
  206. // FIXME: this is probably broken
  207. target := i - 1
  208. if s.bottomUp {
  209. target = lenRes - i
  210. }
  211. if (isInclude && include.Get(i)) || (!isInclude && !exclude.Get(i)) {
  212. targets = append(targets, s.results[target].source+"/"+s.results[target].name)
  213. }
  214. }
  215. return targets, nil
  216. }