forked from googleapis/google-cloud-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_partition.h
More file actions
220 lines (196 loc) · 8.14 KB
/
read_partition.h
File metadata and controls
220 lines (196 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_READ_PARTITION_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_READ_PARTITION_H
#include "google/cloud/spanner/connection.h"
#include "google/cloud/spanner/keys.h"
#include "google/cloud/spanner/version.h"
#include "google/cloud/status_or.h"
#include <google/spanner/v1/spanner.pb.h>
#include <string>
#include <vector>
namespace google {
namespace cloud {
namespace spanner_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
class ReadPartitionTester;
struct ReadPartitionInternals;
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace spanner_internal
namespace spanner {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
/**
* Serializes an instance of `ReadPartition` to a string of bytes.
*
* The serialized string of bytes is suitable for writing to disk or
* transmission to another process.
*
* @note The serialized string may contain NUL and other non-printable
* characters. Therefore, callers should avoid [formatted IO][formatted-io]
* functions that may incorrectly reformat the string data.
*
* @param read_partition - instance to be serialized.
*
* @par Example
* @snippet samples.cc serialize-read-partition
*
* [formatted-io]:
* https://en.cppreference.com/w/cpp/string/basic_string/operator_ltltgtgt
*/
StatusOr<std::string> SerializeReadPartition(
ReadPartition const& read_partition);
/**
* Deserializes the provided string into a `ReadPartition`.
*
* The @p serialized_read_partition argument must be a string that was
* previously returned by a call to `SerializeReadPartition()`.
*
* @note The serialized string may contain NUL and other non-printable
* characters. Therefore, callers should avoid [formatted IO][formatted-io]
* functions that may incorrectly reformat the string data.
*
* @param serialized_read_partition - string representation to be deserialized.
*
* @par Example
* @snippet samples.cc deserialize-read-partition
*
* [formatted-io]:
* https://en.cppreference.com/w/cpp/string/basic_string/operator_ltltgtgt
*/
StatusOr<ReadPartition> DeserializeReadPartition(
std::string const& serialized_read_partition);
/**
* The `ReadPartition` class is a regular type that represents a single
* slice of a parallel Read operation.
*
* Instances of `ReadPartition` are created by `Client::PartitionRead`.
* Once created, `ReadPartition` objects can be serialized, transmitted to
* separate processes, and used to read data in parallel using `Client::Read`.
* If `data_boost` is set, those requests will be executed using the
* independent compute resources of Cloud Spanner Data Boost.
*/
class ReadPartition {
public:
/**
* Constructs an instance of `ReadPartition` that does not specify any table
* or columns to be read.
*/
ReadPartition() = default;
/// @name Copy and move.
///@{
ReadPartition(ReadPartition const&) = default;
ReadPartition(ReadPartition&&) = default;
ReadPartition& operator=(ReadPartition const&) = default;
ReadPartition& operator=(ReadPartition&&) = default;
///@}
std::string TableName() const { return proto_.table(); }
std::vector<std::string> ColumnNames() const {
auto const& columns = proto_.columns();
return std::vector<std::string>(columns.begin(), columns.end());
}
google::cloud::spanner::ReadOptions ReadOptions() const;
/// @name Equality
///@{
friend bool operator==(ReadPartition const& lhs, ReadPartition const& rhs);
friend bool operator!=(ReadPartition const& lhs, ReadPartition const& rhs) {
return !(lhs == rhs);
}
///@}
private:
friend class spanner_internal::ReadPartitionTester;
friend struct spanner_internal::ReadPartitionInternals;
friend StatusOr<std::string> SerializeReadPartition(
ReadPartition const& read_partition);
friend StatusOr<ReadPartition> DeserializeReadPartition(
std::string const& serialized_read_partition);
explicit ReadPartition(google::spanner::v1::ReadRequest proto)
: proto_(std::move(proto)) {}
ReadPartition(std::string transaction_id, bool route_to_leader,
std::string transaction_tag, std::string session_id,
std::string partition_token, std::string table_name,
google::cloud::spanner::KeySet key_set,
std::vector<std::string> column_names, bool data_boost,
google::cloud::spanner::ReadOptions read_options);
// Accessor methods for use by friends.
std::string TransactionId() const { return proto_.transaction().id(); }
bool RouteToLeader() const;
std::string TransactionTag() const {
return proto_.request_options().transaction_tag();
}
std::string SessionId() const { return proto_.session(); }
std::string PartitionToken() const { return proto_.partition_token(); }
google::spanner::v1::KeySet KeySet() const { return proto_.key_set(); }
bool DataBoost() const { return proto_.data_boost_enabled(); }
google::spanner::v1::ReadRequest proto_;
};
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace spanner
// Internal implementation details that callers should not use.
namespace spanner_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
struct ReadPartitionInternals {
static spanner::ReadPartition MakeReadPartition(
std::string transaction_id, bool route_to_leader,
std::string transaction_tag, std::string session_id,
std::string partition_token, std::string table_name,
spanner::KeySet key_set, std::vector<std::string> column_names,
bool data_boost, spanner::ReadOptions read_options) {
return spanner::ReadPartition(
std::move(transaction_id), route_to_leader, std::move(transaction_tag),
std::move(session_id), std::move(partition_token),
std::move(table_name), std::move(key_set), std::move(column_names),
data_boost, std::move(read_options));
}
static spanner::Connection::ReadParams MakeReadParams(
spanner::ReadPartition const& read_partition,
spanner::DirectedReadOption::Type directed_read_option,
spanner::LockHint lock_hint) {
return spanner::Connection::ReadParams{
MakeTransactionFromIds(
read_partition.SessionId(), read_partition.TransactionId(),
read_partition.RouteToLeader(), read_partition.TransactionTag()),
read_partition.TableName(),
FromProto(read_partition.KeySet()),
read_partition.ColumnNames(),
read_partition.ReadOptions(),
read_partition.PartitionToken(),
read_partition.DataBoost(),
std::move(directed_read_option),
lock_hint};
}
};
inline spanner::ReadPartition MakeReadPartition(
std::string transaction_id, bool route_to_leader,
std::string transaction_tag, std::string session_id,
std::string partition_token, std::string table_name,
spanner::KeySet key_set, std::vector<std::string> column_names,
bool data_boost, spanner::ReadOptions read_options) {
return ReadPartitionInternals::MakeReadPartition(
std::move(transaction_id), route_to_leader, std::move(transaction_tag),
std::move(session_id), std::move(partition_token), std::move(table_name),
std::move(key_set), std::move(column_names), data_boost,
std::move(read_options));
}
inline spanner::Connection::ReadParams MakeReadParams(
spanner::ReadPartition const& read_partition,
spanner::DirectedReadOption::Type directed_read_option,
spanner::LockHint lock_hint) {
return ReadPartitionInternals::MakeReadParams(
read_partition, std::move(directed_read_option), lock_hint);
}
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace spanner_internal
} // namespace cloud
} // namespace google
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_READ_PARTITION_H