Skip to content

Commit d7780dc

Browse files
authored
CXX-3082 Add comprehensive examples (mongocxx) (#1216)
* Add missing docs for mongocxx::v_noabi::uri::k_default_uri * Avoid CDRIVER-5732 * Add support for forking API examples * Add support for running components with an instance object * Add support for running components against a live server * Add diagnostics for a component thread terminated via uncaught exception * Add db locker to avoid read-write conflicts across examples * Add helper to set majority read/write concern for collections * Add EXAMPLES_COMPONENT_NAME_STR * Add mongocxx instance examples * Add mongocxx logger examples * Add mongocxx URI examples * Add mongocxx client examples * Add mongocxx database examples * Add mongocxx collection examples * Add mongocxx operation exception examples
1 parent bad155c commit d7780dc

File tree

90 files changed

+5538
-33
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+5538
-33
lines changed

.evergreen/test.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ else
297297
# API version to example clients, so examples will fail when requireApiVersion
298298
# is true.
299299
if [[ -z "${MONGODB_API_VERSION:-}" ]]; then
300+
# Avoid `[__NSCFConstantString initialize] may have been in progress in another thread when fork() was called.` errors on MacOS.
301+
export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
302+
300303
echo "Running examples..."
301304
if ! "${cmake_binary:?}" --build . --target run-examples --parallel 1 >|output.txt 2>&1; then
302305
# Only emit output on failure.

docs/api/mongocxx/examples/clients.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Create a Client
2+
3+
## Single
4+
5+
### Basic Usage
6+
7+
@snippet api/mongocxx/examples/clients/create/single/basic.cpp Example
8+
9+
### With a Custom URI
10+
11+
@snippet api/mongocxx/examples/clients/create/single/with_uri.cpp Example
12+
13+
### With Client Options
14+
15+
#### Stable API Options
16+
17+
@snippet api/mongocxx/examples/clients/create/single/options/stable_api.cpp Example
18+
19+
#### TLS Options
20+
21+
@snippet api/mongocxx/examples/clients/create/single/options/tls.cpp Example
22+
23+
#### Automatic Encryption Options
24+
25+
@snippet api/mongocxx/examples/clients/create/single/options/auto_encryption.cpp Example
26+
27+
#### APM Options
28+
29+
@snippet api/mongocxx/examples/clients/create/single/options/apm.cpp Example
30+
31+
## Pool
32+
33+
### Basic Usage
34+
35+
@snippet api/mongocxx/examples/clients/create/pool/basic.cpp Example
36+
37+
### With Client Options
38+
39+
@snippet api/mongocxx/examples/clients/create/pool/options.cpp Example
40+
41+
### Try Acquire
42+
43+
@snippet api/mongocxx/examples/clients/create/pool/try_acquire.cpp Example
44+
45+
# Use a Client
46+
47+
## List Databases
48+
49+
@snippet api/mongocxx/examples/clients/use/list_databases.cpp Example
50+
51+
## List Databases With Options
52+
53+
@snippet api/mongocxx/examples/clients/use/list_databases_with_options.cpp Example
54+
55+
## List Database Names
56+
57+
@snippet api/mongocxx/examples/clients/use/list_database_names.cpp Example
58+
59+
## List Database Names With Options
60+
61+
@snippet api/mongocxx/examples/clients/use/list_database_names_with_options.cpp Example
62+
63+
# Error Handling
64+
65+
## Invalid Client
66+
67+
@snippet api/mongocxx/examples/clients/errors/invalid_client.cpp Example
68+
69+
## Wait Queue Timeout
70+
71+
@snippet api/mongocxx/examples/clients/errors/wait_queue_timeout.cpp Example
72+
73+
## Invalid Stable API Options
74+
75+
@snippet api/mongocxx/examples/clients/errors/stable_api.cpp Example
76+
77+
## TLS Not Enabled
78+
79+
@snippet api/mongocxx/examples/clients/errors/tls.cpp Example
80+
81+
## Invalid Auto Encryption Options
82+
83+
@snippet api/mongocxx/examples/clients/errors/auto_encryption.cpp Example
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Obtain a Collection
2+
3+
@snippet examples/api/mongocxx/examples/collections/obtain.cpp Example
4+
5+
# Collection Operations
6+
7+
## Drop a Collection
8+
9+
@snippet examples/api/mongocxx/examples/collections/drop.cpp Example
10+
11+
## Rename a Collection
12+
13+
@snippet examples/api/mongocxx/examples/collections/rename.cpp Example
14+
15+
## Set a Read Concern
16+
17+
@snippet examples/api/mongocxx/examples/collections/rc.cpp Example
18+
19+
## Set a Write Concern
20+
21+
@snippet examples/api/mongocxx/examples/collections/wc.cpp Example
22+
23+
## Set a Read Preference
24+
25+
@snippet examples/api/mongocxx/examples/collections/rp.cpp Example
26+
27+
# Index Operations
28+
29+
## List Indexes
30+
31+
@snippet examples/api/mongocxx/examples/collections/list_indexes.cpp Example
32+
33+
## Create an Index
34+
35+
@snippet examples/api/mongocxx/examples/collections/create_index.cpp Example
36+
37+
## Create an Index With Options
38+
39+
@snippet examples/api/mongocxx/examples/collections/create_index_with_options.cpp Example
40+
41+
# Document Operations
42+
43+
## Query the Number of Documents
44+
45+
@snippet examples/api/mongocxx/examples/collections/count.cpp Example
46+
47+
## Estimate the Number of Documents
48+
49+
@snippet examples/api/mongocxx/examples/collections/estimate_count.cpp Example
50+
51+
## Find a Document
52+
53+
@snippet examples/api/mongocxx/examples/collections/find_one.cpp Example
54+
55+
## Find Multiple Documents
56+
57+
@snippet examples/api/mongocxx/examples/collections/find.cpp Example
58+
59+
## Delete a Document
60+
61+
@snippet examples/api/mongocxx/examples/collections/delete_one.cpp Example
62+
63+
## Delete Many Documents
64+
65+
@snippet examples/api/mongocxx/examples/collections/delete_many.cpp Example
66+
67+
## Insert a Document
68+
69+
@snippet examples/api/mongocxx/examples/collections/insert_one.cpp Example
70+
71+
## Insert Many Documents
72+
73+
@snippet examples/api/mongocxx/examples/collections/insert_many.cpp Example
74+
75+
## Replace a Document
76+
77+
@snippet examples/api/mongocxx/examples/collections/replace_one.cpp Example
78+
79+
## Update a Document
80+
81+
@snippet examples/api/mongocxx/examples/collections/update_one.cpp Example
82+
83+
## Update Multiple Documents
84+
85+
@snippet examples/api/mongocxx/examples/collections/update_many.cpp Example
86+
87+
## Find and Delete a Document
88+
89+
@snippet examples/api/mongocxx/examples/collections/find_one_and_delete.cpp Example
90+
91+
## Find and Replace a Document
92+
93+
@snippet examples/api/mongocxx/examples/collections/find_one_and_replace.cpp Example
94+
95+
## Find and Update a Document
96+
97+
@snippet examples/api/mongocxx/examples/collections/find_one_and_update.cpp Example
98+
99+
## Find Distinct Values
100+
101+
@snippet examples/api/mongocxx/examples/collections/distinct.cpp Example
102+
103+
## Execute a Single Bulk Write Operation
104+
105+
@snippet examples/api/mongocxx/examples/collections/write.cpp Example
106+
107+
## Execute Multiple Bulk Write Operations
108+
109+
@snippet examples/api/mongocxx/examples/collections/bulk_write.cpp Example
110+
111+
## Execute an Aggregation Operation
112+
113+
@snippet examples/api/mongocxx/examples/collections/aggregate.cpp Example
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Obtain a Database
2+
3+
@snippet examples/api/mongocxx/examples/databases/obtain.cpp Example
4+
5+
# Database Operations
6+
7+
## Drop a Database
8+
9+
@snippet examples/api/mongocxx/examples/databases/drop.cpp Example
10+
11+
## Run a Command
12+
13+
@snippet examples/api/mongocxx/examples/databases/run_command.cpp Example
14+
15+
## Set a Read Concern
16+
17+
@snippet examples/api/mongocxx/examples/databases/rc.cpp Example
18+
19+
## Set a Write Concern
20+
21+
@snippet examples/api/mongocxx/examples/databases/wc.cpp Example
22+
23+
# Collection Operations
24+
25+
## Create a Collection
26+
27+
@snippet examples/api/mongocxx/examples/databases/create_collection.cpp Example
28+
29+
## Create a Collection With Options
30+
31+
@snippet examples/api/mongocxx/examples/databases/create_collection_with_options.cpp Example
32+
33+
## Query a Collection Exists
34+
35+
@snippet examples/api/mongocxx/examples/databases/has_collection.cpp Example
36+
37+
## List Collections in the Database
38+
39+
@snippet examples/api/mongocxx/examples/databases/list_collections.cpp Example
40+
41+
## List Collection Names in the Database
42+
43+
@snippet examples/api/mongocxx/examples/databases/list_collection_names.cpp Example
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Initialize the C++ Driver
2+
3+
## Basic Usage
4+
5+
@snippet api/mongocxx/examples/instance/basic_usage.cpp Example
6+
7+
## With Static Lifetime
8+
9+
@warning This pattern depends on an exit-time destructor with indeterminate order relative to other objects with static lifetime being destroyed.
10+
11+
@snippet api/mongocxx/examples/instance/current.cpp Example
12+
13+
# Errors
14+
15+
## Instance Recreation
16+
17+
@snippet api/mongocxx/examples/instance/recreation.cpp Example
18+
19+
## Destroyed Instance
20+
21+
@snippet api/mongocxx/examples/instance/destroyed.cpp Example

docs/api/mongocxx/examples/logger.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Basic Usage
2+
3+
@snippet api/mongocxx/examples/logger/basic_usage.cpp Example
4+
5+
# Convert a Log Level to a String
6+
7+
@snippet api/mongocxx/examples/logger/to_string.cpp Example
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# As a Regular Exception
2+
3+
@warning The @ref mongocxx::server_error_category error category is overloaded ([CXX-834](https://jira.mongodb.org/browse/CXX-834)). The error code value may belong to the server, libmongoc, or libmongocrypt depending on the context. Use error code values with caution.
4+
5+
@snippet examples/api/mongocxx/examples/operation_exceptions/regular.cpp Example
6+
7+
# As an Operation Exception
8+
9+
@note Using @ref mongocxx::exception for error handling is recommended for forward compatibility. ([CXX-2377](https://jira.mongodb.org/browse/CXX-2377))
10+
11+
@snippet examples/api/mongocxx/examples/operation_exceptions/operation.cpp Example

docs/api/mongocxx/examples/uri.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Create a URI
2+
3+
## Basic Usage
4+
5+
@snippet api/mongocxx/examples/uri/basic_usage.cpp Example
6+
7+
## Default Value
8+
9+
@snippet api/mongocxx/examples/uri/default_value.cpp Example
10+
11+
# Query a URI
12+
13+
## User Credentials
14+
15+
@snippet api/mongocxx/examples/uri/userpass.cpp Example
16+
17+
## List of Hosts
18+
19+
@snippet api/mongocxx/examples/uri/hosts.cpp Example
20+
21+
## Optional Options
22+
23+
@snippet api/mongocxx/examples/uri/optional.cpp Example
24+
25+
## All URI Options
26+
27+
@snippet api/mongocxx/examples/uri/all_options.cpp Example
28+
29+
# Error Handling
30+
31+
## Invalid URI
32+
33+
@snippet api/mongocxx/examples/uri/invalid.cpp Example

examples/api/bsoncxx/examples/bson_errors/big_string.hh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ namespace examples {
2626
// Used to trigger builder append failure.
2727
struct big_string {
2828
// BSON_SIZE_MAX == 0x7FFFFFFF
29-
std::size_t length{static_cast<std::size_t>(std::numeric_limits<std::int32_t>::max())};
29+
// Leave some room for CDRIVER-5732.
30+
std::size_t length{static_cast<std::size_t>(std::numeric_limits<std::int32_t>::max() - 32u)};
3031

3132
// Allocate an UNINITIALIZED blob of data that will not be accessed due to length checks.
3233
// Leaving memory unitialized (rather than zero-init) should hopefully avoid slow and expensive

examples/api/concern.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2009-present MongoDB, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <mongocxx/collection.hpp>
16+
#include <mongocxx/database.hpp>
17+
#include <mongocxx/read_concern.hpp>
18+
#include <mongocxx/write_concern.hpp>
19+
20+
#include <examples/api/concern.hh>
21+
22+
// Preferred default for most operations.
23+
mongocxx::collection set_rw_concern_majority(mongocxx::collection coll) {
24+
coll.read_concern(rc_majority());
25+
coll.write_concern(wc_majority());
26+
return coll;
27+
}
28+
29+
// Preferred default for most operations.
30+
mongocxx::database set_rw_concern_majority(mongocxx::database db) {
31+
db.read_concern(rc_majority());
32+
db.write_concern(wc_majority());
33+
return db;
34+
}
35+
36+
mongocxx::read_concern rc_majority() {
37+
mongocxx::read_concern rc;
38+
rc.acknowledge_level(mongocxx::read_concern::level::k_majority);
39+
return rc;
40+
}
41+
42+
mongocxx::write_concern wc_majority() {
43+
mongocxx::write_concern wc;
44+
wc.acknowledge_level(mongocxx::write_concern::level::k_majority);
45+
return wc;
46+
}

0 commit comments

Comments
 (0)