Browse Source

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.
Bo-Yi Wu 2 years ago
parent
commit
9d2b830275
3 changed files with 30 additions and 32 deletions
  1. 24 16
      models/repo.go
  2. 3 5
      models/star.go
  3. 3 11
      routers/home.go

+ 24 - 16
models/repo.go

@@ -1184,25 +1184,23 @@ func CountUserRepositories(userID int64, private bool) int64 {
1184 1184
 }
1185 1185
 
1186 1186
 // Repositories returns all repositories
1187
-func Repositories(opts *SearchRepoOptions) (_ []*Repository, err error) {
1187
+func Repositories(opts *SearchRepoOptions) (_ RepositoryList, err error) {
1188 1188
 	if len(opts.OrderBy) == 0 {
1189 1189
 		opts.OrderBy = "id ASC"
1190 1190
 	}
1191 1191
 
1192
-	repos := make([]*Repository, 0, opts.PageSize)
1193
-	return repos, x.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).OrderBy(opts.OrderBy).Find(&repos)
1194
-}
1192
+	repos := make(RepositoryList, 0, opts.PageSize)
1195 1193
 
1196
-// RepositoriesWithUsers returns number of repos in given page.
1197
-func RepositoriesWithUsers(opts *SearchRepoOptions) (_ []*Repository, err error) {
1198
-	repos, err := Repositories(opts)
1199
-	if err != nil {
1200
-		return nil, fmt.Errorf("Repositories: %v", err)
1194
+	if err = x.
1195
+		Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
1196
+		OrderBy(opts.OrderBy).
1197
+		Find(&repos); err != nil {
1198
+		return nil, fmt.Errorf("Repo: %v", err)
1201 1199
 	}
1202 1200
 
1203
-	for i := range repos {
1204
-		if err = repos[i].GetOwner(); err != nil {
1205
-			return nil, err
1201
+	if opts.Searcher != nil || opts.Starred {
1202
+		if err = repos.loadAttributes(x); err != nil {
1203
+			return nil, fmt.Errorf("LoadAttributes: %v", err)
1206 1204
 		}
1207 1205
 	}
1208 1206
 
@@ -1716,7 +1714,7 @@ func GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
1716 1714
 }
1717 1715
 
1718 1716
 // GetRecentUpdatedRepositories returns the list of repositories that are recently updated.
1719
-func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos []*Repository, err error) {
1717
+func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos RepositoryList, err error) {
1720 1718
 	if len(opts.OrderBy) == 0 {
1721 1719
 		opts.OrderBy = "updated_unix DESC"
1722 1720
 	}
@@ -1739,9 +1737,19 @@ func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos []*Repository,
1739 1737
 		}
1740 1738
 	}
1741 1739
 
1742
-	return repos, sess.
1740
+	if err = sess.
1743 1741
 		OrderBy(opts.OrderBy).
1744
-		Find(&repos)
1742
+		Find(&repos); err != nil {
1743
+		return nil, fmt.Errorf("Repo: %v", err)
1744
+	}
1745
+
1746
+	if opts.Searcher != nil || opts.Starred {
1747
+		if err = repos.loadAttributes(x); err != nil {
1748
+			return nil, fmt.Errorf("LoadAttributes: %v", err)
1749
+		}
1750
+	}
1751
+
1752
+	return repos, nil
1745 1753
 }
1746 1754
 
1747 1755
 func getRepositoryCount(e Engine, u *User) (int64, error) {
@@ -1848,7 +1856,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ in
1848 1856
 		return nil, 0, fmt.Errorf("Repo: %v", err)
1849 1857
 	}
1850 1858
 
1851
-	if opts.Starred {
1859
+	if opts.Searcher != nil || opts.Starred {
1852 1860
 		if err = repos.loadAttributes(x); err != nil {
1853 1861
 			return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
1854 1862
 		}

+ 3 - 5
models/star.go

@@ -71,7 +71,7 @@ func (repo *Repository) GetStargazers(page int) ([]*User, error) {
71 71
 }
72 72
 
73 73
 // GetStarredRepos returns the repos the user starred.
74
-func (u *User) GetStarredRepos(private bool, page, pageSize int, orderBy string) (repos []*Repository, err error) {
74
+func (u *User) GetStarredRepos(private bool, page, pageSize int, orderBy string) (repos RepositoryList, err error) {
75 75
 	if len(orderBy) == 0 {
76 76
 		orderBy = "updated_unix DESC"
77 77
 	}
@@ -95,10 +95,8 @@ func (u *User) GetStarredRepos(private bool, page, pageSize int, orderBy string)
95 95
 		return
96 96
 	}
97 97
 
98
-	for _, repo := range repos {
99
-		if err = repo.GetOwner(); err != nil {
100
-			return
101
-		}
98
+	if err = repos.loadAttributes(x); err != nil {
99
+		return
102 100
 	}
103 101
 
104 102
 	return

+ 3 - 11
routers/home.go

@@ -6,16 +6,15 @@ package routers
6 6
 
7 7
 import (
8 8
 	"bytes"
9
-	"fmt"
10 9
 	"strings"
11 10
 
12
-	"github.com/Unknwon/paginater"
13
-
14 11
 	"code.gitea.io/gitea/models"
15 12
 	"code.gitea.io/gitea/modules/base"
16 13
 	"code.gitea.io/gitea/modules/context"
17 14
 	"code.gitea.io/gitea/modules/setting"
18 15
 	"code.gitea.io/gitea/routers/user"
16
+
17
+	"github.com/Unknwon/paginater"
19 18
 )
20 19
 
21 20
 const (
@@ -55,7 +54,7 @@ func Home(ctx *context.Context) {
55 54
 // RepoSearchOptions when calling search repositories
56 55
 type RepoSearchOptions struct {
57 56
 	Counter  func(bool) int64
58
-	Ranger   func(*models.SearchRepoOptions) ([]*models.Repository, error)
57
+	Ranger   func(*models.SearchRepoOptions) (models.RepositoryList, error)
59 58
 	Searcher *models.User
60 59
 	Private  bool
61 60
 	PageSize int
@@ -132,13 +131,6 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
132 131
 	ctx.Data["Keyword"] = keyword
133 132
 	ctx.Data["Total"] = count
134 133
 	ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
135
-
136
-	for _, repo := range repos {
137
-		if err = repo.GetOwner(); err != nil {
138
-			ctx.Handle(500, "GetOwner", fmt.Errorf("%d: %v", repo.ID, err))
139
-			return
140
-		}
141
-	}
142 134
 	ctx.Data["Repos"] = repos
143 135
 
144 136
 	ctx.HTML(200, opts.TplName)