-
Notifications
You must be signed in to change notification settings - Fork 455
Add test_example_59 #1143
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
Add test_example_59 #1143
Changes from all commits
21e7653
d038c92
12635e6
103bcda
cacada0
4df0e3c
af7d801
d697998
b73bfe3
433376a
8edc763
3fc0e8a
8138aba
352afa4
2985bb3
5f513ac
14b86b0
31d615a
57d9a89
4657565
ffb07d0
5709929
3c62a01
c0479ab
036e287
1069a12
a467c55
22890b2
479e472
a5e5a5e
4b242db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2474,6 +2474,210 @@ test_example_56 (mongoc_database_t *db) | |
} | ||
|
||
|
||
/* clang-format on */ | ||
|
||
static bool | ||
insert_pet (mongoc_collection_t *collection, bool is_adoptable) | ||
{ | ||
bson_t *doc = NULL; | ||
bson_error_t error; | ||
bool rc; | ||
|
||
doc = BCON_NEW ("adoptable", BCON_BOOL (is_adoptable)); | ||
|
||
rc = mongoc_collection_insert_one (collection, doc, NULL, NULL, &error); | ||
if (!rc) { | ||
MONGOC_ERROR ("insert into pets.%s failed: %s", | ||
mongoc_collection_get_name (collection), | ||
error.message); | ||
goto cleanup; | ||
} | ||
|
||
cleanup: | ||
bson_destroy (doc); | ||
return rc; | ||
} | ||
|
||
|
||
static bool | ||
pet_setup (mongoc_collection_t *cats_collection, | ||
mongoc_collection_t *dogs_collection) | ||
{ | ||
bool ok = true; | ||
|
||
mongoc_collection_drop (cats_collection, NULL); | ||
mongoc_collection_drop (dogs_collection, NULL); | ||
|
||
ok = insert_pet (cats_collection, true); | ||
if (!ok) { | ||
goto done; | ||
} | ||
|
||
ok = insert_pet (dogs_collection, true); | ||
if (!ok) { | ||
goto done; | ||
} | ||
|
||
ok = insert_pet (dogs_collection, false); | ||
if (!ok) { | ||
goto done; | ||
} | ||
done: | ||
return ok; | ||
} | ||
|
||
|
||
/* | ||
* Increment 'accumulator' by the amount of adoptable pets in the given | ||
* collection. | ||
*/ | ||
static bool | ||
accumulate_adoptable_count (const mongoc_client_session_t *cs, | ||
mongoc_collection_t *collection, | ||
int64_t *accumulator /* OUT */ | ||
) | ||
{ | ||
bson_t *pipeline = NULL; | ||
mongoc_cursor_t *cursor = NULL; | ||
bool rc = false; | ||
const bson_t *doc = NULL; | ||
bson_error_t error; | ||
bson_iter_t iter; | ||
bson_t opts = BSON_INITIALIZER; | ||
|
||
rc = mongoc_client_session_append (cs, &opts, &error); | ||
if (!rc) { | ||
MONGOC_ERROR ("could not apply session options: %s", error.message); | ||
goto cleanup; | ||
} | ||
|
||
pipeline = BCON_NEW ("pipeline", | ||
"[", | ||
"{", | ||
"$match", | ||
"{", | ||
"adoptable", | ||
BCON_BOOL (true), | ||
"}", | ||
"}", | ||
"{", | ||
"$count", | ||
BCON_UTF8 ("adoptableCount"), | ||
"}", | ||
"]"); | ||
|
||
cursor = mongoc_collection_aggregate ( | ||
collection, MONGOC_QUERY_NONE, pipeline, &opts, NULL); | ||
bson_destroy (&opts); | ||
|
||
rc = mongoc_cursor_next (cursor, &doc); | ||
|
||
if (mongoc_cursor_error (cursor, &error)) { | ||
MONGOC_ERROR ("could not get adoptableCount: %s", error.message); | ||
rc = false; | ||
goto cleanup; | ||
} | ||
|
||
if (!rc) { | ||
MONGOC_ERROR ("%s", "cursor has no results"); | ||
goto cleanup; | ||
} | ||
|
||
rc = bson_iter_init_find (&iter, doc, "adoptableCount"); | ||
if (rc) { | ||
*accumulator += bson_iter_as_int64 (&iter); | ||
} else { | ||
MONGOC_ERROR ("%s", "missing key: 'adoptableCount'"); | ||
goto cleanup; | ||
} | ||
|
||
cleanup: | ||
bson_destroy (pipeline); | ||
mongoc_cursor_destroy (cursor); | ||
return rc; | ||
} | ||
|
||
/* | ||
* JIRA: https://jira.mongodb.org/browse/DRIVERS-2181 | ||
*/ | ||
static void | ||
test_example_59 (mongoc_database_t *db) | ||
{ | ||
/* Start Example 59 */ | ||
mongoc_client_t *client = NULL; | ||
mongoc_client_session_t *cs = NULL; | ||
mongoc_collection_t *cats_collection = NULL; | ||
mongoc_collection_t *dogs_collection = NULL; | ||
int64_t adoptable_pets_count = 0; | ||
bson_error_t error; | ||
mongoc_session_opt_t *session_opts; | ||
|
||
client = test_framework_new_default_client (); | ||
|
||
cats_collection = mongoc_client_get_collection (client, "pets", "cats"); | ||
dogs_collection = mongoc_client_get_collection (client, "pets", "dogs"); | ||
|
||
/* Seed 'pets.cats' and 'pets.dogs' with example data */ | ||
if (!pet_setup (cats_collection, dogs_collection)) { | ||
goto cleanup; | ||
} | ||
|
||
/* start a snapshot session */ | ||
session_opts = mongoc_session_opts_new (); | ||
mongoc_session_opts_set_snapshot (session_opts, true); | ||
cs = mongoc_client_start_session (client, session_opts, &error); | ||
mongoc_session_opts_destroy (session_opts); | ||
if (!cs) { | ||
MONGOC_ERROR ("Could not start session: %s", error.message); | ||
goto cleanup; | ||
} | ||
|
||
/* | ||
* Perform the following aggregation pipeline, and accumulate the count in | ||
* `adoptable_pets_count`. | ||
* | ||
* adoptablePetsCount = db.cats.aggregate( | ||
* [ { "$match": { "adoptable": true } }, | ||
* { "$count": "adoptableCatsCount" } ], session=s | ||
* ).next()["adoptableCatsCount"] | ||
* | ||
* adoptablePetsCount += db.dogs.aggregate( | ||
* [ { "$match": { "adoptable": True} }, | ||
* { "$count": "adoptableDogsCount" } ], session=s | ||
* ).next()["adoptableDogsCount"] | ||
* | ||
* Remember in order to apply the client session to | ||
* this operation, you must append the client session to the options passed | ||
* to `mongoc_collection_aggregate`, i.e., | ||
* | ||
* mongoc_client_session_append (cs, &opts, &error); | ||
* cursor = mongoc_collection_aggregate ( | ||
* collection, MONGOC_QUERY_NONE, pipeline, &opts, NULL); | ||
*/ | ||
accumulate_adoptable_count (cs, cats_collection, &adoptable_pets_count); | ||
accumulate_adoptable_count (cs, dogs_collection, &adoptable_pets_count); | ||
|
||
printf ("there are %" PRId64 " adoptable pets\n", adoptable_pets_count); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be helpful to add a comment here describing that we are using an aggregation pipeline to return the count, since other drivers have been displaying the aggregation pipeline command used. The comment could also be describing how the aggregation pipeline command is omitted. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm glad you brought this up, because that's something I'm curious about. It would be a fair amount of duplicate code and a bit lengthy to inline both calls to
I'm leaning towards 1., but I'm not sure how that would look when rendered. I'd like to know what everyone thinks about this, since I'm not sure which way would be the cleanest for the purpose of producing an example. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P.S. I'm planning on reaching out to Jeffrey Allen, the reporter for the original ticket, regarding this before merging. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be the most helpful to include the entire function, but since that is many more lines of code than the Python example, we could do option 3 and just describe the aggregation pipeline run. There are other pages (like this one in the C documentation that walks through writing an aggregation pipeline, so it might not be necessary to show every single step in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. One detail that would be good to include is that the client session must be appended to the options passed to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I got some feed back from Jeffery Allen. It sounds like he's in favor of option 3. (quote from Jeff)
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree option 3 would be the clearest, but I'm also not sure about how strict the formatting constraints are. Maybe the docs team could help make the choice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just saw the addition of the comment! Ignore my comment above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's interesting. I do like when an example is explicit, since this example includes 2 functions, then I suppose no reason why I can't either. I'll make that change for now, and request another review from both you and Kevin to see what you think. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Defer to Jeff's judgement since this example is requested by the documentation team. |
||
/* End Example 59 */ | ||
|
||
if (adoptable_pets_count != 2) { | ||
MONGOC_ERROR ( | ||
"there should be exatly 2 adoptable_pets_count, found: %" PRId64, | ||
adoptable_pets_count); | ||
} | ||
|
||
/* Start Example 59 Post */ | ||
cleanup: | ||
mongoc_collection_destroy (dogs_collection); | ||
mongoc_collection_destroy (cats_collection); | ||
mongoc_client_session_destroy (cs); | ||
mongoc_client_destroy (client); | ||
/* End Example 59 Post */ | ||
} | ||
galon1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/* clang-format off */ | ||
|
||
typedef struct { | ||
bson_mutex_t lock; | ||
mongoc_collection_t *collection; | ||
|
@@ -4000,6 +4204,7 @@ test_sample_commands (void) | |
test_sample_command (test_example_57, 57, db, collection, false); | ||
test_sample_command (test_example_58, 58, db, collection, false); | ||
test_sample_command (test_example_56, 56, db, collection, true); | ||
test_sample_command (test_example_59, 59, db, collection, true); | ||
test_sample_change_stream_command (test_example_change_stream, db); | ||
test_sample_causal_consistency (client); | ||
test_sample_aggregation (db); | ||
|
Uh oh!
There was an error while loading. Please reload this page.