Skip to content

Commit c404c43

Browse files
committed
Use db.ListOptions directly instead of Paginator interface to make it easier to user
1 parent 2f060c5 commit c404c43

File tree

17 files changed

+74
-71
lines changed

17 files changed

+74
-71
lines changed

models/issues/issue_search.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121

2222
// IssuesOptions represents options of an issue.
2323
type IssuesOptions struct { //nolint
24-
db.Paginator
24+
db.ListOptions
2525
RepoIDs []int64 // overwrites RepoCond if the length is not 0
2626
AllPublic bool // include also all public repositories
2727
RepoCond builder.Cond
@@ -100,27 +100,15 @@ func applySorts(sess *xorm.Session, sortType string, priorityRepoID int64) {
100100
}
101101

102102
func applyLimit(sess *xorm.Session, opts *IssuesOptions) *xorm.Session {
103-
if opts.Paginator == nil || opts.Paginator.IsListAll() {
103+
if opts.ListOptions.IsListAll() {
104104
return sess
105105
}
106106

107-
// Warning: Do not use GetSkipTake() for *db.ListOptions
108-
// Its implementation could reset the page size with setting.API.MaxResponseItems
109-
if listOptions, ok := opts.Paginator.(*db.ListOptions); ok {
110-
if listOptions.Page >= 0 && listOptions.PageSize > 0 {
111-
var start int
112-
if listOptions.Page == 0 {
113-
start = 0
114-
} else {
115-
start = (listOptions.Page - 1) * listOptions.PageSize
116-
}
117-
sess.Limit(listOptions.PageSize, start)
118-
}
119-
return sess
107+
start := 0
108+
if opts.ListOptions.Page > 0 {
109+
start = (opts.ListOptions.Page - 1) * opts.ListOptions.PageSize
120110
}
121-
122-
start, limit := opts.Paginator.GetSkipTake()
123-
sess.Limit(limit, start)
111+
sess.Limit(opts.ListOptions.PageSize, start)
124112

125113
return sess
126114
}

models/issues/issue_stats.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,17 @@ func CountIssuesByRepo(ctx context.Context, opts *IssuesOptions) (map[int64]int6
6868
}
6969

7070
// CountIssues number return of issues by given conditions.
71-
func CountIssues(ctx context.Context, opts *IssuesOptions) (int64, error) {
71+
func CountIssues(ctx context.Context, opts *IssuesOptions, otherConds ...builder.Cond) (int64, error) {
7272
sess := db.GetEngine(ctx).
7373
Select("COUNT(issue.id) AS count").
7474
Table("issue").
7575
Join("INNER", "repository", "`issue`.repo_id = `repository`.id")
7676
applyConditions(sess, opts)
7777

78+
for _, cond := range otherConds {
79+
sess.And(cond)
80+
}
81+
7882
return sess.Count()
7983
}
8084

models/issues/issue_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func TestIssues(t *testing.T) {
164164
issues_model.IssuesOptions{
165165
RepoCond: builder.In("repo_id", 1, 3),
166166
SortType: "oldest",
167-
Paginator: &db.ListOptions{
167+
ListOptions: db.ListOptions{
168168
Page: 1,
169169
PageSize: 4,
170170
},
@@ -174,7 +174,7 @@ func TestIssues(t *testing.T) {
174174
{
175175
issues_model.IssuesOptions{
176176
LabelIDs: []int64{1},
177-
Paginator: &db.ListOptions{
177+
ListOptions: db.ListOptions{
178178
Page: 1,
179179
PageSize: 4,
180180
},
@@ -184,7 +184,7 @@ func TestIssues(t *testing.T) {
184184
{
185185
issues_model.IssuesOptions{
186186
LabelIDs: []int64{1, 2},
187-
Paginator: &db.ListOptions{
187+
ListOptions: db.ListOptions{
188188
Page: 1,
189189
PageSize: 4,
190190
},
@@ -326,7 +326,7 @@ func TestCorrectIssueStats(t *testing.T) {
326326

327327
// Now we will get all issueID's that match the "Bugs are nasty" query.
328328
issues, err := issues_model.Issues(context.TODO(), &issues_model.IssuesOptions{
329-
Paginator: &db.ListOptions{
329+
ListOptions: db.ListOptions{
330330
PageSize: issueAmount,
331331
},
332332
RepoIDs: []int64{1},

modules/indexer/issues/bleve/bleve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
266266
indexerQuery = bleve.NewMatchAllQuery()
267267
}
268268

269-
skip, limit := indexer_internal.ParsePaginator(options.Paginator)
269+
skip, limit := indexer_internal.ParsePaginator(&options.ListOptions)
270270
search := bleve.NewSearchRequestOptions(indexerQuery, limit, skip, false)
271271

272272
if options.SortBy == "" {

modules/indexer/issues/db/db.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ func (i *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
7878
return nil, err
7979
}
8080

81+
// If pagesize == 0, return total count only. It's a special case for search count.
82+
if options.ListOptions.PageSize == 0 {
83+
total, err := issue_model.CountIssues(ctx, opt, cond)
84+
if err != nil {
85+
return nil, err
86+
}
87+
return &internal.SearchResult{
88+
Total: total,
89+
}, nil
90+
}
91+
8192
ids, total, err := issue_model.IssueIDs(ctx, opt, cond)
8293
if err != nil {
8394
return nil, err

modules/indexer/issues/db/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func ToDBOptions(ctx context.Context, options *internal.SearchOptions) (*issue_m
5050
}
5151

5252
opts := &issue_model.IssuesOptions{
53-
Paginator: options.Paginator,
53+
ListOptions: options.ListOptions,
5454
RepoIDs: options.RepoIDs,
5555
AllPublic: options.AllPublic,
5656
RepoCond: nil,

modules/indexer/issues/dboptions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func ToSearchOptions(keyword string, opts *issues_model.IssuesOptions) *SearchOp
6565
searchOpt.UpdatedBeforeUnix = optional.Some(opts.UpdatedBeforeUnix)
6666
}
6767

68-
searchOpt.Paginator = opts.Paginator
68+
searchOpt.ListOptions = opts.ListOptions
6969

7070
switch opts.SortType {
7171
case "":

modules/indexer/issues/elasticsearch/elasticsearch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func (b *Indexer) Search(ctx context.Context, options *internal.SearchOptions) (
248248
// TODO: make it configurable since it's configurable in elasticsearch
249249
const maxPageSize = 10000
250250

251-
skip, limit := indexer_internal.ParsePaginator(options.Paginator, maxPageSize)
251+
skip, limit := indexer_internal.ParsePaginator(&options.ListOptions, maxPageSize)
252252
searchResult, err := b.inner.Client.Search().
253253
Index(b.inner.VersionedIndexName()).
254254
Query(query).

modules/indexer/issues/indexer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func SearchIssues(ctx context.Context, opts *SearchOptions) ([]int64, int64, err
308308

309309
// CountIssues counts issues by options. It is a shortcut of SearchIssues(ctx, opts) but only returns the total count.
310310
func CountIssues(ctx context.Context, opts *SearchOptions) (int64, error) {
311-
opts = opts.Copy(func(options *SearchOptions) { opts.Paginator = &db_model.ListOptions{PageSize: 0} })
311+
opts = opts.Copy(func(options *SearchOptions) { options.ListOptions = db_model.ListOptions{PageSize: 0} })
312312

313313
_, total, err := SearchIssues(ctx, opts)
314314
return total, err

modules/indexer/issues/indexer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ func searchIssueWithPaginator(t *testing.T) {
397397
}{
398398
{
399399
SearchOptions{
400-
Paginator: &db.ListOptions{
400+
ListOptions: db.ListOptions{
401401
PageSize: 5,
402402
},
403403
},

modules/indexer/issues/internal/model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ type SearchOptions struct {
106106
UpdatedAfterUnix optional.Option[int64]
107107
UpdatedBeforeUnix optional.Option[int64]
108108

109-
db.Paginator
109+
db.ListOptions
110110

111111
SortBy SortBy // sort by field
112112
}

0 commit comments

Comments
 (0)