diff --git a/docstore/awsdynamodb/query.go b/docstore/awsdynamodb/query.go index 45cce25278..34e0a34e02 100644 --- a/docstore/awsdynamodb/query.go +++ b/docstore/awsdynamodb/query.go @@ -471,17 +471,23 @@ type documentIterator struct { func (it *documentIterator) Next(ctx context.Context, doc driver.Document) error { // 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) + for it.count < it.offset { + if err := it.next(ctx, doc, false); err != nil { + return err + } } + return it.next(ctx, doc, true) +} + +func (it *documentIterator) next(ctx context.Context, doc driver.Document, decode bool) error { // 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 { + 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) { + for it.curr >= len(it.items) { // Make a new query request at the end of this page. + // Note that it.items can be empty, but unless if it.last is not nil + // there may be more items. var err error it.items, it.last, it.asFunc, err = it.qr.run(ctx, it.last) if err != nil { @@ -489,12 +495,10 @@ func (it *documentIterator) Next(ctx context.Context, doc driver.Document) error } it.curr = 0 } - // If there are no more items, return EOF. - if len(it.items) == 0 { - return io.EOF - } - if err := decodeDoc(&dyn.AttributeValue{M: it.items[it.curr]}, doc); err != nil { - return err + if decode { + if err := decodeDoc(&dyn.AttributeValue{M: it.items[it.curr]}, doc); err != nil { + return err + } } it.curr++ it.count++