Skip to content

Commit 454a014

Browse files
authored
CDRIVER-4036 document new API added for snapshot reads (#826)
1 parent 88f7749 commit 454a014

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

src/libmongoc/doc/mongoc_session_opts_get_snapshot.rst

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,21 @@
33
mongoc_session_opts_get_snapshot()
44
==================================
55

6-
TODO CDRIVER-4036: Document this function and give example code.
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
bool
12+
mongoc_session_opts_get_snapshot (const mongoc_session_opt_t *opts);
13+
14+
Return true if this session is configured for snapshot reads, false by default. See :symbol:`mongoc_session_opts_set_snapshot()`.
15+
16+
Parameters
17+
----------
18+
19+
* ``opts``: A :symbol:`mongoc_session_opt_t`.
20+
21+
.. only:: html
22+
23+
.. include:: includes/seealso/session.txt

src/libmongoc/doc/mongoc_session_opts_set_causal_consistency.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Synopsis
1414
1515
Configure causal consistency in a session. If true (the default), each operation in the session will be causally ordered after the previous read or write operation. Set to false to disable causal consistency. See `the MongoDB Manual Entry for Causal Consistency <http://dochub.mongodb.org/core/causal-consistency>`_.
1616

17+
Causal consistency and snapshot reads are mutually exclusive. Attempting to set both to true will result in an error. See :symbol:`mongoc_session_opts_set_snapshot()`.
18+
1719
Unacknowledged writes are not causally consistent. If you execute a write operation with a :symbol:`mongoc_write_concern_t` on which you have called :symbol:`mongoc_write_concern_set_w` with a value of 0, the write does not participate in causal consistency.
1820

1921
Parameters

src/libmongoc/doc/mongoc_session_opts_set_snapshot.rst

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,67 @@
33
mongoc_session_opts_set_snapshot()
44
==================================
55

6-
TODO CDRIVER-4036: Document this function and give example code.
6+
Synopsis
7+
--------
8+
9+
.. code-block:: c
10+
11+
void
12+
mongoc_session_opts_set_snapshot (mongoc_session_opt_t *opts,
13+
bool snapshot);
14+
15+
Configure snapshot reads for a session. If true (false by default), each read operation in the session will be sent with a "snapshot" level read concern. After the first read operation ("find", "aggregate" or "distinct"), subsequent read operations will read from the same point in time as the first read operation. Set to true to enable snapshot reads. See `the official documentation for Read Concern "snapshot" <https://docs.mongodb.com/manual/reference/read-concern-snapshot/>`_.
16+
17+
Snapshot reads and causal consistency are mutually exclusive. Attempting to set both to true will result in an error. See :symbol:`mongoc_session_opts_set_causal_consistency()`.
18+
19+
Snapshot reads can only be used on MongoDB server version 5.0 and later and cannot be used during a transaction. A write operation in a snapshot-enabled session will also result in an error.
20+
21+
Parameters
22+
----------
23+
24+
* ``opts``: A :symbol:`mongoc_session_opt_t`.
25+
* ``snapshot``: True or false.
26+
27+
Example
28+
-------
29+
30+
.. code-block:: c
31+
32+
mongoc_client_t *client;
33+
mongoc_session_opt_t *session_opts;
34+
mongoc_client_session_t *client_session;
35+
mongoc_collection_t *collection;
36+
bson_t query_opts = BSON_INITIALIZER;
37+
bson_t filter = BSON_INITIALIZER;
38+
bson_t pipeline = BSON_INITIALIZER;
39+
40+
client = mongoc_client_new ("mongodb://example/?appname=session-opts-example");
41+
mongoc_client_set_error_api (client, MONGOC_ERROR_API_VERSION_2);
42+
43+
session_opts = mongoc_session_opts_new ();
44+
mongoc_session_opts_set_snapshot (session_opts, true);
45+
client_session = mongoc_client_start_session (client, session_opts, &error);
46+
mongoc_session_opts_destroy (session_opts);
47+
48+
if (!client_session) {
49+
fprintf (stderr, "Failed to start session: %s\n", error.message);
50+
abort ();
51+
}
52+
53+
collection = mongoc_client_get_collection (client, "test", "collection");
54+
r = mongoc_client_session_append (client_session, &find_opts, &error);
55+
if (!r) {
56+
fprintf (stderr, "mongoc_client_session_append failed: %s\n", error.message);
57+
abort ();
58+
}
59+
60+
/* First read operation will set the snapshot time for subsequent reads. */
61+
cursor = mongoc_collection_find_with_opts (collection, filter, &query_opts, NULL);
62+
63+
/* Subsequent read operations will automatically read from the same point
64+
* in time as the first read operation. */
65+
cursor = mongoc_collection_aggregate (collection, MONGOC_QUERY_NONE, pipeline, &query_opts, NULL);
66+
67+
.. only:: html
68+
69+
.. include:: includes/seealso/session.txt

0 commit comments

Comments
 (0)