Skip to content
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

docstore/all: add Offset method implementation #3385

Merged
merged 5 commits into from Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 20 additions & 11 deletions docstore/awsdynamodb/query.go
Expand Up @@ -53,9 +53,10 @@ func (c *collection) RunGetQuery(ctx context.Context, q *driver.Query) (driver.D
return nil, err
}
it := &documentIterator{
qr: qr,
limit: q.Limit,
count: 0, // manually count limit since dynamodb uses "limit" as scan limit before filtering
qr: qr,
offset: q.Offset,
limit: q.Limit,
count: 0, // manually count limit since dynamodb uses "limit" as scan limit before filtering
}
it.items, it.last, it.asFunc, err = it.qr.run(ctx, nil)
if err != nil {
Expand Down Expand Up @@ -458,17 +459,25 @@ func toInCondition(f driver.Filter) expression.ConditionBuilder {
}

type documentIterator struct {
qr *queryRunner
items []map[string]*dyn.AttributeValue
curr int
limit int
count int // number of items returned
last map[string]*dyn.AttributeValue
asFunc func(i interface{}) bool
qr *queryRunner // the query runner
items []map[string]*dyn.AttributeValue // items from the last query
curr int // index of the current item in items
offset int // number of items to skip
limit int // number of items to return
count int // number of items returned
last map[string]*dyn.AttributeValue // lastEvaluatedKey from the last query
asFunc func(i interface{}) bool // for As
}

func (it *documentIterator) Next(ctx context.Context, doc driver.Document) error {
if it.limit > 0 && it.count >= it.limit || it.curr >= len(it.items) && it.last == nil {
// Skip the first 'n' documents where 'n' is the offset.
if it.offset > 0 && it.count < it.offset {
it.curr++
it.count++
return it.Next(ctx, doc)
}
// Only start counting towards the limit after the offset has been reached.
if it.limit > 0 && it.count >= it.offset+it.limit || it.curr >= len(it.items) && it.last == nil {
return io.EOF
}
if it.curr >= len(it.items) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.