From 9d2b830275bf766b9f5e48e3be4b785dc1b2134a Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Wed, 15 Feb 2017 14:01:50 +0800 Subject: [PATCH] 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. --- models/repo.go | 40 ++++++++++++++++++++++++---------------- models/star.go | 8 +++----- routers/home.go | 14 +++----------- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/models/repo.go b/models/repo.go index 97bad686b9..55e9d4b1c9 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1184,25 +1184,23 @@ func CountUserRepositories(userID int64, private bool) int64 { } // Repositories returns all repositories -func Repositories(opts *SearchRepoOptions) (_ []*Repository, err error) { +func Repositories(opts *SearchRepoOptions) (_ RepositoryList, err error) { if len(opts.OrderBy) == 0 { opts.OrderBy = "id ASC" } - repos := make([]*Repository, 0, opts.PageSize) - return repos, x.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).OrderBy(opts.OrderBy).Find(&repos) -} + repos := make(RepositoryList, 0, opts.PageSize) -// RepositoriesWithUsers returns number of repos in given page. -func RepositoriesWithUsers(opts *SearchRepoOptions) (_ []*Repository, err error) { - repos, err := Repositories(opts) - if err != nil { - return nil, fmt.Errorf("Repositories: %v", err) + if err = x. + Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). + OrderBy(opts.OrderBy). + Find(&repos); err != nil { + return nil, fmt.Errorf("Repo: %v", err) } - for i := range repos { - if err = repos[i].GetOwner(); err != nil { - return nil, err + if opts.Searcher != nil || opts.Starred { + if err = repos.loadAttributes(x); err != nil { + 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. -func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos []*Repository, err error) { +func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos RepositoryList, err error) { if len(opts.OrderBy) == 0 { opts.OrderBy = "updated_unix DESC" } @@ -1739,9 +1737,19 @@ func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos []*Repository, } } - return repos, sess. + if err = sess. 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) { @@ -1848,7 +1856,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ in return nil, 0, fmt.Errorf("Repo: %v", err) } - if opts.Starred { + if opts.Searcher != nil || opts.Starred { if err = repos.loadAttributes(x); err != nil { return nil, 0, fmt.Errorf("LoadAttributes: %v", err) } diff --git a/models/star.go b/models/star.go index 7525c8a6fa..96f876ba0a 100644 --- a/models/star.go +++ b/models/star.go @@ -71,7 +71,7 @@ func (repo *Repository) GetStargazers(page int) ([]*User, error) { } // 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 { orderBy = "updated_unix DESC" } @@ -95,10 +95,8 @@ func (u *User) GetStarredRepos(private bool, page, pageSize int, orderBy string) return } - for _, repo := range repos { - if err = repo.GetOwner(); err != nil { - return - } + if err = repos.loadAttributes(x); err != nil { + return } return diff --git a/routers/home.go b/routers/home.go index 6f211f4f62..3de32b52e7 100644 --- a/routers/home.go +++ b/routers/home.go @@ -6,16 +6,15 @@ package routers import ( "bytes" - "fmt" "strings" - "github.com/Unknwon/paginater" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/routers/user" + + "github.com/Unknwon/paginater" ) const ( @@ -55,7 +54,7 @@ func Home(ctx *context.Context) { // RepoSearchOptions when calling search repositories type RepoSearchOptions struct { Counter func(bool) int64 - Ranger func(*models.SearchRepoOptions) ([]*models.Repository, error) + Ranger func(*models.SearchRepoOptions) (models.RepositoryList, error) Searcher *models.User Private bool PageSize int @@ -132,13 +131,6 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { ctx.Data["Keyword"] = keyword ctx.Data["Total"] = count 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.HTML(200, opts.TplName)