Skip to content

Commit 038f863

Browse files
author
Christian Hergert
committed
write-commands: coalesce repeated calls for bulk insert.
1 parent 8ecf633 commit 038f863

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

src/mongoc/mongoc-bulk-operation.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,28 @@ mongoc_bulk_operation_insert (mongoc_bulk_operation_t *bulk,
151151
const bson_t *document)
152152
{
153153
mongoc_write_command_t command = { 0 };
154+
mongoc_write_command_t *last;
155+
156+
ENTRY;
154157

155158
bson_return_if_fail (bulk);
156159
bson_return_if_fail (document);
157160

161+
if (bulk->commands.len) {
162+
last = &_mongoc_array_index (&bulk->commands,
163+
mongoc_write_command_t,
164+
bulk->commands.len - 1);
165+
if (last->type == MONGOC_WRITE_COMMAND_INSERT) {
166+
_mongoc_write_command_insert_append (last, &document, 1);
167+
EXIT;
168+
}
169+
}
170+
158171
_mongoc_write_command_init_insert (&command, &document, 1, bulk->ordered,
159172
false);
160173
_mongoc_array_append_val (&bulk->commands, command);
174+
175+
EXIT;
161176
}
162177

163178

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ void _mongoc_write_command_init_update (mongoc_write_command_t *command,
9393
bool upsert,
9494
bool multi,
9595
bool ordered);
96+
void _mongoc_write_command_insert_append (mongoc_write_command_t *command,
97+
const bson_t * const *documents,
98+
uint32_t n_documents);
9699
void _mongoc_write_command_execute (mongoc_write_command_t *command,
97100
mongoc_client_t *client,
98101
uint32_t hint,

src/mongoc/mongoc-write-command.c

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,9 @@ static bson_t gEmptyWriteConcern = BSON_INITIALIZER;
5555

5656

5757
void
58-
_mongoc_write_command_init_insert
59-
(mongoc_write_command_t *command, /* IN */
60-
const bson_t * const *documents, /* IN */
61-
uint32_t n_documents, /* IN */
62-
bool ordered, /* IN */
63-
bool allow_bulk_op_insert) /* IN */
58+
_mongoc_write_command_insert_append (mongoc_write_command_t *command,
59+
const bson_t * const *documents,
60+
uint32_t n_documents)
6461
{
6562
const char *key;
6663
bson_iter_t iter;
@@ -72,12 +69,8 @@ _mongoc_write_command_init_insert
7269
ENTRY;
7370

7471
BSON_ASSERT (command);
75-
76-
command->type = MONGOC_WRITE_COMMAND_INSERT;
77-
command->u.insert.documents = bson_new ();
78-
command->u.insert.n_documents = n_documents;
79-
command->u.insert.ordered = ordered;
80-
command->u.insert.allow_bulk_op_insert = allow_bulk_op_insert;
72+
BSON_ASSERT (command->type == MONGOC_WRITE_COMMAND_INSERT);
73+
BSON_ASSERT (!n_documents || documents);
8174

8275
for (i = 0; i < n_documents; i++) {
8376
BSON_ASSERT (documents [i]);
@@ -108,6 +101,33 @@ _mongoc_write_command_init_insert
108101
}
109102

110103

104+
void
105+
_mongoc_write_command_init_insert
106+
(mongoc_write_command_t *command, /* IN */
107+
const bson_t * const *documents, /* IN */
108+
uint32_t n_documents, /* IN */
109+
bool ordered, /* IN */
110+
bool allow_bulk_op_insert) /* IN */
111+
{
112+
ENTRY;
113+
114+
BSON_ASSERT (command);
115+
BSON_ASSERT (!n_documents || documents);
116+
117+
command->type = MONGOC_WRITE_COMMAND_INSERT;
118+
command->u.insert.documents = bson_new ();
119+
command->u.insert.n_documents = n_documents;
120+
command->u.insert.ordered = ordered;
121+
command->u.insert.allow_bulk_op_insert = allow_bulk_op_insert;
122+
123+
if (n_documents) {
124+
_mongoc_write_command_insert_append (command, documents, n_documents);
125+
}
126+
127+
EXIT;
128+
}
129+
130+
111131
void
112132
_mongoc_write_command_init_delete (mongoc_write_command_t *command, /* IN */
113133
const bson_t *selector, /* IN */

0 commit comments

Comments
 (0)