Google Cloud Spanner C++ Client 2.13.0
A C++ Client Library for Google Cloud Spanner
Loading...
Searching...
No Matches
read_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_READ_PARTITION_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_READ_PARTITION_H
17
18#include "google/cloud/spanner/connection.h"
19#include "google/cloud/spanner/keys.h"
20#include "google/cloud/spanner/version.h"
21#include "google/cloud/status_or.h"
22#include <google/spanner/v1/spanner.pb.h>
23#include <string>
24#include <vector>
25
26namespace google {
27namespace cloud {
28namespace spanner_internal {
29GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
30class ReadPartitionTester;
31struct ReadPartitionInternals;
32GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
33} // namespace spanner_internal
34
35namespace spanner {
36GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
37
38/**
39 * Serializes an instance of `ReadPartition` to a string of bytes.
40 *
41 * The serialized string of bytes is suitable for writing to disk or
42 * transmission to another process.
43 *
44 * @note The serialized string may contain NUL and other non-printable
45 * characters. Therefore, callers should avoid [formatted IO][formatted-io]
46 * functions that may incorrectly reformat the string data.
47 *
48 * @param read_partition - instance to be serialized.
49 *
50 * @par Example
51 * @snippet samples.cc serialize-read-partition
52 *
53 * [formatted-io]:
54 * https://en.cppreference.com/w/cpp/string/basic_string/operator_ltltgtgt
55 */
56StatusOr<std::string> SerializeReadPartition(
57 ReadPartition const& read_partition);
58
59/**
60 * Deserializes the provided string into a `ReadPartition`.
61 *
62 * The @p serialized_read_partition argument must be a string that was
63 * previously returned by a call to `SerializeReadPartition()`.
64 *
65 * @note The serialized string may contain NUL and other non-printable
66 * characters. Therefore, callers should avoid [formatted IO][formatted-io]
67 * functions that may incorrectly reformat the string data.
68 *
69 * @param serialized_read_partition - string representation to be deserialized.
70 *
71 * @par Example
72 * @snippet samples.cc deserialize-read-partition
73 *
74 * [formatted-io]:
75 * https://en.cppreference.com/w/cpp/string/basic_string/operator_ltltgtgt
76 */
78 std::string const& serialized_read_partition);
79
80/**
81 * The `ReadPartition` class is a regular type that represents a single
82 * slice of a parallel Read operation.
83 *
84 * Instances of `ReadPartition` are created by `Client::PartitionRead`.
85 * Once created, `ReadPartition` objects can be serialized, transmitted to
86 * separate processes, and used to read data in parallel using `Client::Read`.
87 * If `data_boost` is set, those requests will be executed using the
88 * independent compute resources of Cloud Spanner Data Boost.
89 */
90class ReadPartition {
91 public:
92 /**
93 * Constructs an instance of `ReadPartition` that does not specify any table
94 * or columns to be read.
95 */
96 ReadPartition() = default;
97
98 /// @name Copy and move.
99 ///@{
100 ReadPartition(ReadPartition const&) = default;
101 ReadPartition(ReadPartition&&) = default;
102 ReadPartition& operator=(ReadPartition const&) = default;
103 ReadPartition& operator=(ReadPartition&&) = default;
104 ///@}
105
106 std::string TableName() const { return proto_.table(); }
107
108 std::vector<std::string> ColumnNames() const {
109 auto const& columns = proto_.columns();
110 return std::vector<std::string>(columns.begin(), columns.end());
111 }
112
113 google::cloud::spanner::ReadOptions ReadOptions() const;
114
115 /// @name Equality
116 ///@{
117 friend bool operator==(ReadPartition const& lhs, ReadPartition const& rhs);
118 friend bool operator!=(ReadPartition const& lhs, ReadPartition const& rhs) {
119 return !(lhs == rhs);
120 }
121 ///@}
122
123 private:
124 friend class spanner_internal::ReadPartitionTester;
125 friend struct spanner_internal::ReadPartitionInternals;
126 friend StatusOr<std::string> SerializeReadPartition(
127 ReadPartition const& read_partition);
129 std::string const& serialized_read_partition);
130
131 explicit ReadPartition(google::spanner::v1::ReadRequest proto)
132 : proto_(std::move(proto)) {}
133 ReadPartition(std::string transaction_id, bool route_to_leader,
134 std::string transaction_tag, std::string session_id,
135 std::string partition_token, std::string table_name,
136 google::cloud::spanner::KeySet key_set,
137 std::vector<std::string> column_names, bool data_boost,
138 google::cloud::spanner::ReadOptions read_options);
139
140 // Accessor methods for use by friends.
141 std::string TransactionId() const { return proto_.transaction().id(); }
142 bool RouteToLeader() const;
143 std::string TransactionTag() const {
144 return proto_.request_options().transaction_tag();
145 }
146 std::string SessionId() const { return proto_.session(); }
147 std::string PartitionToken() const { return proto_.partition_token(); }
148 google::spanner::v1::KeySet KeySet() const { return proto_.key_set(); }
149 bool DataBoost() const { return proto_.data_boost_enabled(); }
150
151 google::spanner::v1::ReadRequest proto_;
152};
153
154GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
155} // namespace spanner
156
157// Internal implementation details that callers should not use.
158namespace spanner_internal {
159GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
160
161struct ReadPartitionInternals {
162 static spanner::ReadPartition MakeReadPartition(
163 std::string transaction_id, bool route_to_leader,
164 std::string transaction_tag, std::string session_id,
165 std::string partition_token, std::string table_name,
166 spanner::KeySet key_set, std::vector<std::string> column_names,
167 bool data_boost, spanner::ReadOptions read_options) {
168 return spanner::ReadPartition(
169 std::move(transaction_id), route_to_leader, std::move(transaction_tag),
170 std::move(session_id), std::move(partition_token),
171 std::move(table_name), std::move(key_set), std::move(column_names),
172 data_boost, std::move(read_options));
173 }
174
175 static spanner::Connection::ReadParams MakeReadParams(
176 spanner::ReadPartition const& read_partition) {
178 MakeTransactionFromIds(
179 read_partition.SessionId(), read_partition.TransactionId(),
180 read_partition.RouteToLeader(), read_partition.TransactionTag()),
181 read_partition.TableName(),
182 FromProto(read_partition.KeySet()),
183 read_partition.ColumnNames(),
184 read_partition.ReadOptions(),
185 read_partition.PartitionToken(),
186 read_partition.DataBoost()};
187 }
188};
189
190inline spanner::ReadPartition MakeReadPartition(
191 std::string transaction_id, bool route_to_leader,
192 std::string transaction_tag, std::string session_id,
193 std::string partition_token, std::string table_name,
194 spanner::KeySet key_set, std::vector<std::string> column_names,
195 bool data_boost, spanner::ReadOptions read_options) {
196 return ReadPartitionInternals::MakeReadPartition(
197 std::move(transaction_id), route_to_leader, std::move(transaction_tag),
198 std::move(session_id), std::move(partition_token), std::move(table_name),
199 std::move(key_set), std::move(column_names), data_boost,
200 std::move(read_options));
201}
202
203inline spanner::Connection::ReadParams MakeReadParams(
204 spanner::ReadPartition const& read_partition) {
205 return ReadPartitionInternals::MakeReadParams(read_partition);
206}
207
208GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
209} // namespace spanner_internal
210} // namespace cloud
211} // namespace google
212
213#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_READ_PARTITION_H
A connection to a Spanner database instance.
Definition: connection.h:59
The KeySet class is a regular type that represents a collection of Keys.
Definition: keys.h:157
The ReadPartition class is a regular type that represents a single slice of a parallel Read operation...
Definition: read_partition.h:90
std::vector< std::string > ColumnNames() const
Definition: read_partition.h:108
ReadPartition(ReadPartition const &)=default
friend bool operator!=(ReadPartition const &lhs, ReadPartition const &rhs)
Definition: read_partition.h:118
ReadPartition(ReadPartition &&)=default
std::string TableName() const
Definition: read_partition.h:106
friend bool operator==(ReadPartition const &lhs, ReadPartition const &rhs)
ReadPartition & operator=(ReadPartition const &)=default
ReadPartition()=default
Constructs an instance of ReadPartition that does not specify any table or columns to be read.
ReadPartition & operator=(ReadPartition &&)=default
Contains all the Cloud Spanner C++ client types and functions.
Definition: backoff_policy.h:23
StatusOr< ReadPartition > DeserializeReadPartition(std::string const &serialized_read_partition)
Deserializes the provided string into a ReadPartition.
StatusOr< std::string > SerializeReadPartition(ReadPartition const &read_partition)
Serializes an instance of ReadPartition to a string of bytes.
Wrap the arguments to Read().
Definition: connection.h:74
Options passed to Client::Read or Client::PartitionRead.
Definition: read_options.h:32