Skip to content

Commit

Permalink
GODRIVER-2992 Add RemainingBatchLength to ChangeStream (#1626)
Browse files Browse the repository at this point in the history
Co-authored-by: Preston Vasquez <prestonvasquez@icloud.com>
  • Loading branch information
2 people authored and matthewdale committed May 10, 2024
1 parent a1a62cc commit 88812b7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mongo/change_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,12 @@ func (cs *ChangeStream) ID() int64 {
return cs.cursor.ID()
}

// RemainingBatchLength returns the number of documents left in the current batch. If this returns zero, the subsequent
// call to Next or TryNext will do a network request to fetch the next batch.
func (cs *ChangeStream) RemainingBatchLength() int {
return len(cs.batch)
}

// SetBatchSize sets the number of documents to fetch from the database with
// each iteration of the ChangeStream's "Next" or "TryNext" method. This setting
// only affects subsequent document batches fetched from the database.
Expand Down
2 changes: 2 additions & 0 deletions mongo/integration/change_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func TestChangeStream_ReplicaSet(t *testing.T) {
// cause an event to occur so the resume token is updated
generateEvents(mt, 1)
assert.True(mt, cs.Next(context.Background()), "expected next to return true, got false")
assert.Equal(mt, 0, cs.RemainingBatchLength())
firstToken := cs.ResumeToken()

// cause an event on a different collection than the one being watched so the server's PBRT is updated
Expand Down Expand Up @@ -374,6 +375,7 @@ func TestChangeStream_ReplicaSet(t *testing.T) {

// Iterate over one event to get resume token
assert.True(mt, cs.Next(context.Background()), "expected Next to return true, got false")
assert.Equal(mt, numEvents-1, cs.RemainingBatchLength())
token := cs.ResumeToken()
closeStream(cs)

Expand Down

0 comments on commit 88812b7

Please sign in to comment.