15 #include "google/cloud/spanner/transaction.h"
16 #include "google/cloud/spanner/internal/session.h"
17 #include "google/cloud/spanner/internal/transaction_impl.h"
18 #include "absl/memory/memory.h"
19 #include <google/protobuf/duration.pb.h>
28 google::protobuf::Duration ToProto(std::chrono::nanoseconds ns) {
29 auto s = std::chrono::duration_cast<std::chrono::seconds>(ns);
30 ns -= std::chrono::duration_cast<std::chrono::nanoseconds>(s);
31 google::protobuf::Duration proto;
32 proto.set_seconds(s.count());
33 proto.set_nanos(
static_cast<
int>(ns.count()));
37 google::spanner::v1::TransactionOptions MakeOpts(
38 google::spanner::v1::TransactionOptions_ReadOnly ro_opts) {
39 google::spanner::v1::TransactionOptions opts;
40 *opts.mutable_read_only() = std::move(ro_opts);
44 google::spanner::v1::TransactionOptions MakeOpts(
45 google::spanner::v1::TransactionOptions_ReadWrite rw_opts) {
46 google::spanner::v1::TransactionOptions opts;
47 *opts.mutable_read_write() = std::move(rw_opts);
54 ro_opts_.set_strong(
true);
55 ro_opts_.set_return_read_timestamp(
true);
59 *ro_opts_.mutable_read_timestamp() =
61 ro_opts_.set_return_read_timestamp(
true);
65 std::chrono::nanoseconds exact_staleness) {
66 *ro_opts_.mutable_exact_staleness() = ToProto(exact_staleness);
67 ro_opts_.set_return_read_timestamp(
true);
73 absl::optional<std::string> tag) {
74 tag_ = std::move(tag);
79 ro_opts_ = std::move(opts.ro_opts_);
83 *ro_opts_.mutable_min_read_timestamp() =
85 ro_opts_.set_return_read_timestamp(
true);
89 std::chrono::nanoseconds max_staleness) {
90 *ro_opts_.mutable_max_staleness() = ToProto(max_staleness);
91 ro_opts_.set_return_read_timestamp(
true);
95 google::spanner::v1::TransactionSelector selector;
96 *selector.mutable_begin() = MakeOpts(std::move(opts.ro_opts_));
97 impl_ = std::make_shared<spanner_internal::TransactionImpl>(
98 std::move(selector), std::string());
102 google::spanner::v1::TransactionSelector selector;
103 *selector.mutable_begin() = MakeOpts(std::move(opts.rw_opts_));
104 impl_ = std::make_shared<spanner_internal::TransactionImpl>(
105 std::move(selector), std::move(opts.tag_).value_or(std::string()));
109 google::spanner::v1::TransactionSelector selector;
110 *selector.mutable_begin() = MakeOpts(std::move(opts.rw_opts_));
111 impl_ = std::make_shared<spanner_internal::TransactionImpl>(
112 *txn.impl_, std::move(selector),
113 std::move(opts.tag_).value_or(std::string()));
117 google::spanner::v1::TransactionSelector selector;
118 *selector.mutable_single_use() = MakeOpts(std::move(opts.ro_opts_));
119 impl_ = std::make_shared<spanner_internal::TransactionImpl>(
120 std::move(selector), std::string());
123 Transaction::Transaction(std::string session_id, std::string transaction_id,
124 std::string transaction_tag) {
125 google::spanner::v1::TransactionSelector selector;
126 selector.set_id(std::move(transaction_id));
127 impl_ = std::make_shared<spanner_internal::TransactionImpl>(
128 spanner_internal::MakeDissociatedSessionHolder(std::move(session_id)),
129 std::move(selector), std::move(transaction_tag));
137 namespace spanner_internal {
141 std::string session_id, std::string transaction_id,
142 std::string transaction_tag) {
144 std::move(transaction_tag));