@@ -925,7 +925,7 @@ _bulkwriteresult_new (void)
925
925
926
926
static void
927
927
_bulkwriteresult_set_updateresult (
928
- mongoc_bulkwriteresult_t * self , int32_t n , int32_t nModified , const bson_value_t * upserted_id , size_t models_idx )
928
+ mongoc_bulkwriteresult_t * self , int64_t n , int64_t nModified , const bson_value_t * upserted_id , size_t models_idx )
929
929
{
930
930
BSON_ASSERT_PARAM (self );
931
931
BSON_ASSERT (upserted_id || true);
@@ -937,16 +937,16 @@ _bulkwriteresult_set_updateresult (
937
937
bson_free (key );
938
938
}
939
939
940
- BSON_ASSERT (BSON_APPEND_INT32 (& updateresult , "matchedCount" , n ));
941
- BSON_ASSERT (BSON_APPEND_INT32 (& updateresult , "modifiedCount" , nModified ));
940
+ BSON_ASSERT (BSON_APPEND_INT64 (& updateresult , "matchedCount" , n ));
941
+ BSON_ASSERT (BSON_APPEND_INT64 (& updateresult , "modifiedCount" , nModified ));
942
942
if (upserted_id ) {
943
943
BSON_ASSERT (BSON_APPEND_VALUE (& updateresult , "upsertedId" , upserted_id ));
944
944
}
945
945
BSON_ASSERT (bson_append_document_end (& self -> updateresults , & updateresult ));
946
946
}
947
947
948
948
static void
949
- _bulkwriteresult_set_deleteresult (mongoc_bulkwriteresult_t * self , int32_t n , size_t models_idx )
949
+ _bulkwriteresult_set_deleteresult (mongoc_bulkwriteresult_t * self , int64_t n , size_t models_idx )
950
950
{
951
951
BSON_ASSERT_PARAM (self );
952
952
@@ -957,7 +957,7 @@ _bulkwriteresult_set_deleteresult (mongoc_bulkwriteresult_t *self, int32_t n, si
957
957
bson_free (key );
958
958
}
959
959
960
- BSON_ASSERT (BSON_APPEND_INT32 (& deleteresult , "deletedCount" , n ));
960
+ BSON_ASSERT (BSON_APPEND_INT64 (& deleteresult , "deletedCount" , n ));
961
961
BSON_ASSERT (bson_append_document_end (& self -> deleteresults , & deleteresult ));
962
962
}
963
963
@@ -1144,6 +1144,41 @@ lookup_int32 (const bson_t *bson, const char *key, int32_t *out, const char *sou
1144
1144
return false;
1145
1145
}
1146
1146
1147
+ // `lookup_as_int64` looks for `key` as a BSON int32, int64, or double and returns as an int64_t. Doubles are truncated.
1148
+ static int64_t
1149
+ lookup_as_int64 (
1150
+ const bson_t * bson , const char * key , int64_t * out , const char * source , mongoc_bulkwriteexception_t * exc )
1151
+ {
1152
+ BSON_ASSERT_PARAM (bson );
1153
+ BSON_ASSERT_PARAM (key );
1154
+ BSON_ASSERT_PARAM (out );
1155
+ BSON_ASSERT (source || true);
1156
+ BSON_ASSERT_PARAM (exc );
1157
+
1158
+ bson_iter_t iter ;
1159
+ if (bson_iter_init_find (& iter , bson , key ) && BSON_ITER_HOLDS_NUMBER (& iter )) {
1160
+ * out = bson_iter_as_int64 (& iter );
1161
+ return true;
1162
+ }
1163
+ bson_error_t error ;
1164
+ if (source ) {
1165
+ bson_set_error (& error ,
1166
+ MONGOC_ERROR_COMMAND ,
1167
+ MONGOC_ERROR_COMMAND_INVALID_ARG ,
1168
+ "expected to find int32, int64, or double `%s` in %s, but did not" ,
1169
+ key ,
1170
+ source );
1171
+ } else {
1172
+ bson_set_error (& error ,
1173
+ MONGOC_ERROR_COMMAND ,
1174
+ MONGOC_ERROR_COMMAND_INVALID_ARG ,
1175
+ "expected to find int32, int64, or double `%s`, but did not" ,
1176
+ key );
1177
+ }
1178
+ _bulkwriteexception_set_error (exc , & error );
1179
+ return false;
1180
+ }
1181
+
1147
1182
static bool
1148
1183
lookup_double (const bson_t * bson , const char * key , double * out , const char * source , mongoc_bulkwriteexception_t * exc )
1149
1184
{
@@ -1548,35 +1583,37 @@ mongoc_bulkwrite_execute (mongoc_bulkwrite_t *self, const mongoc_bulkwriteopts_t
1548
1583
1549
1584
// Parse top-level fields.
1550
1585
{
1551
- int32_t nInserted ;
1552
- if (!lookup_int32 (& cmd_reply , "nInserted" , & nInserted , NULL , ret .exc )) {
1586
+ // These fields are expected to be int32 as of server 8.0. However, drivers return the values as int64.
1587
+ // Use `lookup_as_int64` to support other numeric types to future-proof.
1588
+ int64_t nInserted ;
1589
+ if (!lookup_as_int64 (& cmd_reply , "nInserted" , & nInserted , NULL , ret .exc )) {
1553
1590
goto batch_fail ;
1554
1591
}
1555
- ret .res -> insertedcount += ( int64_t ) nInserted ;
1592
+ ret .res -> insertedcount += nInserted ;
1556
1593
1557
- int32_t nMatched ;
1558
- if (!lookup_int32 (& cmd_reply , "nMatched" , & nMatched , NULL , ret .exc )) {
1594
+ int64_t nMatched ;
1595
+ if (!lookup_as_int64 (& cmd_reply , "nMatched" , & nMatched , NULL , ret .exc )) {
1559
1596
goto batch_fail ;
1560
1597
}
1561
- ret .res -> matchedcount += ( int64_t ) nMatched ;
1598
+ ret .res -> matchedcount += nMatched ;
1562
1599
1563
- int32_t nModified ;
1564
- if (!lookup_int32 (& cmd_reply , "nModified" , & nModified , NULL , ret .exc )) {
1600
+ int64_t nModified ;
1601
+ if (!lookup_as_int64 (& cmd_reply , "nModified" , & nModified , NULL , ret .exc )) {
1565
1602
goto batch_fail ;
1566
1603
}
1567
- ret .res -> modifiedcount += ( int64_t ) nModified ;
1604
+ ret .res -> modifiedcount += nModified ;
1568
1605
1569
- int32_t nDeleted ;
1570
- if (!lookup_int32 (& cmd_reply , "nDeleted" , & nDeleted , NULL , ret .exc )) {
1606
+ int64_t nDeleted ;
1607
+ if (!lookup_as_int64 (& cmd_reply , "nDeleted" , & nDeleted , NULL , ret .exc )) {
1571
1608
goto batch_fail ;
1572
1609
}
1573
- ret .res -> deletedcount += ( int64_t ) nDeleted ;
1610
+ ret .res -> deletedcount += nDeleted ;
1574
1611
1575
- int32_t nUpserted ;
1576
- if (!lookup_int32 (& cmd_reply , "nUpserted" , & nUpserted , NULL , ret .exc )) {
1612
+ int64_t nUpserted ;
1613
+ if (!lookup_as_int64 (& cmd_reply , "nUpserted" , & nUpserted , NULL , ret .exc )) {
1577
1614
goto batch_fail ;
1578
1615
}
1579
- ret .res -> upsertedcount += ( int64_t ) nUpserted ;
1616
+ ret .res -> upsertedcount += nUpserted ;
1580
1617
}
1581
1618
1582
1619
if (bson_iter_init_find (& iter , & cmd_reply , "writeConcernError" )) {
@@ -1657,16 +1694,16 @@ mongoc_bulkwrite_execute (mongoc_bulkwrite_t *self, const mongoc_bulkwriteopts_t
1657
1694
}
1658
1695
1659
1696
// Parse `idx`.
1660
- int32_t idx ;
1697
+ int64_t idx ;
1661
1698
{
1662
- if (!lookup_int32 (result , "idx" , & idx , "result" , ret .exc )) {
1699
+ if (!lookup_as_int64 (result , "idx" , & idx , "result" , ret .exc )) {
1663
1700
goto batch_fail ;
1664
1701
}
1665
1702
if (idx < 0 ) {
1666
1703
bson_set_error (& error ,
1667
1704
MONGOC_ERROR_COMMAND ,
1668
1705
MONGOC_ERROR_COMMAND_INVALID_ARG ,
1669
- "expected to find non-negative int32 `idx` in "
1706
+ "expected to find non-negative int64 `idx` in "
1670
1707
"result, but did not" );
1671
1708
_bulkwriteexception_set_error (ret .exc , & error );
1672
1709
goto batch_fail ;
@@ -1715,14 +1752,14 @@ mongoc_bulkwrite_execute (mongoc_bulkwrite_t *self, const mongoc_bulkwriteopts_t
1715
1752
case MODEL_OP_UPDATE : {
1716
1753
bson_iter_t result_iter ;
1717
1754
// Parse `n`.
1718
- int32_t n ;
1719
- if (!lookup_int32 (result , "n" , & n , "result" , ret .exc )) {
1755
+ int64_t n ;
1756
+ if (!lookup_as_int64 (result , "n" , & n , "result" , ret .exc )) {
1720
1757
goto batch_fail ;
1721
1758
}
1722
1759
1723
1760
// Parse `nModified`.
1724
- int32_t nModified ;
1725
- if (!lookup_int32 (result , "nModified" , & nModified , "result" , ret .exc )) {
1761
+ int64_t nModified ;
1762
+ if (!lookup_as_int64 (result , "nModified" , & nModified , "result" , ret .exc )) {
1726
1763
goto batch_fail ;
1727
1764
}
1728
1765
@@ -1748,8 +1785,8 @@ mongoc_bulkwrite_execute (mongoc_bulkwrite_t *self, const mongoc_bulkwriteopts_t
1748
1785
}
1749
1786
case MODEL_OP_DELETE : {
1750
1787
// Parse `n`.
1751
- int32_t n ;
1752
- if (!lookup_int32 (result , "n" , & n , "result" , ret .exc )) {
1788
+ int64_t n ;
1789
+ if (!lookup_as_int64 (result , "n" , & n , "result" , ret .exc )) {
1753
1790
goto batch_fail ;
1754
1791
}
1755
1792
0 commit comments