-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(mongox): fix pagination #47
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,16 +17,38 @@ func (c *Collection) Paginate(ctx context.Context, rawFilter any, s *usecasex.So | |
return nil, nil | ||
} | ||
|
||
pFilter, pOpts, err := c.findFilter(ctx, *p, s) | ||
pFilter, err := c.pageFilter(ctx, *p, s) | ||
if err != nil { | ||
return nil, rerror.ErrInternalByWithContext(ctx, err) | ||
} | ||
|
||
filter := rawFilter | ||
if pFilter != nil { | ||
filter = And(rawFilter, "", pFilter) | ||
} | ||
|
||
cursor, err := c.collection.Find(ctx, filter, append([]*options.FindOptions{pOpts}, opts...)...) | ||
sortKey := idKey | ||
sortOrder := 1 | ||
if s != nil && s.Key != "" { | ||
sortKey = s.Key | ||
if s.Reverted { | ||
sortOrder = -1 | ||
} | ||
} | ||
|
||
if p.Cursor != nil && p.Cursor.Last != nil { | ||
sortOrder *= -1 | ||
} | ||
|
||
findOpts := options.Find(). | ||
SetSort(bson.D{{Key: sortKey, Value: sortOrder}, {Key: idKey, Value: sortOrder}}). | ||
SetLimit(limit(*p)) | ||
|
||
if p.Offset != nil { | ||
findOpts.SetSkip(p.Offset.Offset) | ||
} | ||
|
||
cursor, err := c.collection.Find(ctx, filter, append([]*options.FindOptions{findOpts}, opts...)...) | ||
if err != nil { | ||
return nil, rerror.ErrInternalByWithContext(ctx, fmt.Errorf("failed to find: %w", err)) | ||
} | ||
|
@@ -46,6 +68,7 @@ func (c *Collection) Paginate(ctx context.Context, rawFilter any, s *usecasex.So | |
|
||
if p.Cursor != nil && p.Cursor.Last != nil { | ||
reverse(items) | ||
startCursor, endCursor = endCursor, startCursor | ||
} | ||
|
||
for _, item := range items { | ||
|
@@ -59,7 +82,6 @@ func (c *Collection) Paginate(ctx context.Context, rawFilter any, s *usecasex.So | |
return usecasex.NewPageInfo(count, startCursor, endCursor, hasNextPage, hasPreviousPage), nil | ||
} | ||
|
||
|
||
func (c *Collection) PaginateAggregation(ctx context.Context, pipeline []any, s *usecasex.Sort, p *usecasex.Pagination, consumer Consumer, opts ...*options.AggregateOptions) (*usecasex.PageInfo, error) { | ||
if p == nil || p.Cursor == nil && p.Offset == nil { | ||
return nil, nil | ||
|
@@ -155,19 +177,6 @@ func reverse(items []bson.Raw) { | |
} | ||
} | ||
|
||
func (c *Collection) findFilter(ctx context.Context, p usecasex.Pagination, s *usecasex.Sort) (any, *options.FindOptions, error) { | ||
if p.Cursor == nil && p.Offset == nil { | ||
return nil, nil, errors.New("invalid pagination") | ||
} | ||
|
||
opts := findOptionsFromPagination(p, s) | ||
if p.Offset != nil { | ||
return nil, opts, nil | ||
} | ||
f, err := c.pageFilter(ctx, p, s) | ||
return f, opts, err | ||
} | ||
|
||
func (c *Collection) aggregateFilter(ctx context.Context, p usecasex.Pagination, s *usecasex.Sort) ([]any, *options.AggregateOptions, error) { | ||
if p.Cursor == nil && p.Offset == nil { | ||
return nil, nil, errors.New("invalid pagination") | ||
|
@@ -189,21 +198,6 @@ func (c *Collection) aggregateFilter(ctx context.Context, p usecasex.Pagination, | |
return append(stages, bson.M{"$limit": limit(p)}), aggregateOptionsFromPagination(p, s), err | ||
} | ||
|
||
func findOptionsFromPagination(p usecasex.Pagination, s *usecasex.Sort) *options.FindOptions { | ||
o := options.Find().SetAllowDiskUse(true).SetLimit(limit(p)) | ||
|
||
if p.Offset != nil { | ||
o = o.SetSkip(p.Offset.Offset) | ||
} | ||
|
||
collation := options.Collation{ | ||
Locale: "en", | ||
Strength: 2, | ||
} | ||
|
||
return o.SetCollation(&collation).SetSort(sortFilter(p, s)) | ||
} | ||
|
||
func aggregateOptionsFromPagination(_ usecasex.Pagination, _ *usecasex.Sort) *options.AggregateOptions { | ||
collation := options.Collation{ | ||
Locale: "en", | ||
|
@@ -212,56 +206,77 @@ func aggregateOptionsFromPagination(_ usecasex.Pagination, _ *usecasex.Sort) *op | |
return options.Aggregate().SetAllowDiskUse(true).SetCollation(&collation) | ||
} | ||
|
||
func (c *Collection) pageFilter(ctx context.Context, p usecasex.Pagination, s *usecasex.Sort) (any, error) { | ||
func (c *Collection) pageFilter(ctx context.Context, p usecasex.Pagination, s *usecasex.Sort) (bson.M, error) { | ||
if p.Cursor == nil { | ||
return nil, nil | ||
} | ||
|
||
var filter bson.M | ||
sortKey := idKey | ||
sortOrder := 1 | ||
|
||
if s != nil && s.Key != "" { | ||
sortKey = s.Key | ||
if s.Reverted { | ||
sortOrder = -1 | ||
} | ||
} | ||
|
||
var cursor *usecasex.Cursor | ||
var op string | ||
var cur *usecasex.Cursor | ||
|
||
if p.Cursor.First != nil { | ||
if p.Cursor.After != nil { | ||
cursor = p.Cursor.After | ||
op = "$gt" | ||
cur = p.Cursor.After | ||
} else if p.Cursor.Last != nil { | ||
} else if p.Cursor.Before != nil { | ||
cursor = p.Cursor.Before | ||
op = "$lt" | ||
cur = p.Cursor.Before | ||
} else { | ||
return nil, errors.New("neither first nor last are set") | ||
} | ||
if cur == nil { | ||
return nil, nil | ||
} | ||
|
||
var sortKey *string | ||
if s != nil { | ||
sortKey = &s.Key | ||
} | ||
var paginationFilter bson.M | ||
if sortKey == nil || *sortKey == "" { | ||
paginationFilter = bson.M{idKey: bson.M{op: *cur}} | ||
} else { | ||
var cursorDoc bson.M | ||
if err := c.collection.FindOne(ctx, bson.M{idKey: *cur}).Decode(&cursorDoc); err != nil { | ||
return nil, fmt.Errorf("failed to find cursor element") | ||
} | ||
|
||
if cursorDoc[*sortKey] == nil { | ||
return nil, fmt.Errorf("invalied sort key") | ||
if cursor != nil { | ||
cursorDoc, err := c.getCursorDocument(ctx, *cursor) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
paginationFilter = bson.M{ | ||
filter = bson.M{ | ||
"$or": []bson.M{ | ||
{*sortKey: bson.M{op: cursorDoc[*sortKey]}}, | ||
{sortKey: bson.M{op: cursorDoc[sortKey]}}, | ||
{ | ||
*sortKey: cursorDoc[*sortKey], | ||
idKey: bson.M{op: *cur}, | ||
sortKey: cursorDoc[sortKey], | ||
idKey: bson.M{op: cursorDoc[idKey]}, | ||
}, | ||
}, | ||
} | ||
|
||
if sortOrder == -1 { | ||
if op == "$gt" { | ||
op = "$lt" | ||
} else { | ||
op = "$gt" | ||
} | ||
filter = bson.M{ | ||
"$or": []bson.M{ | ||
{sortKey: bson.M{op: cursorDoc[sortKey]}}, | ||
{ | ||
sortKey: cursorDoc[sortKey], | ||
idKey: bson.M{op: cursorDoc[idKey]}, | ||
}, | ||
}, | ||
} | ||
} | ||
} | ||
|
||
return paginationFilter, nil | ||
return filter, nil | ||
Comment on lines
+209
to
+270
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optimize duplicated filter logic for reversed sort order. The logic for creating the filter when the sort order is reversed is duplicated. Consider refactoring to reduce redundancy. if cursor != nil {
cursorDoc, err := c.getCursorDocument(ctx, *cursor)
if err != nil {
return nil, err
}
op1, op2 := op, "$lt"
if sortOrder == -1 {
op1, op2 = "$lt", "$gt"
}
filter = bson.M{
"$or": []bson.M{
{sortKey: bson.M{op1: cursorDoc[sortKey]}},
{
sortKey: cursorDoc[sortKey],
idKey: bson.M{op2: cursorDoc[idKey]},
},
},
}
} |
||
} | ||
|
||
func (c *Collection) getCursorDocument(ctx context.Context, cursor usecasex.Cursor) (bson.M, error) { | ||
var cursorDoc bson.M | ||
err := c.collection.FindOne(ctx, bson.M{idKey: cursor}).Decode(&cursorDoc) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to find cursor element: %w", err) | ||
} | ||
return cursorDoc, nil | ||
} | ||
|
||
func sortFilter(p usecasex.Pagination, s *usecasex.Sort) bson.D { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optimize find options configuration.
The
findOpts
configuration correctly sets sorting and limit options. Consider consolidating the logic for setting options to enhance readability and maintainability.