Skip to content

Commit 198cd89

Browse files
author
Christian Hergert
committed
write-commands: fix index of bulk write upserted index.
When merging upsertion information, the indexes could have contained the actual offset of all mutations, not the index of the command that contained the upsertion. This fixes that, which also requires how many inserts we merged during the mongoc_write_command_insert_append() call.
1 parent 0df5cb8 commit 198cd89

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

src/mongoc/mongoc-write-command-private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ typedef struct
4848
uint8_t allow_bulk_op_insert : 1;
4949
bson_t *documents;
5050
uint32_t n_documents;
51+
uint32_t n_merged;
5152
} insert;
5253
struct {
5354
uint8_t ordered : 1;
@@ -69,6 +70,7 @@ typedef struct
6970
uint32_t nRemoved;
7071
uint32_t nUpserted;
7172
uint32_t offset;
73+
uint32_t n_commands;
7274
bson_t upserted;
7375
bson_t writeErrors;
7476
bson_t writeConcernErrors;

src/mongoc/mongoc-write-command.c

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ _mongoc_write_command_insert_append (mongoc_write_command_t *command,
9797
}
9898
}
9999

100+
if (command->u.insert.n_documents) {
101+
command->u.insert.n_merged++;
102+
}
103+
100104
command->u.insert.n_documents += n_documents;
101105

102106
EXIT;
@@ -119,6 +123,7 @@ _mongoc_write_command_init_insert
119123
command->type = MONGOC_WRITE_COMMAND_INSERT;
120124
command->u.insert.documents = bson_new ();
121125
command->u.insert.n_documents = 0;
126+
command->u.insert.n_merged = 0;
122127
command->u.insert.ordered = ordered;
123128
command->u.insert.allow_bulk_op_insert = allow_bulk_op_insert;
124129

@@ -913,29 +918,19 @@ _mongoc_write_result_merge_legacy (mongoc_write_result_t *result, /* IN */
913918
BSON_ITER_HOLDS_OID (&iter)) {
914919
result->nUpserted += 1;
915920
value = bson_iter_value (&iter);
916-
_mongoc_write_result_append_upsert (result, result->offset, value);
921+
_mongoc_write_result_append_upsert (result, result->n_commands, value);
917922
} else if (bson_iter_init_find (&iter, reply, "upserted") &&
918923
BSON_ITER_HOLDS_ARRAY (&iter)) {
919924
result->nUpserted += n;
920925
if (bson_iter_recurse (&iter, &ar)) {
921926
while (bson_iter_next (&ar)) {
922927
if (BSON_ITER_HOLDS_DOCUMENT (&ar) &&
923-
bson_iter_recurse (&ar, &citer)) {
924-
idx = 0;
925-
value = NULL;
926-
while (bson_iter_next (&citer)) {
927-
if (BSON_ITER_IS_KEY (&citer, "index") &&
928-
BSON_ITER_HOLDS_INT32 (&citer)) {
929-
idx = bson_iter_int32 (&citer);
930-
} else if (BSON_ITER_IS_KEY (&citer, "_id")) {
931-
value = bson_iter_value (&citer);
932-
}
933-
}
934-
if (value) {
935-
_mongoc_write_result_append_upsert (result,
936-
result->offset + idx,
937-
value);
938-
}
928+
bson_iter_recurse (&ar, &citer) &&
929+
bson_iter_find (&citer, "_id")) {
930+
value = bson_iter_value (&citer);
931+
_mongoc_write_result_append_upsert (result,
932+
result->n_commands,
933+
value);
939934
}
940935
}
941936
}
@@ -961,6 +956,12 @@ _mongoc_write_result_merge_legacy (mongoc_write_result_t *result, /* IN */
961956
break;
962957
}
963958

959+
result->n_commands++;
960+
961+
if (command->type == MONGOC_WRITE_COMMAND_INSERT) {
962+
result->n_commands += command->u.insert.n_merged;
963+
}
964+
964965
EXIT;
965966
}
966967

@@ -1016,6 +1017,7 @@ _mongoc_write_result_merge (mongoc_write_result_t *result, /* IN */
10161017
const bson_value_t *value;
10171018
bson_iter_t iter;
10181019
bson_iter_t citer;
1020+
bson_iter_t ar;
10191021
int32_t n_upserted = 0;
10201022
int32_t affected = 0;
10211023

@@ -1046,12 +1048,22 @@ _mongoc_write_result_merge (mongoc_write_result_t *result, /* IN */
10461048
case MONGOC_WRITE_COMMAND_UPDATE:
10471049
if (bson_iter_init_find (&iter, reply, "upserted")) {
10481050
if (BSON_ITER_HOLDS_ARRAY (&iter)) {
1049-
n_upserted = _mongoc_write_result_merge_arrays (result,
1050-
&result->upserted,
1051-
&iter);
1051+
if (bson_iter_recurse (&iter, &ar)) {
1052+
while (bson_iter_next (&ar)) {
1053+
if (BSON_ITER_HOLDS_DOCUMENT (&ar) &&
1054+
bson_iter_recurse (&ar, &citer) &&
1055+
bson_iter_find (&citer, "_id")) {
1056+
value = bson_iter_value (&citer);
1057+
_mongoc_write_result_append_upsert (result,
1058+
result->n_commands,
1059+
value);
1060+
n_upserted++;
1061+
}
1062+
}
1063+
}
10521064
} else {
10531065
value = bson_iter_value (&iter);
1054-
_mongoc_write_result_append_upsert (result, 0, value);
1066+
_mongoc_write_result_append_upsert (result, result->n_commands, value);
10551067
n_upserted = 1;
10561068
}
10571069
result->nUpserted += n_upserted;
@@ -1107,6 +1119,12 @@ _mongoc_write_result_merge (mongoc_write_result_t *result, /* IN */
11071119
break;
11081120
}
11091121

1122+
result->n_commands++;
1123+
1124+
if (command->type == MONGOC_WRITE_COMMAND_INSERT) {
1125+
result->n_commands += command->u.insert.n_merged;
1126+
}
1127+
11101128
EXIT;
11111129
}
11121130

0 commit comments

Comments
 (0)