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

The async_attempt_context is used for all asynchronous transaction operations. More...

#include <couchbase/transactions/async_attempt_context.hxx>

Public Member Functions

virtual void get (const collection &coll, std::string id, async_result_handler &&handler)=0
 Get document from a collection.
 
virtual void get_replica_from_preferred_server_group (const couchbase::collection &coll, const std::string &id, async_result_handler &&handler)=0
 Get a document copy from the selected server group.
 
virtual void remove (transaction_get_result doc, async_err_handler &&handler)=0
 Remove a document from a collection.
 
template<typename Transcoder = codec::default_json_transcoder, typename Document , std::enable_if_t<!std::is_same_v< codec::encoded_value, Document >, bool > = true>
void insert (const collection &coll, std::string id, Document &&content, async_result_handler &&handler)
 Insert a document into a collection.
 
template<typename Transcoder = codec::default_json_transcoder, typename Document , std::enable_if_t<!std::is_same_v< codec::encoded_value, Document >, bool > = true>
void replace (transaction_get_result doc, Document &&content, async_result_handler &&handler)
 Replace the contents of a document in a collection.
 
void query (const scope &scope, std::string statement, transaction_query_options opts, async_query_handler &&handler)
 Perform a query, within a scope.
 
void query (std::string statement, transaction_query_options opts, async_query_handler &&handler)
 Perform a query.
 
void query (std::string statement, async_query_handler &&handler)
 Perform a query.
 
virtual ~async_attempt_context ()=default
 

Detailed Description

The async_attempt_context is used for all asynchronous transaction operations.

In the example below, we get 3 documents in parallel, and update each when the get returns the document:

// [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;
}

Constructor & Destructor Documentation

◆ ~async_attempt_context()

virtual ~async_attempt_context ( )
virtualdefault

Member Function Documentation

◆ get()

virtual void get ( const collection & coll,
std::string id,
async_result_handler && handler )
pure virtual

Get document from a collection.

Fetch the document contents, in the form of a transaction_get_result. This can be used in subsequent calls to async_attempt_context::replace or async_attempt_context::remove

Parameters
collThe collection which contains the document.
idThe document id which is used to uniquely identify it.
handlerThe handler which implements async_result_handler

◆ get_replica_from_preferred_server_group()

virtual void get_replica_from_preferred_server_group ( const couchbase::collection & coll,
const std::string & id,
async_result_handler && handler )
pure virtual

Get a document copy from the selected server group.

Fetch the document contents, in the form of a transaction_get_result. It might be either replica or active copy of the document. One of the use cases for this method is to save on network costs by deploying SDK in the same availability zone as corresponding server group of the nodes.

Parameters
collThe collection which contains the document.
idThe unique id of the document.
handlerThe handler which implements async_result_handler

Select preferred server group in connection options:

cluster_options.network().preferred_server_group(selected_server_group);

Fetch document from the nodes that belong to selected server group only:

auto barrier = std::make_shared<std::promise<std::error_code>>();
auto f = barrier->get_future();
cluster.transactions()->run(
// transaction logic
[=](std::shared_ptr<couchbase::transactions::async_attempt_context> ctx) -> couchbase::error {
ctx->get_replica_from_preferred_server_group(collection, id, [=](auto err_ctx, auto doc) {
if (err_ctx.ec()) {
fmt::print(stderr, "failed to get document \"{}\": {}\n", id, err_ctx.ec().message());
return;
}
fmt::println("document content: {}",
tao::json::to_string(doc.template content_as<tao::json::value>()));
});
return {};
},
// 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;
}
See also
network_options::preferred_server_group
https://docs.couchbase.com/server/current/manage/manage-groups/manage-groups.html

◆ insert()

template<typename Transcoder = codec::default_json_transcoder, typename Document , std::enable_if_t<!std::is_same_v< codec::encoded_value, Document >, bool > = true>
void insert ( const collection & coll,
std::string id,
Document && content,
async_result_handler && handler )
inline

Insert a document into a collection.

Given an id and the content, this inserts a new document into a collection. Note that currently this content can be either a <std::vector<std::byte>> or an object which can be serialized with the codec::tao_json_serializer.

Template Parameters
Contenttype of the document.
Parameters
collCollection to insert the document into.
idThe document id.
contentThe content of the document.
handlerThe handler which implements async_result_handler

◆ query() [1/3]

void query ( const scope & scope,
std::string statement,
transaction_query_options opts,
async_query_handler && handler )

Perform a query, within a scope.

Performs a query given a specific scope. Note that all subsequent transaction operations will be handled by the query service.

Parameters
scopeScope for the query.
statementThe query statement
optsOptions for the query
handlerHandler which implements async_query_handler.

◆ query() [2/3]

void query ( std::string statement,
async_query_handler && handler )
inline

Perform a query.

Performs an unscoped query.

Parameters
statementThe query statement.
handlerHandler which implements async_query_handler

◆ query() [3/3]

void query ( std::string statement,
transaction_query_options opts,
async_query_handler && handler )
inline

Perform a query.

Performs an unscoped query.

Parameters
statementThe query statement.
optsOptions for the query.
handlerHandler which implements async_query_handler.

◆ remove()

virtual void remove ( transaction_get_result doc,
async_err_handler && handler )
pure virtual

Remove a document from a collection.

Removes a document from a collection, where the document was gotten from a previous call to async_attempt_context::get

Parameters
docThe document to remove.
handlerThe handler which implements async_err_handler

◆ replace()

template<typename Transcoder = codec::default_json_transcoder, typename Document , std::enable_if_t<!std::is_same_v< codec::encoded_value, Document >, bool > = true>
void replace ( transaction_get_result doc,
Document && content,
async_result_handler && handler )
inline

Replace the contents of a document in a collection.

Replaces the contents of an existing document. Note that currently this content can be either a <std::vector<std::byte>> or an object which can be serialized with the codec::tao_json_serializer.

Template Parameters
Contenttype of the document
Parameters
docDocument whose content will be replaced. This is gotten from a call to async_attempt_context::get
contentNew content of the document
handlerThe handler which implements async_result_handler

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