From db7a6e7f05bc2a4d1a979d5894d5337390b7d651 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Tue, 16 Mar 2021 14:38:05 -0400 Subject: [PATCH] docstore/memdocstore: apply limit after sorting (#2978) Fixes google/go-cloud#2977 Co-authored-by: Robert van Gent --- docstore/memdocstore/query.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docstore/memdocstore/query.go b/docstore/memdocstore/query.go index 93f29bbb20..c2efd3529a 100644 --- a/docstore/memdocstore/query.go +++ b/docstore/memdocstore/query.go @@ -37,9 +37,6 @@ func (c *collection) RunGetQuery(_ context.Context, q *driver.Query) (driver.Doc var resultDocs []storedDoc for _, doc := range c.docs { - if q.Limit > 0 && len(resultDocs) == q.Limit { - break - } if filtersMatch(q.Filters, doc) { resultDocs = append(resultDocs, doc) } @@ -47,6 +44,11 @@ func (c *collection) RunGetQuery(_ context.Context, q *driver.Query) (driver.Doc if q.OrderByField != "" { sortDocs(resultDocs, q.OrderByField, q.OrderAscending) } + + if q.Limit > 0 && len(resultDocs) > q.Limit { + resultDocs = resultDocs[:q.Limit] + } + // Include the key field in the field paths if there is one. var fps [][]string if len(q.FieldPaths) > 0 && c.keyField != "" {