Skip to content

Commit 60e65d0

Browse files
Make BatchCursor.Next return false if first batch is empty
GODRIVER-1160 Change-Id: I467bb96f8d9ec820f4f0814e939ced4e4d450c28
1 parent bc60ac5 commit 60e65d0

File tree

4 files changed

+47
-10
lines changed

4 files changed

+47
-10
lines changed

mongo/change_stream.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ func (cs *ChangeStream) loopNext(ctx context.Context) {
474474

475475
// Returns true if the underlying cursor's batch is empty
476476
func (cs *ChangeStream) emptyBatch() bool {
477-
return len(cs.cursor.Batch().Data) == 5 // empty BSON array
477+
return cs.cursor.Batch().Empty()
478478
}
479479

480480
// StreamType represents the type of a change stream.

x/bsonx/bsoncore/document_sequence.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ func (ds *DocumentSequence) DocumentCount() int {
7474
}
7575
}
7676

77+
// Empty returns true if the sequence is empty. It always returns true for unknown sequence styles.
78+
func (ds *DocumentSequence) Empty() bool {
79+
if ds == nil {
80+
return true
81+
}
82+
83+
switch ds.Style {
84+
case SequenceStyle:
85+
return len(ds.Data) == 0
86+
case ArrayStyle:
87+
return len(ds.Data) <= 5
88+
default:
89+
return true
90+
}
91+
}
92+
7793
//ResetIterator resets the iteration point for the Next method to the beginning of the document
7894
//sequence.
7995
func (ds *DocumentSequence) ResetIterator() {

x/bsonx/bsoncore/document_sequence_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,29 @@ func TestDocumentSequence(t *testing.T) {
349349
})
350350
}
351351
})
352+
t.Run("Empty", func(t *testing.T) {
353+
testCases := []struct {
354+
name string
355+
ds *DocumentSequence
356+
isEmpty bool
357+
}{
358+
{"ArrayStyle/is empty/nil", nil, true},
359+
{"ArrayStyle/is empty/0", &DocumentSequence{Style: ArrayStyle, Data: []byte{0x05, 0x00, 0x00, 0x00, 0x00}}, true},
360+
{"ArrayStyle/is not empty/non-0", &DocumentSequence{Style: ArrayStyle, Data: genArrayStyle(10)}, false},
361+
{"SequenceStyle/is empty/nil", nil, true},
362+
{"SequenceStyle/is empty/0", &DocumentSequence{Style: SequenceStyle, Data: []byte{}}, true},
363+
{"SequenceStyle/is not empty/non-0", &DocumentSequence{Style: SequenceStyle, Data: genSequenceStyle(10)}, false},
364+
}
365+
366+
for _, tc := range testCases {
367+
t.Run(tc.name, func(t *testing.T) {
368+
isEmpty := tc.ds.Empty()
369+
if isEmpty != tc.isEmpty {
370+
t.Errorf("Unexpected Empty result. got %v; want %v", isEmpty, tc.isEmpty)
371+
}
372+
})
373+
}
374+
})
352375
t.Run("ResetIterator", func(t *testing.T) {
353376
ds := &DocumentSequence{Pos: 1234567890}
354377
want := 0
@@ -368,6 +391,11 @@ func TestDocumentSequence(t *testing.T) {
368391
var ds *DocumentSequence
369392
_ = ds.DocumentCount()
370393
})
394+
t.Run("Empty", func(t *testing.T) {
395+
defer capturePanic()
396+
var ds *DocumentSequence
397+
_ = ds.Empty()
398+
})
371399
t.Run("ResetIterator", func(t *testing.T) {
372400
defer capturePanic()
373401
var ds *DocumentSequence

x/mongo/driver/batch_cursor.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func (bc *BatchCursor) Next(ctx context.Context) bool {
166166

167167
if bc.firstBatch {
168168
bc.firstBatch = false
169-
return true
169+
return !bc.currentBatch.Empty()
170170
}
171171

172172
if bc.id == 0 || bc.server == nil {
@@ -175,14 +175,7 @@ func (bc *BatchCursor) Next(ctx context.Context) bool {
175175

176176
bc.getMore(ctx)
177177

178-
switch bc.currentBatch.Style {
179-
case bsoncore.SequenceStyle:
180-
return len(bc.currentBatch.Data) > 0
181-
case bsoncore.ArrayStyle:
182-
return len(bc.currentBatch.Data) > 5
183-
default:
184-
return false
185-
}
178+
return !bc.currentBatch.Empty()
186179
}
187180

188181
// Batch will return a DocumentSequence for the current batch of documents. The returned

0 commit comments

Comments
 (0)