🐎 optimized PruneUsers() for if logs are disabled

This commit is contained in:
EntireTwix 2021-07-23 15:36:24 -07:00
parent 2b7330f48a
commit dbaa6c6beb
3 changed files with 20 additions and 0 deletions

View file

@ -56,7 +56,11 @@ public:
BankResponse SetBal(const std::string &name, uint32_t amount) noexcept; BankResponse SetBal(const std::string &name, uint32_t amount) noexcept;
BankResponse ImpactBal(const std::string &name, int64_t amount) noexcept; BankResponse ImpactBal(const std::string &name, int64_t amount) noexcept;
bool Contains(const std::string &name) const noexcept; bool Contains(const std::string &name) const noexcept;
#if MAX_LOG_SIZE > 0
BankResponse PruneUsers(time_t threshold_time, uint32_t threshold_bal) noexcept; BankResponse PruneUsers(time_t threshold_time, uint32_t threshold_bal) noexcept;
#else
BankResponse PruneUsers(uint32_t threshold_bal) noexcept;
#endif
BankResponse AddUser(const std::string &name, uint32_t init_bal, const std::string &init_pass) noexcept; BankResponse AddUser(const std::string &name, uint32_t init_bal, const std::string &init_pass) noexcept;
BankResponse DelUser(const std::string &name) noexcept; BankResponse DelUser(const std::string &name) noexcept;

View file

@ -181,7 +181,11 @@ bool Bank::Contains(const std::string &name) const noexcept
{ {
return ValidUsername(name) && users.contains(name); return ValidUsername(name) && users.contains(name);
} }
#if MAX_LOG_SIZE > 0
BankResponse Bank::PruneUsers(time_t threshold_time, uint32_t threshold_bal) noexcept BankResponse Bank::PruneUsers(time_t threshold_time, uint32_t threshold_bal) noexcept
#else
BankResponse Bank::PruneUsers(uint32_t threshold_bal) noexcept
#endif
{ {
std::unique_lock<std::shared_mutex> lock{iter_lock}; std::unique_lock<std::shared_mutex> lock{iter_lock};
size_t deleted_count = 0; size_t deleted_count = 0;

View file

@ -235,6 +235,7 @@ void api::PruneUsers(req_args) const
} }
else else
{ {
#if MAX_LOG_SIZE > 0
auto time = doc.find_field("time").get_int64(); auto time = doc.find_field("time").get_int64();
auto amount = doc.find_field("amount").get_uint64(); auto amount = doc.find_field("amount").get_uint64();
if (time.error() || amount.error()) if (time.error() || amount.error())
@ -245,6 +246,17 @@ void api::PruneUsers(req_args) const
{ {
res = bank.PruneUsers(time.value(), amount.value()); res = bank.PruneUsers(time.value(), amount.value());
} }
#else
auto amount = doc.find_field("amount").get_uint64();
if (amount.error())
{
res = BankResponse{k400BadRequest, "\"Missing JSON arg(s)\""};
}
else
{
res = bank.PruneUsers(amount.value());
}
#endif
} }
RESPONSE_PARSE(std::move(res)); RESPONSE_PARSE(std::move(res));
} }