Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GODRIVER-2992 Add RemainingBatchLength to ChangeStream #1626

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading