Google Cloud Spanner C++ Client  1.32.0
A C++ Client Library for Google Cloud Spanner
query_partition.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_QUERY_PARTITION_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_QUERY_PARTITION_H
17 
18 #include "google/cloud/spanner/connection.h"
19 #include "google/cloud/spanner/sql_statement.h"
20 #include "google/cloud/spanner/version.h"
21 #include "google/cloud/status_or.h"
22 #include <memory>
23 #include <string>
24 
25 namespace google {
26 namespace cloud {
27 namespace spanner_internal {
28 inline namespace SPANNER_CLIENT_NS {
29 struct QueryPartitionInternals;
30 } // namespace SPANNER_CLIENT_NS
31 } // namespace spanner_internal
32 
33 namespace spanner {
34 inline namespace SPANNER_CLIENT_NS {
35 
36 /**
37  * Serializes an instance of `QueryPartition` to a string of bytes.
38  *
39  * The serialized string of bytes is suitable for writing to disk or
40  * transmission to another process.
41  *
42  * @note The serialized string may contain NUL and other non-printable
43  * characters. Therefore, callers should avoid [formatted IO][formatted-io]
44  * functions that may incorrectly reformat the string data.
45  *
46  * @param query_partition - instance to be serialized.
47  *
48  * @par Example
49  * @snippet samples.cc serialize-query-partition
50  *
51  * [formatted-io]:
52  * https://en.cppreference.com/w/cpp/string/basic_string/operator_ltltgtgt
53  */
54 StatusOr<std::string> SerializeQueryPartition(
55  QueryPartition const& query_partition);
56 
57 /**
58  * Deserializes the provided string into a `QueryPartition`.
59  *
60  * The @p serialized_query_partition argument must be a string that was
61  * previously returned by a call to `SerializeQueryPartition()`.
62  *
63  * @note The serialized string may contain NUL and other non-printable
64  * characters. Therefore, callers should avoid [formatted IO][formatted-io]
65  * functions that may incorrectly reformat the string data.
66  *
67  * @param serialized_query_partition
68  *
69  * @par Example
70  * @snippet samples.cc deserialize-query-partition
71  *
72  * [formatted-io]:
73  * https://en.cppreference.com/w/cpp/string/basic_string/operator_ltltgtgt
74  */
76  std::string const& serialized_query_partition);
77 
78 /**
79  * The `QueryPartition` class is a regular type that represents a single slice
80  * of a parallel SQL read.
81  *
82  * Instances of `QueryPartition` are created by `Client::PartitionSql`. Once
83  * created, `QueryPartition` objects can be serialized, transmitted to separate
84  * process, and used to read data in parallel using `Client::ExecuteQuery`.
85  */
87  public:
88  /**
89  * Constructs an instance of `QueryPartition` that is not associated with any
90  * `SqlStatement`.
91  */
92  QueryPartition() = default;
93 
94  /// @name Copy and move
95  ///@{
96  QueryPartition(QueryPartition const&) = default;
98  QueryPartition& operator=(QueryPartition const&) = default;
100  ///@}
101 
102  /**
103  * Accessor for the `SqlStatement` associated with this `QueryPartition`.
104  */
105  SqlStatement const& sql_statement() const { return sql_statement_; }
106 
107  /// @name Equality
108  ///@{
109  friend bool operator==(QueryPartition const& a, QueryPartition const& b);
110  friend bool operator!=(QueryPartition const& a, QueryPartition const& b) {
111  return !(a == b);
112  }
113  ///@}
114 
115  private:
116  friend class QueryPartitionTester;
117  friend struct spanner_internal::SPANNER_CLIENT_NS::QueryPartitionInternals;
118  friend StatusOr<std::string> SerializeQueryPartition(
119  QueryPartition const& query_partition);
120  friend StatusOr<QueryPartition> DeserializeQueryPartition(
121  std::string const& serialized_query_partition);
122 
123  QueryPartition(std::string transaction_id, std::string transaction_tag,
124  std::string session_id, std::string partition_token,
125  SqlStatement sql_statement);
126 
127  // Accessor methods for use by friends.
128  std::string const& partition_token() const { return partition_token_; }
129  std::string const& session_id() const { return session_id_; }
130  std::string const& transaction_tag() const { return transaction_tag_; }
131  std::string const& transaction_id() const { return transaction_id_; }
132 
133  std::string transaction_id_;
134  std::string transaction_tag_;
135  std::string session_id_;
136  std::string partition_token_;
137  SqlStatement sql_statement_;
138 };
139 
140 } // namespace SPANNER_CLIENT_NS
141 } // namespace spanner
142 
143 // Internal implementation details that callers should not use.
144 namespace spanner_internal {
145 inline namespace SPANNER_CLIENT_NS {
146 
147 struct QueryPartitionInternals {
148  static spanner::QueryPartition MakeQueryPartition(
149  std::string const& transaction_id, std::string const& transaction_tag,
150  std::string const& session_id, std::string const& partition_token,
151  spanner::SqlStatement const& sql_statement) {
152  return spanner::QueryPartition(transaction_id, transaction_tag, session_id,
153  partition_token, sql_statement);
154  }
155 
156  static spanner::Connection::SqlParams MakeSqlParams(
157  spanner::QueryPartition const& query_partition) {
158  spanner::QueryOptions query_options; // not serialized
159  return {MakeTransactionFromIds(query_partition.session_id(),
160  query_partition.transaction_id(),
161  query_partition.transaction_tag()),
162  query_partition.sql_statement(), query_options,
163  query_partition.partition_token()};
164  }
165 };
166 
167 inline spanner::QueryPartition MakeQueryPartition(
168  std::string const& transaction_id, std::string const& transaction_tag,
169  std::string const& session_id, std::string const& partition_token,
170  spanner::SqlStatement const& sql_statement) {
171  return QueryPartitionInternals::MakeQueryPartition(
172  transaction_id, transaction_tag, session_id, partition_token,
173  sql_statement);
174 }
175 
176 inline spanner::Connection::SqlParams MakeSqlParams(
177  spanner::QueryPartition const& query_partition) {
178  return QueryPartitionInternals::MakeSqlParams(query_partition);
179 }
180 
181 } // namespace SPANNER_CLIENT_NS
182 } // namespace spanner_internal
183 } // namespace cloud
184 } // namespace google
185 
186 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_QUERY_PARTITION_H