Google Cloud Spanner C++ Client  1.32.0
A C++ Client Library for Google Cloud Spanner
transaction.cc
Go to the documentation of this file.
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
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>
20 
21 namespace google {
22 namespace cloud {
23 namespace spanner {
24 inline namespace SPANNER_CLIENT_NS {
25 
26 namespace {
27 
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()));
34  return proto;
35 }
36 
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);
41  return opts;
42 }
43 
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);
48  return opts;
49 }
50 
51 } // namespace
52 
54  ro_opts_.set_strong(true); // only presence matters, not value
55  ro_opts_.set_return_read_timestamp(true);
56 }
57 
59  *ro_opts_.mutable_read_timestamp() =
60  read_timestamp.get<protobuf::Timestamp>().value();
61  ro_opts_.set_return_read_timestamp(true);
62 }
63 
65  std::chrono::nanoseconds exact_staleness) {
66  *ro_opts_.mutable_exact_staleness() = ToProto(exact_staleness);
67  ro_opts_.set_return_read_timestamp(true);
68 }
69 
70 Transaction::ReadWriteOptions::ReadWriteOptions() = default; // currently none
71 
73  absl::optional<std::string> tag) {
74  tag_ = std::move(tag);
75  return *this;
76 }
77 
79  ro_opts_ = std::move(opts.ro_opts_);
80 }
81 
83  *ro_opts_.mutable_min_read_timestamp() =
84  min_read_timestamp.get<protobuf::Timestamp>().value();
85  ro_opts_.set_return_read_timestamp(true);
86 }
87 
89  std::chrono::nanoseconds max_staleness) {
90  *ro_opts_.mutable_max_staleness() = ToProto(max_staleness);
91  ro_opts_.set_return_read_timestamp(true);
92 }
93 
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());
99 }
100 
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()));
106 }
107 
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()));
114 }
115 
116 Transaction::Transaction(SingleUseOptions opts) {
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());
121 }
122 
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));
130 }
131 
132 Transaction::~Transaction() = default;
133 
134 } // namespace SPANNER_CLIENT_NS
135 } // namespace spanner
136 
137 namespace spanner_internal {
138 inline namespace SPANNER_CLIENT_NS {
139 
140 spanner::Transaction TransactionInternals::MakeTransactionFromIds(
141  std::string session_id, std::string transaction_id,
142  std::string transaction_tag) {
143  return spanner::Transaction(std::move(session_id), std::move(transaction_id),
144  std::move(transaction_tag));
145 }
146 
147 } // namespace SPANNER_CLIENT_NS
148 } // namespace spanner_internal
149 } // namespace cloud
150 } // namespace google