google-cloud-cpp icon indicating copy to clipboard operation
google-cloud-cpp copied to clipboard

Support `std::function<Status(Transaction)>` in `Commit()`

Open coryan opened this issue 2 years ago • 3 comments

The spanner::Client::Commit() function consumes a std::function<StatusOr<Mutations>(Transaction)> mutator parameter:

https://github.com/googleapis/google-cloud-cpp/blob/e84a574ad5c761a4cc771a9b1f1102077fb6ec66/google/cloud/spanner/client.h#L749-L750

Applications using just SQL statements may wonder why they need to return an empty set of spanner::Mutations from the Commit() mutator. If they had no intention of using Mutations they could just return Status{} on success.

coryan avatar Apr 01 '22 18:04 coryan

Nice to have, not urgent. Let's plan for 2022-Q4

coryan avatar Aug 18 '22 18:08 coryan

If they had no intention of using Mutations they could just return Status{} on success.

You can't assign an OK Status to a StatusOr<T>. So, the logical way forward here would be to introduce a new Client::Commit() overload ...

  StatusOr<CommitResult> Commit(
      std::function<Status(Transaction)> const& mutator, Options opts = {});

which could then be called with something like ...

  auto commit =
      client.Commit([&](google::cloud::spanner::Transaction const& txn) {
        auto result = client.Op(txn, ...);
        if (!result) return std::move(result).status();
        // ... etc
        return google::cloud::Status{};
      });

Unfortunately such a call is ambiguous with the existing

  StatusOr<CommitResult> Commit(
      std::function<StatusOr<Mutations>(Transaction)> const& mutator,
      Options opts = {});

overload, so something more complicated would be required, which kind of defeats the purpose.

Applications using just SQL statements may wonder why they need to return an empty set of spanner::Mutations from the Commit() mutator.

I'm not sure why they need wonder too much, or be confused by it. They're just saying that there are no additional mutations.

devbww avatar Aug 30 '22 02:08 devbww

Applications using just SQL statements may wonder why they need to return an empty set of spanner::Mutations from the Commit() mutator.

I'm not sure why they need wonder too much, or be confused by it. They're just saying that there are no additional mutations.

People should only learn as many new concepts as needed to use the library. It seems unfortunate that to use the SQL interface I would need to learn about the spanner::Mutations too, even if I do not plan to use them.

coryan avatar Aug 30 '22 12:08 coryan

After some thought, we don't think this is needed.

coryan avatar Feb 01 '23 19:02 coryan