🐛 multiple methods didnt toggle change flag

This commit is contained in:
EntireTwix 2021-07-03 11:45:39 -07:00
parent 5ac2dcb55b
commit 0b59e273f0

View file

@ -31,14 +31,16 @@ BankResponse
Bank::GetBal(const std::string &name) const noexcept
{
uint64_t res = 0;
users.if_contains(name, [&res](const User &u) { res = u.balance + 1; });
users.if_contains(name, [&res](const User &u)
{ res = u.balance + 1; });
return res ? BankResponse(k200OK, res - 1) : BankResponse(k404NotFound, "User not found");
}
BankResponse Bank::GetLogs(const std::string &name) noexcept
{
BankResponse res;
#if MAX_LOG_SIZE > 0
if (!users.modify_if(name, [&res](User &u) { res = {k200OK, u.log.GetLog()}; }))
if (!users.modify_if(name, [&res](User &u)
{ res = {k200OK, u.log.GetLog()}; }))
{
return BankResponse(k404NotFound, "User not found");
}
@ -69,9 +71,11 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
std::shared_lock<std::shared_mutex> lock{send_funds_l}; //about 10% of this function's cost
#if MAX_LOG_SIZE > 0
Transaction temp(a_name, b_name, amount);
if (!users.modify_if(a_name, [&temp, &state, amount](User &a) {
if (!users.modify_if(a_name, [&temp, &state, amount](User &a)
{
#else
if (!users.modify_if(a_name, [&state, amount](User &a) {
if (!users.modify_if(a_name, [&state, amount](User &a)
{
#endif
//if A can afford it
if (a.balance < amount)
@ -93,12 +97,14 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
if (state.first == k200OK)
{
#if MAX_LOG_SIZE > 0
users.modify_if(b_name, [&temp, amount](User &b) {
users.modify_if(b_name, [&temp, amount](User &b)
{
b.balance += amount;
b.log.AddTrans(std::move(temp));
}); //about 40% of this function's cost
#else
users.modify_if(b_name, [amount](User &b) { b.balance += amount; });
users.modify_if(b_name, [amount](User &b)
{ b.balance += amount; });
#endif
#if CONSERVATIVE_DISK_SAVE
@ -114,13 +120,15 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
bool Bank::VerifyPassword(std::string_view name, std::string_view attempt) const noexcept
{
bool res = false;
users.if_contains(name.data(), [&res, &attempt](const User &u) { res = (u.password == xxHashStringGen{}(attempt)); });
users.if_contains(name.data(), [&res, &attempt](const User &u)
{ res = (u.password == xxHashStringGen{}(attempt)); });
return res;
}
void Bank::ChangePassword(const std::string &name, std::string &&new_pass) noexcept
{
users.modify_if(name, [&new_pass](User &u) { u.password = xxHashStringGen{}(new_pass); });
users.modify_if(name, [&new_pass](User &u)
{ u.password = xxHashStringGen{}(new_pass); });
#if CONSERVATIVE_DISK_SAVE
#if MULTI_THREADED
save_flag.SetChangesOn();
@ -131,7 +139,8 @@ void Bank::ChangePassword(const std::string &name, std::string &&new_pass) noexc
}
BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept
{
if (users.modify_if(name, [amount](User &u) { u.balance = amount; }))
if (users.modify_if(name, [amount](User &u)
{ u.balance = amount; }))
{
#if CONSERVATIVE_DISK_SAVE
#if MULTI_THREADED
@ -163,10 +172,20 @@ BankResponse Bank::AddUser(std::string &&name, std::string &&init_pass) noexcept
return {k400BadRequest, "Invalid Name, breaks size and/or character restrictions"};
}
std::shared_lock<std::shared_mutex> lock{size_l};
return (users.try_emplace_l(
if (users.try_emplace_l(
std::move(name), [](User &) {}, std::move(init_pass)))
? BankResponse(k200OK, "User added!")
: BankResponse(k409Conflict, "User already exists");
{
#if MULTI_THREADED
save_flag.SetChangesOn();
#else
save_flag = true;
#endif
return {k200OK, "User added!"};
}
else
{
return {k409Conflict, "User already exists"};
}
}
BankResponse Bank::AdminAddUser(std::string &&name, uint32_t init_bal, std::string &&init_pass) noexcept
{
@ -175,30 +194,50 @@ BankResponse Bank::AdminAddUser(std::string &&name, uint32_t init_bal, std::stri
return {k400BadRequest, "Invalid Name, breaks size and/or character restrictions"};
}
std::shared_lock<std::shared_mutex> lock{size_l};
return (users.try_emplace_l(
if (users.try_emplace_l(
std::move(name), [](User &) {}, init_bal, std::move(init_pass)))
? BankResponse(k200OK, "User added!")
: BankResponse(k409Conflict, "User already exists");
{
#if MULTI_THREADED
save_flag.SetChangesOn();
#else
save_flag = true;
#endif
return {k200OK, "User added!"};
}
else
{
return {k409Conflict, "User already exists"};
}
}
BankResponse Bank::DelUser(const std::string &name) noexcept
{
std::shared_lock<std::shared_mutex> lock{size_l};
#if RETURN_ON_DEL
uint32_t bal;
if (users.erase_if(name, [this, &bal, &name](User &u) {
if (users.erase_if(name, [this, &bal, &name](User &u)
{
bal = u.balance;
return true;
}))
#else
if (!users.erase(name))
if (users.erase(name))
#endif
{
#if RETURN_ON_DEL
users.modify_if(return_account, [&bal](User &u)
{ u.balance += bal; });
#endif
#if MULTI_THREADED
save_flag.SetChangesOn();
#else
save_flag = true;
#endif
return BankResponse(k200OK, "User deleted!");
}
else
{
return BankResponse(k404NotFound, "User not found");
}
#if RETURN_ON_DEL
users.modify_if(return_account, [&bal](User &u) { u.balance += bal; });
#endif
return BankResponse(k200OK, "User deleted!");
}
void Bank::Save()
{
@ -214,7 +253,8 @@ void Bank::Save()
for (const auto &u : users)
{
//we know it contains this key but we call this func to grab mutex
users.if_contains(u.first, [&temp, &u](const User &u_val) { temp[u.first.data()] = u_val.Serialize(); });
users.if_contains(u.first, [&temp, &u](const User &u_val)
{ temp[u.first.data()] = u_val.Serialize(); });
}
}
if (temp.isNull())