Skip to content

Commit f507c8b

Browse files
committed
Merge branch 'pr-227'
* pr-227: Fixes for batched update/delete Properly respect message size for bulk operations Merge bulk delete Properly respect bulk size n_updates shouldn't be a bitfield Some steps into respecting proper bulk size Support for proper bulk updates bulk: send multiple delete commands at once.
2 parents fe8f61d + 0cae425 commit f507c8b

File tree

4 files changed

+507
-135
lines changed

4 files changed

+507
-135
lines changed

src/mongoc/mongoc-bulk-operation.c

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,26 @@ mongoc_bulk_operation_remove (mongoc_bulk_operation_t *bulk, /* IN */
115115
const bson_t *selector) /* IN */
116116
{
117117
mongoc_write_command_t command = { 0 };
118+
mongoc_write_command_t *last;
119+
120+
ENTRY;
118121

119122
bson_return_if_fail (bulk);
120123
bson_return_if_fail (selector);
121124

122-
ENTRY;
125+
if (bulk->commands.len) {
126+
last = &_mongoc_array_index (&bulk->commands,
127+
mongoc_write_command_t,
128+
bulk->commands.len - 1);
129+
if ((last->type == MONGOC_WRITE_COMMAND_DELETE) &&
130+
last->u.delete.multi) {
131+
_mongoc_write_command_delete_append (last, selector);
132+
EXIT;
133+
}
134+
}
123135

124136
_mongoc_write_command_init_delete (&command, selector, true, bulk->ordered);
137+
125138
_mongoc_array_append_val (&bulk->commands, command);
126139

127140
EXIT;
@@ -133,13 +146,26 @@ mongoc_bulk_operation_remove_one (mongoc_bulk_operation_t *bulk, /* IN */
133146
const bson_t *selector) /* IN */
134147
{
135148
mongoc_write_command_t command = { 0 };
149+
mongoc_write_command_t *last;
150+
151+
ENTRY;
136152

137153
bson_return_if_fail (bulk);
138154
bson_return_if_fail (selector);
139155

140-
ENTRY;
156+
if (bulk->commands.len) {
157+
last = &_mongoc_array_index (&bulk->commands,
158+
mongoc_write_command_t,
159+
bulk->commands.len - 1);
160+
if ((last->type == MONGOC_WRITE_COMMAND_DELETE) &&
161+
!last->u.delete.multi) {
162+
_mongoc_write_command_delete_append (last, selector);
163+
EXIT;
164+
}
165+
}
141166

142167
_mongoc_write_command_init_delete (&command, selector, false, bulk->ordered);
168+
143169
_mongoc_array_append_val (&bulk->commands, command);
144170

145171
EXIT;
@@ -186,6 +212,7 @@ mongoc_bulk_operation_insert (mongoc_bulk_operation_t *bulk,
186212
last = &_mongoc_array_index (&bulk->commands,
187213
mongoc_write_command_t,
188214
bulk->commands.len - 1);
215+
189216
if (last->type == MONGOC_WRITE_COMMAND_INSERT) {
190217
_mongoc_write_command_insert_append (last, &document, 1);
191218
EXIT;
@@ -209,6 +236,7 @@ mongoc_bulk_operation_replace_one (mongoc_bulk_operation_t *bulk,
209236
{
210237
mongoc_write_command_t command = { 0 };
211238
size_t err_off;
239+
mongoc_write_command_t *last;
212240

213241
bson_return_if_fail (bulk);
214242
bson_return_if_fail (selector);
@@ -225,6 +253,16 @@ mongoc_bulk_operation_replace_one (mongoc_bulk_operation_t *bulk,
225253
EXIT;
226254
}
227255

256+
if (bulk->commands.len) {
257+
last = &_mongoc_array_index (&bulk->commands,
258+
mongoc_write_command_t,
259+
bulk->commands.len - 1);
260+
if (last->type == MONGOC_WRITE_COMMAND_UPDATE) {
261+
_mongoc_write_command_update_append (last, selector, document, upsert, false);
262+
EXIT;
263+
}
264+
}
265+
228266
_mongoc_write_command_init_update (&command, selector, document, upsert,
229267
false, bulk->ordered);
230268
_mongoc_array_append_val (&bulk->commands, command);
@@ -239,8 +277,10 @@ mongoc_bulk_operation_update (mongoc_bulk_operation_t *bulk,
239277
const bson_t *document,
240278
bool upsert)
241279
{
280+
bool multi = true;
242281
mongoc_write_command_t command = { 0 };
243282
bson_iter_t iter;
283+
mongoc_write_command_t *last;
244284

245285
bson_return_if_fail (bulk);
246286
bson_return_if_fail (selector);
@@ -258,8 +298,18 @@ mongoc_bulk_operation_update (mongoc_bulk_operation_t *bulk,
258298
}
259299
}
260300

301+
if (bulk->commands.len) {
302+
last = &_mongoc_array_index (&bulk->commands,
303+
mongoc_write_command_t,
304+
bulk->commands.len - 1);
305+
if (last->type == MONGOC_WRITE_COMMAND_UPDATE) {
306+
_mongoc_write_command_update_append (last, selector, document, upsert, multi);
307+
EXIT;
308+
}
309+
}
310+
261311
_mongoc_write_command_init_update (&command, selector, document, upsert,
262-
true, bulk->ordered);
312+
multi, bulk->ordered);
263313
_mongoc_array_append_val (&bulk->commands, command);
264314
EXIT;
265315
}
@@ -273,6 +323,7 @@ mongoc_bulk_operation_update_one (mongoc_bulk_operation_t *bulk,
273323
{
274324
mongoc_write_command_t command = { 0 };
275325
bson_iter_t iter;
326+
mongoc_write_command_t *last;
276327

277328
bson_return_if_fail (bulk);
278329
bson_return_if_fail (selector);
@@ -290,6 +341,16 @@ mongoc_bulk_operation_update_one (mongoc_bulk_operation_t *bulk,
290341
}
291342
}
292343

344+
if (bulk->commands.len) {
345+
last = &_mongoc_array_index (&bulk->commands,
346+
mongoc_write_command_t,
347+
bulk->commands.len - 1);
348+
if (last->type == MONGOC_WRITE_COMMAND_UPDATE) {
349+
_mongoc_write_command_update_append (last, selector, document, upsert, false);
350+
EXIT;
351+
}
352+
}
353+
293354
_mongoc_write_command_init_update (&command, selector, document, upsert,
294355
false, bulk->ordered);
295356
_mongoc_array_append_val (&bulk->commands, command);

src/mongoc/mongoc-error.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ typedef enum
8080
MONGOC_ERROR_COMMAND_INVALID_ARG,
8181

8282
MONGOC_ERROR_COLLECTION_INSERT_FAILED,
83+
MONGOC_ERROR_COLLECTION_UPDATE_FAILED,
84+
MONGOC_ERROR_COLLECTION_DELETE_FAILED,
8385
MONGOC_ERROR_COLLECTION_DOES_NOT_EXIST = 26,
8486

8587
MONGOC_ERROR_GRIDFS_INVALID_FILENAME,

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ typedef struct
4343
struct {
4444
uint8_t ordered : 1;
4545
uint8_t multi : 1;
46-
bson_t *selector;
46+
bson_t *selectors;
47+
uint32_t n_selectors;
48+
uint32_t n_merged;
49+
uint32_t current_n_documents;
4750
} delete;
4851
struct {
4952
uint8_t ordered : 1;
@@ -55,10 +58,9 @@ typedef struct
5558
} insert;
5659
struct {
5760
uint8_t ordered : 1;
58-
uint8_t upsert : 1;
59-
uint8_t multi : 1;
60-
bson_t *selector;
61-
bson_t *update;
61+
bson_t *updates;
62+
uint32_t n_updates;
63+
uint32_t current_n_updates;
6264
} update;
6365
} u;
6466
} mongoc_write_command_t;
@@ -90,7 +92,7 @@ void _mongoc_write_command_init_insert (mongoc_write_command_t *command,
9092
bool ordered,
9193
bool allow_bulk_op_insert);
9294
void _mongoc_write_command_init_delete (mongoc_write_command_t *command,
93-
const bson_t *selector,
95+
const bson_t *selectors,
9496
bool multi,
9597
bool ordered);
9698
void _mongoc_write_command_init_update (mongoc_write_command_t *command,
@@ -102,6 +104,16 @@ void _mongoc_write_command_init_update (mongoc_write_command_t *command,
102104
void _mongoc_write_command_insert_append (mongoc_write_command_t *command,
103105
const bson_t * const *documents,
104106
uint32_t n_documents);
107+
108+
void _mongoc_write_command_update_append (mongoc_write_command_t *command,
109+
const bson_t *selector,
110+
const bson_t *update,
111+
bool upsert,
112+
bool multi);
113+
114+
void _mongoc_write_command_delete_append (mongoc_write_command_t *command,
115+
const bson_t *selector);
116+
105117
void _mongoc_write_command_execute (mongoc_write_command_t *command,
106118
mongoc_client_t *client,
107119
uint32_t hint,

0 commit comments

Comments
 (0)