diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-12-10 09:27:50 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-10 09:27:50 +0800 |
commit | 719bddcd76610a63dadc8555760072957a11cf30 (patch) | |
tree | 0df26092fba7e3e21444fe493e6b349473b6b0cb /models/repo/repo_indexer.go | |
parent | fb8166c6c6b652a0e6fa98681780a6a71090faf3 (diff) | |
download | gitea-719bddcd76610a63dadc8555760072957a11cf30.tar.gz gitea-719bddcd76610a63dadc8555760072957a11cf30.zip |
Move repository model into models/repo (#17933)
* Some refactors related repository model
* Move more methods out of repository
* Move repository into models/repo
* Fix test
* Fix test
* some improvements
* Remove unnecessary function
Diffstat (limited to 'models/repo/repo_indexer.go')
-rw-r--r-- | models/repo/repo_indexer.go | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/models/repo/repo_indexer.go b/models/repo/repo_indexer.go new file mode 100644 index 0000000000..f442cad4d1 --- /dev/null +++ b/models/repo/repo_indexer.go @@ -0,0 +1,125 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package repo + +import ( + "fmt" + + "code.gitea.io/gitea/models/db" + + "xorm.io/builder" +) + +// RepoIndexerType specifies the repository indexer type +type RepoIndexerType int //revive:disable-line:exported + +const ( + // RepoIndexerTypeCode code indexer + RepoIndexerTypeCode RepoIndexerType = iota // 0 + // RepoIndexerTypeStats repository stats indexer + RepoIndexerTypeStats // 1 +) + +// RepoIndexerStatus status of a repo's entry in the repo indexer +// For now, implicitly refers to default branch +type RepoIndexerStatus struct { //revive:disable-line:exported + ID int64 `xorm:"pk autoincr"` + RepoID int64 `xorm:"INDEX(s)"` + CommitSha string `xorm:"VARCHAR(40)"` + IndexerType RepoIndexerType `xorm:"INDEX(s) NOT NULL DEFAULT 0"` +} + +func init() { + db.RegisterModel(new(RepoIndexerStatus)) +} + +// GetUnindexedRepos returns repos which do not have an indexer status +func GetUnindexedRepos(indexerType RepoIndexerType, maxRepoID int64, page, pageSize int) ([]int64, error) { + ids := make([]int64, 0, 50) + cond := builder.Cond(builder.IsNull{ + "repo_indexer_status.id", + }).And(builder.Eq{ + "repository.is_empty": false, + }) + sess := db.GetEngine(db.DefaultContext).Table("repository").Join("LEFT OUTER", "repo_indexer_status", "repository.id = repo_indexer_status.repo_id AND repo_indexer_status.indexer_type = ?", indexerType) + if maxRepoID > 0 { + cond = builder.And(cond, builder.Lte{ + "repository.id": maxRepoID, + }) + } + if page >= 0 && pageSize > 0 { + start := 0 + if page > 0 { + start = (page - 1) * pageSize + } + sess.Limit(pageSize, start) + } + + sess.Where(cond).Cols("repository.id").Desc("repository.id") + err := sess.Find(&ids) + return ids, err +} + +// getIndexerStatus loads repo codes indxer status +func getIndexerStatus(e db.Engine, repo *Repository, indexerType RepoIndexerType) (*RepoIndexerStatus, error) { + switch indexerType { + case RepoIndexerTypeCode: + if repo.CodeIndexerStatus != nil { + return repo.CodeIndexerStatus, nil + } + case RepoIndexerTypeStats: + if repo.StatsIndexerStatus != nil { + return repo.StatsIndexerStatus, nil + } + } + status := &RepoIndexerStatus{RepoID: repo.ID} + if has, err := e.Where("`indexer_type` = ?", indexerType).Get(status); err != nil { + return nil, err + } else if !has { + status.IndexerType = indexerType + status.CommitSha = "" + } + switch indexerType { + case RepoIndexerTypeCode: + repo.CodeIndexerStatus = status + case RepoIndexerTypeStats: + repo.StatsIndexerStatus = status + } + return status, nil +} + +// GetIndexerStatus loads repo codes indxer status +func GetIndexerStatus(repo *Repository, indexerType RepoIndexerType) (*RepoIndexerStatus, error) { + return getIndexerStatus(db.GetEngine(db.DefaultContext), repo, indexerType) +} + +// updateIndexerStatus updates indexer status +func updateIndexerStatus(e db.Engine, repo *Repository, indexerType RepoIndexerType, sha string) error { + status, err := getIndexerStatus(e, repo, indexerType) + if err != nil { + return fmt.Errorf("UpdateIndexerStatus: Unable to getIndexerStatus for repo: %s Error: %v", repo.FullName(), err) + } + + if len(status.CommitSha) == 0 { + status.CommitSha = sha + _, err := e.Insert(status) + if err != nil { + return fmt.Errorf("UpdateIndexerStatus: Unable to insert repoIndexerStatus for repo: %s Sha: %s Error: %v", repo.FullName(), sha, err) + } + return nil + } + status.CommitSha = sha + _, err = e.ID(status.ID).Cols("commit_sha"). + Update(status) + if err != nil { + return fmt.Errorf("UpdateIndexerStatus: Unable to update repoIndexerStatus for repo: %s Sha: %s Error: %v", repo.FullName(), sha, err) + } + return nil +} + +// UpdateIndexerStatus updates indexer status +func UpdateIndexerStatus(repo *Repository, indexerType RepoIndexerType, sha string) error { + return updateIndexerStatus(db.GetEngine(db.DefaultContext), repo, indexerType, sha) +} |