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.
package models
2019-12-15 12:51:28 +03:00
import (
"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
type RepoIndexerType int
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
type RepoIndexerStatus struct {
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
}
2020-02-11 12:34:17 +03:00
// getIndexerStatus loads repo codes indxer status
2021-09-19 14:49:59 +03:00
func ( repo * Repository ) getIndexerStatus ( e db . Engine , 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 }
if has , err := e . 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
}
2020-02-11 12:34:17 +03:00
// GetIndexerStatus loads repo codes indxer status
func ( repo * Repository ) GetIndexerStatus ( indexerType RepoIndexerType ) ( * RepoIndexerStatus , error ) {
2021-09-23 18:45:36 +03:00
return repo . getIndexerStatus ( db . GetEngine ( db . DefaultContext ) , indexerType )
2020-02-11 12:34:17 +03:00
}
// updateIndexerStatus updates indexer status
2021-09-19 14:49:59 +03:00
func ( repo * Repository ) updateIndexerStatus ( e db . Engine , indexerType RepoIndexerType , sha string ) error {
2020-02-11 12:34:17 +03:00
status , err := repo . getIndexerStatus ( e , indexerType )
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
_ , err := e . Insert ( 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 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
_ , err = e . ID ( status . ID ) . Cols ( "commit_sha" ) .
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
}
2020-02-11 12:34:17 +03:00
// UpdateIndexerStatus updates indexer status
func ( repo * Repository ) UpdateIndexerStatus ( indexerType RepoIndexerType , sha string ) error {
2021-09-23 18:45:36 +03:00
return repo . updateIndexerStatus ( db . GetEngine ( db . DefaultContext ) , indexerType , sha )
2020-02-11 12:34:17 +03:00
}