1414
1515#pragma once
1616
17- #include < vector>
18-
1917#include < bsoncxx/builder/basic/array.hpp>
2018#include < bsoncxx/builder/basic/document.hpp>
2119#include < bsoncxx/document/view_or_value.hpp>
2220#include < bsoncxx/string/to_string.hpp>
2321#include < bsoncxx/types/bson_value/view.hpp>
22+ #include < mongocxx/config/private/prelude.hh>
2423#include < mongocxx/exception/error_code.hpp>
2524#include < mongocxx/exception/logic_error.hpp>
2625#include < mongocxx/exception/operation_exception.hpp>
26+ #include < mongocxx/exception/write_exception.hpp>
2727#include < mongocxx/options/index_view.hpp>
2828#include < mongocxx/private/client_session.hh>
2929#include < mongocxx/private/libbson.hh>
3030#include < mongocxx/private/libmongoc.hh>
31-
32- #include < mongocxx/config/private/prelude.hh>
31+ #include < vector>
3332
3433namespace mongocxx {
3534MONGOCXX_INLINE_NAMESPACE_BEGIN
@@ -39,7 +38,8 @@ using bsoncxx::builder::basic::kvp;
3938
4039class index_view ::impl {
4140 public:
42- impl (mongoc_collection_t * collection) : _coll{collection} {}
41+ impl (mongoc_collection_t * collection, mongoc_client_t * client)
42+ : _coll{collection}, _client{client} {}
4343
4444 impl (const impl& i) = default ;
4545
@@ -134,6 +134,26 @@ class index_view::impl {
134134 opts_doc.append (bsoncxx::builder::concatenate_doc{session->_get_impl ().to_document ()});
135135 }
136136
137+ if (options.commit_quorum ()) {
138+ auto server_description = scoped_server_description (libmongoc::client_select_server (
139+ _client, true /* for_writes */ , nullptr /* read_prefs */ , &error));
140+ if (!server_description.sd )
141+ throw_exception<write_exception>(error);
142+
143+ auto is_master = libmongoc::server_description_ismaster (server_description.sd );
144+
145+ bson_iter_t iter;
146+ if (!bson_iter_init_find (&iter, is_master, " maxWireVersion" ) ||
147+ bson_iter_int32 (&iter) < 9 ) {
148+ throw write_exception{
149+ error_code::k_invalid_parameter,
150+ " option 'commitQuorum' not available on the current server version" };
151+ }
152+
153+ command =
154+ make_document (concatenate (command), concatenate (options.commit_quorum ()->view ()));
155+ }
156+
137157 libbson::scoped_bson_t command_bson{command};
138158 libbson::scoped_bson_t opts_bson{opts_doc.view ()};
139159
@@ -219,6 +239,16 @@ class index_view::impl {
219239 }
220240
221241 mongoc_collection_t * _coll;
242+ mongoc_client_t * _client;
243+
244+ class scoped_server_description {
245+ public:
246+ explicit scoped_server_description (mongoc_server_description_t * sd) : sd(sd) {}
247+ ~scoped_server_description () {
248+ mongoc_server_description_destroy (sd);
249+ }
250+ mongoc_server_description_t * sd;
251+ };
222252};
223253MONGOCXX_INLINE_NAMESPACE_END
224254} // namespace mongocxx
0 commit comments