Skip to content

Commit

Permalink
docstore/all: add Offset method, useful for pagination (#3385)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartventer authored Mar 1, 2024
1 parent fb4e4b9 commit 0059557
Show file tree
Hide file tree
Showing 48 changed files with 2,455 additions and 2,821 deletions.
31 changes: 20 additions & 11 deletions docstore/awsdynamodb/query.go
Original file line number Diff line number Diff line change
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.

Loading

0 comments on commit 0059557

Please sign in to comment.