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