Google Cloud Spanner C++ Client
A C++ Client Library for Google Cloud Spanner
keys.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 
17 #include <google/protobuf/util/message_differencer.h>
18 #include <google/spanner/v1/keys.pb.h>
19 
20 namespace google {
21 namespace cloud {
22 namespace spanner {
23 inline namespace SPANNER_CLIENT_NS {
24 
25 namespace {
26 // Appends the values in the given `key` to the `lv` proto.
27 void AppendKey(google::protobuf::ListValue& lv, Key&& key) {
28  for (auto& v : key) {
29  *lv.add_values() = internal::ToProto(std::move(v)).second;
30  }
31 }
32 } // namespace
33 
34 namespace internal {
35 
36 ::google::spanner::v1::KeySet ToProto(KeySet ks) {
37  return std::move(ks.proto_);
38 }
39 
40 KeySet FromProto(::google::spanner::v1::KeySet proto) {
41  return KeySet(std::move(proto));
42 }
43 
44 } // namespace internal
45 
46 bool operator==(KeyBound const& a, KeyBound const& b) {
47  return a.key_ == b.key_ && a.bound_ == b.bound_;
48 }
49 
50 KeySet& KeySet::AddKey(Key key) {
51  if (proto_.all()) return *this;
52  AppendKey(*proto_.add_keys(), std::move(key));
53  return *this;
54 }
55 
56 KeySet& KeySet::AddRange(KeyBound start, KeyBound end) {
57  if (proto_.all()) return *this;
58  auto* range = proto_.add_ranges();
59  auto* start_proto = start.bound() == KeyBound::Bound::kClosed
60  ? range->mutable_start_closed()
61  : range->mutable_start_open();
62  AppendKey(*start_proto, std::move(start).key());
63  auto* end_proto = end.bound() == KeyBound::Bound::kClosed
64  ? range->mutable_end_closed()
65  : range->mutable_end_open();
66  AppendKey(*end_proto, std::move(end).key());
67  return *this;
68 }
69 
70 bool operator==(KeySet const& a, KeySet const& b) {
71  google::protobuf::util::MessageDifferencer differencer;
72  return differencer.Compare(a.proto_, b.proto_);
73 }
74 
75 } // namespace SPANNER_CLIENT_NS
76 } // namespace spanner
77 } // namespace cloud
78 } // namespace google
Bound bound() const
Returns the Bound.
Definition: keys.h:96
The KeySet class is a regular type that represents a collection of Keys.
Definition: keys.h:158
bool operator==(KeySet const &a, KeySet const &b)
Definition: keys.cc:70
Contains all the Cloud Spanner C++ client types and functions.
std::vector< Value > Key
A Key is a collection of Value objects where the i'th value corresponds to the i'th component of the ...
Definition: keys.h:37
The KeyBound class is a regular type that represents an open or closed endpoint for a range of keys.
Definition: keys.h:69
#define SPANNER_CLIENT_NS
Definition: version.h:22