Skip to content

Commit 09a846f

Browse files
committed
refactor numeric keyword detection
1 parent 1b45cd0 commit 09a846f

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

Diff for: modules/indexer/issues/db/db.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,14 @@ func (i *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
4949
// But the two functions are used in modules/notification/indexer, that means we will import services/indexer in modules/notification/indexer.
5050
// So that's the root problem:
5151
// The notification is defined in modules, but it's using lots of things should be in services.
52-
5352
cond := builder.NewCond()
5453
if options.Keyword != "" {
5554
repoCond := builder.In("repo_id", options.RepoIDs)
5655
if len(options.RepoIDs) == 1 {
5756
repoCond = builder.Eq{"repo_id": options.RepoIDs[0]}
5857
}
5958

60-
if options.Index.Has() {
59+
if options.IsKeywordNumeric() {
6160
cond = builder.And(
6261
builder.Eq{"`index`": options.Keyword},
6362
repoCond,

Diff for: modules/indexer/issues/indexer.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"os"
1010
"runtime/pprof"
11-
"strconv"
1211
"sync/atomic"
1312
"time"
1413

@@ -284,11 +283,7 @@ const (
284283
func SearchIssues(ctx context.Context, opts *SearchOptions) ([]int64, int64, error) {
285284
indexer := *globalIndexer.Load()
286285

287-
issueIndex, err := strconv.Atoi(opts.Keyword)
288-
if err == nil {
289-
opts.Index = optional.Option[int64]{int64(issueIndex)}
290-
}
291-
if opts.Keyword == "" || opts.Index.Has() {
286+
if opts.Keyword == "" || opts.IsKeywordNumeric() {
292287
// This is a conservative shortcut.
293288
// If the keyword is empty or an integer, db has better (at least not worse) performance to filter issues.
294289
// When the keyword is empty, it tends to listing rather than searching issues.

Diff for: modules/indexer/issues/internal/model.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package internal
55

66
import (
7+
"strconv"
8+
79
"code.gitea.io/gitea/models/db"
810
"code.gitea.io/gitea/modules/optional"
911
"code.gitea.io/gitea/modules/timeutil"
@@ -89,8 +91,6 @@ type SearchOptions struct {
8991

9092
MilestoneIDs []int64 // milestones the issues have
9193

92-
Index optional.Option[int64] // keyword as potential issue index
93-
9494
ProjectID optional.Option[int64] // project the issues belong to
9595
ProjectColumnID optional.Option[int64] // project column the issues belong to
9696

@@ -126,6 +126,12 @@ func (o *SearchOptions) Copy(edit ...func(options *SearchOptions)) *SearchOption
126126
return &v
127127
}
128128

129+
// used for optimized issue index based search
130+
func (o *SearchOptions) IsKeywordNumeric() bool {
131+
_, err := strconv.Atoi(o.Keyword)
132+
return err == nil
133+
}
134+
129135
type SortBy string
130136

131137
const (

0 commit comments

Comments
 (0)