Skip to content

Commit 6ae8c48

Browse files
committed
Merge pull request #65 from hanumantmk/writeErrorFix
Bulk API fixes
2 parents 6c44719 + dc4b5bf commit 6ae8c48

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ typedef struct
5454
bson_t *documents;
5555
uint32_t n_documents;
5656
uint32_t n_merged;
57+
uint32_t current_n_documents;
5758
} insert;
5859
struct {
5960
uint8_t ordered : 1;

src/mongoc/mongoc-write-command.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ typedef void (*mongoc_write_op_t) (mongoc_write_command_t *command,
5353

5454
static bson_t gEmptyWriteConcern = BSON_INITIALIZER;
5555

56-
56+
static int32_t
57+
_mongoc_write_result_merge_arrays (mongoc_write_result_t *result,
58+
bson_t *dest,
59+
bson_iter_t *iter);
5760
void
5861
_mongoc_write_command_insert_append (mongoc_write_command_t *command,
5962
const bson_t * const *documents,
@@ -379,6 +382,7 @@ _mongoc_write_command_insert_legacy (mongoc_write_command_t *command,
379382

380383
cleanup:
381384
if (gle) {
385+
command->u.insert.current_n_documents = i;
382386
_mongoc_write_result_merge_legacy (result, command, gle);
383387
bson_destroy (gle);
384388
gle = NULL;
@@ -651,6 +655,7 @@ _mongoc_write_command_insert (mongoc_write_command_t *command,
651655
result->failed = true;
652656
}
653657

658+
command->u.insert.current_n_documents = i;
654659
_mongoc_write_result_merge (result, command, &reply);
655660

656661
bson_destroy (&cmd);
@@ -876,6 +881,7 @@ _mongoc_write_result_merge_legacy (mongoc_write_result_t *result, /* IN */
876881
const bson_t *reply) /* IN */
877882
{
878883
const bson_value_t *value;
884+
bson_t holder, write_errors, child;
879885
bson_iter_t iter;
880886
bson_iter_t ar;
881887
bson_iter_t citer;
@@ -909,6 +915,21 @@ _mongoc_write_result_merge_legacy (mongoc_write_result_t *result, /* IN */
909915
code,
910916
"%s", err);
911917
result->failed = true;
918+
919+
bson_init(&holder);
920+
bson_append_array_begin(&holder, "0", 1, &write_errors);
921+
bson_append_document_begin(&write_errors, "0", 1, &child);
922+
bson_append_int32(&child, "index", 5, 0);
923+
bson_append_int32(&child, "code", 4, code);
924+
bson_append_utf8(&child, "errmsg", 6, err, -1);
925+
bson_append_document_end(&write_errors, &child);
926+
bson_append_array_end(&holder, &write_errors);
927+
bson_iter_init(&iter, &holder);
928+
bson_iter_next(&iter);
929+
930+
_mongoc_write_result_merge_arrays (result, &result->writeErrors, &iter);
931+
932+
bson_destroy(&holder);
912933
}
913934

914935
switch (command->type) {
@@ -974,7 +995,7 @@ _mongoc_write_result_merge_legacy (mongoc_write_result_t *result, /* IN */
974995
result->offset += 1;
975996
break;
976997
case MONGOC_WRITE_COMMAND_INSERT:
977-
result->offset += command->u.insert.n_documents;
998+
result->offset += command->u.insert.current_n_documents;
978999
break;
9791000
default:
9801001
break;
@@ -1144,7 +1165,7 @@ _mongoc_write_result_merge (mongoc_write_result_t *result, /* IN */
11441165
result->offset += affected;
11451166
break;
11461167
case MONGOC_WRITE_COMMAND_INSERT:
1147-
result->offset += command->u.insert.n_documents;
1168+
result->offset += command->u.insert.current_n_documents;
11481169
break;
11491170
default:
11501171
break;

tests/test-bulk.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,74 @@ test_index_offset (void)
291291
bson_destroy (sel);
292292
}
293293

294+
static void
295+
test_bulk_edge_over_1000 (void)
296+
{
297+
mongoc_client_t *client;
298+
mongoc_collection_t *collection;
299+
mongoc_bulk_operation_t * bulk_op;
300+
mongoc_write_concern_t * wc = mongoc_write_concern_new();
301+
bson_iter_t iter, error_iter, index;
302+
bson_t doc, result;
303+
bson_error_t error;
304+
int i;
305+
306+
client = mongoc_client_new (gTestUri);
307+
assert (client);
308+
309+
collection = get_test_collection (client, "OVER_1000");
310+
assert (collection);
311+
312+
mongoc_write_concern_set_w(wc, 1);
313+
314+
bulk_op = mongoc_collection_create_bulk_operation(collection, false, wc);
315+
316+
for (i = 0; i < 1010; i+=3) {
317+
bson_init(&doc);
318+
bson_append_int32(&doc, "_id", -1, i);
319+
320+
mongoc_bulk_operation_insert(bulk_op, &doc);
321+
322+
bson_destroy(&doc);
323+
}
324+
325+
mongoc_bulk_operation_execute(bulk_op, NULL, &error);
326+
327+
mongoc_bulk_operation_destroy(bulk_op);
328+
329+
bulk_op = mongoc_collection_create_bulk_operation(collection, false, wc);
330+
for (i = 0; i < 1010; i++) {
331+
bson_init(&doc);
332+
bson_append_int32(&doc, "_id", -1, i);
333+
334+
mongoc_bulk_operation_insert(bulk_op, &doc);
335+
336+
bson_destroy(&doc);
337+
}
338+
339+
mongoc_bulk_operation_execute(bulk_op, &result, &error);
340+
341+
bson_iter_init_find(&iter, &result, "writeErrors");
342+
assert(bson_iter_recurse(&iter, &error_iter));
343+
assert(bson_iter_next(&error_iter));
344+
345+
for (i = 0; i < 1010; i+=3) {
346+
assert(bson_iter_recurse(&error_iter, &index));
347+
assert(bson_iter_find(&index, "index"));
348+
if (bson_iter_int32(&index) != i) {
349+
fprintf(stderr, "index should be %d, but is %d\n", i, bson_iter_int32(&index));
350+
}
351+
assert(bson_iter_int32(&index) == i);
352+
bson_iter_next(&error_iter);
353+
}
354+
355+
mongoc_bulk_operation_destroy(bulk_op);
356+
357+
mongoc_write_concern_destroy(wc);
358+
359+
mongoc_collection_destroy(collection);
360+
mongoc_client_destroy(client);
361+
}
294362

295363
static void
296364
test_bulk_edge_case_372 (void)
@@ -497,6 +565,7 @@ test_bulk_install (TestSuite *suite)
497565
TestSuite_Add (suite, "/BulkOperation/index_offset", test_index_offset);
498566
TestSuite_Add (suite, "/BulkOperation/CDRIVER-372", test_bulk_edge_case_372);
499567
TestSuite_Add (suite, "/BulkOperation/new", test_bulk_new);
568+
TestSuite_Add (suite, "/BulkOperation/over_1000", test_bulk_edge_over_1000);
500569

501570
atexit (cleanup_globals);
502571
}

0 commit comments

Comments
 (0)