Skip to content

Commit 59c152a

Browse files
committed
CXX-1399 Implement session (insert_one only)
1 parent a65c121 commit 59c152a

File tree

12 files changed

+304
-75
lines changed

12 files changed

+304
-75
lines changed

src/mongocxx/bulk_write.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014 MongoDB Inc.
1+
// Copyright 2014-present MongoDB Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
#include <mongocxx/private/collection.hh>
2525
#include <mongocxx/private/libbson.hh>
2626
#include <mongocxx/private/libmongoc.hh>
27+
#include <mongocxx/private/session.hh>
2728
#include <mongocxx/private/write_concern.hh>
2829

2930
#include <mongocxx/config/private/prelude.hh>
@@ -160,7 +161,9 @@ void bulk_write::append(const model::write& operation) {
160161
}
161162
}
162163

163-
bulk_write::bulk_write(const collection& coll, const options::bulk_write& options)
164+
bulk_write::bulk_write(const collection& coll,
165+
const options::bulk_write& options,
166+
const session* session)
164167
: _created_from_collection{true} {
165168
bsoncxx::builder::basic::document options_builder;
166169
if (!options.ordered()) {
@@ -169,6 +172,10 @@ bulk_write::bulk_write(const collection& coll, const options::bulk_write& option
169172
if (options.write_concern()) {
170173
options_builder.append(kvp("writeConcern", options.write_concern()->to_document()));
171174
}
175+
if (session) {
176+
options_builder.append(
177+
bsoncxx::builder::concatenate_doc{session->_get_impl().to_document()});
178+
}
172179

173180
scoped_bson_t bson_options(options_builder.extract());
174181
_impl =

src/mongocxx/bulk_write.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014 MongoDB Inc.
1+
// Copyright 2014-present MongoDB Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
#include <mongocxx/model/write.hpp>
1818
#include <mongocxx/options/bulk_write.hpp>
19+
#include <mongocxx/session.hpp>
1920

2021
#include <mongocxx/config/prelude.hpp>
2122

@@ -93,7 +94,9 @@ class MONGOCXX_API bulk_write {
9394

9495
class MONGOCXX_PRIVATE impl;
9596

96-
MONGOCXX_PRIVATE bulk_write(const collection& coll, const options::bulk_write& options);
97+
MONGOCXX_PRIVATE bulk_write(const collection& coll,
98+
const options::bulk_write& options,
99+
const session* session = nullptr);
97100

98101
bool _created_from_collection;
99102
std::unique_ptr<impl> _impl;

src/mongocxx/client.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014 MongoDB Inc.
1+
// Copyright 2014-present MongoDB Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -232,7 +232,7 @@ class MONGOCXX_API client {
232232
/// options is misconfigured, or if the session is configured with options that the server does
233233
/// not support.
234234
///
235-
session start_session(const options::session& options);
235+
session start_session(const options::session& options = {});
236236

237237
private:
238238
friend class collection;

src/mongocxx/collection.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014 MongoDB Inc.
1+
// Copyright 2014-present MongoDB Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -54,6 +54,7 @@
5454
#include <mongocxx/private/pipeline.hh>
5555
#include <mongocxx/private/read_concern.hh>
5656
#include <mongocxx/private/read_preference.hh>
57+
#include <mongocxx/private/session.hh>
5758
#include <mongocxx/private/write_concern.hh>
5859
#include <mongocxx/result/bulk_write.hpp>
5960
#include <mongocxx/result/delete.hpp>
@@ -219,6 +220,15 @@ bulk_write collection::create_bulk_write(const options::bulk_write& options) {
219220
return writes;
220221
}
221222

223+
bulk_write collection::create_bulk_write(const session& session,
224+
const options::bulk_write& options) {
225+
class bulk_write writes {
226+
*this, options, &session
227+
};
228+
229+
return writes;
230+
}
231+
222232
stdx::optional<result::bulk_write> collection::bulk_write(const class bulk_write& bulk_write) {
223233
mongoc_bulk_operation_t* b = bulk_write._impl->operation_t;
224234

@@ -440,8 +450,9 @@ cursor collection::aggregate(const pipeline& pipeline, const options::aggregate&
440450
rp_ptr));
441451
}
442452

443-
stdx::optional<result::insert_one> collection::insert_one(view_or_value document,
444-
const options::insert& options) {
453+
stdx::optional<result::insert_one> collection::_insert_one(view_or_value document,
454+
const options::insert& options,
455+
const session* session) {
445456
// TODO: We should consider making it possible to convert from an options::insert into
446457
// an options::bulk_write at the type level, removing the need to re-iterate this code
447458
// many times here and below.
@@ -459,10 +470,12 @@ stdx::optional<result::insert_one> collection::insert_one(view_or_value document
459470
bulk_opts.bypass_document_validation(*options.bypass_document_validation());
460471
}
461472

462-
auto bulk_op = create_bulk_write(bulk_opts);
473+
class bulk_write bulk_op {
474+
*this, bulk_opts, session
475+
};
463476
bsoncxx::document::element oid{};
464-
465477
bsoncxx::builder::basic::document new_document;
478+
466479
if (!document.view()["_id"]) {
467480
new_document.append(kvp("_id", bsoncxx::oid()));
468481
new_document.append(concatenate(document));
@@ -482,6 +495,17 @@ stdx::optional<result::insert_one> collection::insert_one(view_or_value document
482495
result::insert_one(std::move(result.value()), std::move(oid.get_value())));
483496
}
484497

498+
stdx::optional<result::insert_one> collection::insert_one(view_or_value document,
499+
const options::insert& options) {
500+
return _insert_one(document, options);
501+
}
502+
503+
stdx::optional<result::insert_one> collection::insert_one(view_or_value document,
504+
const session& session,
505+
const options::insert& options) {
506+
return _insert_one(document, options, &session);
507+
}
508+
485509
stdx::optional<result::replace_one> collection::replace_one(view_or_value filter,
486510
view_or_value replacement,
487511
const options::update& options) {

src/mongocxx/collection.hpp

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014 MongoDB Inc.
1+
// Copyright 2014-present MongoDB Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -52,6 +52,7 @@
5252
#include <mongocxx/result/insert_one.hpp>
5353
#include <mongocxx/result/replace_one.hpp>
5454
#include <mongocxx/result/update.hpp>
55+
#include <mongocxx/session.hpp>
5556
#include <mongocxx/write_concern.hpp>
5657

5758
#include <mongocxx/config/prelude.hpp>
@@ -144,6 +145,8 @@ class MONGOCXX_API collection {
144145
cursor aggregate(const pipeline& pipeline,
145146
const options::aggregate& options = options::aggregate());
146147

148+
///
149+
/// @{
147150
///
148151
/// Creates a new bulk operation to be executed against this collection.
149152
///
@@ -155,6 +158,23 @@ class MONGOCXX_API collection {
155158
///
156159
class bulk_write create_bulk_write(const options::bulk_write& options = {});
157160

161+
///
162+
/// Creates a new bulk operation to be executed against this collection.
163+
///
164+
/// @param session
165+
/// The mongocxx::session with which to perform the bulk operation.
166+
/// @param options
167+
/// Optional arguments; see mongocxx::options::bulk_write.
168+
///
169+
/// @return
170+
/// The newly-created bulk write.
171+
///
172+
class bulk_write create_bulk_write(const session& session,
173+
const options::bulk_write& options = {});
174+
///
175+
/// @}
176+
///
177+
158178
///
159179
/// Sends a write to the server as a bulk write operation.
160180
///
@@ -167,7 +187,7 @@ class MONGOCXX_API collection {
167187
/// The optional result of the bulk operation execution.
168188
/// If the write concern is unacknowledged, the optional will be
169189
/// disengaged.
170-
//
190+
///
171191
/// @exception
172192
/// mongocxx::bulk_write_exception when there are errors processing
173193
/// the writes.
@@ -193,7 +213,7 @@ class MONGOCXX_API collection {
193213
/// @return The optional result of the bulk operation execution.
194214
/// If the write concern is unacknowledged, the optional will be
195215
/// disengaged.
196-
//
216+
///
197217
/// @throws mongocxx::bulk_write_exception when there are errors processing the writes.
198218
///
199219
/// @see mongocxx::bulk_write
@@ -471,6 +491,8 @@ class MONGOCXX_API collection {
471491
bsoncxx::document::view_or_value update,
472492
const options::find_one_and_update& options = options::find_one_and_update());
473493

494+
///
495+
/// @{
474496
///
475497
/// Inserts a single document into the collection. If the document is missing an identifier
476498
/// (@c _id field) one will be generated for it.
@@ -485,10 +507,30 @@ class MONGOCXX_API collection {
485507
/// disengaged.
486508
///
487509
/// @throws mongocxx::bulk_write_exception if the operation fails.
510+
stdx::optional<result::insert_one> insert_one(bsoncxx::document::view_or_value document,
511+
const options::insert& options = {});
512+
///
513+
/// Inserts a single document into the collection. If the document is missing an identifier
514+
/// (@c _id field) one will be generated for it.
515+
///
516+
/// @param document
517+
/// The document to insert.
518+
/// @param session
519+
/// The mongocxx::session with which to perform the insert.
520+
/// @param options
521+
/// Optional arguments, see options::insert.
522+
///
523+
/// @return The optional result of attempting to perform the insert.
524+
/// If the write concern is unacknowledged, the optional will be
525+
/// disengaged.
526+
///
527+
/// @throws mongocxx::bulk_write_exception if the operation fails.
528+
stdx::optional<result::insert_one> insert_one(bsoncxx::document::view_or_value document,
529+
const session& session,
530+
const options::insert& options = {});
531+
///
532+
/// @}
488533
///
489-
stdx::optional<result::insert_one> insert_one(
490-
bsoncxx::document::view_or_value document,
491-
const options::insert& options = options::insert());
492534

493535
///
494536
/// Inserts multiple documents into the collection. If any of the documents are missing
@@ -497,7 +539,7 @@ class MONGOCXX_API collection {
497539
/// @warning This method uses the bulk insert command to execute the insertion as opposed to
498540
/// the legacy OP_INSERT wire protocol message. As a result, using this method to insert many
499541
/// documents on MongoDB < 2.6 will be slow.
500-
//
542+
///
501543
/// @tparam containter_type
502544
/// The container type. Must meet the requirements for the container concept with a value
503545
/// type of model::write.
@@ -729,6 +771,11 @@ class MONGOCXX_API collection {
729771

730772
MONGOCXX_PRIVATE collection(const database& database, void* collection);
731773

774+
MONGOCXX_PRIVATE stdx::optional<result::insert_one> _insert_one(
775+
bsoncxx::document::view_or_value document,
776+
const options::insert& options,
777+
const session* session = nullptr);
778+
732779
class MONGOCXX_PRIVATE impl;
733780

734781
MONGOCXX_PRIVATE impl& _get_impl();

src/mongocxx/exception/error_code.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ class error_category final : public std::error_category {
7878
return "a GridFS file being operated on was discovered to be corrupted";
7979
case error_code::k_instance_destroyed:
8080
return "the mongocxx instance has been destroyed";
81+
case error_code::k_cannot_create_session:
82+
return "failed to create a client session";
83+
case error_code::k_invalid_session:
84+
return "an invalid session was provided";
8185
default:
8286
return "unknown mongocxx error";
8387
}

src/mongocxx/exception/error_code.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ enum class error_code : std::int32_t {
8181
/// The mongocxx::instance has been destroyed.
8282
k_instance_destroyed,
8383

84+
/// mongocxx::client.create_session failed to create a mongocxx::session.
85+
k_cannot_create_session,
86+
87+
/// A failure attempting to pass a mongocxx::session to a method.
88+
k_invalid_session,
89+
8490
// Add new constant string message to error_code.cpp as well!
8591
};
8692

src/mongocxx/private/libmongoc_symbols.hh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ MONGOCXX_LIBMONGOC_SYMBOL(bulk_operation_remove_one_with_opts)
2222
MONGOCXX_LIBMONGOC_SYMBOL(bulk_operation_replace_one_with_opts)
2323
MONGOCXX_LIBMONGOC_SYMBOL(bulk_operation_set_bypass_document_validation)
2424
MONGOCXX_LIBMONGOC_SYMBOL(bulk_operation_set_client)
25+
MONGOCXX_LIBMONGOC_SYMBOL(bulk_operation_set_client_session)
2526
MONGOCXX_LIBMONGOC_SYMBOL(bulk_operation_set_collection)
2627
MONGOCXX_LIBMONGOC_SYMBOL(bulk_operation_set_database)
2728
MONGOCXX_LIBMONGOC_SYMBOL(bulk_operation_set_write_concern)
@@ -42,8 +43,13 @@ MONGOCXX_LIBMONGOC_SYMBOL(client_pool_new)
4243
MONGOCXX_LIBMONGOC_SYMBOL(client_pool_pop)
4344
MONGOCXX_LIBMONGOC_SYMBOL(client_pool_push)
4445
MONGOCXX_LIBMONGOC_SYMBOL(client_pool_try_pop)
46+
MONGOCXX_LIBMONGOC_SYMBOL(client_session_advance_cluster_time)
47+
MONGOCXX_LIBMONGOC_SYMBOL(client_session_append)
48+
MONGOCXX_LIBMONGOC_SYMBOL(client_session_advance_operation_time)
4549
MONGOCXX_LIBMONGOC_SYMBOL(client_session_destroy)
4650
MONGOCXX_LIBMONGOC_SYMBOL(client_session_get_lsid)
51+
MONGOCXX_LIBMONGOC_SYMBOL(client_session_get_cluster_time)
52+
MONGOCXX_LIBMONGOC_SYMBOL(client_session_get_operation_time)
4753
MONGOCXX_LIBMONGOC_SYMBOL(client_session_get_opts)
4854
MONGOCXX_LIBMONGOC_SYMBOL(client_set_read_concern)
4955
MONGOCXX_LIBMONGOC_SYMBOL(client_set_read_prefs)

0 commit comments

Comments
 (0)