🐎 balance can be accumulated rather then each account adding to return_on_del individually

This commit is contained in:
EntireTwix 2021-07-23 15:30:44 -07:00
parent 86ab683704
commit 2b7330f48a

View file

@ -58,8 +58,7 @@ size_t Bank::SumBal() const noexcept
BankResponse Bank::GetBal(const std::string &name) const noexcept BankResponse Bank::GetBal(const std::string &name) const noexcept
{ {
uint32_t res = 0; uint32_t res = 0;
if (!ValidUsername(name) || !users.if_contains(name, [&res](const User &u) if (!ValidUsername(name) || !users.if_contains(name, [&res](const User &u) { res = u.balance; }))
{ res = u.balance; }))
{ {
return {k404NotFound, "\"User not found\""}; return {k404NotFound, "\"User not found\""};
} }
@ -72,8 +71,7 @@ BankResponse Bank::GetBal(const std::string &name) const noexcept
BankResponse Bank::GetLogs(const std::string &name) noexcept BankResponse Bank::GetLogs(const std::string &name) noexcept
{ {
BankResponse res; BankResponse res;
if (!users.modify_if(name, [&res](User &u) if (!users.modify_if(name, [&res](User &u) { res = {k200OK, u.log.GetLogs()}; }))
{ res = {k200OK, u.log.GetLogs()}; }))
{ {
return {k404NotFound, "\"User not found\""}; return {k404NotFound, "\"User not found\""};
} }
@ -127,14 +125,12 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
if (res.first == k200OK) if (res.first == k200OK)
{ {
#if MAX_LOG_SIZE > 0 #if MAX_LOG_SIZE > 0
users.modify_if(b_name, [current_time, &a_name, &b_name, amount](User &b) users.modify_if(b_name, [current_time, &a_name, &b_name, amount](User &b) {
{ b.balance += amount;
b.balance += amount; b.log.AddTrans(a_name, b_name, amount, current_time);
b.log.AddTrans(a_name, b_name, amount, current_time); });
});
#else #else
users.modify_if(b_name, [amount](User &b) users.modify_if(b_name, [amount](User &b) { b.balance += amount; });
{ b.balance += amount; });
#endif #endif
SET_CHANGES_ON; SET_CHANGES_ON;
} }
@ -143,21 +139,18 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
bool Bank::VerifyPassword(const std::string &name, const std::string_view &attempt) const noexcept bool Bank::VerifyPassword(const std::string &name, const std::string_view &attempt) const noexcept
{ {
bool res = false; bool res = false;
users.if_contains(name, [&res, &attempt](const User &u) users.if_contains(name, [&res, &attempt](const User &u) { res = (u.password == xxHashStringGen{}(attempt)); });
{ res = (u.password == xxHashStringGen{}(attempt)); });
return res; return res;
} }
void Bank::ChangePassword(const std::string &name, const std::string &new_pass) noexcept void Bank::ChangePassword(const std::string &name, const std::string &new_pass) noexcept
{ {
SET_CHANGES_ON; SET_CHANGES_ON;
users.modify_if(name, [&new_pass](User &u) users.modify_if(name, [&new_pass](User &u) { u.password = xxHashStringGen{}(new_pass); });
{ u.password = xxHashStringGen{}(new_pass); });
} }
BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept
{ {
if (ValidUsername(name) && users.modify_if(name, [amount](User &u) if (ValidUsername(name) && users.modify_if(name, [amount](User &u) { u.balance = amount; }))
{ u.balance = amount; }))
{ {
SET_CHANGES_ON; SET_CHANGES_ON;
return {k204NoContent, std::nullopt}; //returns new balance return {k204NoContent, std::nullopt}; //returns new balance
@ -174,8 +167,7 @@ BankResponse Bank::ImpactBal(const std::string &name, int64_t amount) noexcept
return {k400BadRequest, "\"Amount cannot be 0\""}; return {k400BadRequest, "\"Amount cannot be 0\""};
} }
uint32_t balance; uint32_t balance;
if (ValidUsername(name) && users.modify_if(name, [&balance, amount](User &u) if (ValidUsername(name) && users.modify_if(name, [&balance, amount](User &u) { balance = (u.balance < (amount * -1) ? u.balance = 0 : u.balance += amount); }))
{ balance = (u.balance < (amount * -1) ? u.balance = 0 : u.balance += amount); }))
{ {
SET_CHANGES_ON; SET_CHANGES_ON;
return {k200OK, std::to_string(balance)}; //may return new balance return {k200OK, std::to_string(balance)}; //may return new balance
@ -193,12 +185,12 @@ BankResponse Bank::PruneUsers(time_t threshold_time, uint32_t threshold_bal) noe
{ {
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;
uint32_t bal = 0;
for (const auto &u : users) for (const auto &u : users)
{ {
#if RETURN_ON_DEL #if RETURN_ON_DEL
uint32_t bal;
if (users.erase_if(u.first, [threshold_time, threshold_bal, &bal, &deleted_count](User &u) -> bool { if (users.erase_if(u.first, [threshold_time, threshold_bal, &bal, &deleted_count](User &u) -> bool {
bal = u.balance; bal += u.balance;
#else #else
if (users.erase_if(u.first, [threshold_time, threshold_bal, &deleted_count](User &u) -> bool { if (users.erase_if(u.first, [threshold_time, threshold_bal, &deleted_count](User &u) -> bool {
#endif #endif
@ -209,17 +201,17 @@ BankResponse Bank::PruneUsers(time_t threshold_time, uint32_t threshold_bal) noe
#endif #endif
})) }))
{ {
#if RETURN_ON_DEL
if(bal)
{
users.modify_if(return_account, [bal](User &u) { u.balance += bal; });
}
#endif
SET_CHANGES_ON; SET_CHANGES_ON;
++deleted_count; ++deleted_count;
} }
} }
#if RETURN_ON_DEL
if (bal)
{
users.modify_if(return_account, [bal](User &u) { u.balance += bal; });
}
#endif
return {k200OK, std::to_string(deleted_count)}; return {k200OK, std::to_string(deleted_count)};
} }
@ -246,12 +238,10 @@ BankResponse Bank::DelUser(const std::string &name) noexcept
std::shared_lock<std::shared_mutex> lock{iter_lock}; std::shared_lock<std::shared_mutex> lock{iter_lock};
#if RETURN_ON_DEL #if RETURN_ON_DEL
uint32_t bal; uint32_t bal;
if (users.if_contains(name, [&bal](const User &u) if (users.if_contains(name, [&bal](const User &u) { bal = u.balance; }) &&
{ bal = u.balance; }) &&
bal) bal)
{ {
users.modify_if(return_account, [bal](User &u) users.modify_if(return_account, [bal](User &u) { u.balance += bal; });
{ u.balance += bal; });
} }
#endif #endif
if (ValidUsername(name) && users.erase(name)) if (ValidUsername(name) && users.erase(name))
@ -270,12 +260,10 @@ void Bank::DelSelf(const std::string &name) noexcept
std::shared_lock<std::shared_mutex> lock{iter_lock}; std::shared_lock<std::shared_mutex> lock{iter_lock};
#if RETURN_ON_DEL #if RETURN_ON_DEL
uint32_t bal; uint32_t bal;
if (users.if_contains(name, [&bal](const User &u) if (users.if_contains(name, [&bal](const User &u) { bal = u.balance; }) &&
{ bal = u.balance; }) &&
bal) bal)
{ {
users.modify_if(return_account, [bal](User &u) users.modify_if(return_account, [bal](User &u) { u.balance += bal; });
{ u.balance += bal; });
} }
#endif #endif
SET_CHANGES_ON; SET_CHANGES_ON;
@ -306,11 +294,10 @@ const char *Bank::Save()
std::unique_lock<std::shared_mutex> lock{iter_lock}; std::unique_lock<std::shared_mutex> lock{iter_lock};
for (const auto &u : users) for (const auto &u : users)
{ {
users.if_contains(u.first, [&users_copy, &u](const User &u_val) users.if_contains(u.first, [&users_copy, &u](const User &u_val) {
{ users_copy.users.emplace_back(u_val.Encode());
users_copy.users.emplace_back(u_val.Encode()); users_copy.keys.emplace_back(u.first);
users_copy.keys.emplace_back(u.first); });
});
} }
} }
FBE::bank_dom::GlobalFinalModel writer; FBE::bank_dom::GlobalFinalModel writer;