Skip to content

Commit b2a93cd

Browse files
committed
Remove unused data from indexer/search
1 parent f2fc87b commit b2a93cd

File tree

18 files changed

+69
-1191
lines changed

18 files changed

+69
-1191
lines changed

models/conversations/conversation_search.go

Lines changed: 8 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ package conversations
66
import (
77
"context"
88
"fmt"
9-
"strconv"
109

1110
"code.gitea.io/gitea/models/db"
1211
"code.gitea.io/gitea/models/organization"
13-
repo_model "code.gitea.io/gitea/models/repo"
1412
user_model "code.gitea.io/gitea/models/user"
15-
"code.gitea.io/gitea/modules/container"
1613
"code.gitea.io/gitea/modules/optional"
1714

1815
"xorm.io/builder"
@@ -21,29 +18,14 @@ import (
2118

2219
// ConversationsOptions represents options of an conversation.
2320
type ConversationsOptions struct { //nolint
24-
Paginator *db.ListOptions
25-
RepoIDs []int64 // overwrites RepoCond if the length is not 0
26-
AllPublic bool // include also all public repositories
27-
RepoCond builder.Cond
28-
AssigneeID int64
29-
PosterID int64
30-
MentionedID int64
31-
ReviewRequestedID int64
32-
ReviewedID int64
33-
SubscriberID int64
34-
MilestoneIDs []int64
35-
ProjectID int64
36-
ProjectColumnID int64
37-
IsClosed optional.Option[bool]
38-
IsPull optional.Option[bool]
39-
LabelIDs []int64
40-
IncludedLabelNames []string
41-
ExcludedLabelNames []string
42-
IncludeMilestones []string
43-
SortType string
44-
ConversationIDs []int64
45-
UpdatedAfterUnix int64
46-
UpdatedBeforeUnix int64
21+
Paginator *db.ListOptions
22+
RepoIDs []int64 // overwrites RepoCond if the length is not 0
23+
AllPublic bool // include also all public repositories
24+
RepoCond builder.Cond
25+
SortType string
26+
ConversationIDs []int64
27+
UpdatedAfterUnix int64
28+
UpdatedBeforeUnix int64
4729
// prioritize conversations from this repo
4830
PriorityRepoID int64
4931
IsArchived optional.Option[bool]
@@ -124,75 +106,6 @@ func applyLimit(sess *xorm.Session, opts *ConversationsOptions) {
124106
sess.Limit(opts.Paginator.PageSize, start)
125107
}
126108

127-
func applyLabelsCondition(sess *xorm.Session, opts *ConversationsOptions) {
128-
if len(opts.LabelIDs) > 0 {
129-
if opts.LabelIDs[0] == 0 {
130-
sess.Where("conversation.id NOT IN (SELECT conversation_id FROM conversation_label)")
131-
} else {
132-
// deduplicate the label IDs for inclusion and exclusion
133-
includedLabelIDs := make(container.Set[int64])
134-
excludedLabelIDs := make(container.Set[int64])
135-
for _, labelID := range opts.LabelIDs {
136-
if labelID > 0 {
137-
includedLabelIDs.Add(labelID)
138-
} else if labelID < 0 { // 0 is not supported here, so just ignore it
139-
excludedLabelIDs.Add(-labelID)
140-
}
141-
}
142-
// ... and use them in a subquery of the form :
143-
// where (select count(*) from conversation_label where conversation_id=conversation.id and label_id in (2, 4, 6)) = 3
144-
// This equality is guaranteed thanks to unique index (conversation_id,label_id) on table conversation_label.
145-
if len(includedLabelIDs) > 0 {
146-
subQuery := builder.Select("count(*)").From("conversation_label").Where(builder.Expr("conversation_id = conversation.id")).
147-
And(builder.In("label_id", includedLabelIDs.Values()))
148-
sess.Where(builder.Eq{strconv.Itoa(len(includedLabelIDs)): subQuery})
149-
}
150-
// or (select count(*)...) = 0 for excluded labels
151-
if len(excludedLabelIDs) > 0 {
152-
subQuery := builder.Select("count(*)").From("conversation_label").Where(builder.Expr("conversation_id = conversation.id")).
153-
And(builder.In("label_id", excludedLabelIDs.Values()))
154-
sess.Where(builder.Eq{"0": subQuery})
155-
}
156-
}
157-
}
158-
}
159-
160-
func applyMilestoneCondition(sess *xorm.Session, opts *ConversationsOptions) {
161-
if len(opts.MilestoneIDs) == 1 && opts.MilestoneIDs[0] == db.NoConditionID {
162-
sess.And("conversation.milestone_id = 0")
163-
} else if len(opts.MilestoneIDs) > 0 {
164-
sess.In("conversation.milestone_id", opts.MilestoneIDs)
165-
}
166-
167-
if len(opts.IncludeMilestones) > 0 {
168-
sess.In("conversation.milestone_id",
169-
builder.Select("id").
170-
From("milestone").
171-
Where(builder.In("name", opts.IncludeMilestones)))
172-
}
173-
}
174-
175-
func applyProjectCondition(sess *xorm.Session, opts *ConversationsOptions) {
176-
if opts.ProjectID > 0 { // specific project
177-
sess.Join("INNER", "project_conversation", "conversation.id = project_conversation.conversation_id").
178-
And("project_conversation.project_id=?", opts.ProjectID)
179-
} else if opts.ProjectID == db.NoConditionID { // show those that are in no project
180-
sess.And(builder.NotIn("conversation.id", builder.Select("conversation_id").From("project_conversation").And(builder.Neq{"project_id": 0})))
181-
}
182-
// opts.ProjectID == 0 means all projects,
183-
// do not need to apply any condition
184-
}
185-
186-
func applyProjectColumnCondition(sess *xorm.Session, opts *ConversationsOptions) {
187-
// opts.ProjectColumnID == 0 means all project columns,
188-
// do not need to apply any condition
189-
if opts.ProjectColumnID > 0 {
190-
sess.In("conversation.id", builder.Select("conversation_id").From("project_conversation").Where(builder.Eq{"project_board_id": opts.ProjectColumnID}))
191-
} else if opts.ProjectColumnID == db.NoConditionID {
192-
sess.In("conversation.id", builder.Select("conversation_id").From("project_conversation").Where(builder.Eq{"project_board_id": 0}))
193-
}
194-
}
195-
196109
func applyRepoConditions(sess *xorm.Session, opts *ConversationsOptions) {
197110
if len(opts.RepoIDs) == 1 {
198111
opts.RepoCond = builder.Eq{"conversation.repo_id": opts.RepoIDs[0]}
@@ -217,85 +130,16 @@ func applyConditions(sess *xorm.Session, opts *ConversationsOptions) {
217130

218131
applyRepoConditions(sess, opts)
219132

220-
if opts.IsClosed.Has() {
221-
sess.And("conversation.is_closed=?", opts.IsClosed.Value())
222-
}
223-
224-
if opts.PosterID > 0 {
225-
applyPosterCondition(sess, opts.PosterID)
226-
}
227-
228-
if opts.MentionedID > 0 {
229-
applyMentionedCondition(sess, opts.MentionedID)
230-
}
231-
232-
if opts.SubscriberID > 0 {
233-
applySubscribedCondition(sess, opts.SubscriberID)
234-
}
235-
236-
applyMilestoneCondition(sess, opts)
237-
238133
if opts.UpdatedAfterUnix != 0 {
239134
sess.And(builder.Gte{"conversation.updated_unix": opts.UpdatedAfterUnix})
240135
}
241136
if opts.UpdatedBeforeUnix != 0 {
242137
sess.And(builder.Lte{"conversation.updated_unix": opts.UpdatedBeforeUnix})
243138
}
244139

245-
applyProjectCondition(sess, opts)
246-
247-
applyProjectColumnCondition(sess, opts)
248-
249-
if opts.IsPull.Has() {
250-
sess.And("conversation.is_pull=?", opts.IsPull.Value())
251-
}
252-
253140
if opts.IsArchived.Has() {
254141
sess.And(builder.Eq{"repository.is_archived": opts.IsArchived.Value()})
255142
}
256-
257-
applyLabelsCondition(sess, opts)
258-
}
259-
260-
func applyPosterCondition(sess *xorm.Session, posterID int64) {
261-
sess.And("conversation.poster_id=?", posterID)
262-
}
263-
264-
func applyMentionedCondition(sess *xorm.Session, mentionedID int64) {
265-
sess.Join("INNER", "conversation_user", "conversation.id = conversation_user.conversation_id").
266-
And("conversation_user.is_mentioned = ?", true).
267-
And("conversation_user.uid = ?", mentionedID)
268-
}
269-
270-
func applySubscribedCondition(sess *xorm.Session, subscriberID int64) {
271-
sess.And(
272-
builder.
273-
NotIn("conversation.id",
274-
builder.Select("conversation_id").
275-
From("conversation_watch").
276-
Where(builder.Eq{"is_watching": false, "user_id": subscriberID}),
277-
),
278-
).And(
279-
builder.Or(
280-
builder.In("conversation.id", builder.
281-
Select("conversation_id").
282-
From("conversation_watch").
283-
Where(builder.Eq{"is_watching": true, "user_id": subscriberID}),
284-
),
285-
builder.In("conversation.id", builder.
286-
Select("conversation_id").
287-
From("comment").
288-
Where(builder.Eq{"poster_id": subscriberID}),
289-
),
290-
builder.Eq{"conversation.poster_id": subscriberID},
291-
builder.In("conversation.repo_id", builder.
292-
Select("id").
293-
From("watch").
294-
Where(builder.And(builder.Eq{"user_id": subscriberID},
295-
builder.In("mode", repo_model.WatchModeNormal, repo_model.WatchModeAuto))),
296-
),
297-
),
298-
)
299143
}
300144

301145
// Conversations returns a list of conversations by given conditions.

models/conversations/conversation_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ func TestConversations(t *testing.T) {
7373
}{
7474
{
7575
conversations_model.ConversationsOptions{
76-
AssigneeID: 1,
77-
SortType: "oldest",
76+
SortType: "oldest",
7877
},
7978
[]int64{1, 6},
8079
},
@@ -91,7 +90,6 @@ func TestConversations(t *testing.T) {
9190
},
9291
{
9392
conversations_model.ConversationsOptions{
94-
LabelIDs: []int64{1},
9593
Paginator: &db.ListOptions{
9694
Page: 1,
9795
PageSize: 4,
@@ -101,7 +99,6 @@ func TestConversations(t *testing.T) {
10199
},
102100
{
103101
conversations_model.ConversationsOptions{
104-
LabelIDs: []int64{1, 2},
105102
Paginator: &db.ListOptions{
106103
Page: 1,
107104
PageSize: 4,
@@ -110,9 +107,7 @@ func TestConversations(t *testing.T) {
110107
[]int64{}, // conversations with **both** label 1 and 2, none of these conversations matches, TODO: add more tests
111108
},
112109
{
113-
conversations_model.ConversationsOptions{
114-
MilestoneIDs: []int64{1},
115-
},
110+
conversations_model.ConversationsOptions{},
116111
[]int64{2},
117112
},
118113
} {

modules/indexer/conversations/bleve/bleve.go

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018 The Gitea Authors. All rights reserved.
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

44
package bleve
@@ -179,78 +179,6 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
179179
queries = append(queries, bleve.NewDisjunctionQuery(repoQueries...))
180180
}
181181

182-
if options.IsPull.Has() {
183-
queries = append(queries, inner_bleve.BoolFieldQuery(options.IsPull.Value(), "is_pull"))
184-
}
185-
if options.IsClosed.Has() {
186-
queries = append(queries, inner_bleve.BoolFieldQuery(options.IsClosed.Value(), "is_closed"))
187-
}
188-
189-
if options.NoLabelOnly {
190-
queries = append(queries, inner_bleve.BoolFieldQuery(true, "no_label"))
191-
} else {
192-
if len(options.IncludedLabelIDs) > 0 {
193-
var includeQueries []query.Query
194-
for _, labelID := range options.IncludedLabelIDs {
195-
includeQueries = append(includeQueries, inner_bleve.NumericEqualityQuery(labelID, "label_ids"))
196-
}
197-
queries = append(queries, bleve.NewConjunctionQuery(includeQueries...))
198-
} else if len(options.IncludedAnyLabelIDs) > 0 {
199-
var includeQueries []query.Query
200-
for _, labelID := range options.IncludedAnyLabelIDs {
201-
includeQueries = append(includeQueries, inner_bleve.NumericEqualityQuery(labelID, "label_ids"))
202-
}
203-
queries = append(queries, bleve.NewDisjunctionQuery(includeQueries...))
204-
}
205-
if len(options.ExcludedLabelIDs) > 0 {
206-
var excludeQueries []query.Query
207-
for _, labelID := range options.ExcludedLabelIDs {
208-
q := bleve.NewBooleanQuery()
209-
q.AddMustNot(inner_bleve.NumericEqualityQuery(labelID, "label_ids"))
210-
excludeQueries = append(excludeQueries, q)
211-
}
212-
queries = append(queries, bleve.NewConjunctionQuery(excludeQueries...))
213-
}
214-
}
215-
216-
if len(options.MilestoneIDs) > 0 {
217-
var milestoneQueries []query.Query
218-
for _, milestoneID := range options.MilestoneIDs {
219-
milestoneQueries = append(milestoneQueries, inner_bleve.NumericEqualityQuery(milestoneID, "milestone_id"))
220-
}
221-
queries = append(queries, bleve.NewDisjunctionQuery(milestoneQueries...))
222-
}
223-
224-
if options.ProjectID.Has() {
225-
queries = append(queries, inner_bleve.NumericEqualityQuery(options.ProjectID.Value(), "project_id"))
226-
}
227-
if options.ProjectColumnID.Has() {
228-
queries = append(queries, inner_bleve.NumericEqualityQuery(options.ProjectColumnID.Value(), "project_board_id"))
229-
}
230-
231-
if options.PosterID.Has() {
232-
queries = append(queries, inner_bleve.NumericEqualityQuery(options.PosterID.Value(), "poster_id"))
233-
}
234-
235-
if options.AssigneeID.Has() {
236-
queries = append(queries, inner_bleve.NumericEqualityQuery(options.AssigneeID.Value(), "assignee_id"))
237-
}
238-
239-
if options.MentionID.Has() {
240-
queries = append(queries, inner_bleve.NumericEqualityQuery(options.MentionID.Value(), "mention_ids"))
241-
}
242-
243-
if options.ReviewedID.Has() {
244-
queries = append(queries, inner_bleve.NumericEqualityQuery(options.ReviewedID.Value(), "reviewed_ids"))
245-
}
246-
if options.ReviewRequestedID.Has() {
247-
queries = append(queries, inner_bleve.NumericEqualityQuery(options.ReviewRequestedID.Value(), "review_requested_ids"))
248-
}
249-
250-
if options.SubscriberID.Has() {
251-
queries = append(queries, inner_bleve.NumericEqualityQuery(options.SubscriberID.Value(), "subscriber_ids"))
252-
}
253-
254182
if options.UpdatedAfterUnix.Has() || options.UpdatedBeforeUnix.Has() {
255183
queries = append(queries, inner_bleve.NumericRangeInclusiveQuery(
256184
options.UpdatedAfterUnix,

modules/indexer/conversations/bleve/bleve_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018 The Gitea Authors. All rights reserved.
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

44
package bleve

modules/indexer/conversations/db/db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 The Gitea Authors. All rights reserved.
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

44
package db

0 commit comments

Comments
 (0)