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

Commit 4375133

Browse files
authored
feat: add a Commit() helper that takes the Mutations directly (#1319)
This convenience function saves the user from having to create a small, but non-trivial lambda. Fixes #1315.
1 parent 6359a20 commit 4375133

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

google/cloud/spanner/client.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ StatusOr<CommitResult> Client::Commit(
251251
std::move(default_commit_backoff_policy));
252252
}
253253

254+
StatusOr<CommitResult> Client::Commit(Mutations mutations) {
255+
return Commit([&mutations](Transaction const&) { return mutations; });
256+
}
257+
254258
StatusOr<CommitResult> Client::Commit(Transaction transaction,
255259
Mutations mutations) {
256260
return conn_->Commit({std::move(transaction), std::move(mutations)});

google/cloud/spanner/client.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,14 @@ class Client {
480480
StatusOr<CommitResult> Commit(
481481
std::function<StatusOr<Mutations>(Transaction)> const& mutator);
482482

483+
/**
484+
* Commits the given @p mutations atomically in order.
485+
*
486+
* This function uses the re-run loop described above with the default
487+
* policies.
488+
*/
489+
StatusOr<CommitResult> Commit(Mutations mutations);
490+
483491
/**
484492
* Commits a read-write transaction.
485493
*

google/cloud/spanner/client_test.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,23 @@ TEST(ClientTest, CommitMutatorPermanentFailure) {
684684
EXPECT_EQ(1, commit_attempts); // no reruns
685685
}
686686

687+
TEST(ClientTest, CommitMutations) {
688+
auto conn = std::make_shared<MockConnection>();
689+
auto mutation = MakeDeleteMutation("table", KeySet::All());
690+
auto timestamp = internal::TimestampFromRFC3339("2020-02-28T04:49:17.335Z");
691+
ASSERT_STATUS_OK(timestamp);
692+
EXPECT_CALL(*conn, Commit(_))
693+
.WillOnce([&mutation, &timestamp](Connection::CommitParams const& cp) {
694+
EXPECT_EQ(cp.mutations, Mutations{mutation});
695+
return CommitResult{*timestamp};
696+
});
697+
698+
Client client(conn);
699+
auto result = client.Commit({mutation});
700+
EXPECT_STATUS_OK(result);
701+
EXPECT_EQ(*timestamp, result->commit_timestamp);
702+
}
703+
687704
MATCHER(DoesNotHaveSession, "not bound to a session") {
688705
return internal::Visit(
689706
arg, [&](internal::SessionHolder& session,

0 commit comments

Comments
 (0)