Skip to content

Commit 6e4e59a

Browse files
authored
cleanup(bigtable): better example code for GC rules (#8969)
1 parent ad5e704 commit 6e4e59a

13 files changed

Lines changed: 62 additions & 160 deletions

google/cloud/bigtable/admin/integration_tests/table_admin_integration_test.cc

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,14 @@ TEST_F(TableAdminIntegrationTest, CreateListGetDeleteTable) {
169169
r.set_parent(instance_name);
170170
r.set_table_id(table_id);
171171

172-
btadmin::GcRule gc_fam;
173-
gc_fam.set_max_num_versions(10);
174-
btadmin::GcRule gc_foo;
175172
auto constexpr kSecondsPerDay =
176173
std::chrono::seconds(std::chrono::hours(24)).count();
177-
gc_foo.mutable_max_age()->set_seconds(kSecondsPerDay);
178174

179175
auto& t = *r.mutable_table();
180176
auto& families = *t.mutable_column_families();
181-
*families["fam"].mutable_gc_rule() = std::move(gc_fam);
182-
*families["foo"].mutable_gc_rule() = std::move(gc_foo);
177+
families["fam"].mutable_gc_rule()->set_max_num_versions(10);
178+
families["foo"].mutable_gc_rule()->mutable_max_age()->set_seconds(
179+
kSecondsPerDay);
183180
for (auto&& split : {"a1000", "a2000", "b3000", "m5000"}) {
184181
r.add_initial_splits()->set_key(std::move(split));
185182
}
@@ -220,20 +217,13 @@ TEST_F(TableAdminIntegrationTest, CreateListGetDeleteTable) {
220217
std::vector<Mod> mods(3);
221218

222219
mods[0].set_id("newfam");
223-
btadmin::GcRule gc_create;
220+
auto& gc_create = *mods[0].mutable_create()->mutable_gc_rule();
224221
auto& intersection = *gc_create.mutable_intersection();
225-
btadmin::GcRule gc_create_1;
226-
gc_create_1.set_max_num_versions(1);
227-
*intersection.add_rules() = std::move(gc_create_1);
228-
btadmin::GcRule gc_create_2;
229-
gc_create_2.mutable_max_age()->set_seconds(7 * kSecondsPerDay);
230-
*intersection.add_rules() = std::move(gc_create_2);
231-
*mods[0].mutable_create()->mutable_gc_rule() = std::move(gc_create);
222+
intersection.add_rules()->set_max_num_versions(1);
223+
intersection.add_rules()->mutable_max_age()->set_seconds(7 * kSecondsPerDay);
232224

233225
mods[1].set_id("fam");
234-
btadmin::GcRule gc_update;
235-
gc_update.set_max_num_versions(2);
236-
*mods[1].mutable_update()->mutable_gc_rule() = std::move(gc_update);
226+
mods[1].mutable_update()->mutable_gc_rule()->set_max_num_versions(2);
237227

238228
mods[2].set_id("foo");
239229
mods[2].set_drop(true);
@@ -303,10 +293,8 @@ TEST_F(TableAdminIntegrationTest, WaitForConsistencyCheck) {
303293
std::string const family = "column_family";
304294

305295
btadmin::Table t;
306-
btadmin::GcRule gc;
307-
gc.set_max_num_versions(10);
308296
auto& families = *t.mutable_column_families();
309-
*families[family].mutable_gc_rule() = std::move(gc);
297+
families[family].mutable_gc_rule()->set_max_num_versions(10);
310298

311299
// Create the new table.
312300
auto table_created =
@@ -399,17 +387,14 @@ TEST_F(TableAdminIntegrationTest, CreateListGetDeleteTableWithLogging) {
399387
r.set_parent(instance_name);
400388
r.set_table_id(table_id);
401389

402-
btadmin::GcRule gc_fam;
403-
gc_fam.set_max_num_versions(10);
404-
btadmin::GcRule gc_foo;
405390
auto constexpr kSecondsPerDay =
406391
std::chrono::seconds(std::chrono::hours(24)).count();
407-
gc_foo.mutable_max_age()->set_seconds(kSecondsPerDay);
408392

409393
auto& t = *r.mutable_table();
410394
auto& families = *t.mutable_column_families();
411-
*families["fam"].mutable_gc_rule() = std::move(gc_fam);
412-
*families["foo"].mutable_gc_rule() = std::move(gc_foo);
395+
families["fam"].mutable_gc_rule()->set_max_num_versions(10);
396+
families["foo"].mutable_gc_rule()->mutable_max_age()->set_seconds(
397+
kSecondsPerDay);
413398
for (auto&& split : {"a1000", "a2000", "b3000", "m5000"}) {
414399
r.add_initial_splits()->set_key(std::move(split));
415400
}
@@ -450,20 +435,13 @@ TEST_F(TableAdminIntegrationTest, CreateListGetDeleteTableWithLogging) {
450435
std::vector<Mod> mods(3);
451436

452437
mods[0].set_id("newfam");
453-
btadmin::GcRule gc_create;
438+
auto& gc_create = *mods[0].mutable_create()->mutable_gc_rule();
454439
auto& intersection = *gc_create.mutable_intersection();
455-
btadmin::GcRule gc_create_1;
456-
gc_create_1.set_max_num_versions(1);
457-
*intersection.add_rules() = std::move(gc_create_1);
458-
btadmin::GcRule gc_create_2;
459-
gc_create_2.mutable_max_age()->set_seconds(7 * kSecondsPerDay);
460-
*intersection.add_rules() = std::move(gc_create_2);
461-
*mods[0].mutable_create()->mutable_gc_rule() = std::move(gc_create);
440+
intersection.add_rules()->set_max_num_versions(1);
441+
intersection.add_rules()->mutable_max_age()->set_seconds(7 * kSecondsPerDay);
462442

463443
mods[1].set_id("fam");
464-
btadmin::GcRule gc_update;
465-
gc_update.set_max_num_versions(2);
466-
*mods[1].mutable_update()->mutable_gc_rule() = std::move(gc_update);
444+
mods[1].mutable_update()->mutable_gc_rule()->set_max_num_versions(2);
467445

468446
mods[2].set_id("foo");
469447
mods[2].set_drop(true);

google/cloud/bigtable/benchmarks/benchmark.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,14 @@ std::string Benchmark::CreateTable() {
103103
auto admin = bigtable_admin::BigtableTableAdminClient(
104104
bigtable_admin::MakeBigtableTableAdminConnection(admin_opts));
105105

106-
google::bigtable::admin::v2::GcRule gc;
107-
gc.set_max_num_versions(1);
108-
109106
google::bigtable::admin::v2::CreateTableRequest r;
110107
r.set_parent(InstanceName(options_.project_id, options_.instance_id));
111108
r.set_table_id(options_.table_id);
112109
for (auto i = 0; i != 10; i++) {
113110
r.add_initial_splits()->set_key("user" + std::to_string(i));
114111
}
115112
auto& families = *r.mutable_table()->mutable_column_families();
116-
*families[kColumnFamily].mutable_gc_rule() = std::move(gc);
113+
families[kColumnFamily].mutable_gc_rule()->set_max_num_versions(1);
117114

118115
(void)admin.CreateTable(std::move(r));
119116
return options_.table_id;

google/cloud/bigtable/benchmarks/mutation_batcher_throughput_benchmark.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,6 @@ int main(int argc, char* argv[]) {
159159
auto generator = google::cloud::internal::MakeDefaultPRNG();
160160
table_id = RandomTableId(generator);
161161

162-
google::bigtable::admin::v2::GcRule gc;
163-
gc.set_max_num_versions(10);
164-
165162
google::bigtable::admin::v2::CreateTableRequest r;
166163
r.set_parent(cbt::InstanceName(options->project_id, options->instance_id));
167164
r.set_table_id(table_id);
@@ -171,7 +168,8 @@ int main(int argc, char* argv[]) {
171168
r.add_initial_splits()->set_key(MakeRowString(key_width, row_index));
172169
}
173170
auto& families = *r.mutable_table()->mutable_column_families();
174-
*families[options->column_family].mutable_gc_rule() = std::move(gc);
171+
families[options->column_family].mutable_gc_rule()->set_max_num_versions(
172+
10);
175173

176174
std::cout << "# Creating Table\n";
177175
auto table = admin.CreateTable(std::move(r));

google/cloud/bigtable/examples/bigtable_hello_app_profile.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,9 @@ void RunAll(std::vector<std::string> const& argv) {
128128
auto table_id = cbt::testing::RandomTableId(generator);
129129

130130
// Create a table to run the tests on
131-
google::bigtable::admin::v2::GcRule gc;
132-
gc.set_max_num_versions(10);
133131
google::bigtable::admin::v2::Table t;
134132
auto& families = *t.mutable_column_families();
135-
*families["fam"].mutable_gc_rule() = std::move(gc);
133+
families["fam"].mutable_gc_rule()->set_max_num_versions(10);
136134
auto schema = admin.CreateTable(cbt::InstanceName(project_id, instance_id),
137135
table_id, std::move(t));
138136
if (!schema) throw std::runtime_error(schema.status().message());

google/cloud/bigtable/examples/bigtable_hello_table_admin.cc

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,14 @@ void HelloWorldTableAdmin(std::vector<std::string> const& argv) {
5555

5656
//! [create table]
5757
// Define the schema
58-
google::bigtable::admin::v2::GcRule gc_max_versions;
59-
gc_max_versions.set_max_num_versions(10);
60-
61-
google::bigtable::admin::v2::GcRule gc_max_age;
6258
auto constexpr kSecondsPerDay =
6359
std::chrono::seconds(std::chrono::hours(24)).count();
64-
gc_max_age.mutable_max_age()->set_seconds(3 * kSecondsPerDay);
6560

6661
google::bigtable::admin::v2::Table t;
6762
auto& families = *t.mutable_column_families();
68-
*families["fam"].mutable_gc_rule() = gc_max_versions;
69-
*families["foo"].mutable_gc_rule() = gc_max_age;
63+
families["fam"].mutable_gc_rule()->set_max_num_versions(10);
64+
families["foo"].mutable_gc_rule()->mutable_max_age()->set_seconds(
65+
3 * kSecondsPerDay);
7066

7167
std::cout << "Creating a table:\n";
7268
std::string instance_name = cbt::InstanceName(project_id, instance_id);
@@ -110,13 +106,10 @@ void HelloWorldTableAdmin(std::vector<std::string> const& argv) {
110106

111107
//! [modify column family]
112108
std::cout << "Update a column family GC rule:\n";
113-
google::bigtable::admin::v2::GcRule updated_gc;
114-
updated_gc.set_max_num_versions(5);
115-
116109
using ::google::bigtable::admin::v2::ModifyColumnFamiliesRequest;
117110
ModifyColumnFamiliesRequest::Modification mod;
118111
mod.set_id("fam");
119-
*mod.mutable_update()->mutable_gc_rule() = std::move(updated_gc);
112+
mod.mutable_update()->mutable_gc_rule()->set_max_num_versions(5);
120113

121114
StatusOr<google::bigtable::admin::v2::Table> updated_schema =
122115
admin.ModifyColumnFamilies(table->name(), {std::move(mod)});

google/cloud/bigtable/examples/bigtable_hello_world.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,9 @@ void BigtableHelloWorld(std::vector<std::string> const& argv) {
5353

5454
//! [create table] [START bigtable_hw_create_table]
5555
// Define the desired schema for the Table.
56-
google::bigtable::admin::v2::GcRule gc;
57-
gc.set_max_num_versions(1);
5856
google::bigtable::admin::v2::Table t;
5957
auto& families = *t.mutable_column_families();
60-
*families["family"].mutable_gc_rule() = std::move(gc);
58+
families["family"].mutable_gc_rule()->set_max_num_versions(1);
6159

6260
// Create a table.
6361
std::string instance_name = cbt::InstanceName(project_id, instance_id);

google/cloud/bigtable/examples/bigtable_table_admin_backup_snippets.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,8 @@ void RunAll(std::vector<std::string> const& argv) {
329329
// Create a table to run the tests on.
330330
google::bigtable::admin::v2::Table t;
331331
auto& families = *t.mutable_column_families();
332-
google::bigtable::admin::v2::GcRule gc1;
333-
gc1.set_max_num_versions(10);
334-
*families["fam"].mutable_gc_rule() = std::move(gc1);
335-
google::bigtable::admin::v2::GcRule gc2;
336-
gc2.set_max_num_versions(3);
337-
*families["foo"].mutable_gc_rule() = std::move(gc2);
332+
families["fam"].mutable_gc_rule()->set_max_num_versions(10);
333+
families["foo"].mutable_gc_rule()->set_max_num_versions(3);
338334

339335
auto table = admin.CreateTable(cbt::InstanceName(project_id, instance_id),
340336
table_id, std::move(t));

google/cloud/bigtable/examples/data_async_snippets.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,9 @@ void RunAll(std::vector<std::string> const& argv) {
342342
// Create a table to run the tests on
343343
std::cout << "\nCreating table to run the examples (" << table_id << ")"
344344
<< std::endl;
345-
google::bigtable::admin::v2::GcRule gc;
346-
gc.set_max_num_versions(10);
347345
google::bigtable::admin::v2::Table t;
348346
auto& families = *t.mutable_column_families();
349-
*families["fam"].mutable_gc_rule() = std::move(gc);
347+
families["fam"].mutable_gc_rule()->set_max_num_versions(10);
350348
auto schema = admin.CreateTable(cbt::InstanceName(project_id, instance_id),
351349
table_id, std::move(t));
352350
if (!schema) throw std::runtime_error(schema.status().message());

google/cloud/bigtable/examples/data_filter_snippets.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -571,12 +571,10 @@ void RunAll(std::vector<std::string> const& argv) {
571571
auto table_id = cbt::testing::RandomTableId(generator);
572572

573573
// Create a table to run the tests on
574-
google::bigtable::admin::v2::GcRule gc;
575-
gc.set_max_num_versions(10);
576574
google::bigtable::admin::v2::Table t;
577575
auto& families = *t.mutable_column_families();
578-
*families["cell_plan"].mutable_gc_rule() = gc;
579-
*families["stats_summary"].mutable_gc_rule() = std::move(gc);
576+
families["cell_plan"].mutable_gc_rule()->set_max_num_versions(10);
577+
families["stats_summary"].mutable_gc_rule()->set_max_num_versions(10);
580578
auto schema = admin.CreateTable(cbt::InstanceName(project_id, instance_id),
581579
table_id, std::move(t));
582580
if (!schema) throw std::runtime_error(schema.status().message());

google/cloud/bigtable/examples/data_snippets.cc

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -714,11 +714,9 @@ void RunMutateExamples(
714714

715715
// Create a table to run the tests on
716716
auto table_id = cbt::testing::RandomTableId(generator);
717-
google::bigtable::admin::v2::GcRule gc;
718-
gc.set_max_num_versions(10);
719717
google::bigtable::admin::v2::Table t;
720718
auto& families = *t.mutable_column_families();
721-
*families["fam"].mutable_gc_rule() = std::move(gc);
719+
families["fam"].mutable_gc_rule()->set_max_num_versions(10);
722720
auto schema = admin.CreateTable(cbt::InstanceName(project_id, instance_id),
723721
table_id, std::move(t));
724722
if (!schema) throw std::runtime_error(schema.status().message());
@@ -744,11 +742,9 @@ void RunWriteExamples(
744742

745743
// Create a table to run the tests on
746744
auto table_id = google::cloud::bigtable::testing::RandomTableId(generator);
747-
google::bigtable::admin::v2::GcRule gc;
748-
gc.set_max_num_versions(11);
749745
google::bigtable::admin::v2::Table t;
750746
auto& families = *t.mutable_column_families();
751-
*families["stats_summary"].mutable_gc_rule() = std::move(gc);
747+
families["stats_summary"].mutable_gc_rule()->set_max_num_versions(11);
752748
auto schema = admin.CreateTable(cbt::InstanceName(project_id, instance_id),
753749
table_id, std::move(t));
754750
if (!schema) throw std::runtime_error(schema.status().message());
@@ -777,11 +773,9 @@ void RunDataExamples(
777773
// Create a table to run the tests on
778774
auto table_id = cbt::testing::RandomTableId(generator);
779775
std::cout << "Creating table " << table_id << std::endl;
780-
google::bigtable::admin::v2::GcRule gc;
781-
gc.set_max_num_versions(10);
782776
google::bigtable::admin::v2::Table t;
783777
auto& families = *t.mutable_column_families();
784-
*families["fam"].mutable_gc_rule() = std::move(gc);
778+
families["fam"].mutable_gc_rule()->set_max_num_versions(10);
785779
auto schema = admin.CreateTable(cbt::InstanceName(project_id, instance_id),
786780
table_id, std::move(t));
787781
if (!schema) throw std::runtime_error(schema.status().message());

0 commit comments

Comments
 (0)