From 2462c0944599ab33dc02745af5ca2d1f150127de Mon Sep 17 00:00:00 2001 From: Joel Date: Fri, 9 Jul 2021 15:47:13 +0200 Subject: [PATCH] [firestore] Set the cursor to empty when the end is reached (#7448) * nil firestore cursor on end * added explainer --- lib/events/firestoreevents/firestoreevents.go | 12 +++++++++++- lib/events/firestoreevents/firestoreevents_test.go | 4 ++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/events/firestoreevents/firestoreevents.go b/lib/events/firestoreevents/firestoreevents.go index bd9303bd5fbd2..d8719049c311c 100644 --- a/lib/events/firestoreevents/firestoreevents.go +++ b/lib/events/firestoreevents/firestoreevents.go @@ -470,6 +470,7 @@ func (l *Log) SearchEvents(fromUTC, toUTC time.Time, namespace string, eventType var values []events.EventFields var parsedStartKey int64 var err error + reachedEnd := false totalSize := 0 if startKey != "" { @@ -501,6 +502,15 @@ func (l *Log) SearchEvents(fromUTC, toUTC time.Time, namespace string, eventType return nil, "", firestorebk.ConvertGRPCError(err) } + // Correctly detecting if you've reached the end of a query in firestore is + // tricky since it doesn't set any flag when it finds that there are no further events. + // This solution here seems to be the the most common but I haven't been able to find + // any documented hard guarantees on firestore not early returning for some reason like response + // size like DynamoDB does. In short, this should work in all cases for lack of a better solution. + if len(docSnaps) < limit { + reachedEnd = true + } + g.WithFields(log.Fields{"duration": time.Since(start)}).Debugf("Query completed.") for _, docSnap := range docSnaps { var e event @@ -546,7 +556,7 @@ func (l *Log) SearchEvents(fromUTC, toUTC time.Time, namespace string, eventType } var lastKeyString string - if lastKey != 0 { + if lastKey != 0 && !reachedEnd { lastKeyString = fmt.Sprintf("%d", lastKey) } diff --git a/lib/events/firestoreevents/firestoreevents_test.go b/lib/events/firestoreevents/firestoreevents_test.go index 4343a94722741..a677bbea47376 100644 --- a/lib/events/firestoreevents/firestoreevents_test.go +++ b/lib/events/firestoreevents/firestoreevents_test.go @@ -105,3 +105,7 @@ func (s *FirestoreeventsSuite) TearDownTest(c *check.C) { func (s *FirestoreeventsSuite) TestSessionEventsCRUD(c *check.C) { s.SessionEventsCRUD(c) } + +func (s *FirestoreeventsSuite) TestPagination(c *check.C) { + s.EventPagination(c) +}