Skip to content

Commit db7a6e7

Browse files
jbavangent
andauthored
docstore/memdocstore: apply limit after sorting (#2978)
Fixes #2977 Co-authored-by: Robert van Gent <rvangent@google.com>
1 parent 32ad8e5 commit db7a6e7

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

docstore/memdocstore/query.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,18 @@ func (c *collection) RunGetQuery(_ context.Context, q *driver.Query) (driver.Doc
3737

3838
var resultDocs []storedDoc
3939
for _, doc := range c.docs {
40-
if q.Limit > 0 && len(resultDocs) == q.Limit {
41-
break
42-
}
4340
if filtersMatch(q.Filters, doc) {
4441
resultDocs = append(resultDocs, doc)
4542
}
4643
}
4744
if q.OrderByField != "" {
4845
sortDocs(resultDocs, q.OrderByField, q.OrderAscending)
4946
}
47+
48+
if q.Limit > 0 && len(resultDocs) > q.Limit {
49+
resultDocs = resultDocs[:q.Limit]
50+
}
51+
5052
// Include the key field in the field paths if there is one.
5153
var fps [][]string
5254
if len(q.FieldPaths) > 0 && c.keyField != "" {

0 commit comments

Comments
 (0)