Skip to content

Commit a75a4eb

Browse files
committed
add lookup_as_int64, and fix verbose results
Fix updateResults and deleteResults to return values as int64 to match spec.
1 parent 93feb70 commit a75a4eb

File tree

1 file changed

+66
-29
lines changed

1 file changed

+66
-29
lines changed

src/libmongoc/src/mongoc/mongoc-bulkwrite.c

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ _bulkwriteresult_new (void)
925925

926926
static void
927927
_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)
929929
{
930930
BSON_ASSERT_PARAM (self);
931931
BSON_ASSERT (upserted_id || true);
@@ -937,16 +937,16 @@ _bulkwriteresult_set_updateresult (
937937
bson_free (key);
938938
}
939939

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));
942942
if (upserted_id) {
943943
BSON_ASSERT (BSON_APPEND_VALUE (&updateresult, "upsertedId", upserted_id));
944944
}
945945
BSON_ASSERT (bson_append_document_end (&self->updateresults, &updateresult));
946946
}
947947

948948
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)
950950
{
951951
BSON_ASSERT_PARAM (self);
952952

@@ -957,7 +957,7 @@ _bulkwriteresult_set_deleteresult (mongoc_bulkwriteresult_t *self, int32_t n, si
957957
bson_free (key);
958958
}
959959

960-
BSON_ASSERT (BSON_APPEND_INT32 (&deleteresult, "deletedCount", n));
960+
BSON_ASSERT (BSON_APPEND_INT64 (&deleteresult, "deletedCount", n));
961961
BSON_ASSERT (bson_append_document_end (&self->deleteresults, &deleteresult));
962962
}
963963

@@ -1144,6 +1144,41 @@ lookup_int32 (const bson_t *bson, const char *key, int32_t *out, const char *sou
11441144
return false;
11451145
}
11461146

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+
11471182
static bool
11481183
lookup_double (const bson_t *bson, const char *key, double *out, const char *source, mongoc_bulkwriteexception_t *exc)
11491184
{
@@ -1548,35 +1583,37 @@ mongoc_bulkwrite_execute (mongoc_bulkwrite_t *self, const mongoc_bulkwriteopts_t
15481583

15491584
// Parse top-level fields.
15501585
{
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)) {
15531590
goto batch_fail;
15541591
}
1555-
ret.res->insertedcount += (int64_t) nInserted;
1592+
ret.res->insertedcount += nInserted;
15561593

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)) {
15591596
goto batch_fail;
15601597
}
1561-
ret.res->matchedcount += (int64_t) nMatched;
1598+
ret.res->matchedcount += nMatched;
15621599

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)) {
15651602
goto batch_fail;
15661603
}
1567-
ret.res->modifiedcount += (int64_t) nModified;
1604+
ret.res->modifiedcount += nModified;
15681605

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)) {
15711608
goto batch_fail;
15721609
}
1573-
ret.res->deletedcount += (int64_t) nDeleted;
1610+
ret.res->deletedcount += nDeleted;
15741611

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)) {
15771614
goto batch_fail;
15781615
}
1579-
ret.res->upsertedcount += (int64_t) nUpserted;
1616+
ret.res->upsertedcount += nUpserted;
15801617
}
15811618

15821619
if (bson_iter_init_find (&iter, &cmd_reply, "writeConcernError")) {
@@ -1657,16 +1694,16 @@ mongoc_bulkwrite_execute (mongoc_bulkwrite_t *self, const mongoc_bulkwriteopts_t
16571694
}
16581695

16591696
// Parse `idx`.
1660-
int32_t idx;
1697+
int64_t idx;
16611698
{
1662-
if (!lookup_int32 (result, "idx", &idx, "result", ret.exc)) {
1699+
if (!lookup_as_int64 (result, "idx", &idx, "result", ret.exc)) {
16631700
goto batch_fail;
16641701
}
16651702
if (idx < 0) {
16661703
bson_set_error (&error,
16671704
MONGOC_ERROR_COMMAND,
16681705
MONGOC_ERROR_COMMAND_INVALID_ARG,
1669-
"expected to find non-negative int32 `idx` in "
1706+
"expected to find non-negative int64 `idx` in "
16701707
"result, but did not");
16711708
_bulkwriteexception_set_error (ret.exc, &error);
16721709
goto batch_fail;
@@ -1715,14 +1752,14 @@ mongoc_bulkwrite_execute (mongoc_bulkwrite_t *self, const mongoc_bulkwriteopts_t
17151752
case MODEL_OP_UPDATE: {
17161753
bson_iter_t result_iter;
17171754
// 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)) {
17201757
goto batch_fail;
17211758
}
17221759

17231760
// 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)) {
17261763
goto batch_fail;
17271764
}
17281765

@@ -1748,8 +1785,8 @@ mongoc_bulkwrite_execute (mongoc_bulkwrite_t *self, const mongoc_bulkwriteopts_t
17481785
}
17491786
case MODEL_OP_DELETE: {
17501787
// 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)) {
17531790
goto batch_fail;
17541791
}
17551792

0 commit comments

Comments
 (0)