From 37863693568635593b02cfc414e099d493e5d0c7 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 1 Nov 2018 08:21:31 +0800 Subject: This commit will reduce join star, repo_topic, topic tables on repo search, so that fix extra columns problem on mssql (#5136) (#5229) * This commit will reduce join star, repo_topic, topic tables on repo search, so that fix extra columns problem on mssql * fix tests --- models/repo_list.go | 55 +++++++++++++++++------------------------------- models/repo_list_test.go | 3 +++ 2 files changed, 22 insertions(+), 36 deletions(-) diff --git a/models/repo_list.go b/models/repo_list.go index c7e292d8a9..049d1834f7 100644 --- a/models/repo_list.go +++ b/models/repo_list.go @@ -173,11 +173,9 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err cond = cond.And(builder.Eq{"is_private": false}) } - var starred bool if opts.OwnerID > 0 { if opts.Starred { - starred = true - cond = builder.Eq{"star.uid": opts.OwnerID} + cond = cond.And(builder.In("id", builder.Select("repo_id").From("star").Where(builder.Eq{"uid": opts.OwnerID}))) } else { var accessCond = builder.NewCond() if opts.Collaborate != util.OptionalBoolTrue { @@ -204,12 +202,23 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err } if opts.Keyword != "" { - var keywordCond = builder.NewCond() - if opts.TopicOnly { - keywordCond = keywordCond.Or(builder.Like{"topic.name", strings.ToLower(opts.Keyword)}) - } else { - keywordCond = keywordCond.Or(builder.Like{"lower_name", strings.ToLower(opts.Keyword)}) - keywordCond = keywordCond.Or(builder.Like{"topic.name", strings.ToLower(opts.Keyword)}) + // separate keyword + var subQueryCond = builder.NewCond() + for _, v := range strings.Split(opts.Keyword, ",") { + subQueryCond = subQueryCond.Or(builder.Like{"topic.name", strings.ToLower(v)}) + } + subQuery := builder.Select("repo_topic.repo_id").From("repo_topic"). + Join("INNER", "topic", "topic.id = repo_topic.topic_id"). + Where(subQueryCond). + GroupBy("repo_topic.repo_id") + + var keywordCond = builder.In("id", subQuery) + if !opts.TopicOnly { + var likes = builder.NewCond() + for _, v := range strings.Split(opts.Keyword, ",") { + likes = likes.Or(builder.Like{"lower_name", strings.ToLower(v)}) + } + keywordCond = keywordCond.Or(likes) } cond = cond.And(keywordCond) } @@ -229,15 +238,6 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err sess := x.NewSession() defer sess.Close() - if starred { - sess.Join("INNER", "star", "star.repo_id = repository.id") - } - - if opts.Keyword != "" { - sess.Join("LEFT", "repo_topic", "repo_topic.repo_id = repository.id") - sess.Join("LEFT", "topic", "repo_topic.topic_id = topic.id") - } - count, err := sess. Where(cond). Count(new(Repository)) @@ -246,27 +246,10 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err return nil, 0, fmt.Errorf("Count: %v", err) } - // Set again after reset by Count() - if starred { - sess.Join("INNER", "star", "star.repo_id = repository.id") - } - - if opts.Keyword != "" { - sess.Join("LEFT", "repo_topic", "repo_topic.repo_id = repository.id") - sess.Join("LEFT", "topic", "repo_topic.topic_id = topic.id") - } - - if opts.Keyword != "" { - sess.Select("repository.*") - sess.GroupBy("repository.id") - sess.OrderBy("repository." + opts.OrderBy.String()) - } else { - sess.OrderBy(opts.OrderBy.String()) - } - repos := make(RepositoryList, 0, opts.PageSize) if err = sess. Where(cond). + OrderBy(opts.OrderBy.String()). Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). Find(&repos); err != nil { return nil, 0, fmt.Errorf("Repo: %v", err) diff --git a/models/repo_list_test.go b/models/repo_list_test.go index 8f4947dbb2..c032af2b80 100644 --- a/models/repo_list_test.go +++ b/models/repo_list_test.go @@ -237,6 +237,9 @@ func TestSearchRepositoryByTopicName(t *testing.T) { {name: "AllPublic/OnlySearchPublicRepositoriesFromTopic", opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql", TopicOnly: true}, count: 1}, + {name: "AllPublic/OnlySearchMultipleKeywordPublicRepositoriesFromTopic", + opts: &SearchRepoOptions{OwnerID: 21, AllPublic: true, Keyword: "graphql,golang", TopicOnly: true}, + count: 2}, } for _, testCase := range testCases { -- cgit v1.2.3