Google Cloud Spanner C++ Client 2.13.0
A C++ Client Library for Google Cloud Spanner
Loading...
Searching...
No Matches
query_partition.h
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// https://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
25namespace google {
26namespace cloud {
27namespace spanner_internal {
28GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
29class QueryPartitionTester;
30struct QueryPartitionInternals;
31GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
32} // namespace spanner_internal
33
34namespace spanner {
35GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
36
37/**
38 * Serializes an instance of `QueryPartition` to a string of bytes.
39 *
40 * The serialized string of bytes is suitable for writing to disk or
41 * transmission to another process.
42 *
43 * @note The serialized string may contain NUL and other non-printable
44 * characters. Therefore, callers should avoid [formatted IO][formatted-io]
45 * functions that may incorrectly reformat the string data.
46 *
47 * @param query_partition - instance to be serialized.
48 *
49 * @par Example
50 * @snippet samples.cc serialize-query-partition
51 *
52 * [formatted-io]:
53 * https://en.cppreference.com/w/cpp/string/basic_string/operator_ltltgtgt
54 */
55StatusOr<std::string> SerializeQueryPartition(
56 QueryPartition const& query_partition);
57
58/**
59 * Deserializes the provided string into a `QueryPartition`.
60 *
61 * The @p serialized_query_partition argument must be a string that was
62 * previously returned by a call to `SerializeQueryPartition()`.
63 *
64 * @note The serialized string may contain NUL and other non-printable
65 * characters. Therefore, callers should avoid [formatted IO][formatted-io]
66 * functions that may incorrectly reformat the string data.
67 *
68 * @param serialized_query_partition
69 *
70 * @par Example
71 * @snippet samples.cc deserialize-query-partition
72 *
73 * [formatted-io]:
74 * https://en.cppreference.com/w/cpp/string/basic_string/operator_ltltgtgt
75 */
77 std::string const& serialized_query_partition);
78
79/**
80 * The `QueryPartition` class is a regular type that represents a single slice
81 * of a parallel SQL read.
82 *
83 * Instances of `QueryPartition` are created by `Client::PartitionQuery`. Once
84 * created, `QueryPartition` objects can be serialized, transmitted to separate
85 * processes, and used to read data in parallel using `Client::ExecuteQuery`.
86 * If `data_boost` is set, those requests will be executed using the
87 * independent compute resources of Cloud Spanner Data Boost.
88 */
89class QueryPartition {
90 public:
91 /**
92 * Constructs an instance of `QueryPartition` that is not associated with any
93 * `SqlStatement`.
94 */
95 QueryPartition() = default;
96
97 /// @name Copy and move
98 ///@{
99 QueryPartition(QueryPartition const&) = default;
100 QueryPartition(QueryPartition&&) = default;
101 QueryPartition& operator=(QueryPartition const&) = default;
103 ///@}
104
105 /**
106 * Accessor for the `SqlStatement` associated with this `QueryPartition`.
107 */
108 SqlStatement const& sql_statement() const { return sql_statement_; }
109
110 /// @name Equality
111 ///@{
112 friend bool operator==(QueryPartition const& a, QueryPartition const& b);
113 friend bool operator!=(QueryPartition const& a, QueryPartition const& b) {
114 return !(a == b);
115 }
116 ///@}
117
118 private:
119 friend class spanner_internal::QueryPartitionTester;
120 friend struct spanner_internal::QueryPartitionInternals;
121 friend StatusOr<std::string> SerializeQueryPartition(
122 QueryPartition const& query_partition);
124 std::string const& serialized_query_partition);
125
126 QueryPartition(std::string transaction_id, bool route_to_leader,
127 std::string transaction_tag, std::string session_id,
128 std::string partition_token, bool data_boost,
129 SqlStatement sql_statement);
130
131 // Accessor methods for use by friends.
132 std::string const& transaction_id() const { return transaction_id_; }
133 bool route_to_leader() const { return route_to_leader_; }
134 std::string const& transaction_tag() const { return transaction_tag_; }
135 std::string const& session_id() const { return session_id_; }
136 std::string const& partition_token() const { return partition_token_; }
137 bool data_boost() const { return data_boost_; }
138
139 std::string transaction_id_;
140 bool route_to_leader_;
141 std::string transaction_tag_;
142 std::string session_id_;
143 std::string partition_token_;
144 bool data_boost_;
145 SqlStatement sql_statement_;
146};
147
148GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
149} // namespace spanner
150
151// Internal implementation details that callers should not use.
152namespace spanner_internal {
153GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
154
155struct QueryPartitionInternals {
156 static spanner::QueryPartition MakeQueryPartition(
157 std::string const& transaction_id, bool route_to_leader,
158 std::string const& transaction_tag, std::string const& session_id,
159 std::string const& partition_token, bool data_boost,
160 spanner::SqlStatement const& sql_statement) {
161 return spanner::QueryPartition(transaction_id, route_to_leader,
162 transaction_tag, session_id, partition_token,
163 data_boost, sql_statement);
164 }
165
166 static spanner::Connection::SqlParams MakeSqlParams(
167 spanner::QueryPartition const& query_partition,
168 spanner::QueryOptions const& query_options) {
169 return {MakeTransactionFromIds(query_partition.session_id(),
170 query_partition.transaction_id(),
171 query_partition.route_to_leader(),
172 query_partition.transaction_tag()),
173 query_partition.sql_statement(), query_options,
174 query_partition.partition_token(), query_partition.data_boost()};
175 }
176};
177
178inline spanner::QueryPartition MakeQueryPartition(
179 std::string const& transaction_id, bool route_to_leader,
180 std::string const& transaction_tag, std::string const& session_id,
181 std::string const& partition_token, bool data_boost,
182 spanner::SqlStatement const& sql_statement) {
183 return QueryPartitionInternals::MakeQueryPartition(
184 transaction_id, route_to_leader, transaction_tag, session_id,
185 partition_token, data_boost, sql_statement);
186}
187
188inline spanner::Connection::SqlParams MakeSqlParams(
189 spanner::QueryPartition const& query_partition,
190 spanner::QueryOptions const& query_options) {
191 return QueryPartitionInternals::MakeSqlParams(query_partition, query_options);
192}
193
194GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
195} // namespace spanner_internal
196} // namespace cloud
197} // namespace google
198
199#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_QUERY_PARTITION_H
A connection to a Spanner database instance.
Definition: connection.h:59
These QueryOptions allow users to configure features about how their SQL queries executes on the serv...
Definition: query_options.h:37
The QueryPartition class is a regular type that represents a single slice of a parallel SQL read.
Definition: query_partition.h:89
QueryPartition & operator=(QueryPartition const &)=default
friend bool operator!=(QueryPartition const &a, QueryPartition const &b)
Definition: query_partition.h:113
QueryPartition(QueryPartition const &)=default
SqlStatement const & sql_statement() const
Accessor for the SqlStatement associated with this QueryPartition.
Definition: query_partition.h:108
QueryPartition & operator=(QueryPartition &&)=default
QueryPartition(QueryPartition &&)=default
friend bool operator==(QueryPartition const &a, QueryPartition const &b)
QueryPartition()=default
Constructs an instance of QueryPartition that is not associated with any SqlStatement.
Represents a potentially parameterized SQL statement.
Definition: sql_statement.h:51
Contains all the Cloud Spanner C++ client types and functions.
Definition: backoff_policy.h:23
StatusOr< QueryPartition > DeserializeQueryPartition(std::string const &serialized_query_partition)
Deserializes the provided string into a QueryPartition.
StatusOr< std::string > SerializeQueryPartition(QueryPartition const &query_partition)
Serializes an instance of QueryPartition to a string of bytes.
Wrap the arguments to ExecuteQuery(), ExecuteDml(), ProfileQuery(), ProfileDml(), and AnalyzeSql().
Definition: connection.h:92