Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions google/cloud/bigtable/instance_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ class InstanceConfig {
return *this;
}

InstanceConfig& insert_tag(std::string const& key, std::string const& value) {
(*proto_.mutable_instance()->mutable_tags())[key] = value;
return *this;
}

InstanceConfig& emplace_tag(std::string const& key, std::string value) {
(*proto_.mutable_instance()->mutable_tags())[key] = std::move(value);
return *this;
}
Comment on lines +75 to +78

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To optimize for cases where the key is an rvalue (e.g., a temporary from a string literal), consider adding an overload for emplace_tag that accepts an rvalue reference for the key. This allows moving the key into the map instead of copying it, improving efficiency.

A similar improvement could be made to emplace_label in a follow-up change for consistency.

  InstanceConfig& emplace_tag(std::string const& key, std::string value) {
    (*proto_.mutable_instance()->mutable_tags())[key] = std::move(value);
    return *this;
  }

  InstanceConfig& emplace_tag(std::string&& key, std::string value) {
    (*proto_.mutable_instance()->mutable_tags())[std::move(key)] =
        std::move(value);
    return *this;
  }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


InstanceConfig& emplace_tag(std::string&& key, std::string value) {
(*proto_.mutable_instance()->mutable_tags())[std::move(key)] =
std::move(value);
return *this;
}

// NOLINT: accessors can (and should) be snake_case.
google::bigtable::admin::v2::CreateInstanceRequest const& as_proto() const& {
return proto_;
Expand Down
20 changes: 20 additions & 0 deletions google/cloud/bigtable/instance_config_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ TEST(InstanceConfigTest, SetLabels) {
EXPECT_EQ("qux", proto.instance().labels().at("baz"));
}


TEST(InstanceConfigTest, SetTags) {
InstanceConfig config("my-instance", "pretty name",
{
{"cluster-1", {"somewhere", 7, ClusterConfig::SSD}},
});

config.insert_tag("tagKeys/123", "tagValues/654").emplace_tag("tagKeys/345", "tagValues/987");

auto proto = config.as_proto();
EXPECT_EQ("my-instance", proto.instance_id());
EXPECT_EQ("pretty name", proto.instance().display_name());
ASSERT_EQ(1, proto.clusters_size());

ASSERT_EQ(2, proto.instance().tags_size());
EXPECT_EQ("tagValues/654", proto.instance().tags().at("tagKeys/123"));
EXPECT_EQ("tagValues/987", proto.instance().tags().at("tagKeys/345"));
}


} // namespace
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace bigtable
Expand Down