Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.

Commit 4846e1c

Browse files
authored
style: use the re-running Commit() in the samples (#1009)
We recommend that users call the Commit() function that reruns the mutating functor after a read-write transaction aborts.
1 parent 2cbd53f commit 4846e1c

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

google/cloud/spanner/samples/samples.cc

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ google::cloud::spanner::Client MakeSampleClient(
698698
//! [START spanner_insert_data]
699699
void InsertData(google::cloud::spanner::Client client) {
700700
namespace spanner = google::cloud::spanner;
701+
using google::cloud::StatusOr;
701702
auto insert_singers = spanner::InsertMutationBuilder(
702703
"Singers", {"SingerId", "FirstName", "LastName"})
703704
.EmplaceRow(1, "Marc", "Richards")
@@ -716,9 +717,10 @@ void InsertData(google::cloud::spanner::Client client) {
716717
.EmplaceRow(2, 3, "Terrified")
717718
.Build();
718719

719-
auto commit_result =
720-
client.Commit(spanner::MakeReadWriteTransaction(),
721-
{std::move(insert_singers), std::move(insert_albums)});
720+
auto commit_result = client.Commit(
721+
[&insert_singers, &insert_albums](spanner::Transaction const&) {
722+
return spanner::Mutations{insert_singers, insert_albums};
723+
});
722724
if (!commit_result) {
723725
throw std::runtime_error(commit_result.status().message());
724726
}
@@ -729,13 +731,15 @@ void InsertData(google::cloud::spanner::Client client) {
729731
//! [START spanner_update_data]
730732
void UpdateData(google::cloud::spanner::Client client) {
731733
namespace spanner = google::cloud::spanner;
732-
auto commit_result =
733-
client.Commit(spanner::MakeReadWriteTransaction(),
734-
{spanner::UpdateMutationBuilder(
735-
"Albums", {"SingerId", "AlbumId", "MarketingBudget"})
736-
.EmplaceRow(1, 1, 100000)
737-
.EmplaceRow(2, 2, 500000)
738-
.Build()});
734+
using google::cloud::StatusOr;
735+
auto commit_result = client.Commit([](spanner::Transaction const&) {
736+
return spanner::Mutations{
737+
spanner::UpdateMutationBuilder(
738+
"Albums", {"SingerId", "AlbumId", "MarketingBudget"})
739+
.EmplaceRow(1, 1, 100000)
740+
.EmplaceRow(2, 2, 500000)
741+
.Build()};
742+
});
739743
if (!commit_result) {
740744
throw std::runtime_error(commit_result.status().message());
741745
}
@@ -746,6 +750,7 @@ void UpdateData(google::cloud::spanner::Client client) {
746750
//! [START spanner_delete_data]
747751
void DeleteData(google::cloud::spanner::Client client) {
748752
namespace spanner = google::cloud::spanner;
753+
using google::cloud::StatusOr;
749754

750755
// Delete each of the albums by individual key, then delete all the singers
751756
// using a key range.
@@ -763,8 +768,10 @@ void DeleteData(google::cloud::spanner::Client client) {
763768
spanner::MakeKeyBoundClosed(5)))
764769
.Build();
765770

766-
auto commit_result = client.Commit(spanner::MakeReadWriteTransaction(),
767-
{delete_albums, delete_singers});
771+
auto commit_result = client.Commit(
772+
[&delete_albums, &delete_singers](spanner::Transaction const&) {
773+
return spanner::Mutations{delete_albums, delete_singers};
774+
});
768775
if (!commit_result) {
769776
throw std::runtime_error(commit_result.status().message());
770777
}
@@ -834,8 +841,8 @@ void ReadWriteTransaction(google::cloud::spanner::Client client) {
834841
};
835842

836843
auto commit = client.Commit(
837-
[&client, &get_current_budget](spanner::Transaction const& txn)
838-
-> google::cloud::StatusOr<spanner::Mutations> {
844+
[&client, &get_current_budget](
845+
spanner::Transaction const& txn) -> StatusOr<spanner::Mutations> {
839846
auto b1 = get_current_budget(client, txn, 1, 1);
840847
if (!b1) return std::move(b1).status();
841848
auto b2 = get_current_budget(client, txn, 2, 2);
@@ -942,15 +949,17 @@ void DmlPartitionedUpdate(google::cloud::spanner::Client client) {
942949
//! [START spanner_write_data_for_struct_queries]
943950
void WriteDataForStructQueries(google::cloud::spanner::Client client) {
944951
namespace spanner = google::cloud::spanner;
945-
auto commit_result =
946-
client.Commit(spanner::MakeReadWriteTransaction(),
947-
{spanner::InsertMutationBuilder(
948-
"Singers", {"SingerId", "FirstName", "LastName"})
949-
.EmplaceRow(6, "Elena", "Campbell")
950-
.EmplaceRow(7, "Gabriel", "Wright")
951-
.EmplaceRow(8, "Benjamin", "Martinez")
952-
.EmplaceRow(9, "Hannah", "Harris")
953-
.Build()});
952+
using google::cloud::StatusOr;
953+
auto commit_result = client.Commit([](spanner::Transaction const&) {
954+
return spanner::Mutations{
955+
spanner::InsertMutationBuilder("Singers",
956+
{"SingerId", "FirstName", "LastName"})
957+
.EmplaceRow(6, "Elena", "Campbell")
958+
.EmplaceRow(7, "Gabriel", "Wright")
959+
.EmplaceRow(8, "Benjamin", "Martinez")
960+
.EmplaceRow(9, "Hannah", "Harris")
961+
.Build()};
962+
});
954963
if (!commit_result) {
955964
throw std::runtime_error(commit_result.status().message());
956965
}

0 commit comments

Comments
 (0)