🐛 patched for MAX_LOG_SIZE == 1 usecase

This commit is contained in:
EntireTwix 2021-07-19 23:33:03 -07:00
parent a37825fd51
commit 3bb7640cd6
4 changed files with 32 additions and 28 deletions

View file

@ -16,9 +16,9 @@ struct User
User(uint32_t init_bal, XXH64_hash_t init_pass) noexcept;
#if MAX_LOG_SIZE > 0
User(uint32_t init_bal, XXH64_hash_t init_pass, const Json::Value &log_j) noexcept;
#endif
User(const bank_dom::User &u) noexcept;
bank_dom::User Encode() const noexcept;
#endif
Json::Value Serialize() const; //to be removed later
};

View file

@ -92,8 +92,11 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
std::shared_lock<std::shared_mutex> lock{iter_lock};
#if MAX_LOG_SIZE > 0
time_t current_time = time(NULL);
if (!users.modify_if(a_name, [current_time, &a_name, &b_name, &res, amount](User &a)
#else
if (!users.modify_if(a_name, [&a_name, &b_name, &res, amount](User &a)
#endif
if (!users.modify_if(a_name, [current_time, &a_name, &b_name, &res, amount](User &a) {
{
//if A can afford it
if (a.balance < amount)
{
@ -201,7 +204,11 @@ BankResponse Bank::PruneUsers(time_t threshold_time, uint32_t threshold_bal) noe
for (const auto &u : users)
{
users.erase_if(u.first, [threshold_time, threshold_bal, &deleted_count](User &u) -> bool {
if (u.log.data.back().time < threshold_time && u.balance < threshold_bal)
#if MAX_LOG_SIZE > 0
if (u.data.back().time < threshold_time && u.balance < threshold_bal)
#else
if (u.balance < threshold_bal)
#endif
{
return ++deleted_count;
}

View file

@ -37,17 +37,14 @@ void api::GetBal(req_args, const std::string &name) const
}
void api::GetLogs(req_args)
{
if constexpr (MAX_LOG_SIZE > 0)
{
#if MAX_LOG_SIZE > 0
RESPONSE_PARSE(bank.GetLogs(NAME_PARAM));
}
else
{
#else
static thread_local auto resp = HttpResponse::newCustomHttpResponse(BankResponse{k404NotFound, "\"Logs are Disabled\""});
CORS;
CACHE_FOREVER;
callback(resp);
}
#endif
}
void api::SendFunds(req_args) const
{

View file

@ -16,9 +16,9 @@ User::User(uint32_t init_bal, const std::string &init_pass) noexcept : balance(i
*/
User::User(uint32_t init_bal, XXH64_hash_t init_pass) noexcept : balance(init_bal), password(init_pass) {}
#if MAX_LOG_SIZE > 0
User::User(const bank_dom::User &u) noexcept : balance(u.balance), password(u.password)
{
#if MAX_LOG_SIZE > 0
if (u.logs)
{
for (uint32_t i = (u.logs.value().data.size() - MAX_LOG_SIZE); i < u.logs.value().data.size(); ++i)
@ -27,8 +27,8 @@ User::User(const bank_dom::User &u) noexcept : balance(u.balance), password(u.pa
log.data.emplace_front(temp.from, temp.to, temp.amount, temp.time);
}
}
}
#endif
}
bank_dom::User User::Encode() const noexcept
{
#if MAX_LOG_SIZE > 0