Skip to content

Commit af8e6c2

Browse files
author
Divjot Arora
committed
Allow insertion of 16MiB documents.
GODRIVER-1324 Change-Id: I739164acff37a0405c1623ab192aa5ab16962326
1 parent 35e91c3 commit af8e6c2

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

mongo/collection_internal_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"errors"
1212
"fmt"
1313
"os"
14+
"strings"
1415
"testing"
1516
"time"
1617

@@ -69,6 +70,36 @@ func initCollection(t *testing.T, coll *Collection) {
6970
require.Nil(t, err)
7071
}
7172

73+
func create16MBDocument(t *testing.T) bsoncore.Document {
74+
// 4 bytes = document length
75+
// 1 byte = element type (ObjectID = \x07)
76+
// 4 bytes = key name ("_id" + \x00)
77+
// 12 bytes = ObjectID value
78+
// 1 byte = element type (string = \x02)
79+
// 4 bytes = key name ("key" + \x00)
80+
// 4 bytes = string length
81+
// X bytes = string of length X bytes
82+
// 1 byte = \x00
83+
// 1 byte = \x00
84+
//
85+
// Therefore the string length should be: 1024*1024*16 - 32
86+
87+
targetDocSize := 1024 * 1024 * 16
88+
strSize := targetDocSize - 32
89+
var b strings.Builder
90+
b.Grow(strSize)
91+
for i := 0; i < strSize; i++ {
92+
b.WriteByte('A')
93+
}
94+
95+
idx, doc := bsoncore.AppendDocumentStart(nil)
96+
doc = bsoncore.AppendObjectIDElement(doc, "_id", primitive.NewObjectID())
97+
doc = bsoncore.AppendStringElement(doc, "key", b.String())
98+
doc, _ = bsoncore.AppendDocumentEnd(doc, idx)
99+
require.Equal(t, targetDocSize, len(doc), "expected document length %v, got %v", targetDocSize, len(doc))
100+
return doc
101+
}
102+
72103
func TestCollection_initialize(t *testing.T) {
73104
dbName := "foo"
74105
collName := "bar"
@@ -625,6 +656,36 @@ func TestCollection_InsertMany_Batches(t *testing.T) {
625656

626657
}
627658

659+
func TestCollection_InsertMany_LargeDocumentBatches(t *testing.T) {
660+
// TODO(GODRIVER-425): remove this as part a larger project to
661+
// refactor integration and other longrunning tasks.
662+
if os.Getenv("EVR_TASK_ID") == "" {
663+
t.Skip("skipping long running integration test outside of evergreen")
664+
}
665+
666+
client := createMonitoredClient(t, monitor)
667+
db := client.Database("insertmany_largebatches_db")
668+
coll := db.Collection("insertmany_largebatches_coll")
669+
err := coll.Drop(ctx)
670+
require.NoError(t, err, "Drop error: %v", err)
671+
docs := []interface{}{create16MBDocument(t), create16MBDocument(t)}
672+
673+
drainChannels()
674+
_, err = coll.InsertMany(ctx, docs)
675+
require.NoError(t, err, "InsertMany error: %v", err)
676+
677+
for i := 0; i < 2; i++ {
678+
var evt *event.CommandStartedEvent
679+
select {
680+
case evt = <-startedChan:
681+
default:
682+
t.Fatalf("no insert event found for iteration %v", i)
683+
}
684+
685+
require.Equal(t, "insert", evt.CommandName, "expected 'insert' event, got '%v'", evt.CommandName)
686+
}
687+
}
688+
628689
func TestCollection_InsertMany_ErrorCases(t *testing.T) {
629690
if testing.Short() {
630691
t.Skip("skipping integration test in short mode")

x/mongo/driver/batches.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,13 @@ func (b *Batches) AdvanceBatch(maxCount, targetBatchSize int) error {
3939
if len(b.Current) > 0 {
4040
return nil
4141
}
42-
if targetBatchSize > reservedCommandBufferBytes {
43-
targetBatchSize -= reservedCommandBufferBytes
44-
}
4542

4643
if maxCount <= 0 {
4744
maxCount = 1
4845
}
4946

5047
splitAfter := 0
51-
size := 1
48+
size := 0
5249
for i, doc := range b.Documents {
5350
if i == maxCount {
5451
break

0 commit comments

Comments
 (0)