You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.7 KiB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "xorm.io/builder"
  8. )
  9. // RepoIndexerType specifies the repository indexer type
  10. type RepoIndexerType int
  11. const (
  12. // RepoIndexerTypeCode code indexer
  13. RepoIndexerTypeCode RepoIndexerType = iota // 0
  14. // RepoIndexerTypeStats repository stats indexer
  15. RepoIndexerTypeStats // 1
  16. )
  17. // RepoIndexerStatus status of a repo's entry in the repo indexer
  18. // For now, implicitly refers to default branch
  19. type RepoIndexerStatus struct {
  20. ID int64 `xorm:"pk autoincr"`
  21. RepoID int64 `xorm:"INDEX(s)"`
  22. CommitSha string `xorm:"VARCHAR(40)"`
  23. IndexerType RepoIndexerType `xorm:"INDEX(s) NOT NULL DEFAULT 0"`
  24. }
  25. // GetUnindexedRepos returns repos which do not have an indexer status
  26. func GetUnindexedRepos(indexerType RepoIndexerType, maxRepoID int64, page, pageSize int) ([]int64, error) {
  27. ids := make([]int64, 0, 50)
  28. cond := builder.Cond(builder.IsNull{
  29. "repo_indexer_status.id",
  30. }).And(builder.Eq{
  31. "repository.is_empty": false,
  32. })
  33. sess := x.Table("repository").Join("LEFT OUTER", "repo_indexer_status", "repository.id = repo_indexer_status.repo_id AND repo_indexer_status.indexer_type = ?", indexerType)
  34. if maxRepoID > 0 {
  35. cond = builder.And(cond, builder.Lte{
  36. "repository.id": maxRepoID,
  37. })
  38. }
  39. if page >= 0 && pageSize > 0 {
  40. start := 0
  41. if page > 0 {
  42. start = (page - 1) * pageSize
  43. }
  44. sess.Limit(pageSize, start)
  45. }
  46. sess.Where(cond).Cols("repository.id").Desc("repository.id")
  47. err := sess.Find(&ids)
  48. return ids, err
  49. }
  50. // getIndexerStatus loads repo codes indxer status
  51. func (repo *Repository) getIndexerStatus(e Engine, indexerType RepoIndexerType) (*RepoIndexerStatus, error) {
  52. switch indexerType {
  53. case RepoIndexerTypeCode:
  54. if repo.CodeIndexerStatus != nil {
  55. return repo.CodeIndexerStatus, nil
  56. }
  57. case RepoIndexerTypeStats:
  58. if repo.StatsIndexerStatus != nil {
  59. return repo.StatsIndexerStatus, nil
  60. }
  61. }
  62. status := &RepoIndexerStatus{RepoID: repo.ID}
  63. if has, err := e.Where("`indexer_type` = ?", indexerType).Get(status); err != nil {
  64. return nil, err
  65. } else if !has {
  66. status.IndexerType = indexerType
  67. status.CommitSha = ""
  68. }
  69. switch indexerType {
  70. case RepoIndexerTypeCode:
  71. repo.CodeIndexerStatus = status
  72. case RepoIndexerTypeStats:
  73. repo.StatsIndexerStatus = status
  74. }
  75. return status, nil
  76. }
  77. // GetIndexerStatus loads repo codes indxer status
  78. func (repo *Repository) GetIndexerStatus(indexerType RepoIndexerType) (*RepoIndexerStatus, error) {
  79. return repo.getIndexerStatus(x, indexerType)
  80. }
  81. // updateIndexerStatus updates indexer status
  82. func (repo *Repository) updateIndexerStatus(e Engine, indexerType RepoIndexerType, sha string) error {
  83. status, err := repo.getIndexerStatus(e, indexerType)
  84. if err != nil {
  85. return fmt.Errorf("UpdateIndexerStatus: Unable to getIndexerStatus for repo: %s Error: %v", repo.FullName(), err)
  86. }
  87. if len(status.CommitSha) == 0 {
  88. status.CommitSha = sha
  89. _, err := e.Insert(status)
  90. if err != nil {
  91. return fmt.Errorf("UpdateIndexerStatus: Unable to insert repoIndexerStatus for repo: %s Sha: %s Error: %v", repo.FullName(), sha, err)
  92. }
  93. return nil
  94. }
  95. status.CommitSha = sha
  96. _, err = e.ID(status.ID).Cols("commit_sha").
  97. Update(status)
  98. if err != nil {
  99. return fmt.Errorf("UpdateIndexerStatus: Unable to update repoIndexerStatus for repo: %s Sha: %s Error: %v", repo.FullName(), sha, err)
  100. }
  101. return nil
  102. }
  103. // UpdateIndexerStatus updates indexer status
  104. func (repo *Repository) UpdateIndexerStatus(indexerType RepoIndexerType, sha string) error {
  105. return repo.updateIndexerStatus(x, indexerType, sha)
  106. }