Google Cloud Spanner C++ Client  1.32.0
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 
15 #include "google/cloud/spanner/keys.h"
16 #include "google/cloud/spanner/value.h"
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 namespace {
25 // Appends the values in the given `key` to the `lv` proto.
26 void AppendKey(google::protobuf::ListValue& lv, Key&& key) {
27  for (auto& v : key) {
28  *lv.add_values() = spanner_internal::ToProto(std::move(v)).second;
29  }
30 }
31 } // namespace
32 
33 bool operator==(KeyBound const& a, KeyBound const& b) {
34  return a.key_ == b.key_ && a.bound_ == b.bound_;
35 }
36 
37 KeySet& KeySet::AddKey(Key key) {
38  if (proto_.all()) return *this;
39  AppendKey(*proto_.add_keys(), std::move(key));
40  return *this;
41 }
42 
44  if (proto_.all()) return *this;
45  auto* range = proto_.add_ranges();
46  auto* start_proto = start.bound() == KeyBound::Bound::kClosed
47  ? range->mutable_start_closed()
48  : range->mutable_start_open();
49  AppendKey(*start_proto, std::move(start).key());
50  auto* end_proto = end.bound() == KeyBound::Bound::kClosed
51  ? range->mutable_end_closed()
52  : range->mutable_end_open();
53  AppendKey(*end_proto, std::move(end).key());
54  return *this;
55 }
56 
57 bool operator==(KeySet const& a, KeySet const& b) {
58  google::protobuf::util::MessageDifferencer differencer;
59  return differencer.Compare(a.proto_, b.proto_);
60 }
61 
62 } // namespace SPANNER_CLIENT_NS
63 } // namespace spanner
64 } // namespace cloud
65 } // namespace google