Skip to content

Commit

Permalink
doc(spanner): add an example for CommitAtLeastOnce() (#11905)
Browse files Browse the repository at this point in the history
  • Loading branch information
devbww authored Jun 16, 2023
1 parent b49acc1 commit 019be5c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions google/cloud/spanner/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,9 @@ class Client {
* @note Prefer the `Commit` overloads if you want exactly-once semantics
* or want to reapply mutations after a `kAborted` error.
*
* @par Example
* @snippet samples.cc commit-at-least-once
*
* @param transaction_options Execute the commit in a temporary transaction
* with these options.
* @param mutations The mutations to be executed when this transaction
Expand Down
31 changes: 31 additions & 0 deletions google/cloud/spanner/samples/samples.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,33 @@ void UpdateData(google::cloud::spanner::Client client) {
}
//! [END spanner_update_data]

void DeleteDataAtLeastOnce(google::cloud::spanner::Client client) {
//! [commit-at-least-once]
namespace spanner = ::google::cloud::spanner;

// Delete the album with key (2,2) without automatic re-run (e.g., if the
// transaction was aborted) or replay protection, but using a single RPC.
auto commit_result = client.CommitAtLeastOnce(
spanner::Transaction::ReadWriteOptions(),
spanner::Mutations{
spanner::DeleteMutationBuilder(
"Albums", spanner::KeySet().AddKey(spanner::MakeKey(2, 2)))
.Build()});

if (commit_result) {
std::cout << "Delete was successful\n";
} else if (commit_result.status().code() ==
google::cloud::StatusCode::kNotFound) {
std::cout << "Delete was successful but seemingly replayed\n";
} else if (commit_result.status().code() ==
google::cloud::StatusCode::kAborted) {
std::cout << "Delete was aborted\n";
} else {
throw std::move(commit_result).status();
}
//! [commit-at-least-once]
}

//! [START spanner_delete_data]
void DeleteData(google::cloud::spanner::Client client) {
namespace spanner = ::google::cloud::spanner;
Expand Down Expand Up @@ -3946,6 +3973,7 @@ int RunOneCommand(std::vector<std::string> argv) {
{"create-client-with-query-options", CreateClientWithQueryOptionsCommand},
make_command_entry("insert-data", InsertData),
make_command_entry("update-data", UpdateData),
make_command_entry("delete-data-at-least-once", DeleteDataAtLeastOnce),
make_command_entry("delete-data", DeleteData),
make_command_entry("insert-datatypes-data", InsertDatatypesData),
make_command_entry("query-with-array-parameter", QueryWithArrayParameter),
Expand Down Expand Up @@ -4702,6 +4730,9 @@ void RunAll(bool emulator) {
SampleBanner("spanner_dml_standard_delete");
DmlStandardDelete(client);

SampleBanner("delete-data-at-least-once");
DeleteDataAtLeastOnce(client);

SampleBanner("spanner_delete_data");
DeleteData(client);

Expand Down

0 comments on commit 019be5c

Please sign in to comment.