refactor: small optimize for sql query (#940)

* refactor: small optimize for sql query

* fix: get owner name if Searcher is not nil or user star page.
This commit is contained in:
Bo-Yi Wu 2017-02-15 14:01:50 +08:00 committed by Lunny Xiao
parent 837d346090
commit 9d2b830275
3 changed files with 30 additions and 32 deletions

View File

@ -1184,25 +1184,23 @@ func CountUserRepositories(userID int64, private bool) int64 {
} }
// Repositories returns all repositories // Repositories returns all repositories
func Repositories(opts *SearchRepoOptions) (_ []*Repository, err error) { func Repositories(opts *SearchRepoOptions) (_ RepositoryList, err error) {
if len(opts.OrderBy) == 0 { if len(opts.OrderBy) == 0 {
opts.OrderBy = "id ASC" opts.OrderBy = "id ASC"
} }
repos := make([]*Repository, 0, opts.PageSize) repos := make(RepositoryList, 0, opts.PageSize)
return repos, x.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).OrderBy(opts.OrderBy).Find(&repos)
}
// RepositoriesWithUsers returns number of repos in given page. if err = x.
func RepositoriesWithUsers(opts *SearchRepoOptions) (_ []*Repository, err error) { Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
repos, err := Repositories(opts) OrderBy(opts.OrderBy).
if err != nil { Find(&repos); err != nil {
return nil, fmt.Errorf("Repositories: %v", err) return nil, fmt.Errorf("Repo: %v", err)
} }
for i := range repos { if opts.Searcher != nil || opts.Starred {
if err = repos[i].GetOwner(); err != nil { if err = repos.loadAttributes(x); err != nil {
return nil, err return nil, fmt.Errorf("LoadAttributes: %v", err)
} }
} }
@ -1716,7 +1714,7 @@ func GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
} }
// GetRecentUpdatedRepositories returns the list of repositories that are recently updated. // GetRecentUpdatedRepositories returns the list of repositories that are recently updated.
func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos []*Repository, err error) { func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos RepositoryList, err error) {
if len(opts.OrderBy) == 0 { if len(opts.OrderBy) == 0 {
opts.OrderBy = "updated_unix DESC" opts.OrderBy = "updated_unix DESC"
} }
@ -1739,9 +1737,19 @@ func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos []*Repository,
} }
} }
return repos, sess. if err = sess.
OrderBy(opts.OrderBy). OrderBy(opts.OrderBy).
Find(&repos) Find(&repos); err != nil {
return nil, fmt.Errorf("Repo: %v", err)
}
if opts.Searcher != nil || opts.Starred {
if err = repos.loadAttributes(x); err != nil {
return nil, fmt.Errorf("LoadAttributes: %v", err)
}
}
return repos, nil
} }
func getRepositoryCount(e Engine, u *User) (int64, error) { func getRepositoryCount(e Engine, u *User) (int64, error) {
@ -1848,7 +1856,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ in
return nil, 0, fmt.Errorf("Repo: %v", err) return nil, 0, fmt.Errorf("Repo: %v", err)
} }
if opts.Starred { if opts.Searcher != nil || opts.Starred {
if err = repos.loadAttributes(x); err != nil { if err = repos.loadAttributes(x); err != nil {
return nil, 0, fmt.Errorf("LoadAttributes: %v", err) return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
} }

View File

@ -71,7 +71,7 @@ func (repo *Repository) GetStargazers(page int) ([]*User, error) {
} }
// GetStarredRepos returns the repos the user starred. // GetStarredRepos returns the repos the user starred.
func (u *User) GetStarredRepos(private bool, page, pageSize int, orderBy string) (repos []*Repository, err error) { func (u *User) GetStarredRepos(private bool, page, pageSize int, orderBy string) (repos RepositoryList, err error) {
if len(orderBy) == 0 { if len(orderBy) == 0 {
orderBy = "updated_unix DESC" orderBy = "updated_unix DESC"
} }
@ -95,10 +95,8 @@ func (u *User) GetStarredRepos(private bool, page, pageSize int, orderBy string)
return return
} }
for _, repo := range repos { if err = repos.loadAttributes(x); err != nil {
if err = repo.GetOwner(); err != nil { return
return
}
} }
return return

View File

@ -6,16 +6,15 @@ package routers
import ( import (
"bytes" "bytes"
"fmt"
"strings" "strings"
"github.com/Unknwon/paginater"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/routers/user" "code.gitea.io/gitea/routers/user"
"github.com/Unknwon/paginater"
) )
const ( const (
@ -55,7 +54,7 @@ func Home(ctx *context.Context) {
// RepoSearchOptions when calling search repositories // RepoSearchOptions when calling search repositories
type RepoSearchOptions struct { type RepoSearchOptions struct {
Counter func(bool) int64 Counter func(bool) int64
Ranger func(*models.SearchRepoOptions) ([]*models.Repository, error) Ranger func(*models.SearchRepoOptions) (models.RepositoryList, error)
Searcher *models.User Searcher *models.User
Private bool Private bool
PageSize int PageSize int
@ -132,13 +131,6 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
ctx.Data["Keyword"] = keyword ctx.Data["Keyword"] = keyword
ctx.Data["Total"] = count ctx.Data["Total"] = count
ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5) ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
for _, repo := range repos {
if err = repo.GetOwner(); err != nil {
ctx.Handle(500, "GetOwner", fmt.Errorf("%d: %v", repo.ID, err))
return
}
}
ctx.Data["Repos"] = repos ctx.Data["Repos"] = repos
ctx.HTML(200, opts.TplName) ctx.HTML(200, opts.TplName)