2017-01-25 10:43:02 +08: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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-09-17 04:16:21 +08:00
|
|
|
"code.gitea.io/gitea/modules/indexer"
|
2017-01-25 10:43:02 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
)
|
|
|
|
|
2017-09-17 04:16:21 +08:00
|
|
|
// issueIndexerUpdateQueue queue of issue ids to be updated
|
|
|
|
var issueIndexerUpdateQueue chan int64
|
2017-01-25 10:43:02 +08:00
|
|
|
|
|
|
|
// InitIssueIndexer initialize issue indexer
|
|
|
|
func InitIssueIndexer() {
|
2017-09-17 04:16:21 +08:00
|
|
|
indexer.InitIssueIndexer(populateIssueIndexer)
|
|
|
|
issueIndexerUpdateQueue = make(chan int64, setting.Indexer.UpdateQueueLength)
|
2017-01-25 10:43:02 +08:00
|
|
|
go processIssueIndexerUpdateQueue()
|
|
|
|
}
|
|
|
|
|
|
|
|
// populateIssueIndexer populate the issue indexer with issue data
|
|
|
|
func populateIssueIndexer() error {
|
|
|
|
for page := 1; ; page++ {
|
2017-02-26 13:59:31 +08:00
|
|
|
repos, _, err := Repositories(&SearchRepoOptions{
|
2017-01-25 10:43:02 +08:00
|
|
|
Page: page,
|
|
|
|
PageSize: 10,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Repositories: %v", err)
|
|
|
|
}
|
|
|
|
if len(repos) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, repo := range repos {
|
|
|
|
issues, err := Issues(&IssuesOptions{
|
|
|
|
RepoID: repo.ID,
|
|
|
|
IsClosed: util.OptionalBoolNone,
|
|
|
|
IsPull: util.OptionalBoolNone,
|
|
|
|
})
|
2017-09-17 04:16:21 +08:00
|
|
|
updates := make([]indexer.IssueIndexerUpdate, len(issues))
|
|
|
|
for i, issue := range issues {
|
|
|
|
updates[i] = issue.update()
|
2017-01-25 10:43:02 +08:00
|
|
|
}
|
2017-09-17 04:16:21 +08:00
|
|
|
if err = indexer.BatchUpdateIssues(updates...); err != nil {
|
|
|
|
return fmt.Errorf("BatchUpdate: %v", err)
|
2017-01-25 10:43:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func processIssueIndexerUpdateQueue() {
|
|
|
|
for {
|
|
|
|
select {
|
2017-09-17 04:16:21 +08:00
|
|
|
case issueID := <-issueIndexerUpdateQueue:
|
|
|
|
issue, err := GetIssueByID(issueID)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(4, "issuesIndexer.Index: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err = indexer.UpdateIssue(issue.update()); err != nil {
|
2017-01-25 10:43:02 +08:00
|
|
|
log.Error(4, "issuesIndexer.Index: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 04:16:21 +08:00
|
|
|
func (issue *Issue) update() indexer.IssueIndexerUpdate {
|
|
|
|
comments := make([]string, 0, 5)
|
|
|
|
for _, comment := range issue.Comments {
|
|
|
|
if comment.Type == CommentTypeComment {
|
|
|
|
comments = append(comments, comment.Content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return indexer.IssueIndexerUpdate{
|
|
|
|
IssueID: issue.ID,
|
|
|
|
Data: &indexer.IssueIndexerData{
|
|
|
|
RepoID: issue.RepoID,
|
|
|
|
Title: issue.Title,
|
|
|
|
Content: issue.Content,
|
|
|
|
Comments: comments,
|
|
|
|
},
|
2017-01-25 10:43:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateIssueIndexer add/update an issue to the issue indexer
|
2017-09-17 04:16:21 +08:00
|
|
|
func UpdateIssueIndexer(issueID int64) {
|
|
|
|
select {
|
|
|
|
case issueIndexerUpdateQueue <- issueID:
|
|
|
|
default:
|
|
|
|
go func() {
|
|
|
|
issueIndexerUpdateQueue <- issueID
|
|
|
|
}()
|
|
|
|
}
|
2017-01-25 10:43:02 +08:00
|
|
|
}
|