Google Cloud Spanner C++ Client
A C++ Client Library for Google Cloud Spanner
keys.h
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 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_KEYS_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_KEYS_H
17 
20 #include <google/spanner/v1/keys.pb.h>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 namespace google {
26 namespace cloud {
27 namespace spanner {
28 inline namespace SPANNER_CLIENT_NS {
29 
37 using Key = std::vector<Value>;
38 
45 template <typename... Ts>
46 Key MakeKey(Ts&&... ts) {
47  return Key{Value(std::forward<Ts>(ts))...};
48 }
49 
69 class KeyBound {
70  public:
73  enum class Bound { kClosed, kOpen };
74 
76  KeyBound() = delete;
77 
79  KeyBound(Key key, Bound bound) : key_(std::move(key)), bound_(bound) {}
80 
83  KeyBound(KeyBound const&) = default;
84  KeyBound& operator=(KeyBound const&) = default;
85  KeyBound(KeyBound&&) = default;
86  KeyBound& operator=(KeyBound&&) = default;
88 
90  Key const& key() const& { return key_; }
91 
93  Key&& key() && { return std::move(key_); }
94 
96  Bound bound() const { return bound_; }
97 
100  friend bool operator==(KeyBound const& a, KeyBound const& b);
101  friend bool operator!=(KeyBound const& a, KeyBound const& b) {
102  return !(a == b);
103  }
105 
106  private:
107  Key key_;
108  Bound bound_;
109 };
110 
118 template <typename... Ts>
120  return KeyBound(MakeKey(std::forward<Ts>(ts)...), KeyBound::Bound::kClosed);
121 }
122 
130 template <typename... Ts>
132  return KeyBound(MakeKey(std::forward<Ts>(ts)...), KeyBound::Bound::kOpen);
133 }
134 
135 class KeySet;
136 namespace internal {
137 ::google::spanner::v1::KeySet ToProto(KeySet);
138 KeySet FromProto(::google::spanner::v1::KeySet);
139 } // namespace internal
140 
158 class KeySet {
159  public:
166  static KeySet All() {
167  KeySet ks;
168  ks.proto_.set_all(true);
169  return ks;
170  }
171 
173  KeySet() = default;
174 
175  // Copy and move constructors and assignment operators.
176  KeySet(KeySet const&) = default;
177  KeySet& operator=(KeySet const&) = default;
178  KeySet(KeySet&&) = default;
179  KeySet& operator=(KeySet&&) = default;
180 
187  KeySet& AddKey(Key key);
188 
195  KeySet& AddRange(KeyBound start, KeyBound end);
196 
199  friend bool operator==(KeySet const& a, KeySet const& b);
200  friend bool operator!=(KeySet const& a, KeySet const& b) { return !(a == b); }
202 
203  private:
204  friend ::google::spanner::v1::KeySet internal::ToProto(KeySet);
205  friend KeySet internal::FromProto(::google::spanner::v1::KeySet);
206  explicit KeySet(google::spanner::v1::KeySet proto)
207  : proto_(std::move(proto)) {}
208 
209  google::spanner::v1::KeySet proto_;
210 };
211 
212 } // namespace SPANNER_CLIENT_NS
213 } // namespace spanner
214 } // namespace cloud
215 } // namespace google
216 
217 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_KEYS_H
KeyBound MakeKeyBoundClosed(Ts &&... ts)
Returns a "closed" KeyBound with a Key constructed from the given arguments.
Definition: keys.h:119
Key MakeKey(Ts &&... ts)
Constructs a Key from the given arguments.
Definition: keys.h:46
The Value class represents a type-safe, nullable Spanner value.
Definition: value.h:162
Bound bound() const
Returns the Bound.
Definition: keys.h:96
KeyBound MakeKeyBoundOpen(Ts &&... ts)
Returns an "open" KeyBound with a Key constructed from the given arguments.
Definition: keys.h:131
friend bool operator!=(KeySet const &a, KeySet const &b)
Definition: keys.h:200
Key const & key() const &
Returns the Key.
Definition: keys.h:90
KeyBound(Key key, Bound bound)
Constructs an instance with the given key and bound.
Definition: keys.h:79
bool operator==(Backup const &a, Backup const &b)
Definition: backup.cc:29
The KeySet class is a regular type that represents a collection of Keys.
Definition: keys.h:158
Bound
An enum indicating whether the Key is included (closed) or excluded (open).
Definition: keys.h:73
Contains all the Cloud Spanner C++ client types and functions.
Key && key() &&
Returns the Key (by move).
Definition: keys.h:93
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
static KeySet All()
Returns a KeySet that represents the set of "All" keys for the index.
Definition: keys.h:166
friend bool operator!=(KeyBound const &a, KeyBound const &b)
Definition: keys.h:101
#define SPANNER_CLIENT_NS
Definition: version.h:22