Refactor for issues loadattributes of a repository (#971)
* refactor for issues loadattributes of a repository * refactors
This commit is contained in:
21
models/helper.go
Normal file
21
models/helper.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
// 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
|
||||||
|
|
||||||
|
func keysInt64(m map[int64]struct{}) []int64 {
|
||||||
|
var keys = make([]int64, 0, len(m))
|
||||||
|
for k, _ := range m {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
|
||||||
|
func valuesRepository(m map[int64]*Repository) []*Repository {
|
||||||
|
var values = make([]*Repository, 0, len(m))
|
||||||
|
for _, v := range m {
|
||||||
|
values = append(values, v)
|
||||||
|
}
|
||||||
|
return values
|
||||||
|
}
|
@ -1128,11 +1128,8 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
|
|||||||
return nil, fmt.Errorf("Find: %v", err)
|
return nil, fmt.Errorf("Find: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: use IssueList to improve performance.
|
if err := IssueList(issues).LoadAttributes(); err != nil {
|
||||||
for i := range issues {
|
return nil, fmt.Errorf("LoadAttributes: %v", err)
|
||||||
if err := issues[i].LoadAttributes(); err != nil {
|
|
||||||
return nil, fmt.Errorf("LoadAttributes [%d]: %v", issues[i].ID, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return issues, nil
|
return issues, nil
|
||||||
@ -1399,62 +1396,3 @@ func updateIssue(e Engine, issue *Issue) error {
|
|||||||
func UpdateIssue(issue *Issue) error {
|
func UpdateIssue(issue *Issue) error {
|
||||||
return updateIssue(x, issue)
|
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)
|
|
||||||
}
|
|
||||||
|
320
models/issue_list.go
Normal file
320
models/issue_list.go
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user