mixed_sources.go 6.7 KB

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