0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-25 23:14:13 +01:00

ircd::stats: Use vector for item iteration; add name convenience member.

This commit is contained in:
Jason Volk 2020-12-18 03:25:16 -08:00
parent e86e42cdf9
commit f7945918c6
4 changed files with 41 additions and 24 deletions

View file

@ -55,7 +55,7 @@ namespace ircd::stats
template<> struct item<uint16_t>;
extern const size_t NAME_MAX_LEN;
extern std::map<string_view, item<void> *> items;
extern std::vector<item<void> *> items;
string_view string(const mutable_buffer &, const item<void> &);
std::ostream &operator<<(std::ostream &, const item<void> &);
@ -78,6 +78,7 @@ struct ircd::stats::item<void>
{
std::type_index type {typeid(void)};
json::strung feature;
json::string name;
public:
// Access features

View file

@ -95,12 +95,11 @@ ircd::stats::item<void>::item(const std::type_index &type,
{
opts
}
,name
{
this->operator[]("name")
}
{
const json::string name
{
this->operator[]("name")
};
if(unlikely(!name))
throw invalid
{
@ -116,32 +115,49 @@ ircd::stats::item<void>::item(const std::type_index &type,
NAME_MAX_LEN
};
if(unlikely(!items.emplace(name, this).second))
static const auto less
{
[](const auto &a, const auto &b)
{
return a->name < b->name;
}
};
const auto exists
{
std::binary_search(begin(items), end(items), this, less)
};
if(unlikely(exists))
throw invalid
{
"Stats item named '%s' already exists",
name
};
if(items.empty())
items.reserve(4096);
items.emplace_back(this);
std::sort(begin(items), end(items), less);
}
ircd::stats::item<void>::~item()
noexcept
{
const json::string name
if(!name)
return;
const auto eit
{
this->operator[]("name")
std::remove_if(begin(items), end(items), [this]
(const auto &item)
{
return item->name == this->name;
})
};
if(name)
{
const auto it
{
items.find(name)
};
assert(data(it->first) == data(name));
items.erase(it);
}
items.erase(eit, end(items));
}
ircd::string_view

View file

@ -1599,9 +1599,9 @@ console_cmd__stats(opt &out, const string_view &line)
param[0] == "-a"? param[1]: param[0]
};
for(const auto &[name_, item] : stats::items)
for(const auto &item : stats::items)
{
if(prefix && !startswith(name_, prefix))
if(prefix && !startswith(item->name, prefix))
continue;
assert(item);
@ -1615,7 +1615,7 @@ console_cmd__stats(opt &out, const string_view &line)
const fmt::bsprintf<128> name
{
"%s ", trunc(name_, name_width)
"%s ", trunc(item->name, name_width)
};
out

View file

@ -51,12 +51,12 @@ ircd::stats::get_stats(client &client,
ircd::time<milliseconds>()
};
for(const auto &[name_, item] : items)
for(const auto &item : items)
{
char buf[256], name[2][128], val[64];
const string_view _name
{
replace(name[0], name_, '.', '_')
replace(name[0], item->name, '.', '_')
};
const string_view line