Couchbase Transactions C++ Client  1.0.0
Transactions client for couchbase
Public Member Functions | Protected Member Functions | List of all members
couchbase::transactions::attempt_context Class Referenceabstract

Provides methods to perform transactional operations. More...

#include <attempt_context.hxx>

Public Member Functions

virtual transaction_get_result get (std::shared_ptr< collection > collection, const std::string &id)=0
 
virtual boost::optional< transaction_get_resultget_optional (std::shared_ptr< collection > collection, const std::string &id)=0
 
template<typename Content >
transaction_get_result replace (std::shared_ptr< collection > collection, const transaction_get_result &document, const Content &content)
 
template<typename Content >
transaction_get_result insert (std::shared_ptr< collection > collection, const std::string &id, const Content &content)
 
virtual void remove (std::shared_ptr< couchbase::collection > collection, transaction_get_result &document)=0
 
virtual void commit ()=0
 
virtual void rollback ()=0
 

Protected Member Functions

virtual transaction_get_result insert_raw (std::shared_ptr< collection > collection, const std::string &id, const nlohmann::json &content)=0
 
virtual transaction_get_result replace_raw (std::shared_ptr< collection > collection, const transaction_get_result &document, const nlohmann::json &content)=0
 

Detailed Description

Provides methods to perform transactional operations.

An attempt_context object makes all the transactional kv operations available. Note they can throw a transaction_operation_failed exception, which needs to NOT be caught, or if caught, rethrown, for transactions to work properly.

Examples:
examples/game_server.cxx.

Member Function Documentation

◆ commit()

virtual void couchbase::transactions::attempt_context::commit ( )
pure virtual

Commits the transaction. All staged replaces, inserts and removals will be written.

After this, no further operations are permitted on this instance, and they will result in an exception that will, if not caught in the transaction logic, cause the transaction to fail.

Exceptions
transaction_operation_failedwhich either should not be caught by the lambda, or rethrown if it is caught.

◆ get()

virtual transaction_get_result couchbase::transactions::attempt_context::get ( std::shared_ptr< collection collection,
const std::string &  id 
)
pure virtual

Gets a document from the specified Couchbase collection matching the specified id.

Parameters
collectionthe Couchbase collection the document exists on
idthe document's ID
Returns
an TransactionDocument containing the document
Exceptions
transaction_operation_failedwhich either should not be caught by the lambda, or rethrown if it is caught.
Examples:
examples/game_server.cxx.

◆ get_optional()

virtual boost::optional<transaction_get_result> couchbase::transactions::attempt_context::get_optional ( std::shared_ptr< collection collection,
const std::string &  id 
)
pure virtual

Gets a document from the specified Couchbase collection matching the specified id.

Parameters
collectionthe Couchbase collection the document exists on
idthe document's ID
Returns
a TransactionDocument containing the document, if it exists.
Exceptions
transaction_operation_failedwhich either should not be caught by the lambda, or rethrown if it is caught.

◆ insert()

template<typename Content >
transaction_get_result couchbase::transactions::attempt_context::insert ( std::shared_ptr< collection collection,
const std::string &  id,
const Content &  content 
)
inline

Inserts a new document into the specified Couchbase collection.

As with replace, the insert is staged until the transaction is committed. Due to technical limitations it is not as possible to completely hide the staged data from the rest of the Couchbase platform, as an empty document must be created.

This staged data effectively locks the document from other transactional writes until the attempt completes (commits or rolls back).

Parameters
collectionthe Couchbase collection in which to insert the doc
idthe document's unique ID
contentthe content to insert
Returns
the doc, updated with its new CAS value and ID, and converted to a TransactionDocument
Exceptions
transaction_operation_failedwhich either should not be caught by the lambda, or rethrown if it is caught.

◆ insert_raw()

virtual transaction_get_result couchbase::transactions::attempt_context::insert_raw ( std::shared_ptr< collection collection,
const std::string &  id,
const nlohmann::json &  content 
)
protectedpure virtual
Stability
Internal:

◆ remove()

virtual void couchbase::transactions::attempt_context::remove ( std::shared_ptr< couchbase::collection collection,
transaction_get_result document 
)
pure virtual

Removes the specified document, using the document's last TransactionDocument::cas

As with replace, the remove is staged until the transaction is committed. That is, the document will continue to exist, and the rest of the Couchbase platform will continue to see it.

This staged data effectively locks the document from other transactional writes until the attempt completes (commits or rolls back).

Parameters
documentthe document to be removed
Exceptions
transaction_operation_failedwhich either should not be caught by the lambda, or rethrown if it is caught.
Examples:
examples/game_server.cxx.

◆ replace()

template<typename Content >
transaction_get_result couchbase::transactions::attempt_context::replace ( std::shared_ptr< collection collection,
const transaction_get_result document,
const Content &  content 
)
inline

Mutates the specified document with new content, using the document's last TransactionDocument::cas().

The mutation is staged until the transaction is committed. That is, any read of the document by any Couchbase component will see the document's current value, rather than this staged or 'dirty' data. If the attempt is rolled back, the staged mutation will be removed.

This staged data effectively locks the document from other transactional writes until the attempt completes (commits or rolls back).

If the mutation fails, the transaction will automatically rollback this attempt, then retry.

Parameters
documentthe doc to be updated
contentthe content to replace the doc with.
Returns
the document, updated with its new CAS value.
Exceptions
transaction_operation_failedwhich either should not be caught by the lambda, or rethrown if it is caught.
Examples:
examples/game_server.cxx.

◆ replace_raw()

virtual transaction_get_result couchbase::transactions::attempt_context::replace_raw ( std::shared_ptr< collection collection,
const transaction_get_result document,
const nlohmann::json &  content 
)
protectedpure virtual
Stability
Internal:

◆ rollback()

virtual void couchbase::transactions::attempt_context::rollback ( )
pure virtual

Rollback the transaction. All staged mutations will be unstaged.

Typically, this is called internally to rollback transaction when errors occur in the lambda. Though it can be called explicitly from the app logic within the transaction as well, perhaps that is better modeled as a custom exception that you raise instead.

Exceptions
transaction_operation_failedwhich either should not be caught by the lambda, or rethrown if it is caught.

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