Skip to content

Commit

Permalink
[firestore] Set the cursor to empty when the end is reached (gravitat…
Browse files Browse the repository at this point in the history
…ional#7448)

* nil firestore cursor on end

* added explainer
  • Loading branch information
xacrimon authored Jul 9, 2021
1 parent 3f041da commit 2462c09
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/events/firestoreevents/firestoreevents.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
4 changes: 4 additions & 0 deletions lib/events/firestoreevents/firestoreevents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 2462c09

Please sign in to comment.