summaryrefslogtreecommitdiffstats
path: root/models/repo_indexer.go
diff options
context:
space:
mode:
authorEthan Koenig <ethantkoenig@gmail.com>2018-01-14 09:34:41 -0800
committerLauris BH <lauris@nix.lv>2018-01-14 19:34:41 +0200
commitccdb94992dd30664f64cd4f5ef715b16b626a0d2 (patch)
treed2edd6ecefd2702e09d243de31b0b119892e4a06 /models/repo_indexer.go
parenta8325dd1cbb02fb6135d078c257b4103be6b62f2 (diff)
downloadgitea-ccdb94992dd30664f64cd4f5ef715b16b626a0d2.tar.gz
gitea-ccdb94992dd30664f64cd4f5ef715b16b626a0d2.zip
Asynchronously populate the repo indexer (#3366)
* Populate repo indexer in background * Check if no repos exist * race cond
Diffstat (limited to 'models/repo_indexer.go')
-rw-r--r--models/repo_indexer.go58
1 files changed, 41 insertions, 17 deletions
diff --git a/models/repo_indexer.go b/models/repo_indexer.go
index 8cc08904ed..41c9f0fa31 100644
--- a/models/repo_indexer.go
+++ b/models/repo_indexer.go
@@ -70,34 +70,58 @@ func InitRepoIndexer() {
if !setting.Indexer.RepoIndexerEnabled {
return
}
- indexer.InitRepoIndexer(populateRepoIndexer)
repoIndexerOperationQueue = make(chan repoIndexerOperation, setting.Indexer.UpdateQueueLength)
+ indexer.InitRepoIndexer(populateRepoIndexerAsynchronously)
go processRepoIndexerOperationQueue()
}
-// populateRepoIndexer populate the repo indexer with data
-func populateRepoIndexer() error {
- log.Info("Populating repository indexer (this may take a while)")
- for page := 1; ; page++ {
- repos, _, err := SearchRepositoryByName(&SearchRepoOptions{
- Page: page,
- PageSize: RepositoryListDefaultPageSize,
- OrderBy: SearchOrderByID,
- Private: true,
- })
+// populateRepoIndexerAsynchronously asynchronously populates the repo indexer
+// with pre-existing data. This should only be run when the indexer is created
+// for the first time.
+func populateRepoIndexerAsynchronously() error {
+ exist, err := x.Table("repository").Exist()
+ if err != nil {
+ return err
+ } else if !exist {
+ return nil
+ }
+
+ var maxRepoID int64
+ if _, err = x.Select("MAX(id)").Table("repository").Get(&maxRepoID); err != nil {
+ return err
+ }
+ go populateRepoIndexer(maxRepoID)
+ return nil
+}
+
+// populateRepoIndexer populate the repo indexer with pre-existing data. This
+// should only be run when the indexer is created for the first time.
+func populateRepoIndexer(maxRepoID int64) {
+ log.Info("Populating the repo indexer with existing repositories")
+ // start with the maximum existing repo ID and work backwards, so that we
+ // don't include repos that are created after gitea starts; such repos will
+ // already be added to the indexer, and we don't need to add them again.
+ for maxRepoID > 0 {
+ repos := make([]*Repository, 0, RepositoryListDefaultPageSize)
+ err := x.Where("id <= ?", maxRepoID).
+ OrderBy("id DESC").
+ Limit(RepositoryListDefaultPageSize).
+ Find(&repos)
if err != nil {
- return err
+ log.Error(4, "populateRepoIndexer: %v", err)
+ return
} else if len(repos) == 0 {
- return nil
+ break
}
for _, repo := range repos {
- if err = updateRepoIndexer(repo); err != nil {
- // only log error, since this should not prevent
- // gitea from starting up
- log.Error(4, "updateRepoIndexer: repoID=%d, %v", repo.ID, err)
+ repoIndexerOperationQueue <- repoIndexerOperation{
+ repo: repo,
+ deleted: false,
}
+ maxRepoID = repo.ID - 1
}
}
+ log.Info("Done populating the repo indexer with existing repositories")
}
func updateRepoIndexer(repo *Repository) error {