Skip to content

Commit 85ad0a1

Browse files
committed
CXX-791 Options for bulk ops should be on the options::bulk_op type
1 parent 3a458fd commit 85ad0a1

File tree

9 files changed

+146
-93
lines changed

9 files changed

+146
-93
lines changed

src/mongocxx/bulk_write.cpp

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,16 @@ bulk_write& bulk_write::operator=(bulk_write&&) noexcept = default;
3333

3434
bulk_write::~bulk_write() = default;
3535

36-
bulk_write::bulk_write(bool ordered)
37-
: _impl(stdx::make_unique<impl>(libmongoc::bulk_operation_new(ordered))) {
36+
bulk_write::bulk_write(options::bulk_write options)
37+
: _impl(stdx::make_unique<impl>(libmongoc::bulk_operation_new(options.ordered()))) {
38+
auto options_wc = options.write_concern();
39+
if (options_wc)
40+
libmongoc::bulk_operation_set_write_concern(_impl->operation_t,
41+
options_wc->_impl->write_concern_t);
42+
43+
auto options_bdv = options.bypass_document_validation();
44+
if (options_bdv)
45+
libmongoc::bulk_operation_set_bypass_document_validation(_impl->operation_t, *options_bdv);
3846
}
3947

4048
void bulk_write::append(const model::write& operation) {
@@ -87,20 +95,5 @@ void bulk_write::append(const model::write& operation) {
8795
}
8896
}
8997

90-
void bulk_write::bypass_document_validation(bool bypass_document_validation) {
91-
libmongoc::bulk_operation_set_bypass_document_validation(_impl->operation_t,
92-
bypass_document_validation);
93-
}
94-
95-
void bulk_write::write_concern(class write_concern wc) {
96-
libmongoc::bulk_operation_set_write_concern(_impl->operation_t, wc._impl->write_concern_t);
97-
}
98-
99-
class write_concern bulk_write::write_concern() const {
100-
class write_concern wc(stdx::make_unique<write_concern::impl>(libmongoc::write_concern_copy(
101-
libmongoc::bulk_operation_get_write_concern(_impl->operation_t))));
102-
return wc;
103-
}
104-
10598
MONGOCXX_INLINE_NAMESPACE_END
10699
} // namespace mongocxx

src/mongocxx/bulk_write.hpp

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616

1717
#include <mongocxx/config/prelude.hpp>
1818

19-
#include <mongocxx/write_concern.hpp>
19+
#include <mongocxx/options/bulk_write.hpp>
2020
#include <mongocxx/model/write.hpp>
21-
#include <bsoncxx/stdx/optional.hpp>
2221

