Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/mongocxx/include/mongocxx/v1/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ class transaction {
/// Return the current "writeConcern" field.
///
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v1::stdx::optional<v1::write_concern>) write_concern() const;

class internal;

private:
/* explicit(false) */ transaction(void* impl);
};

} // namespace v1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#pragma once

#include <mongocxx/v1/transaction-fwd.hpp>

#include <mongocxx/config/prelude.hpp>

namespace mongocxx {
Expand All @@ -29,7 +31,7 @@ class transaction;
namespace mongocxx {
namespace options {

using ::mongocxx::v_noabi::options::transaction;
using v_noabi::options::transaction;

} // namespace options
} // namespace mongocxx
Expand All @@ -40,3 +42,6 @@ using ::mongocxx::v_noabi::options::transaction;
/// @file
/// Declares @ref mongocxx::v_noabi::options::transaction.
///
/// @par Includes
/// - @ref mongocxx/v1/transaction-fwd.hpp
///
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@

#pragma once

#include <mongocxx/options/transaction-fwd.hpp> // IWYU pragma: export

//

#include <mongocxx/v1/transaction.hpp> // IWYU pragma: export

#include <chrono>
#include <memory>
#include <memory> // IWYU pragma: keep: backward compatibility, to be removed.
#include <utility>

#include <mongocxx/client_session-fwd.hpp>
#include <mongocxx/options/transaction-fwd.hpp> // IWYU pragma: export
#include <mongocxx/client_session-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
#include <mongocxx/read_concern-fwd.hpp>
#include <mongocxx/read_preference-fwd.hpp>
#include <mongocxx/write_concern-fwd.hpp>
Expand All @@ -35,33 +41,61 @@ namespace options {
/// Used by MongoDB transaction operations.
///
class transaction {
private:
v1::transaction _txn;

public:
MONGOCXX_ABI_EXPORT_CDECL() transaction();
///
/// Default initialization.
///
transaction() = default;

///
/// Copy constructs transaction options.
///
MONGOCXX_ABI_EXPORT_CDECL() transaction(transaction const&);
MONGOCXX_ABI_EXPORT_CDECL() transaction(transaction const& other);

///
/// Copy assigns transaction options.
///
MONGOCXX_ABI_EXPORT_CDECL(transaction&) operator=(transaction const&);
MONGOCXX_ABI_EXPORT_CDECL(transaction&) operator=(transaction const& other);

///
/// Move constructs transaction options.
///
MONGOCXX_ABI_EXPORT_CDECL() transaction(transaction&&) noexcept;
transaction(transaction&& other) noexcept = default;

///
/// Move assigns transaction options.
///
MONGOCXX_ABI_EXPORT_CDECL(transaction&) operator=(transaction&&) noexcept;
transaction& operator=(transaction&& other) noexcept = default;

///
/// Destroys the transaction options.
///
MONGOCXX_ABI_EXPORT_CDECL() ~transaction() noexcept;
~transaction() noexcept = default;

///
/// Construct with the @ref mongocxx::v1 equivalent.
///
/* explicit(false) */ transaction(v1::transaction txn) : _txn{std::move(txn)} {}

///
/// Convert to the @ref mongocxx::v1 equivalent.
///
/// @par Postconditions:
/// - `*this` is in an assign-or-destroy-only state.
///
explicit operator v1::transaction() && {
return std::move(_txn);
}

///
/// Convert to the @ref mongocxx::v1 equivalent.
///
explicit operator v1::transaction() const& {
return _txn;
}

///
/// Sets the transaction read concern.
Expand All @@ -73,15 +107,15 @@ class transaction {
/// A reference to the object on which this member function is being called. This facilitates
/// method chaining.
///
MONGOCXX_ABI_EXPORT_CDECL(transaction&) read_concern(mongocxx::v_noabi::read_concern const& rc);
MONGOCXX_ABI_EXPORT_CDECL(transaction&) read_concern(v_noabi::read_concern const& rc);

///
/// Gets the current transaction read concern.
///
/// @return
/// An optional containing the read concern. If the read concern has not been set, a
/// disengaged optional is returned.
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional<mongocxx::v_noabi::read_concern>)
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional<v_noabi::read_concern>)
read_concern() const;

///
Expand All @@ -95,7 +129,7 @@ class transaction {
/// method chaining.
///
MONGOCXX_ABI_EXPORT_CDECL(transaction&)
write_concern(mongocxx::v_noabi::write_concern const& wc);
write_concern(v_noabi::write_concern const& wc);

///
/// Gets the current transaction write concern.
Expand All @@ -105,7 +139,7 @@ class transaction {
/// @return
/// An optional containing the write concern. If the write concern has not been set, a
/// disengaged optional is returned.
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional<mongocxx::v_noabi::write_concern>)
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional<v_noabi::write_concern>)
write_concern() const;

///
Expand All @@ -119,15 +153,15 @@ class transaction {
/// method chaining.
///
MONGOCXX_ABI_EXPORT_CDECL(transaction&)
read_preference(mongocxx::v_noabi::read_preference const& rp);
read_preference(v_noabi::read_preference const& rp);

///
/// Gets the current transaction read preference.
///
/// @return
/// An optional containing the read preference. If the read preference has not been set, a
/// disengaged optional is returned.
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional<mongocxx::v_noabi::read_preference>)
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional<v_noabi::read_preference>)
read_preference() const;

///
Expand All @@ -148,26 +182,41 @@ class transaction {
/// An optional containing the timeout. If the max commit time has not been set,
/// a disengaged optional is returned.
///
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional<std::chrono::milliseconds>)
max_commit_time_ms() const;

private:
friend ::mongocxx::v_noabi::client_session;

class impl;
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional<std::chrono::milliseconds>) max_commit_time_ms() const;

impl& _get_impl();
impl const& _get_impl() const;
std::unique_ptr<impl> _impl;
class internal;
};

} // namespace options
} // namespace v_noabi
} // namespace mongocxx

namespace mongocxx {
namespace v_noabi {

///
/// Convert to the @ref v_noabi equivalent of `v`.
///
inline v_noabi::options::transaction from_v1(v1::transaction v) {
return {std::move(v)};
}

///
/// Convert to the @ref mongocxx::v1 equivalent of `v`.
///
inline v1::transaction to_v1(v_noabi::options::transaction v) {
return v1::transaction{std::move(v)};
}

} // namespace v_noabi
} // namespace mongocxx

#include <mongocxx/config/postlude.hpp>

///
/// @file
/// Provides @ref mongocxx::v_noabi::options::transaction.
///
/// @par Includes
/// - @ref mongocxx/v1/transaction.hpp
///
125 changes: 124 additions & 1 deletion src/mongocxx/lib/mongocxx/v1/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,127 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <mongocxx/v1/transaction.hpp>
#include <mongocxx/v1/transaction.hh>

//

#include <bsoncxx/v1/stdx/optional.hpp>

#include <mongocxx/v1/read_concern.hh>
#include <mongocxx/v1/read_preference.hh>
#include <mongocxx/v1/write_concern.hh>

#include <chrono>

#include <mongocxx/private/mongoc.hh>
#include <mongocxx/private/utility.hh>

namespace mongocxx {
namespace v1 {

namespace {

mongoc_transaction_opt_t* to_mongoc(void* ptr) {
return static_cast<mongoc_transaction_opt_t*>(ptr);
}

} // namespace

transaction::~transaction() {
libmongoc::transaction_opts_destroy(to_mongoc(_impl));
}

transaction::transaction(transaction&& other) noexcept : _impl{exchange(other._impl, nullptr)} {}

transaction& transaction::operator=(transaction&& other) noexcept {
if (this != &other) {
libmongoc::transaction_opts_destroy(to_mongoc(exchange(_impl, exchange(other._impl, nullptr))));
}
return *this;
}

transaction::transaction(transaction const& other) : _impl{libmongoc::transaction_opts_clone(to_mongoc(other._impl))} {}

transaction& transaction::operator=(transaction const& other) {
if (this != &other) {
libmongoc::transaction_opts_destroy(
to_mongoc(exchange(_impl, libmongoc::transaction_opts_clone(to_mongoc(other._impl)))));
}
return *this;
}

transaction::transaction() : _impl{libmongoc::transaction_opts_new()} {}

transaction& transaction::max_commit_time_ms(std::chrono::milliseconds v) {
libmongoc::transaction_opts_set_max_commit_time_ms(to_mongoc(_impl), v.count());
return *this;
}

bsoncxx::v1::stdx::optional<std::chrono::milliseconds> transaction::max_commit_time_ms() const {
bsoncxx::v1::stdx::optional<std::chrono::milliseconds> ret;

// DEFAULT_MAX_COMMIT_TIME_MS (0) is equivalent to "unset".
if (auto const v = libmongoc::transaction_opts_get_max_commit_time_ms(to_mongoc(_impl))) {
ret.emplace(v);
}

return ret;
}

transaction& transaction::read_concern(v1::read_concern const& v) {
internal::set_read_concern(*this, v1::read_concern::internal::as_mongoc(v));
return *this;
}

bsoncxx::v1::stdx::optional<v1::read_concern> transaction::read_concern() const {
if (auto const rc = libmongoc::transaction_opts_get_read_concern(to_mongoc(_impl))) {
return v1::read_concern::internal::make(libmongoc::read_concern_copy(rc));
}

return {};
}

transaction& transaction::read_preference(v1::read_preference const& v) {
internal::set_read_preference(*this, v1::read_preference::internal::as_mongoc(v));
return *this;
}

bsoncxx::v1::stdx::optional<v1::read_preference> transaction::read_preference() const {
if (auto const rp = libmongoc::transaction_opts_get_read_prefs(to_mongoc(_impl))) {
return v1::read_preference::internal::make(libmongoc::read_prefs_copy(rp));
}

return {};
}

transaction& transaction::write_concern(v1::write_concern const& v) {
internal::set_write_concern(*this, v1::write_concern::internal::as_mongoc(v));
return *this;
}

bsoncxx::v1::stdx::optional<v1::write_concern> transaction::write_concern() const {
if (auto const wc = libmongoc::transaction_opts_get_write_concern(to_mongoc(_impl))) {
return v1::write_concern::internal::make(libmongoc::write_concern_copy(wc));
}

return {};
}

mongoc_transaction_opt_t const* transaction::internal::as_mongoc(transaction const& self) {
return to_mongoc(self._impl);
}

void transaction::internal::set_read_concern(transaction& self, mongoc_read_concern_t const* v) {
libmongoc::transaction_opts_set_read_concern(to_mongoc(self._impl), v);
}

void transaction::internal::set_read_preference(transaction& self, mongoc_read_prefs_t const* v) {
libmongoc::transaction_opts_set_read_prefs(to_mongoc(self._impl), v);
}

void transaction::internal::set_write_concern(transaction& self, mongoc_write_concern_t const* v) {
libmongoc::transaction_opts_set_write_concern(to_mongoc(self._impl), v);
}

} // namespace v1
} // namespace mongocxx
36 changes: 36 additions & 0 deletions src/mongocxx/lib/mongocxx/v1/transaction.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2009-present MongoDB, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <mongocxx/v1/transaction.hpp> // IWYU pragma: export

//

#include <mongocxx/private/mongoc.hh>

namespace mongocxx {
namespace v1 {

class transaction::internal {
public:
static mongoc_transaction_opt_t const* as_mongoc(transaction const& self);

static void set_read_concern(transaction& self, mongoc_read_concern_t const* v);
static void set_read_preference(transaction& self, mongoc_read_prefs_t const* v);
static void set_write_concern(transaction& self, mongoc_write_concern_t const* v);
};

} // namespace v1
} // namespace mongocxx
Loading