Skip to content

CDRIVER-5816: mongoc_bulkwrite_new and mongoc_bulkwrite_set_client #1805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/libmongoc/doc/mongoc_bulkwrite_new.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
:man_page: mongoc_bulkwrite_new

mongoc_bulkwrite_new()
======================

Synopsis
--------

.. code-block:: c

mongoc_bulkwrite_t *
mongoc_bulkwrite_new (void);

Description
-----------

Returns a new :symbol:`mongoc_bulkwrite_t`. Free with :symbol:`mongoc_bulkwrite_destroy()`.

A client must be assigned with :symbol:`mongoc_bulkwrite_set_client()` prior to execution.
17 changes: 17 additions & 0 deletions src/libmongoc/doc/mongoc_bulkwrite_set_client.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
:man_page: mongoc_bulkwrite_set_client

mongoc_bulkwrite_set_client()
=============================

Synopsis
--------

.. code-block:: c

void
mongoc_bulkwrite_set_client (mongoc_bulkwrite_t *self, mongoc_client_t *client);

Description
-----------

Sets the client that will be used to execute the :symbol:`mongoc_bulkwrite_t`.
2 changes: 2 additions & 0 deletions src/libmongoc/doc/mongoc_bulkwrite_t.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ to execute the operation.
mongoc_bulkwrite_deletemanyopts_t
mongoc_bulkwrite_append_deletemany
mongoc_bulkwritereturn_t
mongoc_bulkwrite_new
mongoc_bulkwrite_set_client
mongoc_bulkwrite_set_session
mongoc_bulkwrite_execute
mongoc_bulkwrite_destroy
44 changes: 42 additions & 2 deletions src/libmongoc/src/mongoc/mongoc-bulkwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,18 @@ mongoc_bulkwrite_t *
mongoc_client_bulkwrite_new (mongoc_client_t *self)
{
BSON_ASSERT_PARAM (self);
mongoc_bulkwrite_t *bw = bson_malloc0 (sizeof (mongoc_bulkwrite_t));
mongoc_bulkwrite_t *bw = mongoc_bulkwrite_new ();
bw->client = self;
bw->operation_id = ++self->cluster.operation_id;
return bw;
}

mongoc_bulkwrite_t *
mongoc_bulkwrite_new (void)
{
mongoc_bulkwrite_t *bw = bson_malloc0 (sizeof (mongoc_bulkwrite_t));
_mongoc_buffer_init (&bw->ops, NULL, 0, NULL, NULL);
_mongoc_array_init (&bw->arrayof_modeldata, sizeof (modeldata_t));
bw->operation_id = ++self->cluster.operation_id;
return bw;
}

Expand Down Expand Up @@ -1462,12 +1469,36 @@ _bulkwritereturn_apply_result (mongoc_bulkwritereturn_t *self,
return true;
}

void
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just removed the MONGOC_EXPORT here per #1806 (comment).

mongoc_bulkwrite_set_client (mongoc_bulkwrite_t *self, mongoc_client_t *client)
{
BSON_ASSERT_PARAM (self);
BSON_ASSERT_PARAM (client);

if (self->session) {
BSON_ASSERT (self->session->client == client);
}

/* NOP if the client is not changing; otherwise, assign it and increment and
* fetch its operation_id. */
if (self->client == client) {
return;
}

self->client = client;
self->operation_id = ++client->cluster.operation_id;
}

void
mongoc_bulkwrite_set_session (mongoc_bulkwrite_t *self, mongoc_client_session_t *session)
{
BSON_ASSERT_PARAM (self);
BSON_OPTIONAL_PARAM (session);

if (self->client && session) {
BSON_ASSERT (self->client == session->client);
}

self->session = session;
}

Expand Down Expand Up @@ -1495,6 +1526,15 @@ mongoc_bulkwrite_execute (mongoc_bulkwrite_t *self, const mongoc_bulkwriteopts_t
ret.res = _bulkwriteresult_new ();
ret.exc = _bulkwriteexception_new ();

if (!self->client) {
bson_set_error (&error,
MONGOC_ERROR_COMMAND,
MONGOC_ERROR_COMMAND_INVALID_ARG,
"bulk write requires a client and one has not been set");
_bulkwriteexception_set_error (ret.exc, &error);
goto fail;
}

if (self->executed) {
bson_set_error (&error, MONGOC_ERROR_COMMAND, MONGOC_ERROR_COMMAND_INVALID_ARG, "bulk write already executed");
_bulkwriteexception_set_error (ret.exc, &error);
Expand Down
8 changes: 8 additions & 0 deletions src/libmongoc/src/mongoc/mongoc-bulkwrite.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ mongoc_bulkwriteexception_destroy (mongoc_bulkwriteexception_t *self);
typedef struct _mongoc_bulkwrite_t mongoc_bulkwrite_t;
MONGOC_EXPORT (mongoc_bulkwrite_t *)
mongoc_client_bulkwrite_new (mongoc_client_t *self);

typedef struct _mongoc_bulkwrite_insertoneopts_t mongoc_bulkwrite_insertoneopts_t;
MONGOC_EXPORT (mongoc_bulkwrite_insertoneopts_t *)
mongoc_bulkwrite_insertoneopts_new (void);
Expand Down Expand Up @@ -243,6 +244,13 @@ typedef struct {
mongoc_bulkwriteexception_t *exc; // NULL if no error.
} mongoc_bulkwritereturn_t;

// `mongoc_bulkwrite_new` and `mongoc_bulkwrite_set_client` may be used by
// language bindings that want to assemble a `mongoc_bulkwrite_t` and defer
// `mongoc_client_t` assignment to execution time.
MONGOC_EXPORT (mongoc_bulkwrite_t *)
mongoc_bulkwrite_new (void);
MONGOC_EXPORT (void)
mongoc_bulkwrite_set_client (mongoc_bulkwrite_t *self, mongoc_client_t *client);
// `mongoc_bulkwrite_set_session` sets an optional explicit session.
// `*session` may be modified when `mongoc_bulkwrite_execute` is called.
MONGOC_EXPORT (void)
Expand Down
45 changes: 45 additions & 0 deletions src/libmongoc/tests/test-mongoc-bulkwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,43 @@ test_bulkwrite_many_namespaces (void *ctx)
mongoc_client_destroy (client);
}

static void
test_bulkwrite_execute_requires_client (void *ctx)
{
BSON_UNUSED (ctx);
bool ok;
bson_error_t error;

mongoc_client_t *client = test_framework_new_default_client ();
mongoc_bulkwrite_t *bw = mongoc_bulkwrite_new ();
ok = mongoc_bulkwrite_append_insertone (bw, "db.coll", tmp_bson ("{}"), NULL, &error);
ASSERT_OR_PRINT (ok, error);

// Attempt execution without assigning a client
{
mongoc_bulkwritereturn_t bwr = mongoc_bulkwrite_execute (bw, NULL);
ASSERT (bwr.exc);
ASSERT (mongoc_bulkwriteexception_error (bwr.exc, &error));
ASSERT_ERROR_CONTAINS (error,
MONGOC_ERROR_COMMAND,
MONGOC_ERROR_COMMAND_INVALID_ARG,
"bulk write requires a client and one has not been set");
mongoc_bulkwriteexception_destroy (bwr.exc);
mongoc_bulkwriteresult_destroy (bwr.res);
}

// Assign a client and execute successfully
{
mongoc_bulkwrite_set_client (bw, client);
mongoc_bulkwritereturn_t bwr = mongoc_bulkwrite_execute (bw, NULL);
ASSERT_NO_BULKWRITEEXCEPTION (bwr);
mongoc_bulkwriteresult_destroy (bwr.res);
mongoc_bulkwriteexception_destroy (bwr.exc);
}

mongoc_bulkwrite_destroy (bw);
mongoc_client_destroy (client);
}

void
test_bulkwrite_install (TestSuite *suite)
Expand Down Expand Up @@ -677,4 +714,12 @@ test_bulkwrite_install (TestSuite *suite)
test_framework_skip_if_max_wire_version_less_than_25, // require server 8.0
test_framework_skip_if_mongos // Creating 100k collections is very slow (~5 minutes) on mongos.
);

TestSuite_AddFull (suite,
"/bulkwrite/execute_requires_client",
test_bulkwrite_execute_requires_client,
NULL /* dtor */,
NULL /* ctx */,
test_framework_skip_if_max_wire_version_less_than_25 // require server 8.0
);
}