Run an asynchronous transaction.
auto barrier = std::make_shared<std::promise<std::error_code>>();
auto f = barrier->get_future();
cluster.transactions()->run(
[=](std::shared_ptr<couchbase::transactions::async_attempt_context> ctx) ->
couchbase::error {
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;
}
ctx->replace(doc,
tao::json::value{ { "some", "other async content" } },
[=](auto err_ctx_2, auto ) {
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 ) {
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 ) {
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 {};
},
[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;
}