2017-01-24 21:43:02 -05: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-16 16:16:21 -04:00
|
|
|
"code.gitea.io/gitea/modules/indexer"
|
2017-01-24 21:43:02 -05:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
)
|
|
|
|
|
2017-09-16 16:16:21 -04:00
|
|
|
// issueIndexerUpdateQueue queue of issue ids to be updated
|
|
|
|
var issueIndexerUpdateQueue chan int64
|
2017-01-24 21:43:02 -05:00
|
|
|
|
|
|
|
// InitIssueIndexer initialize issue indexer
|
|
|
|
func InitIssueIndexer() {
|
2017-09-16 16:16:21 -04:00
|
|
|
indexer.InitIssueIndexer(populateIssueIndexer)
|
|
|
|
issueIndexerUpdateQueue = make(chan int64, setting.Indexer.UpdateQueueLength)
|
2017-01-24 21:43:02 -05:00
|
|
|
go processIssueIndexerUpdateQueue()
|
|
|
|
}
|
|
|
|
|
|
|
|
// populateIssueIndexer populate the issue indexer with issue data
|
|
|
|
func populateIssueIndexer() error {
|
2017-09-24 20:08:48 -04:00
|
|
|
batch := indexer.IssueIndexerBatch()
|
2017-01-24 21:43:02 -05:00
|
|
|
for page := 1; ; page++ {
|
2017-10-10 16:37:18 -04:00
|
|
|
repos, _, err := SearchRepositoryByName(&SearchRepoOptions{
|
2017-10-26 17:16:13 -04:00
|
|
|
Page: page,
|
2017-12-31 09:45:46 -05:00
|
|
|
PageSize: RepositoryListDefaultPageSize,
|
2017-10-26 17:16:13 -04:00
|
|
|
OrderBy: SearchOrderByID,
|
|
|
|
Private: true,
|
|
|
|
Collaborate: util.OptionalBoolFalse,
|
2017-01-24 21:43:02 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Repositories: %v", err)
|
|
|
|
}
|
|
|
|
if len(repos) == 0 {
|
2017-09-24 20:08:48 -04:00
|
|
|
return batch.Flush()
|
2017-01-24 21:43:02 -05:00
|
|
|
}
|
|
|
|
for _, repo := range repos {
|
|
|
|
issues, err := Issues(&IssuesOptions{
|
2017-12-25 18:25:16 -05:00
|
|
|
RepoIDs: []int64{repo.ID},
|
2017-01-24 21:43:02 -05:00
|
|
|
IsClosed: util.OptionalBoolNone,
|
|
|
|
IsPull: util.OptionalBoolNone,
|
|
|
|
})
|
2017-09-24 20:08:48 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-01-24 21:43:02 -05:00
|
|
|
}
|
2017-11-21 00:28:22 -05:00
|
|
|
if err = IssueList(issues).LoadComments(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-24 20:08:48 -04:00
|
|
|
for _, issue := range issues {
|
2018-02-05 13:29:17 -05:00
|
|
|
if err := issue.update().AddToFlushingBatch(batch); err != nil {
|
2017-09-24 20:08:48 -04:00
|
|
|
return err
|
|
|
|
}
|
2017-01-24 21:43:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func processIssueIndexerUpdateQueue() {
|
2017-09-24 20:08:48 -04:00
|
|
|
batch := indexer.IssueIndexerBatch()
|
2017-01-24 21:43:02 -05:00
|
|
|
for {
|
2017-09-24 20:08:48 -04:00
|
|
|
var issueID int64
|
2017-01-24 21:43:02 -05:00
|
|
|
select {
|
2017-09-24 20:08:48 -04:00
|
|
|
case issueID = <-issueIndexerUpdateQueue:
|
|
|
|
default:
|
|
|
|
// flush whatever updates we currently have, since we
|
|
|
|
// might have to wait a while
|
|
|
|
if err := batch.Flush(); err != nil {
|
|
|
|
log.Error(4, "IssueIndexer: %v", err)
|
2017-01-24 21:43:02 -05:00
|
|
|
}
|
2017-09-24 20:08:48 -04:00
|
|
|
issueID = <-issueIndexerUpdateQueue
|
|
|
|
}
|
|
|
|
issue, err := GetIssueByID(issueID)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(4, "GetIssueByID: %v", err)
|
2018-02-05 13:29:17 -05:00
|
|
|
} else if err = issue.update().AddToFlushingBatch(batch); err != nil {
|
2017-09-24 20:08:48 -04:00
|
|
|
log.Error(4, "IssueIndexer: %v", err)
|
2017-01-24 21:43:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-16 16:16:21 -04: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-24 21:43:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 06:53:02 -05:00
|
|
|
// updateNeededCols whether a change to the specified columns requires updating
|
|
|
|
// the issue indexer
|
|
|
|
func updateNeededCols(cols []string) bool {
|
|
|
|
for _, col := range cols {
|
|
|
|
switch col {
|
|
|
|
case "name", "content":
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateIssueIndexerCols update an issue in the issue indexer, given changes
|
|
|
|
// to the specified columns
|
|
|
|
func UpdateIssueIndexerCols(issueID int64, cols ...string) {
|
2019-01-17 09:23:22 -05:00
|
|
|
updateNeededCols(cols)
|
2017-12-17 06:53:02 -05:00
|
|
|
}
|
|
|
|
|
2017-01-24 21:43:02 -05:00
|
|
|
// UpdateIssueIndexer add/update an issue to the issue indexer
|
2017-09-16 16:16:21 -04:00
|
|
|
func UpdateIssueIndexer(issueID int64) {
|
|
|
|
select {
|
|
|
|
case issueIndexerUpdateQueue <- issueID:
|
|
|
|
default:
|
|
|
|
go func() {
|
|
|
|
issueIndexerUpdateQueue <- issueID
|
|
|
|
}()
|
|
|
|
}
|
2017-01-24 21:43:02 -05:00
|
|
|
}
|