2322
namespace mongocxx {
2423
MONGOCXX_INLINE_NAMESPACE_BEGIN
@@ -53,7 +52,7 @@ class MONGOCXX_API bulk_write {
5352
/// be reported after attempting all operations. Unordered bulk writes may be more efficient
5453
/// than ordered bulk writes.
5554
///
56-
explicit bulk_write(bool ordered);
55+
explicit bulk_write(options::bulk_write options = {});
5756

5857
///
5958
/// Move constructs a bulk write operation.
@@ -88,29 +87,6 @@ class MONGOCXX_API bulk_write {
8887
///
8988
void append(const model::write& operation);
9089

91-
///
92-
/// Whether or not to bypass document validation for this operation.
93-
///
94-
/// @param bypass_document_validation
95-
/// Whether or not to bypass document validation.
96-
///
97-
void bypass_document_validation(bool bypass_document_validation);
98-
99-
///
100-
/// Sets the write_concern for this operation.
101-
///
102-
/// @param wc
103-
/// The write_concern to set
104-
///
105-
void write_concern(class write_concern wc);
106-
107-
///
108-
/// Gets the write_concern for the bulk write.
109-
///
110-
/// @return The current write_concern.
111-
///
112-
class write_concern write_concern() const;
113-
11490
private:
11591
friend class collection;
11692

src/mongocxx/collection.cpp

Lines changed: 63 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,25 @@ cursor collection::aggregate(const pipeline& pipeline, const options::aggregate&
285285

286286
stdx::optional<result::insert_one> collection::insert_one(view_or_value document,
287287
const options::insert& options) {
288-
class bulk_write bulk_op(false);
288+
289+
// TODO: We should consider making it possible to convert from an options::insert into
290+
// an options::bulk_write at the type level, removing the need to re-iterate this code
291+
// many times here and below.
292+
//
293+
// See comments in: https://github.com/mongodb/mongo-cxx-driver/pull/409
294+
295+
options::bulk_write bulk_opts;
296+
bulk_opts.ordered(false);
297+
298+
if (options.write_concern()) {
299+
bulk_opts.write_concern(*options.write_concern());
300+
}
301+
302+
if (options.bypass_document_validation()) {
303+
bulk_opts.bypass_document_validation(*options.bypass_document_validation());
304+
}
305+
306+
class bulk_write bulk_op(bulk_opts);
289307
bsoncxx::document::element oid{};
290308

291309
bsoncxx::builder::stream::document new_document;
@@ -299,14 +317,6 @@ stdx::optional<result::insert_one> collection::insert_one(view_or_value document
299317
oid = document.view()["_id"];
300318
}
301319

302-
if (options.write_concern()) {
303-
bulk_op.write_concern(*options.write_concern());
304-
}
305-
306-
if (options.bypass_document_validation()) {
307-
bulk_op.bypass_document_validation(*options.bypass_document_validation());
308-
}
309-
310320
auto result = bulk_write(bulk_op);
311321
if (!result) {
312322
return stdx::optional<result::insert_one>();
@@ -319,17 +329,20 @@ stdx::optional<result::insert_one> collection::insert_one(view_or_value document
319329
stdx::optional<result::replace_one> collection::replace_one(view_or_value filter,
320330
view_or_value replacement,
321331
const options::update& options) {
322-
class bulk_write bulk_op(false);
323-
model::replace_one replace_op(filter, replacement);
332+
options::bulk_write bulk_opts;
333+
bulk_opts.ordered(false);
334+
335+
if (options.bypass_document_validation())
336+
bulk_opts.bypass_document_validation(*options.bypass_document_validation());
337+
if (options.write_concern()) bulk_opts.write_concern(*options.write_concern());
324338

339+
class bulk_write bulk_op(bulk_opts);
340+
341+
model::replace_one replace_op(filter, replacement);
325342
if (options.upsert()) replace_op.upsert(options.upsert().value());
326343

327344
bulk_op.append(replace_op);
328345

329-
if (options.bypass_document_validation())
330-
bulk_op.bypass_document_validation(*options.bypass_document_validation());
331-
if (options.write_concern()) bulk_op.write_concern(*options.write_concern());
332-
333346
auto result = bulk_write(bulk_op);
334347
if (!result) {
335348
return stdx::optional<result::replace_one>();
@@ -340,17 +353,20 @@ stdx::optional<result::replace_one> collection::replace_one(view_or_value filter
340353

341354
stdx::optional<result::update> collection::update_many(view_or_value filter, view_or_value update,
342355
const options::update& options) {
343-
class bulk_write bulk_op(false);
344-
model::update_many update_op(filter, update);
356+
options::bulk_write bulk_opts;
357+
bulk_opts.ordered(false);
345358

359+
if (options.bypass_document_validation())
360+
bulk_opts.bypass_document_validation(*options.bypass_document_validation());
361+
if (options.write_concern()) bulk_opts.write_concern(*options.write_concern());
362+
363+
class bulk_write bulk_op(bulk_opts);
364+
365+
model::update_many update_op(filter, update);
346366
if (options.upsert()) update_op.upsert(options.upsert().value());
347367

348368
bulk_op.append(update_op);
349369

350-
if (options.bypass_document_validation())
351-
bulk_op.bypass_document_validation(*options.bypass_document_validation());
352-
if (options.write_concern()) bulk_op.write_concern(*options.write_concern());
353-
354370
auto result = bulk_write(bulk_op);
355371
if (!result) {
356372
return stdx::optional<result::update>();
@@ -361,12 +377,17 @@ stdx::optional<result::update> collection::update_many(view_or_value filter, vie
361377

362378
stdx::optional<result::delete_result> collection::delete_many(
363379
view_or_value filter, const options::delete_options& options) {
364-
class bulk_write bulk_op(false);
380+
381+
options::bulk_write bulk_opts;
382+
bulk_opts.ordered(false);
383+
384+
if (options.write_concern()) bulk_opts.write_concern(*options.write_concern());
385+
386+
class bulk_write bulk_op(bulk_opts);
387+
365388
model::delete_many delete_op(filter);
366389
bulk_op.append(delete_op);
367390

368-
if (options.write_concern()) bulk_op.write_concern(*options.write_concern());
369-
370391
auto result = bulk_write(bulk_op);
371392
if (!result) {
372393
return stdx::optional<result::delete_result>();
@@ -377,17 +398,21 @@ stdx::optional<result::delete_result> collection::delete_many(
377398

378399
stdx::optional<result::update> collection::update_one(view_or_value filter, view_or_value update,
379400
const options::update& options) {
380-
class bulk_write bulk_op(false);
381-
model::update_one update_op(filter, update);
382401

402+
options::bulk_write bulk_opts;
403+
bulk_opts.ordered(false);
404+
405+
if (options.bypass_document_validation())
406+
bulk_opts.bypass_document_validation(*options.bypass_document_validation());
407+
if (options.write_concern()) bulk_opts.write_concern(*options.write_concern());
408+
409+
class bulk_write bulk_op(bulk_opts);
410+
411+
model::update_one update_op(filter, update);
383412
if (options.upsert()) update_op.upsert(options.upsert().value());
384413

385414
bulk_op.append(update_op);
386415

387-
if (options.bypass_document_validation())
388-
bulk_op.bypass_document_validation(*options.bypass_document_validation());
389-
if (options.write_concern()) bulk_op.write_concern(*options.write_concern());
390-
391416
auto result = bulk_write(bulk_op);
392417
if (!result) {
393418
return stdx::optional<result::update>();
@@ -398,12 +423,17 @@ stdx::optional<result::update> collection::update_one(view_or_value filter, view
398423

399424
stdx::optional<result::delete_result> collection::delete_one(
400425
view_or_value filter, const options::delete_options& options) {
401-
class bulk_write bulk_op(false);
426+
427+
options::bulk_write bulk_opts;
428+
bulk_opts.ordered(false);
429+
430+
if (options.write_concern()) bulk_opts.write_concern(*options.write_concern());
431+
432+
class bulk_write bulk_op(bulk_opts);
433+
402434
model::delete_one delete_op(filter);
403435
bulk_op.append(delete_op);
404436

405-
if (options.write_concern()) bulk_op.write_concern(*options.write_concern());
406-
407437
auto result = bulk_write(bulk_op);
408438
if (!result) {
409439
return stdx::optional<result::delete_result>();

src/mongocxx/collection.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ template <typename write_model_iterator_type>
614614
MONGOCXX_INLINE stdx::optional<result::bulk_write> collection::bulk_write(
615615
write_model_iterator_type begin, write_model_iterator_type end,
616616
const options::bulk_write& options) {
617-
class bulk_write writes(options.ordered().value_or(true));
617+
class bulk_write writes(options);
618618

619619
std::for_each(begin, end, [&](const model::write& current) { writes.append(current); });
620620

src/mongocxx/insert_many_builder.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,24 @@
2222
namespace mongocxx {
2323
MONGOCXX_INLINE_NAMESPACE_BEGIN
2424

25-
insert_many_builder::insert_many_builder(const options::insert& options)
26-
: _writes{options.ordered().value_or(true)}, _inserted_ids{}, _index{0} {
27-
if (options.write_concern()) {
28-
_writes.write_concern(*options.write_concern());
25+
namespace {
26+
27+
options::bulk_write make_bulk_write_options(const options::insert& insert_options) {
28+
options::bulk_write bw;
29+
bw.ordered(insert_options.ordered().value_or(true));
30+
if (insert_options.write_concern()) {
31+
bw.write_concern(*insert_options.write_concern());
2932
}
30-
if (options.bypass_document_validation()) {
31-
_writes.bypass_document_validation(*options.bypass_document_validation());
33+
if (insert_options.bypass_document_validation()) {
34+
bw.bypass_document_validation(*insert_options.bypass_document_validation());
3235
}
36+
return bw;
37+
}
38+
39+
} // namespace
40+
41+
insert_many_builder::insert_many_builder(const options::insert& options)
42+
: _writes{make_bulk_write_options(options)}, _inserted_ids{}, _index{0} {
3343
};
3444

3545
void insert_many_builder::operator()(const bsoncxx::document::view& doc) {

src/mongocxx/options/bulk_write.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,31 @@ namespace mongocxx {
2020
MONGOCXX_INLINE_NAMESPACE_BEGIN
2121
namespace options {
2222

23+
bulk_write::bulk_write()
24+
: _ordered(true) {}
25+
2326
void bulk_write::ordered(bool ordered) {
2427
_ordered = ordered;
2528
}
2629

30+
bool bulk_write::ordered() const {
31+
return _ordered;
32+
}
33+
2734
void bulk_write::write_concern(class write_concern wc) {
2835
_write_concern = std::move(wc);
2936
}
3037

31-
const stdx::optional<bool>& bulk_write::ordered() const {
32-
return _ordered;
38+
const stdx::optional<class write_concern>& bulk_write::write_concern() const {
39+
return _write_concern;
40+
}
41+
42+
void bulk_write::bypass_document_validation(bool bypass_document_validation) {
43+
_bypass_document_validation = bypass_document_validation;
44+
}
45+
46+
const stdx::optional<bool> bulk_write::bypass_document_validation() const {
47+
return _bypass_document_validation;
3348
}
3449

3550
} // namespace options

src/mongocxx/options/bulk_write.hpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ class MONGOCXX_API bulk_write {
3131

3232
public:
3333

34+
///
35+
/// Constructs a new bulk_write object. By default, bulk writes are considered ordered
36+
/// as this is the only safe choice. If you want an unordered update, you must call
37+
/// ordered(false) to switch to unordered mode.
38+
///
39+
bulk_write();
40+
3441
///
3542
/// Sets whether the writes must be executed in order by the server.
3643
///
@@ -47,9 +54,9 @@ class MONGOCXX_API bulk_write {
4754
///
4855
/// Gets the current value of the ordered option.
4956
///
50-
/// @return The optional value of the ordered option.
57+
/// @return The value of the ordered option.
5158
///
52-
const stdx::optional<bool>& ordered() const;
59+
bool ordered() const;
5360

5461
///
5562
/// Sets the write_concern for this operation.
@@ -71,9 +78,26 @@ class MONGOCXX_API bulk_write {
7178
///
7279
const stdx::optional<class write_concern>& write_concern() const;
7380

81+
///
82+
/// Set whether or not to bypass document validation for this operation.
83+
///
84+
/// @param bypass_document_validation
85+
/// Whether or not to bypass document validation.
86+
///
87+
void bypass_document_validation(bool bypass_document_validation);
88+
89+
///
90+
/// The current setting for bypassing document validation for this operation.
91+
///
92+
/// @return
93+
/// The current document validation bypass setting.
94+
///
95+
const stdx::optional<bool> bypass_document_validation() const;
96+
7497
private:
75-
stdx::optional<bool> _ordered;
98+
bool _ordered;
7699
stdx::optional<class write_concern> _write_concern;
100+
stdx::optional<bool> _bypass_document_validation;
77101
};
78102

79103
} // namespace options

0 commit comments

Comments
 (0)