@@ -476,9 +476,19 @@ _mongoc_write_command_insert (mongoc_write_command_t *command,
476
476
mongoc_write_result_t * result ,
477
477
bson_error_t * error )
478
478
{
479
- bson_t cmd = BSON_INITIALIZER ;
479
+ const uint8_t * data ;
480
+ bson_iter_t iter ;
481
+ const char * key ;
482
+ uint32_t len ;
483
+ bson_t tmp ;
484
+ bson_t ar ;
485
+ bson_t cmd ;
480
486
bson_t reply ;
487
+ char str [12 ];
488
+ size_t overhead ;
489
+ bool has_more ;
481
490
bool ret ;
491
+ int i ;
482
492
483
493
ENTRY ;
484
494
@@ -489,7 +499,9 @@ _mongoc_write_command_insert (mongoc_write_command_t *command,
489
499
BSON_ASSERT (hint );
490
500
BSON_ASSERT (collection );
491
501
492
- if (!command -> u .insert .n_documents ) {
502
+ if (!command -> u .insert .n_documents ||
503
+ !bson_iter_init (& iter , command -> u .insert .documents ) ||
504
+ !bson_iter_next (& iter )) {
493
505
bson_set_error (error ,
494
506
MONGOC_ERROR_COLLECTION ,
495
507
MONGOC_ERROR_COLLECTION_INSERT_FAILED ,
@@ -498,12 +510,54 @@ _mongoc_write_command_insert (mongoc_write_command_t *command,
498
510
EXIT ;
499
511
}
500
512
513
+ overhead = 1 + strlen ("documents" ) + 1 ;
514
+
515
+ again :
516
+ bson_init (& cmd );
517
+ has_more = false;
518
+ i = 0 ;
519
+
501
520
BSON_APPEND_UTF8 (& cmd , "insert" , collection );
502
521
BSON_APPEND_DOCUMENT (& cmd , "writeConcern" ,
503
522
WRITE_CONCERN_DOC (write_concern ));
504
523
BSON_APPEND_BOOL (& cmd , "ordered" , command -> u .insert .ordered );
505
- BSON_APPEND_ARRAY (& cmd , "documents" , command -> u .insert .documents );
506
524
525
+ if ((command -> u .insert .documents -> len < client -> cluster .max_bson_size ) &&
526
+ (command -> u .insert .documents -> len < client -> cluster .max_msg_size )) {
527
+ BSON_APPEND_ARRAY (& cmd , "documents" , command -> u .insert .documents );
528
+ GOTO (fast_path );
529
+ } else {
530
+ bson_append_array_begin (& cmd , "documents" , 9 , & ar );
531
+
532
+ do {
533
+ if (!BSON_ITER_HOLDS_DOCUMENT (& iter )) {
534
+ BSON_ASSERT (false);
535
+ }
536
+
537
+ bson_iter_document (& iter , & len , & data );
538
+
539
+ if (len > (client -> cluster .max_msg_size - cmd .len - overhead )) {
540
+ has_more = true;
541
+ break ;
542
+ }
543
+
544
+ bson_uint32_to_string (i , & key , str , sizeof str );
545
+
546
+ if (!bson_init_static (& tmp , data , len )) {
547
+ BSON_ASSERT (false);
548
+ }
549
+
550
+ BSON_APPEND_DOCUMENT (& ar , key , & tmp );
551
+
552
+ bson_destroy (& tmp );
553
+
554
+ i ++ ;
555
+ } while (bson_iter_next (& iter ));
556
+
557
+ bson_append_array_end (& cmd , & ar );
558
+ }
559
+
560
+ fast_path :
507
561
ret = mongoc_client_command_simple (client , database , & cmd , NULL ,
508
562
& reply , error );
509
563
@@ -513,8 +567,12 @@ _mongoc_write_command_insert (mongoc_write_command_t *command,
513
567
514
568
_mongoc_write_result_merge (result , command , & reply );
515
569
516
- bson_destroy (& reply );
517
570
bson_destroy (& cmd );
571
+ bson_destroy (& reply );
572
+
573
+ if (has_more && (ret || !command -> u .insert .ordered )) {
574
+ GOTO (again );
575
+ }
518
576
519
577
EXIT ;
520
578
}
0 commit comments