Skip to content

Commit 07240c1

Browse files
author
Christian Hergert
committed
write-commands: perform command splitting with insert write command.
1 parent 9604272 commit 07240c1

File tree

1 file changed

+62
-4
lines changed

1 file changed

+62
-4
lines changed

src/mongoc/mongoc-write-command.c

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,19 @@ _mongoc_write_command_insert (mongoc_write_command_t *command,
476476
mongoc_write_result_t *result,
477477
bson_error_t *error)
478478
{
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;
480486
bson_t reply;
487+
char str [12];
488+
size_t overhead;
489+
bool has_more;
481490
bool ret;
491+
int i;
482492

483493
ENTRY;
484494

@@ -489,7 +499,9 @@ _mongoc_write_command_insert (mongoc_write_command_t *command,
489499
BSON_ASSERT (hint);
490500
BSON_ASSERT (collection);
491501

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)) {
493505
bson_set_error (error,
494506
MONGOC_ERROR_COLLECTION,
495507
MONGOC_ERROR_COLLECTION_INSERT_FAILED,
@@ -498,12 +510,54 @@ _mongoc_write_command_insert (mongoc_write_command_t *command,
498510
EXIT;
499511
}
500512

513+
overhead = 1 + strlen ("documents") + 1;
514+
515+
again:
516+
bson_init (&cmd);
517+
has_more = false;
518+
i = 0;
519+
501520
BSON_APPEND_UTF8 (&cmd, "insert", collection);
502521
BSON_APPEND_DOCUMENT (&cmd, "writeConcern",
503522
WRITE_CONCERN_DOC (write_concern));
504523
BSON_APPEND_BOOL (&cmd, "ordered", command->u.insert.ordered);
505-
BSON_APPEND_ARRAY (&cmd, "documents", command->u.insert.documents);
506524

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:
507561
ret = mongoc_client_command_simple (client, database, &cmd, NULL,
508562
&reply, error);
509563

@@ -513,8 +567,12 @@ _mongoc_write_command_insert (mongoc_write_command_t *command,
513567

514568
_mongoc_write_result_merge (result, command, &reply);
515569

516-
bson_destroy (&reply);
517570
bson_destroy (&cmd);
571+
bson_destroy (&reply);
572+
573+
if (has_more && (ret || !command->u.insert.ordered)) {
574+
GOTO (again);
575+
}
518576

519577
EXIT;
520578
}

0 commit comments

Comments
 (0)