Skip to content
Open
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
24 changes: 24 additions & 0 deletions internal/integration/crud_prose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1017,3 +1017,27 @@ func TestClientBulkWriteProse(t *testing.T) {
assert.Equal(mt, num, int(n), "expected %d documents, got: %d", num, n)
})
}

func TestBatchSize(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Pinned))
mt.Setup()

var hello struct {
MaxBsonObjectSize int
MaxMessageSizeBytes int
}
err := mt.DB.RunCommand(context.Background(), bson.D{{"hello", 1}}).Decode(&hello)
require.NoError(mt, err, "Hello error: %v", err)

var docs []any
limit := hello.MaxBsonObjectSize - 30
for need := hello.MaxMessageSizeBytes - 350; need > 0; need -= limit {
if need >= limit {
Comment on lines +1032 to +1035
docs = append(docs, bson.D{{"x", string(make([]byte, limit))}})
} else {
docs = append(docs, bson.D{{"x", string(make([]byte, need))}})
}
}
_, err = mt.Coll.InsertMany(context.Background(), docs)
assert.NoError(mt, err, "InsertMany error: %v", err)
}
4 changes: 2 additions & 2 deletions x/mongo/driver/batches.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (b *Batches) AppendBatchSequence(dst []byte, maxCount, totalSize int) (int,
idx, dst = bsoncore.ReserveLength(dst)
dst = append(dst, b.Identifier...)
dst = append(dst, 0x00)
var size int
size := len(dst)
var n int
for i := b.offset; i < len(b.Documents); i++ {
if n == maxCount {
Expand Down Expand Up @@ -69,7 +69,7 @@ func (b *Batches) AppendBatchArray(dst []byte, maxCount, totalSize int) (int, []
}
l := len(dst)
aidx, dst := bsoncore.AppendArrayElementStart(dst, b.Identifier)
var size int
size := len(dst)
var n int
for i := b.offset; i < len(b.Documents); i++ {
if n == maxCount {
Expand Down
6 changes: 4 additions & 2 deletions x/mongo/driver/batches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ func TestAppendBatchSequence(t *testing.T) {
batches := newTestBatches(t)

got := []byte{42}
sizeLimit := len(batches.Documents[0]) + len(batches.Documents[1])
var n int
var err error
n, got, err = batches.AppendBatchSequence(got, 2, len(batches.Documents[0]))
n, got, err = batches.AppendBatchSequence(got, 2, sizeLimit)
assert.NoError(t, err)
assert.Equal(t, 1, n)

Expand All @@ -57,9 +58,10 @@ func TestAppendBatchArray(t *testing.T) {
batches := newTestBatches(t)

got := []byte{42}
sizeLimit := len(batches.Documents[0]) + len(batches.Documents[1])
var n int
var err error
n, got, err = batches.AppendBatchArray(got, 2, len(batches.Documents[0]))
n, got, err = batches.AppendBatchArray(got, 2, sizeLimit)
assert.NoError(t, err)
assert.Equal(t, 1, n)

Expand Down
Loading