diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2017-02-22 22:03:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-22 22:03:59 +0800 |
commit | 1f7837d6d62471d72e2b2c15f8ed07cd0a43fda9 (patch) | |
tree | da1e57bb80753a8a5560f12163a6c4301c2ccf1c /models/issue.go | |
parent | 29c6f32a3b2701dcbcb0d94c999c922e59bb38ec (diff) | |
download | gitea-1f7837d6d62471d72e2b2c15f8ed07cd0a43fda9.tar.gz gitea-1f7837d6d62471d72e2b2c15f8ed07cd0a43fda9.zip |
Refactor for issues loadattributes of a repository (#971)
* refactor for issues loadattributes of a repository
* refactors
Diffstat (limited to 'models/issue.go')
-rw-r--r-- | models/issue.go | 66 |
1 files changed, 2 insertions, 64 deletions
diff --git a/models/issue.go b/models/issue.go index b160004fd5..4677da401e 100644 --- a/models/issue.go +++ b/models/issue.go @@ -1128,11 +1128,8 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) { return nil, fmt.Errorf("Find: %v", err) } - // FIXME: use IssueList to improve performance. - for i := range issues { - if err := issues[i].LoadAttributes(); err != nil { - return nil, fmt.Errorf("LoadAttributes [%d]: %v", issues[i].ID, err) - } + if err := IssueList(issues).LoadAttributes(); err != nil { + return nil, fmt.Errorf("LoadAttributes: %v", err) } return issues, nil @@ -1399,62 +1396,3 @@ func updateIssue(e Engine, issue *Issue) error { func UpdateIssue(issue *Issue) error { return updateIssue(x, issue) } - -// IssueList defines a list of issues -type IssueList []*Issue - -func (issues IssueList) getRepoIDs() []int64 { - repoIDs := make([]int64, 0, len(issues)) - for _, issue := range issues { - var has bool - for _, repoID := range repoIDs { - if repoID == issue.RepoID { - has = true - break - } - } - if !has { - repoIDs = append(repoIDs, issue.RepoID) - } - } - return repoIDs -} - -func (issues IssueList) loadRepositories(e Engine) ([]*Repository, error) { - if len(issues) == 0 { - return nil, nil - } - - repoIDs := issues.getRepoIDs() - rows, err := e. - Where("id > 0"). - In("id", repoIDs). - Rows(new(Repository)) - if err != nil { - return nil, fmt.Errorf("find repository: %v", err) - } - defer rows.Close() - - repositories := make([]*Repository, 0, len(repoIDs)) - repoMaps := make(map[int64]*Repository, len(repoIDs)) - for rows.Next() { - var repo Repository - err = rows.Scan(&repo) - if err != nil { - return nil, fmt.Errorf("find repository: %v", err) - } - - repositories = append(repositories, &repo) - repoMaps[repo.ID] = &repo - } - - for _, issue := range issues { - issue.Repo = repoMaps[issue.RepoID] - } - return repositories, nil -} - -// LoadRepositories loads issues' all repositories -func (issues IssueList) LoadRepositories() ([]*Repository, error) { - return issues.loadRepositories(x) -} |