Skip to content

Commit

Permalink
feat: add a Commit() helper that takes the Mutations directly (go…
Browse files Browse the repository at this point in the history
…ogleapis/google-cloud-cpp-spanner#1319)

This convenience function saves the user from having to create a
small, but non-trivial lambda.

Fixes googleapis/google-cloud-cpp-spanner#1315.
  • Loading branch information
devbww authored Feb 28, 2020
1 parent 508770a commit 671efe5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions google/cloud/spanner/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ StatusOr<CommitResult> Client::Commit(
std::move(default_commit_backoff_policy));
}

StatusOr<CommitResult> Client::Commit(Mutations mutations) {
return Commit([&mutations](Transaction const&) { return mutations; });
}

StatusOr<CommitResult> Client::Commit(Transaction transaction,
Mutations mutations) {
return conn_->Commit({std::move(transaction), std::move(mutations)});
Expand Down
8 changes: 8 additions & 0 deletions google/cloud/spanner/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,14 @@ class Client {
StatusOr<CommitResult> Commit(
std::function<StatusOr<Mutations>(Transaction)> const& mutator);

/**
* Commits the given @p mutations atomically in order.
*
* This function uses the re-run loop described above with the default
* policies.
*/
StatusOr<CommitResult> Commit(Mutations mutations);

/**
* Commits a read-write transaction.
*
Expand Down
17 changes: 17 additions & 0 deletions google/cloud/spanner/client_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,23 @@ TEST(ClientTest, CommitMutatorPermanentFailure) {
EXPECT_EQ(1, commit_attempts); // no reruns
}

TEST(ClientTest, CommitMutations) {
auto conn = std::make_shared<MockConnection>();
auto mutation = MakeDeleteMutation("table", KeySet::All());
auto timestamp = internal::TimestampFromRFC3339("2020-02-28T04:49:17.335Z");
ASSERT_STATUS_OK(timestamp);
EXPECT_CALL(*conn, Commit(_))
.WillOnce([&mutation, &timestamp](Connection::CommitParams const& cp) {
EXPECT_EQ(cp.mutations, Mutations{mutation});
return CommitResult{*timestamp};
});

Client client(conn);
auto result = client.Commit({mutation});
EXPECT_STATUS_OK(result);
EXPECT_EQ(*timestamp, result->commit_timestamp);
}

MATCHER(DoesNotHaveSession, "not bound to a session") {
return internal::Visit(
arg, [&](internal::SessionHolder& session,
Expand Down

0 comments on commit 671efe5

Please sign in to comment.