From dbaa6c6bebbbc78627c3d9f19bda65e8d8a8081d Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Fri, 23 Jul 2021 15:36:24 -0700 Subject: [PATCH] :racehorse: optimized `PruneUsers()` for if logs are disabled --- include/bank.h | 4 ++++ src/bank.cpp | 4 ++++ src/bank_api.cpp | 12 ++++++++++++ 3 files changed, 20 insertions(+) diff --git a/include/bank.h b/include/bank.h index e72cd8c..ea8d975 100644 --- a/include/bank.h +++ b/include/bank.h @@ -56,7 +56,11 @@ public: BankResponse SetBal(const std::string &name, uint32_t amount) noexcept; BankResponse ImpactBal(const std::string &name, int64_t amount) 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; +#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 DelUser(const std::string &name) noexcept; diff --git a/src/bank.cpp b/src/bank.cpp index d61f859..c60da37 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -181,7 +181,11 @@ bool Bank::Contains(const std::string &name) const noexcept { return ValidUsername(name) && users.contains(name); } +#if MAX_LOG_SIZE > 0 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 lock{iter_lock}; size_t deleted_count = 0; diff --git a/src/bank_api.cpp b/src/bank_api.cpp index 25eb521..0659ce7 100644 --- a/src/bank_api.cpp +++ b/src/bank_api.cpp @@ -235,6 +235,7 @@ void api::PruneUsers(req_args) const } else { +#if MAX_LOG_SIZE > 0 auto time = doc.find_field("time").get_int64(); auto amount = doc.find_field("amount").get_uint64(); if (time.error() || amount.error()) @@ -245,6 +246,17 @@ void api::PruneUsers(req_args) const { 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)); }