Skip to content

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

Merged
merged 31 commits into from
Nov 16, 2022
Merged

Add test_example_59 #1143

merged 31 commits into from
Nov 16, 2022

Conversation

kkloberdanz
Copy link
Contributor

@kkloberdanz kkloberdanz commented Nov 9, 2022

CDRIVER-4295

related: DRIVERS-2181

Implement a snapshot query example for the manual.

@kkloberdanz kkloberdanz marked this pull request as ready for review November 11, 2022 14:29
Comment on lines 2580 to 2589
value = bson_iter_value (&iter);
if (value->value_type == BSON_TYPE_INT32) {
*accumulator += value->value.v_int32;
} else if (value->value_type == BSON_TYPE_INT32) {
*accumulator += value->value.v_int64;
} else {
MONGOC_ERROR ("%s", "'adoptableCount' must be an integer");
rc = false;
goto cleanup;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggest using bson_iter_as_int64 to handle multiple possible numeric types.

Suggested change
value = bson_iter_value (&iter);
if (value->value_type == BSON_TYPE_INT32) {
*accumulator += value->value.v_int32;
} else if (value->value_type == BSON_TYPE_INT32) {
*accumulator += value->value.v_int64;
} else {
MONGOC_ERROR ("%s", "'adoptableCount' must be an integer");
rc = false;
goto cleanup;
}
*accumulator += bson_iter_as_int64 (&iter);

@kevinAlbs
Copy link
Collaborator

Looking good. Include CDRIVER-4295 in the commit message before merging. That will result in a comment with the commit being added to the Jira ticket.

Copy link
Contributor

@galon1 galon1 left a comment

Choose a reason for hiding this comment

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

LGTM! I left one comment about adding more explanation in one part of the example, but I don't think it should block merging if the added explanation is unnecessary.


accumulate_adoptable_count (cats_collection, &adoptable_pets_count);
accumulate_adoptable_count (dogs_collection, &adoptable_pets_count);

Copy link
Contributor

@galon1 galon1 Nov 14, 2022

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 accumulate_adoptable_count in this function, but I'm wondering if I should do one of the following:

  1. Include the entire function accumulate_adoptable_count in the example, i.e., place the start example comment just above the declaration of accumulate_adoptable_count, and end example comment stays where it is within test_example_59.
  2. Place the text of accumulate_adoptable_count in a comment in test_example_59.
  3. Explain in a comment what is happening in the function accumulate_adoptable_count purely in English, and omit code in the comment.
  4. Inline all the code from accumulate_adoptable_count within test_example_59.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 accumulate_adoptable_count .

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 mongoc_collection_aggregate. If we go with option 3., then I suppose that could be explained also in a comment, but it seems like showing the code for it would be the best.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)

Hey Kyle, thanks for reaching out. So my recollection with that ticket is that the main goal is to emphasize the snapshot functionality in the example. Therefore, I'm inclined to say that we can exclude the agg pipeline from the example, and leave a comment explaining the aggregation. That way, users aren't too overwhelmed by a large example and they'll still be able to see the snapshot behavior in action

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

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

I just saw the addition of the comment! Ignore my comment above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Comment on lines 2635 to 2658
/*
* 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);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Jeff has checked off on using this comment as a part of the documentation.

@kkloberdanz
Copy link
Contributor Author

FYI, Gil brought up that another C driver example is lengthy and has multiple functions, so it seems that if I were to include multiple functions as a part of the example, that would fit with how things are currently shown. I pushed that in order for us to compare and decide which way we would prefer.

For reference, the example that Gil found: https://www.mongodb.com/docs/v6.0/core/transactions/

Copy link
Contributor

@galon1 galon1 left a comment

Choose a reason for hiding this comment

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

I think the example looks good! I just have one question about using python in this example.

Copy link
Collaborator

@kevinAlbs kevinAlbs left a comment

Choose a reason for hiding this comment

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

LGTM with small fixes.


accumulate_adoptable_count (cats_collection, &adoptable_pets_count);
accumulate_adoptable_count (dogs_collection, &adoptable_pets_count);

Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

@kkloberdanz kkloberdanz merged commit 42296db into mongodb:master Nov 16, 2022
@kkloberdanz kkloberdanz deleted the CDRIVER-4295 branch November 16, 2022 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants