Couchbase C++ SDK 1.0.2 (rev. 51f4775)
Loading...
Searching...
No Matches
transactions Class Referenceabstract

The transactions object is used to initiate a transaction. More...

#include <couchbase/transactions.hxx>

Public Member Functions

virtual ~transactions ()=default
 
virtual auto run (txn_logic &&logic, const transaction_options &cfg) -> std::pair< error, transaction_result >=0
 Run a blocking transaction.
 
auto run (txn_logic &&logic) -> std::pair< error, transaction_result >
 
virtual void run (async_txn_logic &&logic, async_txn_complete_logic &&complete_callback, const transaction_options &cfg)=0
 Run an asynchronous transaction.
 
void run (async_txn_logic &&logic, async_txn_complete_logic &&complete_callback)
 

Detailed Description

The transactions object is used to initiate a transaction.

Examples
async_game_server.cxx, and game_server.cxx.

Constructor & Destructor Documentation

◆ ~transactions()

virtual ~transactions ( )
virtualdefault

Member Function Documentation

◆ run() [1/4]

void run ( async_txn_logic && logic,
async_txn_complete_logic && complete_callback )
inline

◆ run() [2/4]

virtual void run ( async_txn_logic && logic,
async_txn_complete_logic && complete_callback,
const transaction_options & cfg )
pure virtual

Run an asynchronous transaction.

You can supply a lambda or function which uses a yielded async_attempt_context to perform a transaction, where each transaction operation is asynchronous. A simple usage would be to get 3 document and replace the contents of each. In the example below, we get the 3 documents in parallel, and update each when the get returns the document. This can be significantly faster than getting each document in serial, and updating it using the blocking api:

// [4.1] create promise to retrieve result from the transaction
auto barrier = std::make_shared<std::promise<std::error_code>>();
auto f = barrier->get_future();
cluster.transactions()->run(
// [4.2] closure argument to run() method encapsulates logic, that has to be run in
// transaction
[=](std::shared_ptr<couchbase::transactions::async_attempt_context> ctx) -> couchbase::error {
// [4.3] get document
ctx->get(collection, id_1, [=](auto err_ctx_1, auto doc) {
if (err_ctx_1.ec()) {
fmt::print(
stderr, "failed to get document \"{}\": {}\n", id_1, err_ctx_1.ec().message());
return;
}
// [4.4] replace document's content
ctx->replace(doc,
tao::json::value{ { "some", "other async content" } },
[=](auto err_ctx_2, auto /*res*/) {
if (err_ctx_2.ec()) {
fmt::print(stderr,
"error replacing content in doc {}: {}\n",
id_1,
err_ctx_2.ec().message());
} else {
fmt::print("successfully replaced: {}\n", id_1);
}
});
});
ctx->get(collection, id_2, [=](auto err_ctx_1, auto doc) {
if (err_ctx_1.ec()) {
fmt::print("error getting doc {}: {}", id_2, err_ctx_1.ec().message());
return;
}
ctx->replace(doc,
tao::json::value{ { "some", "other async content" } },
[=](auto err_ctx_2, auto /*res*/) {
if (err_ctx_2.ec()) {
fmt::print(stderr,
"error replacing content in doc {}: {}\n",
id_2,
err_ctx_2.ec().message());
} else {
fmt::print("successfully replaced: {}\n", id_2);
}
});
});
ctx->get(collection, id_3, [=](auto err_ctx_1, auto doc) {
if (err_ctx_1.ec()) {
fmt::print(stderr, "error getting doc {}: {}\n", id_3, err_ctx_1.ec().message());
return;
}
ctx->replace(doc,
tao::json::value{ { "some", "other async content" } },
[=](auto err_ctx_2, auto /*res*/) {
if (err_ctx_2.ec()) {
fmt::print(stderr,
"error replacing content in doc {}: {}\n",
id_3,
err_ctx_2.ec().message());
} else {
fmt::print("successfully replaced: {}\n", id_3);
}
});
});
return {};
},
// [4.5], second closure represents transaction completion logic
[barrier](auto tx_err, auto tx_res) {
if (tx_err.ec()) {
fmt::print(stderr,
"error in async transaction {}, {}\n",
tx_res.transaction_id,
tx_err.ec().message());
}
barrier->set_value(tx_err.ec());
});
if (auto async_err = f.get()) {
fmt::print(stderr, "received async error from future: message - {}\n", async_err.message());
retval = 1;
}
Parameters
logica lambda or function which uses the yielded async_attempt_context to perform the desired transactional operations.
complete_callbacka lambda or function to which is yielded a {transaction_error_context} and {transaction_result}.
cfgif passed in, these options override the defaults, or those set in the cluster_options.

◆ run() [3/4]

auto run ( txn_logic && logic) -> std::pair<error, transaction_result>
inline

◆ run() [4/4]

virtual auto run ( txn_logic && logic,
const transaction_options & cfg ) -> std::pair< error, transaction_result >
pure virtual

Run a blocking transaction.

You can supply a lambda or function which uses a yielded attempt_context to perform a transaction, where each transaction operation is blocking. A simple usage would be to get a document and replace the contents:

auto [tx_err, tx_res] = cluster.transactions()->run(
// [3.1] closure argument to run() method encapsulates logic, that has to be run in
// transaction
[=](std::shared_ptr<couchbase::transactions::attempt_context> ctx) -> couchbase::error {
// [3.2] get document
auto [err_ctx, doc] = ctx->get(collection, id_1);
if (err_ctx.ec()) {
fmt::print(stderr, "failed to get document \"{}\": {}\n", id_1, err_ctx.ec().message());
// [3.3] don't continue the transaction logic
return {};
}
// [3.4] replace document's content
ctx->replace(doc, tao::json::value{ { "some", "other content" } });
return {};
});
// [3.5] check the overall status of the transaction
if (tx_err.ec()) {
fmt::print(stderr,
"error in transaction {}, cause: {}\n",
tx_err.ec().message(),
tx_err.cause().has_value() ? tx_err.cause().value().ec().message() : "");
retval = 1;
} else {
fmt::print("transaction {} completed successfully\n", tx_res.transaction_id);
}
Parameters
logica lambda or function which uses the yielded attempt_context to perform the desired transactional operations.
cfgif passed in, these options override the defaults, or those set in the cluster_options.
Returns
an error, and a transaction_result representing the results of the transaction.

The documentation for this class was generated from the following file: