Skip to content

Commit d41f348

Browse files
authored
GODRIVER-1900 add SetBatchSize to driver.BatchCursor (#699)
1 parent 69f9561 commit d41f348

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

mongo/integration/cursor_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,34 @@ func TestCursor(t *testing.T) {
218218
assert.Equal(mt, failpointData.ErrorCode, mongoErr.Code, "expected code %v, got: %v", failpointData.ErrorCode, mongoErr.Code)
219219
})
220220
})
221+
// For versions < 3.2, the first find will get all the documents
222+
mt.RunOpts("set batchSize", mtest.NewOptions().MinServerVersion("3.2"), func(mt *mtest.T) {
223+
initCollection(mt, mt.Coll)
224+
mt.ClearEvents()
225+
226+
// create cursor with batchSize 0
227+
cursor, err := mt.Coll.Find(mtest.Background, bson.D{}, options.Find().SetBatchSize(0))
228+
assert.Nil(mt, err, "Find error: %v", err)
229+
defer cursor.Close(mtest.Background)
230+
evt := mt.GetStartedEvent()
231+
assert.Equal(mt, "find", evt.CommandName, "expected 'find' event, got '%v'", evt.CommandName)
232+
sizeVal, err := evt.Command.LookupErr("batchSize")
233+
assert.Nil(mt, err, "expected find command to have batchSize")
234+
batchSize := sizeVal.Int32()
235+
assert.Equal(mt, int32(0), batchSize, "expected batchSize 0, got %v", batchSize)
236+
237+
// make sure that the getMore sends the new batchSize
238+
batchCursor := mongo.BatchCursorFromCursor(cursor)
239+
batchCursor.SetBatchSize(4)
240+
assert.True(mt, cursor.Next(mtest.Background), "expected Next true, got false")
241+
evt = mt.GetStartedEvent()
242+
assert.NotNil(mt, evt, "expected getMore event, got nil")
243+
assert.Equal(mt, "getMore", evt.CommandName, "expected 'getMore' event, got '%v'", evt.CommandName)
244+
sizeVal, err = evt.Command.LookupErr("batchSize")
245+
assert.Nil(mt, err, "expected getMore command to have batchSize")
246+
batchSize = sizeVal.Int32()
247+
assert.Equal(mt, int32(4), batchSize, "expected batchSize 4, got %v", batchSize)
248+
})
221249
}
222250

223251
type tryNextCursor interface {

x/mongo/driver/batch_cursor.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,11 @@ func (bc *BatchCursor) PostBatchResumeToken() bsoncore.Document {
388388
return bc.postBatchResumeToken
389389
}
390390

391+
// SetBatchSize sets the batchSize for future getMores.
392+
func (bc *BatchCursor) SetBatchSize(size int32) {
393+
bc.batchSize = size
394+
}
395+
391396
func (bc *BatchCursor) getOperationDeployment() Deployment {
392397
if bc.connection != nil {
393398
return &loadBalancedCursorDeployment{

x/mongo/driver/batch_cursor_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package driver
2+
3+
import (
4+
"testing"
5+
6+
"go.mongodb.org/mongo-driver/internal/testutil/assert"
7+
)
8+
9+
func TestBatchCursor(t *testing.T) {
10+
t.Run("setBatchSize", func(t *testing.T) {
11+
var size int32
12+
bc := &BatchCursor{
13+
batchSize: size,
14+
}
15+
assert.Equal(t, size, bc.batchSize, "expected batchSize %v, got %v", size, bc.batchSize)
16+
17+
size = int32(4)
18+
bc.SetBatchSize(size)
19+
assert.Equal(t, size, bc.batchSize, "expected batchSize %v, got %v", size, bc.batchSize)
20+
})
21+
}

0 commit comments

Comments
 (0)