query_builder.go 6.4 KB

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