Google Cloud Spanner C++ Client
A C++ Client Library for Google Cloud Spanner
transaction.h
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 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_TRANSACTION_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_TRANSACTION_H
17 
18 #include "google/cloud/spanner/internal/transaction_impl.h"
21 #include <google/spanner/v1/transaction.pb.h>
22 #include <chrono>
23 #include <memory>
24 #include <string>
25 
26 namespace google {
27 namespace cloud {
28 namespace spanner {
29 inline namespace SPANNER_CLIENT_NS {
30 
31 class Transaction; // defined below
32 
33 // Internal forward declarations to befriend.
34 namespace internal {
35 template <typename T>
36 Transaction MakeSingleUseTransaction(T&&);
37 template <typename Functor>
38 VisitInvokeResult<Functor> Visit(Transaction, Functor&&);
39 Transaction MakeTransactionFromIds(std::string session_id,
40  std::string transaction_id);
41 } // namespace internal
42 
63 class Transaction {
64  public:
69  public:
70  // Strong: Guarantees visibility of the effects of all transactions that
71  // committed before the start of the reads.
73 
74  // Exact Staleness: Executes all reads at `read_timestamp`.
75  explicit ReadOnlyOptions(Timestamp read_timestamp);
76 
77  // Exact Staleness: Executes all reads at a timestamp `exact_staleness`
78  // old. The actual timestamp is chosen soon after the reads are started.
79  explicit ReadOnlyOptions(std::chrono::nanoseconds exact_staleness);
80 
81  private:
82  friend Transaction;
83  google::spanner::v1::TransactionOptions_ReadOnly ro_opts_;
84  };
85 
90  public:
91  // There are currently no read-write options.
93 
94  private:
95  friend Transaction;
96  google::spanner::v1::TransactionOptions_ReadWrite rw_opts_;
97  };
98 
110  public:
111  // Strong or Exact Staleness: See ReadOnlyOptions.
112  // NOLINTNEXTLINE(google-explicit-constructor)
114 
115  // Bounded Staleness: Executes all reads at a timestamp that is not
116  // before `min_read_timestamp`.
117  explicit SingleUseOptions(Timestamp min_read_timestamp);
118 
119  // Bounded Staleness: Executes all reads at a timestamp that is not
120  // before `NOW - max_staleness`.
121  explicit SingleUseOptions(std::chrono::nanoseconds max_staleness);
122 
123  private:
124  friend Transaction;
125  google::spanner::v1::TransactionOptions_ReadOnly ro_opts_;
126  };
127 
130 
136  explicit Transaction(ReadOnlyOptions opts);
138  explicit Transaction(ReadWriteOptions opts);
140  Transaction(Transaction const& txn, ReadWriteOptions opts);
142 
143  ~Transaction();
144 
147  Transaction(Transaction&&) = default;
148  Transaction& operator=(Transaction&&) = default;
149  Transaction(Transaction const&) = default;
150  Transaction& operator=(Transaction const&) = default;
152 
155  friend bool operator==(Transaction const& a, Transaction const& b) {
156  return a.impl_ == b.impl_;
157  }
158  friend bool operator!=(Transaction const& a, Transaction const& b) {
159  return !(a == b);
160  }
162 
163  private:
164  // Friendship for access by internal helpers.
165  template <typename T>
166  friend Transaction internal::MakeSingleUseTransaction(T&&);
167  template <typename Functor>
168  friend internal::VisitInvokeResult<Functor> internal::Visit(Transaction,
169  Functor&&);
170  friend Transaction internal::MakeTransactionFromIds(
171  std::string session_id, std::string transaction_id);
172 
173  // Construction of a single-use transaction.
174  explicit Transaction(SingleUseOptions opts);
175  // Construction of a transaction with existing IDs.
176  Transaction(std::string session_id, std::string transaction_id);
177 
178  std::shared_ptr<internal::TransactionImpl> impl_;
179 };
180 
187  Transaction::ReadOnlyOptions opts = {}) {
188  return Transaction(std::move(opts));
189 }
190 
197  Transaction::ReadWriteOptions opts = {}) {
198  return Transaction(std::move(opts));
199 }
200 
208  Transaction const& txn, Transaction::ReadWriteOptions opts = {}) {
209  return Transaction(txn, std::move(opts));
210 }
211 
212 namespace internal {
213 
214 template <typename T>
215 Transaction MakeSingleUseTransaction(T&& opts) {
216  // Requires that `opts` is implicitly convertible to SingleUseOptions.
217  Transaction::SingleUseOptions su_opts = std::forward<T>(opts);
218  return Transaction(std::move(su_opts));
219 }
220 
221 template <typename Functor>
222 // Pass `txn` by value, despite being used only once. This avoids the
223 // possibility of `txn` being destroyed by `f` before `Visit()` can
224 // return. Therefore, ...
225 // NOLINTNEXTLINE(performance-unnecessary-value-param)
226 VisitInvokeResult<Functor> Visit(Transaction txn, Functor&& f) {
227  return txn.impl_->Visit(std::forward<Functor>(f));
228 }
229 
230 } // namespace internal
231 } // namespace SPANNER_CLIENT_NS
232 } // namespace spanner
233 } // namespace cloud
234 } // namespace google
235 
236 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_TRANSACTION_H
Transaction MakeReadWriteTransaction(Transaction const &txn, Transaction::ReadWriteOptions opts={})
Create a read-write transaction configured with opts, and sharing lock priority with txn.
Definition: transaction.h:207
Options for "single-use", ReadOnly transactions, where Spanner chooses the read timestamp,...
Definition: transaction.h:109
friend bool operator!=(Transaction const &a, Transaction const &b)
Definition: transaction.h:158
Transaction MakeReadOnlyTransaction(Transaction::ReadOnlyOptions opts={})
Create a read-only transaction configured with opts.
Definition: transaction.h:186
friend bool operator==(Transaction const &a, Transaction const &b)
Definition: transaction.h:155
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