Skip to content

Commit b724d49

Browse files
committed
apply default read/write concerns to search index operations
Apply the default read and write concern. The default read and write concern is not sent in the outgoing command.
1 parent bccc743 commit b724d49

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/mongocxx/lib/mongocxx/v_noabi/mongocxx/private/search_index_view.hh

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,30 @@ inline namespace v_noabi {
1818
using bsoncxx::builder::basic::kvp;
1919
using bsoncxx::builder::basic::make_document;
2020

21+
struct collection_deleter {
22+
void operator()(mongoc_collection_t* ptr) noexcept {
23+
libmongoc::collection_destroy(ptr);
24+
}
25+
};
26+
27+
using collection_ptr = std::unique_ptr<mongoc_collection_t, collection_deleter>;
28+
29+
// `copy_and_apply_default_rw_concerns` copies the mongoc_collection_t and applies a default
30+
// readConcern and writeConcern. Used to prevent sending a readConcern or writeConcern.
31+
static collection_ptr copy_and_apply_default_rw_concerns(mongoc_collection_t* coll) {
32+
auto* wc_default = libmongoc::write_concern_new();
33+
auto* rc_default = libmongoc::read_concern_new();
34+
auto coll_copy = libmongoc::collection_copy(coll);
35+
36+
mongoc_collection_set_read_concern(coll_copy, rc_default);
37+
mongoc_collection_set_write_concern(coll_copy, wc_default);
38+
39+
libmongoc::read_concern_destroy(rc_default);
40+
libmongoc::write_concern_destroy(wc_default);
41+
42+
return collection_ptr(coll_copy);
43+
}
44+
2145
class search_index_view::impl {
2246
public:
2347
impl(mongoc_collection_t* collection, mongoc_client_t* client)
@@ -56,8 +80,9 @@ class search_index_view::impl {
5680

5781
libbson::scoped_bson_t opts_bson(opts_doc.view());
5882

83+
auto coll_copy = copy_and_apply_default_rw_concerns(_coll);
5984
return libmongoc::collection_aggregate(
60-
_coll, mongoc_query_flags_t(), stages.bson(), opts_bson.bson(), rp_ptr);
85+
coll_copy.get(), mongoc_query_flags_t(), stages.bson(), opts_bson.bson(), rp_ptr);
6186
}
6287

6388
std::string create_one(const client_session* session, const search_index_model& model) {
@@ -107,8 +132,9 @@ class search_index_view::impl {
107132
libbson::scoped_bson_t command_bson{command};
108133
libbson::scoped_bson_t opts_bson{opts_doc.view()};
109134

135+
auto coll_copy = copy_and_apply_default_rw_concerns(_coll);
110136
auto result = libmongoc::collection_write_command_with_opts(
111-
_coll, command_bson.bson(), opts_bson.bson(), reply.bson_for_init(), &error);
137+
coll_copy.get(), command_bson.bson(), opts_bson.bson(), reply.bson_for_init(), &error);
112138

113139
if (!result) {
114140
throw_exception<operation_exception>(reply.steal(), error);
@@ -133,8 +159,9 @@ class search_index_view::impl {
133159
libbson::scoped_bson_t opts_bson{opts_doc.view()};
134160
bson_error_t error;
135161

162+
auto coll_copy = copy_and_apply_default_rw_concerns(_coll);
136163
bool result = libmongoc::collection_write_command_with_opts(
137-
_coll, command_bson.bson(), opts_bson.bson(), reply.bson_for_init(), &error);
164+
coll_copy.get(), command_bson.bson(), opts_bson.bson(), reply.bson_for_init(), &error);
138165

139166
const uint32_t serverErrorNamespaceNotFound = 26;
140167
if (error.domain == MONGOC_ERROR_QUERY && error.code == serverErrorNamespaceNotFound) {
@@ -167,8 +194,9 @@ class search_index_view::impl {
167194
libbson::scoped_bson_t opts_bson{opts_doc.view()};
168195
bson_error_t error;
169196

197+
auto coll_copy = copy_and_apply_default_rw_concerns(_coll);
170198
bool result = libmongoc::collection_write_command_with_opts(
171-
_coll, command_bson.bson(), opts_bson.bson(), reply.bson_for_init(), &error);
199+
coll_copy.get(), command_bson.bson(), opts_bson.bson(), reply.bson_for_init(), &error);
172200

173201
if (!result) {
174202
throw_exception<operation_exception>(reply.steal(), error);

0 commit comments

Comments
 (0)