Google Cloud Bigtable C++ Client 2.13.0
A C++ Client Library for Google Cloud Bigtable
Loading...
Searching...
No Matches
column_family.h
1// Copyright 2017 Google Inc.
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// https://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_BIGTABLE_COLUMN_FAMILY_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_COLUMN_FAMILY_H
17
18#include "google/cloud/bigtable/version.h"
19#include "absl/meta/type_traits.h"
20#include <google/bigtable/admin/v2/bigtable_table_admin.pb.h>
21#include <google/bigtable/admin/v2/table.pb.h>
22#include <google/protobuf/util/message_differencer.h>
23#include <chrono>
24#include <memory>
25#include <string>
26
27namespace google {
28namespace cloud {
29namespace bigtable {
30GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
31/**
32 * Implement a thin wrapper around google::bigtable::admin::v2::GcRule.
33 *
34 * Provides functions to create GcRules in a convenient form.
35 */
36class GcRule {
37 public:
38 /// Create a garbage collection rule that keeps the last @p n versions.
39 static GcRule MaxNumVersions(std::int32_t n) {
40 GcRule tmp;
41 tmp.gc_rule_.set_max_num_versions(n);
42 return tmp;
43 }
44
45 /**
46 * Return a garbage collection rule that deletes cells in a column older than
47 * the given duration.
48 *
49 * The function accepts any instantiation of `std::chrono::duration<>` for the
50 * @p duration parameter. For example:
51 *
52 * @code
53 * auto rule1 = bigtable::GcRule::MaxAge(std::chrono::hours(48));
54 * auto rule2 = bigtable::GcRule::MaxAge(std::chrono::seconds(48 * 3600));
55 * @endcode
56 *
57 * @tparam Rep a placeholder to match the Rep tparam for @p duration type, the
58 * semantics of this template parameter are documented in
59 * `std::chrono::duration<>` (in brief, the underlying arithmetic type
60 * used to store the number of ticks), for our purposes it is simply a
61 * formal parameter.
62 * @tparam Period a placeholder to match the Period tparam for @p duration
63 * type, the semantics of this template parameter are documented in
64 * `std::chrono::duration<>` (in brief, the length of the tick in seconds,
65 * expressed as a `std::ratio<>`), for our purposes it is simply a formal
66 * parameter.
67 *
68 * @see
69 * [std::chrono::duration<>](http://en.cppreference.com/w/cpp/chrono/duration)
70 * for more details.
71 */
72 template <typename Rep, typename Period>
73 static GcRule MaxAge(std::chrono::duration<Rep, Period> duration) {
74 GcRule tmp;
75 auto& max_age = *tmp.gc_rule_.mutable_max_age();
76 auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
77 max_age.set_seconds(seconds.count());
78 std::chrono::nanoseconds nanos =
79 std::chrono::duration_cast<std::chrono::nanoseconds>(duration) -
80 seconds;
81 max_age.set_nanos(static_cast<std::int32_t>(nanos.count()));
82 return tmp;
83 }
84
85 /**
86 * Return a GcRule that deletes cells if all the rules passed in would delete
87 * the cells.
88 *
89 * @tparam GcRuleTypes the type of the GC rule arguments. They must all be
90 * convertible to GcRule.
91 * @param gc_rules the set of GC rules.
92 */
93 template <typename... GcRuleTypes>
94 static GcRule Intersection(GcRuleTypes&&... gc_rules) {
95 // This ugly thing provides a better compile-time error message than just
96 // letting the compiler figure things out N levels deep as it recurses on
97 // `add_intersection()`.
98 static_assert(
99 absl::conjunction<std::is_convertible<GcRuleTypes, GcRule>...>::value,
100 "The arguments to Intersection must be convertible to GcRule");
101 GcRule tmp;
102 auto& intersection = *tmp.gc_rule_.mutable_intersection();
103 std::initializer_list<GcRule> list{std::forward<GcRuleTypes>(gc_rules)...};
104 for (GcRule const& rule : list) {
105 *intersection.add_rules() = rule.as_proto();
106 }
107 return tmp;
108 }
109
110 /**
111 * Return a GcRule that deletes cells if any the rules passed in would delete
112 * the cells.
113 *
114 * @tparam GcRuleTypes the type of the GC rule arguments. They must all be
115 * convertible to GcRule.
116 * @param gc_rules the set of GC rules.
117 */
118 template <typename... GcRuleTypes>
119 static GcRule Union(GcRuleTypes&&... gc_rules) {
120 // This ugly thing provides a better compile-time error message than just
121 // letting the compiler figure things out N levels deep as it recurses on
122 // `add_intersection()`.
123 static_assert(
124 absl::conjunction<std::is_convertible<GcRuleTypes, GcRule>...>::value,
125 "The arguments to Union must be convertible to GcRule");
126 GcRule tmp;
127 auto& gc_rule_union = *tmp.gc_rule_.mutable_union_();
128 std::initializer_list<GcRule> list{std::forward<GcRuleTypes>(gc_rules)...};
129 for (GcRule const& rule : list) {
130 *gc_rule_union.add_rules() = rule.as_proto();
131 }
132 return tmp;
133 }
134
135 /// Convert to the proto form.
136 google::bigtable::admin::v2::GcRule const& as_proto() const& {
137 return gc_rule_;
138 }
139
140 /// Move the internal proto out.
141 google::bigtable::admin::v2::GcRule&& as_proto() && {
142 return std::move(gc_rule_);
143 }
144
145 ///@{
146 /// @name Use default constructors and assignments.
147 GcRule(GcRule&&) = default;
148 GcRule& operator=(GcRule&&) = default;
149 GcRule(GcRule const&) = default;
150 GcRule& operator=(GcRule const&) = default;
151 ///@}
152
153 friend bool operator==(GcRule const& a, GcRule const& b) noexcept {
154 return google::protobuf::util::MessageDifferencer::Equivalent(a.gc_rule_,
155 b.gc_rule_);
156 }
157 friend bool operator!=(GcRule const& a, GcRule const& b) noexcept {
158 return !(a == b);
159 }
160
161 private:
162 GcRule() = default;
163
164 google::bigtable::admin::v2::GcRule gc_rule_;
165};
166
167/**
168 * Define the interfaces to create column family modifications.
169 *
170 * Applications can modify a Cloud Bigtable schema through a set of column
171 * family modifications. These modifications may include creating new column
172 * families, deleting existing column families, or changing the garbage
173 * collection rules for existing column families.
174 *
175 * This class contain helper functions to create the different protos
176 * encapsulating these changes.
177 */
179 public:
180 /// Return a modification that creates a new column family.
181 static ColumnFamilyModification Create(std::string id, GcRule gc) {
183 tmp.mod_.set_id(std::move(id));
184 *tmp.mod_.mutable_create()->mutable_gc_rule() = std::move(gc).as_proto();
185 return tmp;
186 }
187
188 /// Return a modification that creates a new column family.
189 static ColumnFamilyModification Update(std::string id, GcRule gc) {
191 tmp.mod_.set_id(std::move(id));
192 *tmp.mod_.mutable_update()->mutable_gc_rule() = std::move(gc).as_proto();
193 return tmp;
194 }
195
196 /// Return a modification that drops the @p id column family.
197 static ColumnFamilyModification Drop(std::string id) {
199 tmp.mod_.set_id(std::move(id));
200 tmp.mod_.set_drop(true);
201 return tmp;
202 }
203
204 /// Convert to the proto form.
205 ::google::bigtable::admin::v2::ModifyColumnFamiliesRequest::
206 Modification const&
207 as_proto() const& {
208 return mod_;
209 }
210
211 /// Move out the underlying proto contents.
212 ::google::bigtable::admin::v2::ModifyColumnFamiliesRequest::Modification&&
213 as_proto() && {
214 return std::move(mod_);
215 }
216
217 ///@{
218 /// @name Use default constructors and assignments.
223 default;
224 ///@}
225
226 friend bool operator==(ColumnFamilyModification const& a,
227 ColumnFamilyModification const& b) noexcept {
228 return google::protobuf::util::MessageDifferencer::Equivalent(a.mod_,
229 b.mod_);
230 }
231 friend bool operator!=(ColumnFamilyModification const& a,
232 ColumnFamilyModification const& b) noexcept {
233 return !(a == b);
234 }
235
236 private:
237 ColumnFamilyModification() = default;
238
239 ::google::bigtable::admin::v2::ModifyColumnFamiliesRequest::Modification mod_;
240};
241
242GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
243} // namespace bigtable
244} // namespace cloud
245} // namespace google
246
247#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_COLUMN_FAMILY_H
Define the interfaces to create column family modifications.
Definition: column_family.h:178
ColumnFamilyModification & operator=(ColumnFamilyModification const &)=default
ColumnFamilyModification(ColumnFamilyModification const &)=default
static ColumnFamilyModification Create(std::string id, GcRule gc)
Return a modification that creates a new column family.
Definition: column_family.h:181
ColumnFamilyModification(ColumnFamilyModification &&)=default
ColumnFamilyModification & operator=(ColumnFamilyModification &&)=default
friend bool operator==(ColumnFamilyModification const &a, ColumnFamilyModification const &b) noexcept
Definition: column_family.h:226
friend bool operator!=(ColumnFamilyModification const &a, ColumnFamilyModification const &b) noexcept
Definition: column_family.h:231
static ColumnFamilyModification Drop(std::string id)
Return a modification that drops the id column family.
Definition: column_family.h:197
static ColumnFamilyModification Update(std::string id, GcRule gc)
Return a modification that creates a new column family.
Definition: column_family.h:189
::google::bigtable::admin::v2::ModifyColumnFamiliesRequest::Modification const & as_proto() const &
Convert to the proto form.
Definition: column_family.h:207
::google::bigtable::admin::v2::ModifyColumnFamiliesRequest::Modification && as_proto() &&
Move out the underlying proto contents.
Definition: column_family.h:213
Implement a thin wrapper around google::bigtable::admin::v2::GcRule.
Definition: column_family.h:36
GcRule & operator=(GcRule const &)=default
GcRule(GcRule const &)=default
static GcRule Union(GcRuleTypes &&... gc_rules)
Return a GcRule that deletes cells if any the rules passed in would delete the cells.
Definition: column_family.h:119
friend bool operator==(GcRule const &a, GcRule const &b) noexcept
Definition: column_family.h:153
static GcRule MaxNumVersions(std::int32_t n)
Create a garbage collection rule that keeps the last n versions.
Definition: column_family.h:39
google::bigtable::admin::v2::GcRule const & as_proto() const &
Convert to the proto form.
Definition: column_family.h:136
static GcRule MaxAge(std::chrono::duration< Rep, Period > duration)
Return a garbage collection rule that deletes cells in a column older than the given duration.
Definition: column_family.h:73
google::bigtable::admin::v2::GcRule && as_proto() &&
Move the internal proto out.
Definition: column_family.h:141
static GcRule Intersection(GcRuleTypes &&... gc_rules)
Return a GcRule that deletes cells if all the rules passed in would delete the cells.
Definition: column_family.h:94
friend bool operator!=(GcRule const &a, GcRule const &b) noexcept
Definition: column_family.h:157
GcRule & operator=(GcRule &&)=default
Contains all the Cloud Bigtable C++ client APIs.
Definition: admin_client.h:28