Skip to content

Commit 7fd0a5b

Browse files
authored
Refactor to use optional.Option for issue index search option (#29739)
Signed-off-by: 6543 <[email protected]>
1 parent 67e9f0d commit 7fd0a5b

File tree

12 files changed

+178
-225
lines changed

12 files changed

+178
-225
lines changed

modules/indexer/internal/bleve/query.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package bleve
55

66
import (
7+
"code.gitea.io/gitea/modules/optional"
8+
79
"github.com/blevesearch/bleve/v2"
810
"github.com/blevesearch/bleve/v2/search/query"
911
)
@@ -39,18 +41,18 @@ func BoolFieldQuery(value bool, field string) *query.BoolFieldQuery {
3941
return q
4042
}
4143

42-
func NumericRangeInclusiveQuery(min, max *int64, field string) *query.NumericRangeQuery {
44+
func NumericRangeInclusiveQuery(min, max optional.Option[int64], field string) *query.NumericRangeQuery {
4345
var minF, maxF *float64
4446
var minI, maxI *bool
45-
if min != nil {
47+
if min.Has() {
4648
minF = new(float64)
47-
*minF = float64(*min)
49+
*minF = float64(min.Value())
4850
minI = new(bool)
4951
*minI = true
5052
}
51-
if max != nil {
53+
if max.Has() {
5254
maxF = new(float64)
53-
*maxF = float64(*max)
55+
*maxF = float64(max.Value())
5456
maxI = new(bool)
5557
*maxI = true
5658
}

modules/indexer/issues/bleve/bleve.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -224,38 +224,41 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
224224
queries = append(queries, bleve.NewDisjunctionQuery(milestoneQueries...))
225225
}
226226

227-
if options.ProjectID != nil {
228-
queries = append(queries, inner_bleve.NumericEqualityQuery(*options.ProjectID, "project_id"))
227+
if options.ProjectID.Has() {
228+
queries = append(queries, inner_bleve.NumericEqualityQuery(options.ProjectID.Value(), "project_id"))
229229
}
230-
if options.ProjectBoardID != nil {
231-
queries = append(queries, inner_bleve.NumericEqualityQuery(*options.ProjectBoardID, "project_board_id"))
230+
if options.ProjectBoardID.Has() {
231+
queries = append(queries, inner_bleve.NumericEqualityQuery(options.ProjectBoardID.Value(), "project_board_id"))
232232
}
233233

234-
if options.PosterID != nil {
235-
queries = append(queries, inner_bleve.NumericEqualityQuery(*options.PosterID, "poster_id"))
234+
if options.PosterID.Has() {
235+
queries = append(queries, inner_bleve.NumericEqualityQuery(options.PosterID.Value(), "poster_id"))
236236
}
237237

238-
if options.AssigneeID != nil {
239-
queries = append(queries, inner_bleve.NumericEqualityQuery(*options.AssigneeID, "assignee_id"))
238+
if options.AssigneeID.Has() {
239+
queries = append(queries, inner_bleve.NumericEqualityQuery(options.AssigneeID.Value(), "assignee_id"))
240240
}
241241

242-
if options.MentionID != nil {
243-
queries = append(queries, inner_bleve.NumericEqualityQuery(*options.MentionID, "mention_ids"))
242+
if options.MentionID.Has() {
243+
queries = append(queries, inner_bleve.NumericEqualityQuery(options.MentionID.Value(), "mention_ids"))
244244
}
245245

246-
if options.ReviewedID != nil {
247-
queries = append(queries, inner_bleve.NumericEqualityQuery(*options.ReviewedID, "reviewed_ids"))
246+
if options.ReviewedID.Has() {
247+
queries = append(queries, inner_bleve.NumericEqualityQuery(options.ReviewedID.Value(), "reviewed_ids"))
248248
}
249-
if options.ReviewRequestedID != nil {
250-
queries = append(queries, inner_bleve.NumericEqualityQuery(*options.ReviewRequestedID, "review_requested_ids"))
249+
if options.ReviewRequestedID.Has() {
250+
queries = append(queries, inner_bleve.NumericEqualityQuery(options.ReviewRequestedID.Value(), "review_requested_ids"))
251251
}
252252

253-
if options.SubscriberID != nil {
254-
queries = append(queries, inner_bleve.NumericEqualityQuery(*options.SubscriberID, "subscriber_ids"))
253+
if options.SubscriberID.Has() {
254+
queries = append(queries, inner_bleve.NumericEqualityQuery(options.SubscriberID.Value(), "subscriber_ids"))
255255
}
256256

257-
if options.UpdatedAfterUnix != nil || options.UpdatedBeforeUnix != nil {
258-
queries = append(queries, inner_bleve.NumericRangeInclusiveQuery(options.UpdatedAfterUnix, options.UpdatedBeforeUnix, "updated_unix"))
257+
if options.UpdatedAfterUnix.Has() || options.UpdatedBeforeUnix.Has() {
258+
queries = append(queries, inner_bleve.NumericRangeInclusiveQuery(
259+
options.UpdatedAfterUnix,
260+
options.UpdatedBeforeUnix,
261+
"updated_unix"))
259262
}
260263

261264
var indexerQuery query.Query = bleve.NewConjunctionQuery(queries...)

modules/indexer/issues/db/options.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,6 @@ import (
1515
)
1616

1717
func ToDBOptions(ctx context.Context, options *internal.SearchOptions) (*issue_model.IssuesOptions, error) {
18-
// See the comment of issues_model.SearchOptions for the reason why we need to convert
19-
convertID := func(id *int64) int64 {
20-
if id == nil {
21-
return 0
22-
}
23-
if *id == 0 {
24-
return db.NoConditionID
25-
}
26-
return *id
27-
}
28-
convertInt64 := func(i *int64) int64 {
29-
if i == nil {
30-
return 0
31-
}
32-
return *i
33-
}
3418
var sortType string
3519
switch options.SortBy {
3620
case internal.SortByCreatedAsc:
@@ -53,6 +37,18 @@ func ToDBOptions(ctx context.Context, options *internal.SearchOptions) (*issue_m
5337
sortType = "newest"
5438
}
5539

40+
// See the comment of issues_model.SearchOptions for the reason why we need to convert
41+
convertID := func(id optional.Option[int64]) int64 {
42+
if !id.Has() {
43+
return 0
44+
}
45+
value := id.Value()
46+
if value == 0 {
47+
return db.NoConditionID
48+
}
49+
return value
50+
}
51+
5652
opts := &issue_model.IssuesOptions{
5753
Paginator: options.Paginator,
5854
RepoIDs: options.RepoIDs,
@@ -73,8 +69,8 @@ func ToDBOptions(ctx context.Context, options *internal.SearchOptions) (*issue_m
7369
IncludeMilestones: nil,
7470
SortType: sortType,
7571
IssueIDs: nil,
76-
UpdatedAfterUnix: convertInt64(options.UpdatedAfterUnix),
77-
UpdatedBeforeUnix: convertInt64(options.UpdatedBeforeUnix),
72+
UpdatedAfterUnix: options.UpdatedAfterUnix.Value(),
73+
UpdatedBeforeUnix: options.UpdatedBeforeUnix.Value(),
7874
PriorityRepoID: 0,
7975
IsArchived: optional.None[bool](),
8076
Org: nil,

modules/indexer/issues/dboptions.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package issues
66
import (
77
"code.gitea.io/gitea/models/db"
88
issues_model "code.gitea.io/gitea/models/issues"
9+
"code.gitea.io/gitea/modules/optional"
910
)
1011

1112
func ToSearchOptions(keyword string, opts *issues_model.IssuesOptions) *SearchOptions {
@@ -38,13 +39,12 @@ func ToSearchOptions(keyword string, opts *issues_model.IssuesOptions) *SearchOp
3839
}
3940

4041
// See the comment of issues_model.SearchOptions for the reason why we need to convert
41-
convertID := func(id int64) *int64 {
42+
convertID := func(id int64) optional.Option[int64] {
4243
if id > 0 {
43-
return &id
44+
return optional.Some(id)
4445
}
4546
if id == db.NoConditionID {
46-
var zero int64
47-
return &zero
47+
return optional.None[int64]()
4848
}
4949
return nil
5050
}
@@ -59,10 +59,10 @@ func ToSearchOptions(keyword string, opts *issues_model.IssuesOptions) *SearchOp
5959
searchOpt.SubscriberID = convertID(opts.SubscriberID)
6060

6161
if opts.UpdatedAfterUnix > 0 {
62-
searchOpt.UpdatedAfterUnix = &opts.UpdatedAfterUnix
62+
searchOpt.UpdatedAfterUnix = optional.Some(opts.UpdatedAfterUnix)
6363
}
6464
if opts.UpdatedBeforeUnix > 0 {
65-
searchOpt.UpdatedBeforeUnix = &opts.UpdatedBeforeUnix
65+
searchOpt.UpdatedBeforeUnix = optional.Some(opts.UpdatedBeforeUnix)
6666
}
6767

6868
searchOpt.Paginator = opts.Paginator

modules/indexer/issues/elasticsearch/elasticsearch.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -195,43 +195,43 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
195195
query.Must(elastic.NewTermsQuery("milestone_id", toAnySlice(options.MilestoneIDs)...))
196196
}
197197

198-
if options.ProjectID != nil {
199-
query.Must(elastic.NewTermQuery("project_id", *options.ProjectID))
198+
if options.ProjectID.Has() {
199+
query.Must(elastic.NewTermQuery("project_id", options.ProjectID.Value()))
200200
}
201-
if options.ProjectBoardID != nil {
202-
query.Must(elastic.NewTermQuery("project_board_id", *options.ProjectBoardID))
201+
if options.ProjectBoardID.Has() {
202+
query.Must(elastic.NewTermQuery("project_board_id", options.ProjectBoardID.Value()))
203203
}
204204

205-
if options.PosterID != nil {
206-
query.Must(elastic.NewTermQuery("poster_id", *options.PosterID))
205+
if options.PosterID.Has() {
206+
query.Must(elastic.NewTermQuery("poster_id", options.PosterID.Value()))
207207
}
208208

209-
if options.AssigneeID != nil {
210-
query.Must(elastic.NewTermQuery("assignee_id", *options.AssigneeID))
209+
if options.AssigneeID.Has() {
210+
query.Must(elastic.NewTermQuery("assignee_id", options.AssigneeID.Value()))
211211
}
212212

213-
if options.MentionID != nil {
214-
query.Must(elastic.NewTermQuery("mention_ids", *options.MentionID))
213+
if options.MentionID.Has() {
214+
query.Must(elastic.NewTermQuery("mention_ids", options.MentionID.Value()))
215215
}
216216

217-
if options.ReviewedID != nil {
218-
query.Must(elastic.NewTermQuery("reviewed_ids", *options.ReviewedID))
217+
if options.ReviewedID.Has() {
218+
query.Must(elastic.NewTermQuery("reviewed_ids", options.ReviewedID.Value()))
219219
}
220-
if options.ReviewRequestedID != nil {
221-
query.Must(elastic.NewTermQuery("review_requested_ids", *options.ReviewRequestedID))
220+
if options.ReviewRequestedID.Has() {
221+
query.Must(elastic.NewTermQuery("review_requested_ids", options.ReviewRequestedID.Value()))
222222
}
223223

224-
if options.SubscriberID != nil {
225-
query.Must(elastic.NewTermQuery("subscriber_ids", *options.SubscriberID))
224+
if options.SubscriberID.Has() {
225+
query.Must(elastic.NewTermQuery("subscriber_ids", options.SubscriberID.Value()))
226226
}
227227

228-
if options.UpdatedAfterUnix != nil || options.UpdatedBeforeUnix != nil {
228+
if options.UpdatedAfterUnix.Has() || options.UpdatedBeforeUnix.Has() {
229229
q := elastic.NewRangeQuery("updated_unix")
230-
if options.UpdatedAfterUnix != nil {
231-
q.Gte(*options.UpdatedAfterUnix)
230+
if options.UpdatedAfterUnix.Has() {
231+
q.Gte(options.UpdatedAfterUnix.Value())
232232
}
233-
if options.UpdatedBeforeUnix != nil {
234-
q.Lte(*options.UpdatedBeforeUnix)
233+
if options.UpdatedBeforeUnix.Has() {
234+
q.Lte(options.UpdatedBeforeUnix.Value())
235235
}
236236
query.Must(q)
237237
}

modules/indexer/issues/indexer_test.go

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -134,63 +134,60 @@ func searchIssueInRepo(t *testing.T) {
134134
}
135135

136136
func searchIssueByID(t *testing.T) {
137-
int64Pointer := func(x int64) *int64 {
138-
return &x
139-
}
140137
tests := []struct {
141138
opts SearchOptions
142139
expectedIDs []int64
143140
}{
144141
{
145-
SearchOptions{
146-
PosterID: int64Pointer(1),
142+
opts: SearchOptions{
143+
PosterID: optional.Some(int64(1)),
147144
},
148-
[]int64{11, 6, 3, 2, 1},
145+
expectedIDs: []int64{11, 6, 3, 2, 1},
149146
},
150147
{
151-
SearchOptions{
152-
AssigneeID: int64Pointer(1),
148+
opts: SearchOptions{
149+
AssigneeID: optional.Some(int64(1)),
153150
},
154-
[]int64{6, 1},
151+
expectedIDs: []int64{6, 1},
155152
},
156153
{
157-
SearchOptions{
158-
MentionID: int64Pointer(4),
154+
opts: SearchOptions{
155+
MentionID: optional.Some(int64(4)),
159156
},
160-
[]int64{1},
157+
expectedIDs: []int64{1},
161158
},
162159
{
163-
SearchOptions{
164-
ReviewedID: int64Pointer(1),
160+
opts: SearchOptions{
161+
ReviewedID: optional.Some(int64(1)),
165162
},
166-
[]int64{},
163+
expectedIDs: []int64{},
167164
},
168165
{
169-
SearchOptions{
170-
ReviewRequestedID: int64Pointer(1),
166+
opts: SearchOptions{
167+
ReviewRequestedID: optional.Some(int64(1)),
171168
},
172-
[]int64{12},
169+
expectedIDs: []int64{12},
173170
},
174171
{
175-
SearchOptions{
176-
SubscriberID: int64Pointer(1),
172+
opts: SearchOptions{
173+
SubscriberID: optional.Some(int64(1)),
177174
},
178-
[]int64{11, 6, 5, 3, 2, 1},
175+
expectedIDs: []int64{11, 6, 5, 3, 2, 1},
179176
},
180177
{
181178
// issue 20 request user 15 and team 5 which user 15 belongs to
182179
// the review request number of issue 20 should be 1
183-
SearchOptions{
184-
ReviewRequestedID: int64Pointer(15),
180+
opts: SearchOptions{
181+
ReviewRequestedID: optional.Some(int64(15)),
185182
},
186-
[]int64{12, 20},
183+
expectedIDs: []int64{12, 20},
187184
},
188185
{
189186
// user 20 approved the issue 20, so return nothing
190-
SearchOptions{
191-
ReviewRequestedID: int64Pointer(20),
187+
opts: SearchOptions{
188+
ReviewRequestedID: optional.Some(int64(20)),
192189
},
193-
[]int64{},
190+
expectedIDs: []int64{},
194191
},
195192
}
196193

@@ -318,16 +315,13 @@ func searchIssueByLabelID(t *testing.T) {
318315
}
319316

320317
func searchIssueByTime(t *testing.T) {
321-
int64Pointer := func(i int64) *int64 {
322-
return &i
323-
}
324318
tests := []struct {
325319
opts SearchOptions
326320
expectedIDs []int64
327321
}{
328322
{
329323
SearchOptions{
330-
UpdatedAfterUnix: int64Pointer(0),
324+
UpdatedAfterUnix: optional.Some(int64(0)),
331325
},
332326
[]int64{22, 21, 17, 16, 15, 14, 13, 12, 11, 20, 6, 5, 19, 18, 10, 7, 4, 9, 8, 3, 2, 1},
333327
},
@@ -363,28 +357,25 @@ func searchIssueWithOrder(t *testing.T) {
363357
}
364358

365359
func searchIssueInProject(t *testing.T) {
366-
int64Pointer := func(i int64) *int64 {
367-
return &i
368-
}
369360
tests := []struct {
370361
opts SearchOptions
371362
expectedIDs []int64
372363
}{
373364
{
374365
SearchOptions{
375-
ProjectID: int64Pointer(1),
366+
ProjectID: optional.Some(int64(1)),
376367
},
377368
[]int64{5, 3, 2, 1},
378369
},
379370
{
380371
SearchOptions{
381-
ProjectBoardID: int64Pointer(1),
372+
ProjectBoardID: optional.Some(int64(1)),
382373
},
383374
[]int64{1},
384375
},
385376
{
386377
SearchOptions{
387-
ProjectBoardID: int64Pointer(0), // issue with in default board
378+
ProjectBoardID: optional.Some(int64(0)), // issue with in default board
388379
},
389380
[]int64{2},
390381
},

0 commit comments

Comments
 (0)