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