Skip to content

Commit

Permalink
GODRIVER-2992 [master] Add RemainingBatchLength to ChangeStream (#1633)
Browse files Browse the repository at this point in the history
Co-authored-by: Felipe Gasper <FGasper@users.noreply.github.com>
  • Loading branch information
blink1073 and FGasper authored May 8, 2024
1 parent 8fd83f7 commit 1649d33
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/integration/change_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,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 @@ -373,6 +374,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
6 changes: 6 additions & 0 deletions mongo/change_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,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

0 comments on commit 1649d33

Please sign in to comment.