Google Cloud Spanner C++ Client
A C++ Client Library for Google Cloud Spanner
mutations.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_MUTATIONS_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_MUTATIONS_H
17 
20 #include <google/spanner/v1/mutation.pb.h>
21 #include <vector>
22 
23 namespace google {
24 namespace cloud {
25 namespace spanner {
26 inline namespace SPANNER_CLIENT_NS {
27 
28 namespace internal {
29 template <typename Op>
30 class WriteMutationBuilder;
32 } // namespace internal
33 
48 class Mutation {
49  public:
55  Mutation() = default;
56 
57  Mutation(Mutation&&) = default;
58  Mutation& operator=(Mutation&&) = default;
59  Mutation(Mutation const&) = default;
60  Mutation& operator=(Mutation const&) = default;
61 
62  friend bool operator==(Mutation const& lhs, Mutation const& rhs);
63  friend bool operator!=(Mutation const& lhs, Mutation const& rhs) {
64  return !(lhs == rhs);
65  }
66 
68  google::spanner::v1::Mutation as_proto() && { return std::move(m_); }
69  google::spanner::v1::Mutation as_proto() const& { return m_; }
70 
78  friend void PrintTo(Mutation const& m, std::ostream* os);
79 
80  private:
81  google::spanner::v1::Mutation& proto() & { return m_; }
82 
83  template <typename Op>
84  friend class internal::WriteMutationBuilder;
86  explicit Mutation(google::spanner::v1::Mutation m) : m_(std::move(m)) {}
87 
88  google::spanner::v1::Mutation m_;
89 };
90 
95 using Mutations = std::vector<Mutation>;
96 
97 // This namespace contains implementation details. It is not part of the public
98 // API, and subject to change without notice.
99 namespace internal {
100 
101 template <typename Op>
102 class WriteMutationBuilder {
103  public:
104  WriteMutationBuilder(std::string table_name,
105  std::vector<std::string> column_names) {
106  auto& field = Op::mutable_field(m_.proto());
107  field.set_table(std::move(table_name));
108  field.mutable_columns()->Reserve(static_cast<int>(column_names.size()));
109  for (auto& name : column_names) {
110  field.add_columns(std::move(name));
111  }
112  }
113 
114  Mutation Build() const& { return m_; }
115  Mutation&& Build() && { return std::move(m_); }
116 
117  WriteMutationBuilder& AddRow(std::vector<Value> values) & {
118  auto& lv = *Op::mutable_field(m_.proto()).add_values();
119  for (auto& v : values) {
120  std::tie(std::ignore, *lv.add_values()) = internal::ToProto(std::move(v));
121  }
122  return *this;
123  }
124 
125  WriteMutationBuilder&& AddRow(std::vector<Value> values) && {
126  return std::move(AddRow(std::move(values)));
127  }
128 
129  template <typename... Ts>
130  WriteMutationBuilder& EmplaceRow(Ts&&... values) & {
131  return AddRow({Value(std::forward<Ts>(values))...});
132  }
133 
134  template <typename... Ts>
135  WriteMutationBuilder&& EmplaceRow(Ts&&... values) && {
136  return std::move(EmplaceRow(std::forward<Ts>(values)...));
137  }
138 
139  private:
140  Mutation m_;
141 };
142 
143 struct InsertOp {
144  static google::spanner::v1::Mutation::Write& mutable_field(
145  google::spanner::v1::Mutation& m) {
146  return *m.mutable_insert();
147  }
148 };
149 
150 struct UpdateOp {
151  static google::spanner::v1::Mutation::Write& mutable_field(
152  google::spanner::v1::Mutation& m) {
153  return *m.mutable_update();
154  }
155 };
156 
157 struct InsertOrUpdateOp {
158  static google::spanner::v1::Mutation::Write& mutable_field(
159  google::spanner::v1::Mutation& m) {
160  return *m.mutable_insert_or_update();
161  }
162 };
163 
164 struct ReplaceOp {
165  static google::spanner::v1::Mutation::Write& mutable_field(
166  google::spanner::v1::Mutation& m) {
167  return *m.mutable_replace();
168  }
169 };
170 
171 class DeleteMutationBuilder {
172  public:
173  DeleteMutationBuilder(std::string table_name, KeySet keys) {
174  auto& field = *m_.proto().mutable_delete_();
175  field.set_table(std::move(table_name));
176  *field.mutable_key_set() = internal::ToProto(std::move(keys));
177  }
178 
179  Mutation Build() const& { return m_; }
180  Mutation&& Build() && { return std::move(m_); }
181 
182  private:
183  Mutation m_;
184 };
185 
186 } // namespace internal
187 
200 using InsertMutationBuilder =
201  internal::WriteMutationBuilder<internal::InsertOp>;
202 
215 template <typename... Ts>
216 Mutation MakeInsertMutation(std::string table_name,
217  std::vector<std::string> columns, Ts&&... values) {
218  return InsertMutationBuilder(std::move(table_name), std::move(columns))
219  .EmplaceRow(std::forward<Ts>(values)...)
220  .Build();
221 }
222 
235 using UpdateMutationBuilder =
236  internal::WriteMutationBuilder<internal::UpdateOp>;
237 
250 template <typename... Ts>
251 Mutation MakeUpdateMutation(std::string table_name,
252  std::vector<std::string> columns, Ts&&... values) {
253  return UpdateMutationBuilder(std::move(table_name), std::move(columns))
254  .EmplaceRow(std::forward<Ts>(values)...)
255  .Build();
256 }
257 
271  internal::WriteMutationBuilder<internal::InsertOrUpdateOp>;
272 
285 template <typename... Ts>
286 Mutation MakeInsertOrUpdateMutation(std::string table_name,
287  std::vector<std::string> columns,
288  Ts&&... values) {
289  return InsertOrUpdateMutationBuilder(std::move(table_name),
290  std::move(columns))
291  .EmplaceRow(std::forward<Ts>(values)...)
292  .Build();
293 }
294 
308  internal::WriteMutationBuilder<internal::ReplaceOp>;
309 
322 template <typename... Ts>
323 Mutation MakeReplaceMutation(std::string table_name,
324  std::vector<std::string> columns, Ts&&... values) {
325  return ReplaceMutationBuilder(std::move(table_name), std::move(columns))
326  .EmplaceRow(std::forward<Ts>(values)...)
327  .Build();
328 }
329 
343 
356 inline Mutation MakeDeleteMutation(std::string table_name, KeySet keys) {
357  return DeleteMutationBuilder(std::move(table_name), std::move(keys)).Build();
358 }
359 
360 } // namespace SPANNER_CLIENT_NS
361 } // namespace spanner
362 } // namespace cloud
363 } // namespace google
364 
365 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_MUTATIONS_H
Mutation MakeReplaceMutation(std::string table_name, std::vector< std::string > columns, Ts &&... values)
Creates a simple "replace" mutation for the values in values.
Definition: mutations.h:323
google::spanner::v1::Mutation as_proto() const &
Definition: mutations.h:69
Mutation MakeInsertMutation(std::string table_name, std::vector< std::string > columns, Ts &&... values)
Creates a simple insert mutation for the values in values.
Definition: mutations.h:216
The Value class represents a type-safe, nullable Spanner value.
Definition: value.h:162
void PrintTo(Mutation const &m, std::ostream *os)
Definition: mutations.cc:29
internal::WriteMutationBuilder< internal::UpdateOp > UpdateMutationBuilder
A helper class to construct "update" mutations.
Definition: mutations.h:236
std::vector< Mutation > Mutations
An ordered sequence of mutations to pass to Client::Commit() or return from the Client::Commit() muta...
Definition: mutations.h:95
bool operator==(Backup const &a, Backup const &b)
Definition: backup.cc:29
Mutation MakeInsertOrUpdateMutation(std::string table_name, std::vector< std::string > columns, Ts &&... values)
Creates a simple "insert or update" mutation for the values in values.
Definition: mutations.h:286
Mutation MakeUpdateMutation(std::string table_name, std::vector< std::string > columns, Ts &&... values)
Creates a simple update mutation for the values in values.
Definition: mutations.h:251
The KeySet class is a regular type that represents a collection of Keys.
Definition: keys.h:158
Contains all the Cloud Spanner C++ client types and functions.
google::spanner::v1::Mutation as_proto() &&
Convert the mutation to the underlying proto.
Definition: mutations.h:68
internal::WriteMutationBuilder< internal::InsertOrUpdateOp > InsertOrUpdateMutationBuilder
A helper class to construct "insert_or_update" mutations.
Definition: mutations.h:271
internal::DeleteMutationBuilder DeleteMutationBuilder
A helper class to construct "delete" mutations.
Definition: mutations.h:342
friend bool operator!=(Mutation const &lhs, Mutation const &rhs)
Definition: mutations.h:63
Mutation MakeDeleteMutation(std::string table_name, KeySet keys)
Creates a simple "delete" mutation for the values in keys.
Definition: mutations.h:356
#define SPANNER_CLIENT_NS
Definition: version.h:22
internal::WriteMutationBuilder< internal::InsertOp > InsertMutationBuilder
A helper class to construct "insert" mutations.
Definition: mutations.h:201
A wrapper for Cloud Spanner mutations.
Definition: mutations.h:48
internal::WriteMutationBuilder< internal::ReplaceOp > ReplaceMutationBuilder
A helper class to construct "replace" mutations.
Definition: mutations.h:308