Skip to content

Commit

Permalink
Refactor for issues loadattributes of a repository (go-gitea#971)
Browse files Browse the repository at this point in the history
* refactor for issues loadattributes of a repository

* refactors
  • Loading branch information
lunny authored Feb 22, 2017
1 parent 29c6f32 commit 1f7837d
Show file tree
Hide file tree
Showing 3 changed files with 343 additions and 64 deletions.
21 changes: 21 additions & 0 deletions models/helper.go
Original file line number Diff line number Diff line change
@@ -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
}
66 changes: 2 additions & 64 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Loading

0 comments on commit 1f7837d

Please sign in to comment.