Skip to content

Commit 1649d33

Browse files
blink1073FGasper
andauthored
GODRIVER-2992 [master] Add RemainingBatchLength to ChangeStream (#1633)
Co-authored-by: Felipe Gasper <[email protected]>
1 parent 8fd83f7 commit 1649d33

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

internal/integration/change_stream_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func TestChangeStream_ReplicaSet(t *testing.T) {
9898
// cause an event to occur so the resume token is updated
9999
generateEvents(mt, 1)
100100
assert.True(mt, cs.Next(context.Background()), "expected next to return true, got false")
101+
assert.Equal(mt, 0, cs.RemainingBatchLength())
101102
firstToken := cs.ResumeToken()
102103

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

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

mongo/change_stream.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,12 @@ func (cs *ChangeStream) ID() int64 {
579579
return cs.cursor.ID()
580580
}
581581

582+
// RemainingBatchLength returns the number of documents left in the current batch. If this returns zero, the subsequent
583+
// call to Next or TryNext will do a network request to fetch the next batch.
584+
func (cs *ChangeStream) RemainingBatchLength() int {
585+
return len(cs.batch)
586+
}
587+
582588
// SetBatchSize sets the number of documents to fetch from the database with
583589
// each iteration of the ChangeStream's "Next" or "TryNext" method. This setting
584590
// only affects subsequent document batches fetched from the database.

0 commit comments

Comments
 (0)