Google Cloud Spanner C++ Client  1.33.0
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 
15 #include "google/cloud/spanner/read_partition.h"
16 #include <google/spanner/v1/spanner.pb.h>
17 
18 namespace google {
19 namespace cloud {
20 namespace spanner {
22 
23 ReadPartition::ReadPartition(std::string transaction_id,
24  std::string transaction_tag,
25  std::string session_id,
26  std::string partition_token,
27  std::string table_name,
28  google::cloud::spanner::KeySet key_set,
29  std::vector<std::string> column_names,
30  google::cloud::spanner::ReadOptions read_options) {
31  proto_.set_session(std::move(session_id));
32  proto_.mutable_transaction()->set_id(std::move(transaction_id));
33  proto_.set_table(std::move(table_name));
34  proto_.set_index(std::move(read_options.index_name));
35  for (auto& column : column_names) {
36  *proto_.mutable_columns()->Add() = std::move(column);
37  }
38  *proto_.mutable_key_set() = spanner_internal::ToProto(std::move(key_set));
39  proto_.set_limit(read_options.limit);
40  proto_.set_partition_token(std::move(partition_token));
41  if (read_options.request_priority) {
42  auto* request_options = proto_.mutable_request_options();
43  switch (*read_options.request_priority) {
45  request_options->set_priority(
46  google::spanner::v1::RequestOptions::PRIORITY_LOW);
47  break;
49  request_options->set_priority(
50  google::spanner::v1::RequestOptions::PRIORITY_MEDIUM);
51  break;
53  request_options->set_priority(
54  google::spanner::v1::RequestOptions::PRIORITY_HIGH);
55  break;
56  }
57  }
58  if (read_options.request_tag.has_value()) {
59  proto_.mutable_request_options()->set_request_tag(
60  *std::move(read_options.request_tag));
61  }
62  proto_.mutable_request_options()->set_transaction_tag(
63  std::move(transaction_tag));
64 }
65 
66 google::cloud::spanner::ReadOptions ReadPartition::ReadOptions() const {
67  google::cloud::spanner::ReadOptions options;
68  options.index_name = proto_.index();
69  options.limit = proto_.limit();
70  if (proto_.has_request_options()) {
71  switch (proto_.request_options().priority()) {
72  case google::spanner::v1::RequestOptions::PRIORITY_LOW:
74  break;
75  case google::spanner::v1::RequestOptions::PRIORITY_MEDIUM:
77  break;
78  case google::spanner::v1::RequestOptions::PRIORITY_HIGH:
80  break;
81  default:
82  break;
83  }
84  if (!proto_.request_options().request_tag().empty()) {
85  options.request_tag = proto_.request_options().request_tag();
86  }
87  }
88  return options;
89 }
90 
91 bool operator==(ReadPartition const& lhs, ReadPartition const& rhs) {
92  google::protobuf::util::MessageDifferencer differencer;
93  return differencer.Compare(lhs.proto_, rhs.proto_);
94 }
95 
96 bool operator!=(ReadPartition const& lhs, ReadPartition const& rhs) {
97  return !(lhs == rhs);
98 }
99 
100 StatusOr<std::string> SerializeReadPartition(
101  ReadPartition const& read_partition) {
102  std::string serialized_proto;
103  if (read_partition.proto_.SerializeToString(&serialized_proto)) {
104  return serialized_proto;
105  }
107  "Failed to serialize SqlPartition");
108 }
109 
111  std::string const& serialized_read_partition) {
112  google::spanner::v1::ReadRequest proto;
113  if (!proto.ParseFromString(serialized_read_partition)) {
115  "Failed to deserialize into SqlPartition");
116  }
117  return ReadPartition(std::move(proto));
118 }
119 
121 } // namespace spanner
122 } // namespace cloud
123 } // namespace google