Skip to content

Commit 24904ae

Browse files
committed
clarify calculation of opmsg_overhead for collection bulk write
1 parent e5c978d commit 24904ae

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/libmongoc/src/mongoc/mongoc-write-command.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,6 @@ _mongoc_write_opmsg (mongoc_write_command_t *command,
561561
int32_t max_msg_size;
562562
int32_t max_bson_obj_size;
563563
int32_t max_document_count;
564-
uint32_t header;
565564
uint32_t payload_batch_size = 0;
566565
uint32_t payload_total_offset = 0;
567566
bool ship_it = false;
@@ -618,18 +617,18 @@ _mongoc_write_opmsg (mongoc_write_command_t *command,
618617
EXIT;
619618
}
620619

621-
/*
622-
* OP_MSG header == 16 byte
623-
* + 4 bytes flagBits
624-
* + 1 byte payload type = 0
625-
* + 1 byte payload type = 1
626-
* + 4 byte size of payload
627-
* == 26 bytes opcode overhead
628-
* + X Full command document {insert: "test", writeConcern: {...}}
629-
* + Y command identifier ("documents", "deletes", "updates") ( + \0)
630-
*/
631-
632-
header = 26 + parts.assembled.command->len + gCommandFieldLens[command->type] + 1;
620+
// Calculate overhead of OP_MSG data. See OP_MSG spec for description of fields.
621+
uint32_t opmsg_overhead = 0;
622+
{
623+
opmsg_overhead += 16; // OP_MSG.MsgHeader
624+
opmsg_overhead += 4; // OP_MSG.flagBits
625+
opmsg_overhead += 1; // OP_MSG.Section[0].payloadType (0)
626+
opmsg_overhead += parts.assembled.command->len; // OP_MSG.Section[0].payload.document
627+
opmsg_overhead += 1; // OP_MSG.Section[1].payloadType (1)
628+
opmsg_overhead += 4; // OP_MSG.Section[1].payload.size
629+
opmsg_overhead += gCommandFieldLens[command->type] + 1; // OP_MSG.Section[1].payload.identifier
630+
// OP_MSG.Section[1].payload.documents is omitted. Calculated below with remaining size.
631+
}
633632

634633
do {
635634
uint32_t ulen;
@@ -646,7 +645,8 @@ _mongoc_write_opmsg (mongoc_write_command_t *command,
646645
result->failed = true;
647646
break;
648647

649-
} else if (bson_cmp_less_equal_us (payload_batch_size + header + ulen, max_msg_size) || document_count == 0) {
648+
} else if (bson_cmp_less_equal_us (payload_batch_size + opmsg_overhead + ulen, max_msg_size) ||
649+
document_count == 0) {
650650
/* The current batch is still under max batch size in bytes */
651651
payload_batch_size += ulen;
652652

0 commit comments

Comments
 (0)