Add TagSet::toString method

This commit is contained in:
sfc-gh-tclinkenbeard 2022-03-18 20:10:56 -07:00
parent fdab73d7b3
commit 004e73b2f1
3 changed files with 44 additions and 6 deletions

View File

@ -185,7 +185,7 @@ ACTOR Future<bool> throttleCommandActor(Reference<IDatabase> db, std::vector<Str
printf("Tag `%s' has been throttled\n", tokens[3].toString().c_str());
} else if (tokencmp(tokens[1], "off")) {
int nextIndex = 2;
TagSet tagSet;
state TagSet tagSet;
bool throttleTypeSpecified = false;
bool is_error = false;
Optional<TagThrottleType> throttleType = TagThrottleType::MANUAL;
@ -260,12 +260,12 @@ ACTOR Future<bool> throttleCommandActor(Reference<IDatabase> db, std::vector<Str
if (tagSet.size() > 0) {
bool success = wait(ThrottleApi::unthrottleTags(db, tagSet, throttleType, priority));
if (success) {
printf("Unthrottled tag `%s'%s\n", tokens[3].toString().c_str(), priorityString.c_str());
fmt::print("Unthrottled {0}{1}\n", tagSet.toString(), priorityString);
} else {
printf("Tag `%s' was not %sthrottled%s\n",
tokens[3].toString().c_str(),
throttleTypeString,
priorityString.c_str());
fmt::print("{0} was not {1}throttled{2}\n",
tagSet.toString(Capitalize::True),
throttleTypeString,
priorityString);
}
} else {
bool unthrottled = wait(ThrottleApi::unthrottleAll(db, throttleType, priority));

View File

@ -47,6 +47,19 @@ size_t TagSet::size() const {
return tags.size();
}
std::string TagSet::toString(Capitalize capitalize) const {
ASSERT(!tags.empty());
if (tags.size() == 1) {
std::string start = capitalize ? "Tag" : "tag";
return format("%s `%s'", start.c_str(), tags[0].toString().c_str());
}
std::string result = capitalize ? "Tags (" : "tags (";
for (int index = 0; index < tags.size() - 1; ++index) {
result += format("`%s', ", tags[index].toString().c_str());
}
return result + format("`%s')", tags.back().toString().c_str());
}
// Format for throttle key:
//
// tagThrottleKeysPrefix + [auto-throttled (1-byte 0/1)] + [priority (1-byte)] + [tag list]
@ -112,3 +125,24 @@ TagThrottleValue TagThrottleValue::fromValue(const ValueRef& value) {
}
FDB_DEFINE_BOOLEAN_PARAM(ContainsRecommended);
FDB_DEFINE_BOOLEAN_PARAM(Capitalize);
TEST_CASE("TagSet/toString") {
{
TagSet tagSet;
tagSet.addTag("a"_sr);
ASSERT(tagSet.toString() == "tag `a'");
ASSERT(tagSet.toString(Capitalize::True) == "Tag `a'");
}
{
// Order is not guaranteed when multiple tags are present
TagSet tagSet;
tagSet.addTag("a"_sr);
tagSet.addTag("b"_sr);
auto tagString = tagSet.toString();
ASSERT(tagString == "tags (`a', `b')" || tagString == "tags (`b', `a')");
auto capitalizedTagString = tagSet.toString(Capitalize::True);
ASSERT(capitalizedTagString == "Tags (`a', `b')" || capitalizedTagString == "Tags (`b', `a')");
}
return Void();
}

View File

@ -41,6 +41,7 @@ typedef StringRef TransactionTagRef;
typedef Standalone<TransactionTagRef> TransactionTag;
FDB_DECLARE_BOOLEAN_PARAM(ContainsRecommended);
FDB_DECLARE_BOOLEAN_PARAM(Capitalize);
class TagSet {
public:
@ -97,6 +98,9 @@ public:
const Arena& getArena() const { return arena; }
// Used by fdbcli commands
std::string toString(Capitalize = Capitalize::False) const;
private:
size_t bytes;
Arena arena;