Use map for QueryKindAmounts

This commit is contained in:
Mikhail f. Shiryaev 2022-01-10 13:49:53 +01:00
parent ae7e5691b8
commit a8c83dce14
No known key found for this signature in database
GPG Key ID: 4B02ED204C7D93F4
2 changed files with 22 additions and 31 deletions

View File

@ -505,38 +505,33 @@ ProcessList::UserInfo ProcessList::getUserInfo(bool get_profile_events) const
return per_user_infos;
}
void ProcessList::increaseQueryKindAmount(const IAST::QueryKind & query_kind) const
void ProcessList::increaseQueryKindAmount(const IAST::QueryKind & query_kind)
{
if (query_kind == IAST::QueryKind::Insert)
query_kind_amounts->insert++;
else if (query_kind == IAST::QueryKind::Select)
query_kind_amounts->select++;
auto found = query_kind_amounts.find(query_kind);
if (found == query_kind_amounts.end())
query_kind_amounts[query_kind] = 1;
else
found->second += 1;
}
void ProcessList::decreaseQueryKindAmount(const IAST::QueryKind & query_kind) const
void ProcessList::decreaseQueryKindAmount(const IAST::QueryKind & query_kind)
{
if (!(query_kind == IAST::QueryKind::Insert || query_kind == IAST::QueryKind::Select))
return;
QueryAmount amount = getQueryKindAmount(query_kind);
if (amount == 0)
{
throw Exception(ErrorCodes::LOGICAL_ERROR, "Wrong query kind amount: decrease to negative on '{}'", query_kind, amount);
}
if (query_kind == IAST::QueryKind::Insert)
query_kind_amounts->insert--;
else if (query_kind == IAST::QueryKind::Select)
query_kind_amounts->select--;
auto found = query_kind_amounts.find(query_kind);
/// TODO: we could just rebuild the map, as we have saved all query_kind.
if (found == query_kind_amounts.end())
throw Exception(ErrorCodes::LOGICAL_ERROR, "Wrong query kind amount: decrease before increase on '{}'", query_kind);
else if (found->second == 0)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Wrong query kind amount: decrease to negative on '{}'", query_kind, found->second);
else
found->second -= 1;
}
ProcessList::QueryAmount ProcessList::getQueryKindAmount(const IAST::QueryKind & query_kind) const
{
if (query_kind == IAST::QueryKind::Insert)
return query_kind_amounts->insert;
else if (query_kind == IAST::QueryKind::Select)
return query_kind_amounts->select;
else
auto found = query_kind_amounts.find(query_kind);
if (found == query_kind_amounts.end())
return 0;
return found->second;
}
}

View File

@ -271,11 +271,7 @@ public:
/// User -> queries
using UserToQueries = std::unordered_map<String, ProcessListForUser>;
struct QueryKindAmounts
{
QueryAmount insert;
QueryAmount select;
};
using QueryKindAmounts = std::unordered_map<IAST::QueryKind, QueryAmount>;
protected:
friend class ProcessListEntry;
@ -306,10 +302,10 @@ protected:
size_t max_select_queries_amount = 0;
/// amount of queries by query kind.
QueryKindAmounts * query_kind_amounts = new QueryKindAmounts{0, 0};
QueryKindAmounts query_kind_amounts;
void increaseQueryKindAmount(const IAST::QueryKind & query_kind) const;
void decreaseQueryKindAmount(const IAST::QueryKind & query_kind) const;
void increaseQueryKindAmount(const IAST::QueryKind & query_kind);
void decreaseQueryKindAmount(const IAST::QueryKind & query_kind);
QueryAmount getQueryKindAmount(const IAST::QueryKind & query_kind) const;
public: