Skip to content

add stub API for client bulk write #1574

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

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 3 additions & 0 deletions src/libmongoc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ set (SOURCES ${SOURCES}
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-stream-tls-openssl-bio.c
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-openssl.c
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-ocsp-cache.c
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-bulkwrite.c
)

set (HEADERS
Expand All @@ -665,6 +666,7 @@ set (HEADERS
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc.h
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-apm.h
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-bulk-operation.h
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-bulkwrite.h
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-change-stream.h
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-client.h
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-client-pool.h
Expand Down Expand Up @@ -1216,6 +1218,7 @@ if (ENABLE_EXAMPLES AND ENABLE_SHARED)
mongoc_add_example (mongoc-ping ${PROJECT_SOURCE_DIR}/examples/mongoc-ping.c)
mongoc_add_example (mongoc-tail ${PROJECT_SOURCE_DIR}/examples/mongoc-tail.c)
mongoc_add_example (example-collection-command ${PROJECT_SOURCE_DIR}/examples/example-collection-command.c)
mongoc_add_example (example-bulkwrite ${PROJECT_SOURCE_DIR}/examples/example-bulkwrite.c)

# examples/aggregation/
mongoc_add_example (aggregation1 ${PROJECT_SOURCE_DIR}/examples/aggregation/aggregation1.c)
Expand Down
79 changes: 79 additions & 0 deletions src/libmongoc/examples/example-bulkwrite.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// example-bulkwrite shows use of `mongoc_client_bulkwrite`.

#include <mongoc/mongoc.h>

#define HANDLE_ERROR(...) \
if (1) { \
fprintf (stderr, __VA_ARGS__); \
fprintf (stderr, "\n"); \
goto fail; \
} else \
(void) 0

int
main (int argc, char *argv[])
{
bool ok = false;

mongoc_init ();

bson_error_t error;
mongoc_client_t *client = mongoc_client_new ("mongodb://localhost:27017");
mongoc_bulkwriteoptions_t *bwo = mongoc_bulkwriteoptions_new ();
mongoc_bulkwriteoptions_set_verboseresults (bwo, true);
mongoc_bulkwrite_t *bw = mongoc_client_bulkwrite_new (client, bwo);

// Insert a document to `db.coll1`
{
bson_t *doc = BCON_NEW ("foo", "1");
if (!mongoc_client_bulkwrite_append_insertone (bw, "db.coll1", -1, doc, NULL, &error)) {
HANDLE_ERROR ("error appending insert one: %s", error.message);
}
bson_destroy (doc);
}
// Insert a document to `db.coll2`
{
bson_t *doc = BCON_NEW ("foo", "2");
if (!mongoc_client_bulkwrite_append_insertone (bw, "db.coll2", -1, doc, NULL, &error)) {
HANDLE_ERROR ("error appending insert one: %s", error.message);
}
bson_destroy (doc);
}

mongoc_bulkwritereturn_t bwr = mongoc_bulkwrite_execute (bw);

printf ("insert count: %" PRId64 "\n", mongoc_bulkwriteresult_insertedcount (bwr.res));

// Print verbose results.
{
const bson_t *vr = mongoc_bulkwriteresult_verboseresults (bwr.res);
BSON_ASSERT (vr);
char *vr_str = bson_as_relaxed_extended_json (vr, NULL);
printf ("verbose results: %s\n", vr_str);
bson_free (vr_str);
}

// Print exception.
if (bwr.exc) {
const bson_t *error_doc;
mongoc_bulkwriteexception_error (bwr.exc, &error, &error_doc);
if (mongoc_error_has_label (error_doc, "RetryableWriteError")) {
printf ("error has label: RetryableWriteError\n");
}
printf ("error: %s\n", error.message);
char *error_doc_str = bson_as_relaxed_extended_json (error_doc, NULL);
printf ("exception: %s\n", error_doc_str);
bson_free (error_doc_str);
}

mongoc_bulkwriteresult_destroy (bwr.res);
mongoc_bulkwriteexception_destroy (bwr.exc);
mongoc_bulkwrite_destroy (bw);

ok = true;
fail:
mongoc_client_destroy (client);
mongoc_bulkwriteoptions_destroy (bwo);
mongoc_cleanup ();
return ok ? EXIT_SUCCESS : EXIT_FAILURE;
}
Loading