2017-10-27 02:10:54 -04:00
|
|
|
// 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.
|
|
|
|
|
2021-12-09 20:27:50 -05:00
|
|
|
package repo
|
2017-10-27 02:10:54 -04:00
|
|
|
|
2019-12-15 04:51:28 -05:00
|
|
|
import (
|
2022-05-20 10:08:52 -04:00
|
|
|
"context"
|
2019-12-15 04:51:28 -05:00
|
|
|
"fmt"
|
|
|
|
|
2021-09-19 07:49:59 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-17 07:34:35 -05:00
|
|
|
|
2019-12-15 04:51:28 -05:00
|
|
|
"xorm.io/builder"
|
|
|
|
)
|
|
|
|
|
2020-02-11 04:34:17 -05:00
|
|
|
// RepoIndexerType specifies the repository indexer type
|
2021-12-09 20:27:50 -05:00
|
|
|
type RepoIndexerType int //revive:disable-line:exported
|
2020-02-11 04:34:17 -05:00
|
|
|
|
|
|
|
const (
|
|
|
|
// RepoIndexerTypeCode code indexer
|
|
|
|
RepoIndexerTypeCode RepoIndexerType = iota // 0
|
|
|
|
// RepoIndexerTypeStats repository stats indexer
|
|
|
|
RepoIndexerTypeStats // 1
|
|
|
|
)
|
|
|
|
|
2017-10-27 02:10:54 -04:00
|
|
|
// RepoIndexerStatus status of a repo's entry in the repo indexer
|
|
|
|
// For now, implicitly refers to default branch
|
2021-12-09 20:27:50 -05:00
|
|
|
type RepoIndexerStatus struct { //revive:disable-line:exported
|
2020-02-11 04:34:17 -05:00
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
RepoID int64 `xorm:"INDEX(s)"`
|
|
|
|
CommitSha string `xorm:"VARCHAR(40)"`
|
|
|
|
IndexerType RepoIndexerType `xorm:"INDEX(s) NOT NULL DEFAULT 0"`
|
2017-10-27 02:10:54 -04:00
|
|
|
}
|
|
|
|
|
2021-09-19 07:49:59 -04:00
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(RepoIndexerStatus))
|
|
|
|
}
|
|
|
|
|
2019-12-15 04:51:28 -05:00
|
|
|
// GetUnindexedRepos returns repos which do not have an indexer status
|
2020-02-11 04:34:17 -05:00
|
|
|
func GetUnindexedRepos(indexerType RepoIndexerType, maxRepoID int64, page, pageSize int) ([]int64, error) {
|
2019-12-15 04:51:28 -05:00
|
|
|
ids := make([]int64, 0, 50)
|
|
|
|
cond := builder.Cond(builder.IsNull{
|
|
|
|
"repo_indexer_status.id",
|
2020-02-14 07:42:30 -05:00
|
|
|
}).And(builder.Eq{
|
|
|
|
"repository.is_empty": false,
|
2019-12-15 04:51:28 -05:00
|
|
|
})
|
2021-09-23 11:45:36 -04:00
|
|
|
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)
|
2019-12-15 04:51:28 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
// GetIndexerStatus loads repo codes indxer status
|
|
|
|
func GetIndexerStatus(ctx context.Context, repo *Repository, indexerType RepoIndexerType) (*RepoIndexerStatus, error) {
|
2020-02-11 04:34:17 -05:00
|
|
|
switch indexerType {
|
|
|
|
case RepoIndexerTypeCode:
|
|
|
|
if repo.CodeIndexerStatus != nil {
|
|
|
|
return repo.CodeIndexerStatus, nil
|
|
|
|
}
|
|
|
|
case RepoIndexerTypeStats:
|
|
|
|
if repo.StatsIndexerStatus != nil {
|
|
|
|
return repo.StatsIndexerStatus, nil
|
|
|
|
}
|
2017-10-27 02:10:54 -04:00
|
|
|
}
|
2020-02-14 07:42:30 -05:00
|
|
|
status := &RepoIndexerStatus{RepoID: repo.ID}
|
2022-05-20 10:08:52 -04:00
|
|
|
if has, err := db.GetEngine(ctx).Where("`indexer_type` = ?", indexerType).Get(status); err != nil {
|
2020-02-11 04:34:17 -05:00
|
|
|
return nil, err
|
2017-10-27 02:10:54 -04:00
|
|
|
} else if !has {
|
2020-02-14 07:42:30 -05:00
|
|
|
status.IndexerType = indexerType
|
2017-10-27 02:10:54 -04:00
|
|
|
status.CommitSha = ""
|
|
|
|
}
|
2020-02-11 04:34:17 -05:00
|
|
|
switch indexerType {
|
|
|
|
case RepoIndexerTypeCode:
|
|
|
|
repo.CodeIndexerStatus = status
|
|
|
|
case RepoIndexerTypeStats:
|
|
|
|
repo.StatsIndexerStatus = status
|
|
|
|
}
|
|
|
|
return status, nil
|
2017-10-27 02:10:54 -04:00
|
|
|
}
|
|
|
|
|
2022-05-20 10:08:52 -04:00
|
|
|
// UpdateIndexerStatus updates indexer status
|
|
|
|
func UpdateIndexerStatus(ctx context.Context, repo *Repository, indexerType RepoIndexerType, sha string) error {
|
|
|
|
status, err := GetIndexerStatus(ctx, repo, indexerType)
|
2020-02-11 04:34:17 -05:00
|
|
|
if err != nil {
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("UpdateIndexerStatus: Unable to getIndexerStatus for repo: %s Error: %w", repo.FullName(), err)
|
2017-10-27 02:10:54 -04:00
|
|
|
}
|
2020-02-11 04:34:17 -05:00
|
|
|
|
|
|
|
if len(status.CommitSha) == 0 {
|
|
|
|
status.CommitSha = sha
|
2022-05-20 10:08:52 -04:00
|
|
|
if err := db.Insert(ctx, status); err != nil {
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("UpdateIndexerStatus: Unable to insert repoIndexerStatus for repo: %s Sha: %s Error: %w", repo.FullName(), sha, err)
|
2019-12-15 04:51:28 -05:00
|
|
|
}
|
|
|
|
return nil
|
2017-10-27 02:10:54 -04:00
|
|
|
}
|
2020-02-11 04:34:17 -05:00
|
|
|
status.CommitSha = sha
|
2022-05-20 10:08:52 -04:00
|
|
|
_, err = db.GetEngine(ctx).ID(status.ID).Cols("commit_sha").
|
2020-02-11 04:34:17 -05:00
|
|
|
Update(status)
|
2019-12-15 04:51:28 -05:00
|
|
|
if err != nil {
|
2022-10-24 15:29:17 -04:00
|
|
|
return fmt.Errorf("UpdateIndexerStatus: Unable to update repoIndexerStatus for repo: %s Sha: %s Error: %w", repo.FullName(), sha, err)
|
2019-12-15 04:51:28 -05:00
|
|
|
}
|
|
|
|
return nil
|
2017-10-27 02:10:54 -04:00
|
|
|
}
|