-
Notifications
You must be signed in to change notification settings - Fork 549
CXX-3082 Add comprehensive examples (mongocxx, Part 2) #1236
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
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
70d8e85
doxygen: apply consistent @see list syntax
eramongodb 2d4d3d4
db_lock: use write concern majority when dropping locked databases
eramongodb 6d961c3
db_lock: avoid server error due to lengthy API example component name
eramongodb 23c573e
runner: add support for disabling forking even when available
eramongodb 7c04555
runner: use bsoncxx::from_json instead of the basic builder
eramongodb aacdf63
examples: avoid mongocryptd conflicting with sharded clusters
eramongodb e577762
examples: add client sessions
eramongodb 370a321
examples: add index views
eramongodb 66697ec
examples: add change streams
eramongodb 9ec151e
examples: database error handling
eramongodb 2218aee
examples: add collection error handling
eramongodb 7a037b7
CXX-3031 add 8.0 test tasks (#1234)
kevinAlbs 8076d03
CXX-2995 bump minimum required C Driver version to d934cd5d (#1235)
eramongodb c6ef3c0
Merge remote-tracking branch 'upstream/master' into cxx-3082
eramongodb d8b68b8
Remove redundant include directives
eramongodb e4da4c7
Use unique db name for change stream example
eramongodb 17f3efa
Remove use of client session in change streams
eramongodb 7825986
Continue looping until all events are observed (or timeout)
eramongodb aea4907
Refactor CLI argument parsing
eramongodb e00669f
Add API examples runner support for filtering by component name
eramongodb d7bbc79
Fix typo in error message for --verbose
eramongodb 33eb869
Remove stray comment from prior changes
eramongodb 9c568fa
Fix argument to parse_use_fork
eramongodb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Obtain a Change Stream | ||
|
|
||
| ## From a Client | ||
|
|
||
| @snippet api/mongocxx/examples/change_streams/from_client.cpp Example | ||
|
|
||
| ## From a Database | ||
|
|
||
| @snippet api/mongocxx/examples/change_streams/from_database.cpp Example | ||
|
|
||
| ## From a Collection | ||
|
|
||
| @snippet api/mongocxx/examples/change_streams/from_collection.cpp Example | ||
|
|
||
| # Use a Change Stream | ||
|
|
||
| ## Basic Usage | ||
|
|
||
| @snippet api/mongocxx/examples/change_streams/basic.cpp Example | ||
|
|
||
| ## With Pipeline | ||
|
|
||
| @snippet api/mongocxx/examples/change_streams/with_pipeline.cpp Example |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # Create a Client Session | ||
|
|
||
| ## Basic Usage | ||
|
|
||
| @snippet api/mongocxx/examples/client_sessions/create/basic.cpp Example | ||
|
|
||
| ## With Options | ||
|
|
||
| @snippet api/mongocxx/examples/client_sessions/create/with_options.cpp Example | ||
|
|
||
| # Use a Client Session | ||
|
|
||
| @see | ||
| - [Causal Consistency and Read and Write Concerns (MongoDB Manual)](https://www.mongodb.com/docs/manual/core/causal-consistency-read-write-concerns/) | ||
|
|
||
| ## Basic Usage | ||
|
|
||
| @snippet api/mongocxx/examples/client_sessions/use/basic.cpp Example | ||
|
|
||
| ## With Transactions | ||
|
|
||
| @snippet api/mongocxx/examples/client_sessions/use/transactions.cpp Example |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Copyright 2009-present MongoDB, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <chrono> | ||
|
|
||
| #include <bsoncxx/json.hpp> | ||
|
|
||
| #include <mongocxx/client.hpp> | ||
| #include <mongocxx/collection.hpp> | ||
| #include <mongocxx/uri.hpp> | ||
|
|
||
| #include <examples/api/concern.hh> | ||
| #include <examples/api/db_lock.hh> | ||
| #include <examples/api/runner.hh> | ||
| #include <examples/macros.hh> | ||
|
|
||
| namespace { | ||
|
|
||
| // [Example] | ||
| void example(mongocxx::collection coll) { | ||
| mongocxx::change_stream stream = coll.watch(); | ||
|
|
||
| auto result_opt = coll.insert_one(bsoncxx::from_json(R"({"x": 1})")); | ||
| EXPECT(result_opt); | ||
| auto id = result_opt->inserted_id(); | ||
|
|
||
| int count = 0; | ||
| auto now = [] { return std::chrono::steady_clock::now(); }; | ||
| auto start = now(); | ||
|
|
||
| // periodicNoopIntervalSecs: 10 (default) | ||
| while (count == 0 && now() - start < std::chrono::seconds(10)) { | ||
| for (bsoncxx::document::view change : stream) { | ||
| ++count; | ||
|
|
||
| EXPECT(change["operationType"]); | ||
| EXPECT(change["operationType"].get_string().value.compare("insert") == 0); | ||
|
|
||
| EXPECT(change["ns"]); | ||
| EXPECT(change["ns"]["db"].get_string().value.compare("db") == 0); | ||
| EXPECT(change["ns"]["coll"].get_string().value.compare("coll") == 0); | ||
|
|
||
| EXPECT(change["fullDocument"]); | ||
| EXPECT(change["fullDocument"]["x"]); | ||
|
|
||
| EXPECT(change["documentKey"]); | ||
| EXPECT(change["documentKey"]["_id"].get_oid().value == id); | ||
| } | ||
| } | ||
|
|
||
| EXPECT(count == 1); | ||
| } | ||
| // [Example] | ||
|
|
||
| } // namespace | ||
|
|
||
| RUNNER_REGISTER_COMPONENT_FOR_REPLICA() { | ||
| mongocxx::client client{mongocxx::uri{}}; | ||
|
|
||
| { | ||
| db_lock guard{client, "db"}; | ||
|
|
||
| example(set_rw_concern_majority(guard.get()).create_collection("coll")); | ||
| } | ||
| } | ||
66 changes: 66 additions & 0 deletions
66
examples/api/mongocxx/examples/change_streams/from_client.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Copyright 2009-present MongoDB, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <bsoncxx/json.hpp> | ||
|
|
||
| #include <mongocxx/change_stream.hpp> | ||
| #include <mongocxx/client.hpp> | ||
| #include <mongocxx/options/change_stream.hpp> | ||
| #include <mongocxx/uri.hpp> | ||
|
|
||
| #include <examples/api/runner.hh> | ||
| #include <examples/macros.hh> | ||
|
|
||
| namespace { | ||
|
|
||
| // [Example] | ||
| void example(mongocxx::client client) { | ||
| // Basic usage. | ||
| { | ||
| mongocxx::change_stream stream = client.watch(); | ||
|
|
||
| EXPECT(stream.get_resume_token()); | ||
| } | ||
|
|
||
| // With options. | ||
| { | ||
| mongocxx::options::change_stream opts; | ||
|
|
||
| opts.batch_size(1); | ||
| // ... other change stream options. | ||
|
|
||
| mongocxx::change_stream stream = client.watch(opts); | ||
|
|
||
| EXPECT(stream.get_resume_token()); | ||
| } | ||
|
|
||
| // With a pipeline. | ||
| { | ||
| mongocxx::pipeline pipeline; | ||
|
|
||
| pipeline.match(bsoncxx::from_json(R"({"operationType": "insert"})")); | ||
| // ... other pipeline options. | ||
|
|
||
| mongocxx::change_stream stream = client.watch(pipeline); | ||
|
|
||
| EXPECT(stream.get_resume_token()); | ||
| } | ||
| } | ||
| // [Example] | ||
|
|
||
| } // namespace | ||
|
|
||
| RUNNER_REGISTER_COMPONENT_FOR_REPLICA() { | ||
| example(mongocxx::client{mongocxx::uri{}}); | ||
| } |
73 changes: 73 additions & 0 deletions
73
examples/api/mongocxx/examples/change_streams/from_collection.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // Copyright 2009-present MongoDB, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <bsoncxx/json.hpp> | ||
|
|
||
| #include <mongocxx/change_stream.hpp> | ||
| #include <mongocxx/client.hpp> | ||
| #include <mongocxx/options/change_stream.hpp> | ||
| #include <mongocxx/uri.hpp> | ||
|
|
||
| #include <examples/api/db_lock.hh> | ||
| #include <examples/api/runner.hh> | ||
| #include <examples/macros.hh> | ||
|
|
||
| namespace { | ||
|
|
||
| // [Example] | ||
| void example(mongocxx::collection coll) { | ||
| // Basic usage. | ||
| { | ||
| mongocxx::change_stream stream = coll.watch(); | ||
|
|
||
| EXPECT(stream.get_resume_token()); | ||
| } | ||
|
|
||
| // With options. | ||
| { | ||
| mongocxx::options::change_stream opts; | ||
|
|
||
| opts.batch_size(1); | ||
| // ... other change stream options. | ||
|
|
||
| mongocxx::change_stream stream = coll.watch(opts); | ||
|
|
||
| EXPECT(stream.get_resume_token()); | ||
| } | ||
|
|
||
| // With a pipeline. | ||
| { | ||
| mongocxx::pipeline pipeline; | ||
|
|
||
| pipeline.match(bsoncxx::from_json(R"({"operationType": "insert"})")); | ||
| // ... other pipeline options. | ||
|
|
||
| mongocxx::change_stream stream = coll.watch(pipeline); | ||
|
|
||
| EXPECT(stream.get_resume_token()); | ||
| } | ||
| } | ||
| // [Example] | ||
|
|
||
| } // namespace | ||
|
|
||
| RUNNER_REGISTER_COMPONENT_FOR_REPLICA() { | ||
| mongocxx::client client{mongocxx::uri{}}; | ||
|
|
||
| { | ||
| db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; | ||
|
|
||
| example(guard.get()["coll"]); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.