Google Cloud Spanner C++ Client  1.32.0
A C++ Client Library for Google Cloud Spanner
read_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_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 
26 namespace google {
27 namespace cloud {
28 namespace spanner_internal {
29 inline namespace SPANNER_CLIENT_NS {
30 struct ReadPartitionInternals;
31 } // namespace SPANNER_CLIENT_NS
32 } // namespace spanner_internal
33 
34 namespace spanner {
35 inline namespace SPANNER_CLIENT_NS {
36 
37 /**
38  * Serializes an instance of `ReadPartition` 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 read_partition - instance to be serialized.
48  *
49  * @par Example
50  * @snippet samples.cc serialize-read-partition
51  *
52  * [formatted-io]:
53  * https://en.cppreference.com/w/cpp/string/basic_string/operator_ltltgtgt
54  */
55 StatusOr<std::string> SerializeReadPartition(
56  ReadPartition const& read_partition);
57 
58 /**
59  * Deserializes the provided string into a `ReadPartition`.
60  *
61  * The @p serialized_read_partition argument must be a string that was
62  * previously returned by a call to `SerializeReadPartition()`.
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_read_partition - string representation to be deserialized.
69  *
70  * @par Example
71  * @snippet samples.cc deserialize-read-partition
72  *
73  * [formatted-io]:
74  * https://en.cppreference.com/w/cpp/string/basic_string/operator_ltltgtgt
75  */
77  std::string const& serialized_read_partition);
78 
79 /**
80  * The `ReadPartition` class is a regular type that represents a single
81  * slice of a parallel Read operation.
82  *
83  * Instances of `ReadPartition` are created by `Client::PartitionRead`. Once
84  * created, `ReadPartition` objects can be serialized, transmitted to separate
85  * process, and used to read data in parallel using `Client::Read`.
86  */
88  public:
89  /**
90  * Constructs an instance of `ReadPartition` that does not specify any table
91  * or columns to be read.
92  */
93  ReadPartition() = default;
94 
95  /// @name Copy and move.
96  ///@{
97  ReadPartition(ReadPartition const&) = default;
99  ReadPartition& operator=(ReadPartition const&) = default;
101  ///@}
102 
103  std::string TableName() const { return proto_.table(); }
104 
105  std::vector<std::string> ColumnNames() const {
106  auto const& columns = proto_.columns();
107  return std::vector<std::string>(columns.begin(), columns.end());
108  }
109 
110  google::cloud::spanner::ReadOptions ReadOptions() const;
111 
112  /// @name Equality
113  ///@{
114  friend bool operator==(ReadPartition const& lhs, ReadPartition const& rhs);
115  friend bool operator!=(ReadPartition const& lhs, ReadPartition const& rhs);
116  ///@}
117 
118  private:
119  friend class ReadPartitionTester;
120  friend struct spanner_internal::SPANNER_CLIENT_NS::ReadPartitionInternals;
121  friend StatusOr<std::string> SerializeReadPartition(
122  ReadPartition const& read_partition);
123  friend StatusOr<ReadPartition> DeserializeReadPartition(
124  std::string const& serialized_read_partition);
125 
126  explicit ReadPartition(google::spanner::v1::ReadRequest proto)
127  : proto_(std::move(proto)) {}
128  ReadPartition(std::string transaction_id, std::string transaction_tag,
129  std::string session_id, std::string partition_token,
130  std::string table_name, google::cloud::spanner::KeySet key_set,
131  std::vector<std::string> column_names,
132  google::cloud::spanner::ReadOptions read_options);
133 
134  // Accessor methods for use by friends.
135  std::string PartitionToken() const { return proto_.partition_token(); }
136  std::string SessionId() const { return proto_.session(); }
137  std::string TransactionId() const { return proto_.transaction().id(); }
138  std::string TransactionTag() const {
139  return proto_.request_options().transaction_tag();
140  }
141  google::spanner::v1::KeySet KeySet() const { return proto_.key_set(); }
142 
143  google::spanner::v1::ReadRequest proto_;
144 };
145 
146 } // namespace SPANNER_CLIENT_NS
147 } // namespace spanner
148 
149 // Internal implementation details that callers should not use.
150 namespace spanner_internal {
151 inline namespace SPANNER_CLIENT_NS {
152 
153 struct ReadPartitionInternals {
154  static spanner::ReadPartition MakeReadPartition(
155  std::string transaction_id, std::string transaction_tag,
156  std::string session_id, std::string partition_token,
157  std::string table_name, spanner::KeySet key_set,
158  std::vector<std::string> column_names,
159  spanner::ReadOptions read_options) {
160  return spanner::ReadPartition(
161  std::move(transaction_id), std::move(transaction_tag),
162  std::move(session_id), std::move(partition_token),
163  std::move(table_name), std::move(key_set), std::move(column_names),
164  std::move(read_options));
165  }
166 
167  static spanner::Connection::ReadParams MakeReadParams(
168  spanner::ReadPartition const& read_partition) {
169  return spanner::Connection::ReadParams{
170  MakeTransactionFromIds(read_partition.SessionId(),
171  read_partition.TransactionId(),
172  read_partition.TransactionTag()),
173  read_partition.TableName(),
174  FromProto(read_partition.KeySet()),
175  read_partition.ColumnNames(),
176  read_partition.ReadOptions(),
177  read_partition.PartitionToken()};
178  }
179 };
180 
181 inline spanner::ReadPartition MakeReadPartition(
182  std::string transaction_id, std::string transaction_tag,
183  std::string session_id, std::string partition_token, std::string table_name,
184  spanner::KeySet key_set, std::vector<std::string> column_names,
185  spanner::ReadOptions read_options) {
186  return ReadPartitionInternals::MakeReadPartition(
187  std::move(transaction_id), std::move(transaction_tag),
188  std::move(session_id), std::move(partition_token), std::move(table_name),
189  std::move(key_set), std::move(column_names), std::move(read_options));
190 }
191 
192 inline spanner::Connection::ReadParams MakeReadParams(
193  spanner::ReadPartition const& read_partition) {
194  return ReadPartitionInternals::MakeReadParams(read_partition);
195 }
196 
197 } // namespace SPANNER_CLIENT_NS
198 } // namespace spanner_internal
199 } // namespace cloud
200 } // namespace google
201 
202 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_READ_PARTITION_H