Google Cloud Spanner C++ Client
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 
16 #include "google/cloud/spanner/internal/session.h"
17 #include "google/cloud/spanner/internal/transaction_impl.h"
18 #include "google/cloud/internal/make_unique.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 
53 Transaction::ReadOnlyOptions::ReadOnlyOptions() {
54  ro_opts_.set_strong(true); // only presence matters, not value
55  ro_opts_.set_return_read_timestamp(true);
56 }
57 
58 Transaction::ReadOnlyOptions::ReadOnlyOptions(Timestamp read_timestamp) {
59  *ro_opts_.mutable_read_timestamp() =
60  internal::TimestampToProto(read_timestamp);
61  ro_opts_.set_return_read_timestamp(true);
62 }
63 
64 Transaction::ReadOnlyOptions::ReadOnlyOptions(
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 
72 Transaction::SingleUseOptions::SingleUseOptions(ReadOnlyOptions opts) {
73  ro_opts_ = std::move(opts.ro_opts_);
74 }
75 
76 Transaction::SingleUseOptions::SingleUseOptions(Timestamp min_read_timestamp) {
77  *ro_opts_.mutable_min_read_timestamp() =
78  internal::TimestampToProto(min_read_timestamp);
79  ro_opts_.set_return_read_timestamp(true);
80 }
81 
82 Transaction::SingleUseOptions::SingleUseOptions(
83  std::chrono::nanoseconds max_staleness) {
84  *ro_opts_.mutable_max_staleness() = ToProto(max_staleness);
85  ro_opts_.set_return_read_timestamp(true);
86 }
87 
88 Transaction::Transaction(ReadOnlyOptions opts) {
89  google::spanner::v1::TransactionSelector selector;
90  *selector.mutable_begin() = MakeOpts(std::move(opts.ro_opts_));
91  impl_ = std::make_shared<internal::TransactionImpl>(std::move(selector));
92 }
93 
94 Transaction::Transaction(ReadWriteOptions opts) {
95  google::spanner::v1::TransactionSelector selector;
96  *selector.mutable_begin() = MakeOpts(std::move(opts.rw_opts_));
97  impl_ = std::make_shared<internal::TransactionImpl>(std::move(selector));
98 }
99 
100 Transaction::Transaction(Transaction const& txn, ReadWriteOptions opts) {
101  google::spanner::v1::TransactionSelector selector;
102  *selector.mutable_begin() = MakeOpts(std::move(opts.rw_opts_));
103  impl_ = std::make_shared<internal::TransactionImpl>(*txn.impl_,
104  std::move(selector));
105 }
106 
107 Transaction::Transaction(SingleUseOptions opts) {
108  google::spanner::v1::TransactionSelector selector;
109  *selector.mutable_single_use() = MakeOpts(std::move(opts.ro_opts_));
110  impl_ = std::make_shared<internal::TransactionImpl>(std::move(selector));
111 }
112 
113 Transaction::Transaction(std::string session_id, std::string transaction_id) {
114  google::spanner::v1::TransactionSelector selector;
115  selector.set_id(std::move(transaction_id));
116  impl_ = std::make_shared<internal::TransactionImpl>(
117  internal::MakeDissociatedSessionHolder(std::move(session_id)),
118  std::move(selector));
119 }
120 
121 Transaction::~Transaction() = default;
122 
123 namespace internal {
124 
125 Transaction MakeTransactionFromIds(std::string session_id,
126  std::string transaction_id) {
127  return Transaction(std::move(session_id), std::move(transaction_id));
128 }
129 
130 } // namespace internal
131 
132 } // namespace SPANNER_CLIENT_NS
133 } // namespace spanner
134 } // namespace cloud
135 } // namespace google
Contains all the Cloud Spanner C++ client types and functions.
#define SPANNER_CLIENT_NS
Definition: version.h:22
The representation of a Cloud Spanner transaction.
Definition: transaction.h:63
A representation of the Spanner TIMESTAMP type: An instant in time.
Definition: timestamp.h:73