Skip to content
Snippets Groups Projects
Commit 3b940253 authored by Danis Kirasirov's avatar Danis Kirasirov
Browse files

Использование offset/limit в items.BatchProcessor

parent 67448583
No related branches found
No related tags found
No related merge requests found
...@@ -16,9 +16,7 @@ type BatchProcessor struct { ...@@ -16,9 +16,7 @@ type BatchProcessor struct {
FindPublishedOptions *FindPublishedOptions FindPublishedOptions *FindPublishedOptions
Filter *Filter Filter *Filter
// Deprecated использовать offset, limit limit int
pageSize, pageNum int
sort []string sort []string
processed int processed int
} }
...@@ -39,7 +37,7 @@ func (b *BatchProcessor) getBatch(ctx context.Context) ([]*Item, bool, error) { ...@@ -39,7 +37,7 @@ func (b *BatchProcessor) getBatch(ctx context.Context) ([]*Item, bool, error) {
Regular: b.FindPublishedOptions.Regular, Regular: b.FindPublishedOptions.Regular,
Hidden: b.FindPublishedOptions.Hidden, Hidden: b.FindPublishedOptions.Hidden,
Templates: b.FindPublishedOptions.Templates, Templates: b.FindPublishedOptions.Templates,
FindOptions: *options.NewFindOptions(b.pageNum, b.pageSize, b.sort...), FindOptions: *options.GetFindOptions(b.processed, b.limit, b.sort...),
}, },
) )
} else { } else {
...@@ -54,14 +52,13 @@ func (b *BatchProcessor) getBatch(ctx context.Context) ([]*Item, bool, error) { ...@@ -54,14 +52,13 @@ func (b *BatchProcessor) getBatch(ctx context.Context) ([]*Item, bool, error) {
Regular: b.FindOptions.Regular, Regular: b.FindOptions.Regular,
Hidden: b.FindOptions.Hidden, Hidden: b.FindOptions.Hidden,
Templates: b.FindOptions.Templates, Templates: b.FindOptions.Templates,
FindOptions: *options.NewFindOptions(b.pageNum, b.pageSize, b.sort...), FindOptions: *options.GetFindOptions(b.processed, b.limit, b.sort...),
}, },
) )
} }
if err == nil { if err == nil {
b.processed += len(res) b.processed += len(res)
b.pageNum++
} }
return res, b.processed != total, err return res, b.processed != total, err
...@@ -86,32 +83,35 @@ func (b *BatchProcessor) next(ctx context.Context) (res []*Item, next bool, err ...@@ -86,32 +83,35 @@ func (b *BatchProcessor) next(ctx context.Context) (res []*Item, next bool, err
} }
func (b *BatchProcessor) reducePageSize() bool { func (b *BatchProcessor) reducePageSize() bool {
if b.pageSize == 1 { if b.limit == 1 {
return false return false
} }
b.pageNum = 2 * b.pageNum b.limit /= 2
b.pageSize = b.pageSize / 2
return true return true
} }
func (b *BatchProcessor) Do(ctx context.Context, f func(batch []*Item) error) (int, error) { func (b *BatchProcessor) Do(ctx context.Context, f func(batch []*Item) error) (int, error) {
if b.FindOptions == nil && b.FindPublishedOptions == nil { if b.FindOptions == nil && b.FindPublishedOptions == nil {
b.FindOptions = new(FindOptions) b.FindOptions = new(FindOptions)
} }
if b.FindOptions != nil { if b.FindOptions != nil {
b.pageSize = b.FindOptions.PageSize b.limit = b.FindOptions.PageSize
b.sort = b.FindOptions.Sort b.sort = b.FindOptions.Sort
if b.FindOptions.Limit != 0 {
b.limit = b.FindOptions.Limit
}
} }
if b.FindPublishedOptions != nil { if b.FindPublishedOptions != nil {
b.pageSize = b.FindPublishedOptions.PageSize b.limit = b.FindPublishedOptions.PageSize
b.sort = b.FindPublishedOptions.Sort b.sort = b.FindPublishedOptions.Sort
if b.FindPublishedOptions.Limit != 0 {
b.limit = b.FindPublishedOptions.Limit
}
} }
if b.pageSize == 0 { if b.limit == 0 {
b.pageSize = 128 b.limit = 128
} }
if b.Filter != nil && (len(b.Filter.ID) > 0 || len(b.Filter.Q) > 0) && !data.Contains("_id", b.sort) { if b.Filter != nil && (len(b.Filter.ID) > 0 || len(b.Filter.Q) > 0) && !data.Contains("_id", b.sort) {
......
...@@ -39,12 +39,18 @@ type FindOptions struct { ...@@ -39,12 +39,18 @@ type FindOptions struct {
FieldOptions FieldOptions
} }
// Deprecated использовать GetFindOptions
// NewFindOptions создает новые результаты поиска // NewFindOptions создает новые результаты поиска
func NewFindOptions(pageNum, pageSize int, sort ...string) *FindOptions { func NewFindOptions(pageNum, pageSize int, sort ...string) *FindOptions {
return GetFindOptions(pageNum*pageSize, pageSize, sort...)
}
// GetFindOptions создает новые параметры поиска
func GetFindOptions(offset, limit int, sort ...string) *FindOptions {
return &FindOptions{ return &FindOptions{
PaginationOptions: PaginationOptions{ PaginationOptions: PaginationOptions{
PageNum: pageNum, Offset: offset,
PageSize: pageSize, Limit: limit,
}, },
SortOptions: SortOptions{ SortOptions: SortOptions{
Sort: sort, Sort: sort,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment