Skip to content

Commit 11a63f9

Browse files
committed
CDRIVER-657 - split large inserts, fix upsert offset
Simplify and correct how bulk operations are split into batches. Fix "offset" calculations for merged "upserts" array in bulk operation result.
1 parent 792115a commit 11a63f9

File tree

6 files changed

+322
-318
lines changed

6 files changed

+322
-318
lines changed

src/mongoc/mongoc-bulk-operation.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ mongoc_bulk_operation_execute (mongoc_bulk_operation_t *bulk, /* IN */
366366
mongoc_write_command_t *command;
367367
uint32_t hint = 0;
368368
bool ret;
369+
uint32_t offset = 0;
369370
int i;
370371

371372
ENTRY;
@@ -421,13 +422,16 @@ mongoc_bulk_operation_execute (mongoc_bulk_operation_t *bulk, /* IN */
421422

422423
_mongoc_write_command_execute (command, bulk->client, hint,
423424
bulk->database, bulk->collection,
424-
bulk->write_concern, &bulk->result);
425+
bulk->write_concern, offset,
426+
&bulk->result);
425427

426428
hint = command->hint;
427429

428430
if (bulk->result.failed && bulk->ordered) {
429431
GOTO (cleanup);
430432
}
433+
434+
offset += command->n_documents;
431435
}
432436

433437
cleanup:

src/mongoc/mongoc-collection.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ mongoc_collection_insert_bulk (mongoc_collection_t *collection,
10931093

10941094
_mongoc_write_command_execute (&command, collection->client, 0,
10951095
collection->db, collection->collection,
1096-
write_concern, &result);
1096+
write_concern, 0, &result);
10971097

10981098
collection->gle = bson_new ();
10991099
ret = _mongoc_write_result_complete (&result, collection->gle, error);
@@ -1178,7 +1178,7 @@ mongoc_collection_insert (mongoc_collection_t *collection,
11781178

11791179
_mongoc_write_command_execute (&command, collection->client, 0,
11801180
collection->db, collection->collection,
1181-
write_concern, &result);
1181+
write_concern, 0, &result);
11821182

11831183
collection->gle = bson_new ();
11841184
ret = _mongoc_write_result_complete (&result, collection->gle, error);
@@ -1271,7 +1271,7 @@ mongoc_collection_update (mongoc_collection_t *collection,
12711271

12721272
_mongoc_write_command_execute (&command, collection->client, 0,
12731273
collection->db, collection->collection,
1274-
write_concern, &result);
1274+
write_concern, 0, &result);
12751275

12761276
collection->gle = bson_new ();
12771277
ret = _mongoc_write_result_complete (&result, collection->gle, error);
@@ -1399,7 +1399,7 @@ mongoc_collection_remove (mongoc_collection_t *collection,
13991399

14001400
_mongoc_write_command_execute (&command, collection->client, 0,
14011401
collection->db, collection->collection,
1402-
write_concern, &result);
1402+
write_concern, 0, &result);
14031403

14041404
collection->gle = bson_new ();
14051405
ret = _mongoc_write_result_complete (&result, collection->gle, error);

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,47 +37,39 @@ BSON_BEGIN_DECLS
3737

3838
typedef struct
3939
{
40-
int type;
40+
int type;
4141
uint32_t hint;
42+
bson_t *documents;
43+
uint32_t n_documents;
4244
union {
4345
struct {
4446
uint8_t ordered : 1;
4547
uint8_t multi : 1;
46-
bson_t *selectors;
47-
uint32_t n_selectors;
48-
uint32_t n_merged;
49-
uint32_t current_n_documents;
5048
} delete;
5149
struct {
5250
uint8_t ordered : 1;
5351
uint8_t allow_bulk_op_insert : 1;
54-
bson_t *documents;
55-
uint32_t n_documents;
56-
uint32_t n_merged;
57-
uint32_t current_n_documents;
5852
} insert;
5953
struct {
6054
uint8_t ordered : 1;
61-
bson_t *updates;
62-
uint32_t n_updates;
63-
uint32_t current_n_updates;
6455
} update;
6556
} u;
6657
} mongoc_write_command_t;
6758

6859

6960
typedef struct
7061
{
62+
/* true after a legacy update prevents us from calculating nModified */
7163
bool omit_nModified;
7264
uint32_t nInserted;
7365
uint32_t nMatched;
7466
uint32_t nModified;
7567
uint32_t nRemoved;
7668
uint32_t nUpserted;
77-
uint32_t offset;
78-
uint32_t n_commands;
79-
bson_t upserted;
69+
/* like [{"index": int, "_id": value}, ...] */
8070
bson_t writeErrors;
71+
/* like [{"index": int, "code": int, "errmsg": str}, ...] */
72+
bson_t upserted;
8173
bson_t writeConcernError;
8274
bool failed;
8375
bson_error_t error;
@@ -120,14 +112,16 @@ void _mongoc_write_command_execute (mongoc_write_command_t *command,
120112
const char *database,
121113
const char *collection,
122114
const mongoc_write_concern_t *write_concern,
123-
mongoc_write_result_t *result);
115+
uint32_t offset, mongoc_write_result_t *result);
124116
void _mongoc_write_result_init (mongoc_write_result_t *result);
125117
void _mongoc_write_result_merge (mongoc_write_result_t *result,
126118
mongoc_write_command_t *command,
127-
const bson_t *reply);
119+
const bson_t *reply,
120+
uint32_t offset);
128121
void _mongoc_write_result_merge_legacy (mongoc_write_result_t *result,
129122
mongoc_write_command_t *command,
130-
const bson_t *reply);
123+
const bson_t *reply,
124+
uint32_t offset);
131125
bool _mongoc_write_result_complete (mongoc_write_result_t *result,
132126
bson_t *reply,
133127
bson_error_t *error);

0 commit comments

Comments
 (0)