@@ -561,7 +561,6 @@ _mongoc_write_opmsg (mongoc_write_command_t *command,
561
561
int32_t max_msg_size ;
562
562
int32_t max_bson_obj_size ;
563
563
int32_t max_document_count ;
564
- uint32_t header ;
565
564
uint32_t payload_batch_size = 0 ;
566
565
uint32_t payload_total_offset = 0 ;
567
566
bool ship_it = false;
@@ -618,18 +617,18 @@ _mongoc_write_opmsg (mongoc_write_command_t *command,
618
617
EXIT ;
619
618
}
620
619
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
+ }
633
632
634
633
do {
635
634
uint32_t ulen ;
@@ -646,7 +645,8 @@ _mongoc_write_opmsg (mongoc_write_command_t *command,
646
645
result -> failed = true;
647
646
break ;
648
647
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 ) {
650
650
/* The current batch is still under max batch size in bytes */
651
651
payload_batch_size += ulen ;
652
652
0 commit comments