Google Cloud Spanner C++ Client
A C++ Client Library for Google Cloud Spanner
read_partition.cc
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 
16 #include <google/spanner/v1/spanner.pb.h>
17 
18 namespace google {
19 namespace cloud {
20 namespace spanner {
21 inline namespace SPANNER_CLIENT_NS {
22 
23 ReadPartition::ReadPartition(std::string transaction_id, std::string session_id,
24  std::string partition_token,
25  std::string table_name,
26  google::cloud::spanner::KeySet key_set,
27  std::vector<std::string> column_names,
28  google::cloud::spanner::ReadOptions read_options) {
29  proto_.set_session(std::move(session_id));
30  proto_.mutable_transaction()->set_id(std::move(transaction_id));
31  proto_.set_table(std::move(table_name));
32  proto_.set_index(std::move(read_options.index_name));
33  for (auto& column : column_names) {
34  *proto_.mutable_columns()->Add() = std::move(column);
35  }
36  *proto_.mutable_key_set() = internal::ToProto(std::move(key_set));
37  proto_.set_limit(read_options.limit);
38  proto_.set_partition_token(std::move(partition_token));
39 }
40 
41 bool operator==(ReadPartition const& lhs, ReadPartition const& rhs) {
42  google::protobuf::util::MessageDifferencer differencer;
43  return differencer.Compare(lhs.proto_, rhs.proto_);
44 }
45 
46 bool operator!=(ReadPartition const& lhs, ReadPartition const& rhs) {
47  return !(lhs == rhs);
48 }
49 
50 StatusOr<std::string> SerializeReadPartition(
51  ReadPartition const& read_partition) {
52  std::string serialized_proto;
53  if (read_partition.proto_.SerializeToString(&serialized_proto)) {
54  return serialized_proto;
55  }
56  return Status(StatusCode::kInvalidArgument,
57  "Failed to serialize SqlPartition");
58 }
59 
60 StatusOr<ReadPartition> DeserializeReadPartition(
61  std::string const& serialized_read_partition) {
62  google::spanner::v1::ReadRequest proto;
63  if (!proto.ParseFromString(serialized_read_partition)) {
64  return Status(StatusCode::kInvalidArgument,
65  "Failed to deserialize into SqlPartition");
66  }
67  return ReadPartition(std::move(proto));
68 }
69 
70 namespace internal {
71 ReadPartition MakeReadPartition(std::string transaction_id,
72  std::string session_id,
73  std::string partition_token,
74  std::string table_name, KeySet key_set,
75  std::vector<std::string> column_names,
76  ReadOptions read_options) {
77  return ReadPartition(std::move(transaction_id), std::move(session_id),
78  std::move(partition_token), std::move(table_name),
79  std::move(key_set), std::move(column_names),
80  std::move(read_options));
81 }
82 
83 Connection::ReadParams MakeReadParams(ReadPartition const& read_partition) {
84  return Connection::ReadParams{
85  MakeTransactionFromIds(read_partition.SessionId(),
86  read_partition.TransactionId()),
87  read_partition.TableName(),
88  FromProto(read_partition.KeySet()),
89  read_partition.ColumnNames(),
90  read_partition.ReadOptions(),
91  read_partition.PartitionToken()};
92 }
93 
94 } // namespace internal
95 } // namespace SPANNER_CLIENT_NS
96 } // namespace spanner
97 } // namespace cloud
98 } // namespace google
bool operator!=(ReadPartition const &lhs, ReadPartition const &rhs)
StatusOr< std::string > SerializeReadPartition(ReadPartition const &read_partition)
Serializes an instance of ReadPartition to a string of bytes.
StatusOr< ReadPartition > DeserializeReadPartition(std::string const &serialized_read_partition)
Deserializes the provided string into a ReadPartition.
Contains all the Cloud Spanner C++ client types and functions.
The ReadPartition class is a regular type that represents a single slice of a parallel Read operation...
#define SPANNER_CLIENT_NS
Definition: version.h:22
bool operator==(ReadPartition const &lhs, ReadPartition const &rhs)