🐛 fixing how Conservative saving flag impacts code

This commit is contained in:
EntireTwix 2021-07-03 18:35:28 -07:00
parent 8c373d6f19
commit e157586557
3 changed files with 42 additions and 42 deletions

View file

@ -82,7 +82,9 @@ int main(int argc, char **argv)
Op(bank.ChangePassword("twix", "root"), "change pass: ", 1000000); Op(bank.ChangePassword("twix", "root"), "change pass: ", 1000000);
Op(bank.GetLogs("twix"), "get logs: ", 10000); Op(bank.GetLogs("twix"), "get logs: ", 10000);
Op(bank.Save(), "saving: ", 1); Op(bank.Save(), "saving: ", 1);
#if CONSERVATIVE_DISK_SAVE
Op(bank.GetChangeState(), "change flag: ", 10000); Op(bank.GetChangeState(), "change flag: ", 10000);
#endif
//GetBal scalining test //GetBal scalining test
// std::default_random_engine generator; // std::default_random_engine generator;

View file

@ -51,7 +51,9 @@ private:
public: public:
std::string admin_account; std::string admin_account;
#if CONSERVATIVE_DISK_SAVE
bool GetChangeState() const noexcept; bool GetChangeState() const noexcept;
#endif
BankResponse GetBal(const std::string &name) const noexcept; BankResponse GetBal(const std::string &name) const noexcept;
BankResponse GetLogs(const std::string &name) noexcept; BankResponse GetLogs(const std::string &name) noexcept;

View file

@ -18,6 +18,7 @@ __attribute__((always_inline)) inline bool ValidUsrname(const std::string &name)
using namespace drogon; using namespace drogon;
#if CONSERVATIVE_DISK_SAVE
bool Bank::GetChangeState() const noexcept bool Bank::GetChangeState() const noexcept
{ {
#if MULTI_THREADED #if MULTI_THREADED
@ -26,21 +27,20 @@ bool Bank::GetChangeState() const noexcept
return save_flag; return save_flag;
#endif #endif
} }
#endif
BankResponse BankResponse
Bank::GetBal(const std::string &name) const noexcept Bank::GetBal(const std::string &name) const noexcept
{ {
uint64_t res = 0; uint64_t res = 0;
users.if_contains(name, [&res](const User &u) users.if_contains(name, [&res](const User &u) { res = u.balance + 1; });
{ res = u.balance + 1; });
return res ? BankResponse(k200OK, res - 1) : BankResponse(k404NotFound, "User not found"); return res ? BankResponse(k200OK, res - 1) : BankResponse(k404NotFound, "User not found");
} }
BankResponse Bank::GetLogs(const std::string &name) noexcept BankResponse Bank::GetLogs(const std::string &name) noexcept
{ {
BankResponse res; BankResponse res;
#if MAX_LOG_SIZE > 0 #if MAX_LOG_SIZE > 0
if (!users.modify_if(name, [&res](User &u) if (!users.modify_if(name, [&res](User &u) { res = {k200OK, u.log.GetLog()}; }))
{ res = {k200OK, u.log.GetLog()}; }))
{ {
return BankResponse(k404NotFound, "User not found"); return BankResponse(k404NotFound, "User not found");
} }
@ -71,40 +71,36 @@ 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 std::shared_lock<std::shared_mutex> lock{send_funds_l}; //about 10% of this function's cost
#if MAX_LOG_SIZE > 0 #if MAX_LOG_SIZE > 0
Transaction temp(a_name, b_name, amount); 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 #else
if (!users.modify_if(a_name, [&state, amount](User &a) if (!users.modify_if(a_name, [&state, amount](User &a) {
{
#endif #endif
//if A can afford it //if A can afford it
if (a.balance < amount) if (a.balance < amount)
{ {
state = {k400BadRequest, "Sender has insufficient funds"}; state = {k400BadRequest, "Sender has insufficient funds"};
} }
else else
{ {
a.balance -= amount; a.balance -= amount;
#if MAX_LOG_SIZE > 0 #if MAX_LOG_SIZE > 0
a.log.AddTrans(Transaction(temp)); //about 40% of this function's cost a.log.AddTrans(Transaction(temp)); //about 40% of this function's cost
#endif #endif
state = {k200OK, "Transfer successful!"}; state = {k200OK, "Transfer successful!"};
} }
})) }))
{ {
return {k404NotFound, "Sender does not exist"}; return {k404NotFound, "Sender does not exist"};
} }
if (state.first == k200OK) if (state.first == k200OK)
{ {
#if MAX_LOG_SIZE > 0 #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.balance += amount; b.log.AddTrans(std::move(temp));
b.log.AddTrans(std::move(temp)); }); //about 40% of this function's cost
}); //about 40% of this function's cost
#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
#if CONSERVATIVE_DISK_SAVE #if CONSERVATIVE_DISK_SAVE
@ -120,15 +116,13 @@ 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 Bank::VerifyPassword(std::string_view name, std::string_view attempt) const noexcept
{ {
bool res = false; bool res = false;
users.if_contains(name.data(), [&res, &attempt](const User &u) users.if_contains(name.data(), [&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, std::string &&new_pass) noexcept void Bank::ChangePassword(const std::string &name, std::string &&new_pass) noexcept
{ {
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); });
#if CONSERVATIVE_DISK_SAVE #if CONSERVATIVE_DISK_SAVE
#if MULTI_THREADED #if MULTI_THREADED
save_flag.SetChangesOn(); save_flag.SetChangesOn();
@ -139,8 +133,7 @@ void Bank::ChangePassword(const std::string &name, std::string &&new_pass) noexc
} }
BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept
{ {
if (users.modify_if(name, [amount](User &u) if (users.modify_if(name, [amount](User &u) { u.balance = amount; }))
{ u.balance = amount; }))
{ {
#if CONSERVATIVE_DISK_SAVE #if CONSERVATIVE_DISK_SAVE
#if MULTI_THREADED #if MULTI_THREADED
@ -175,10 +168,12 @@ BankResponse Bank::AddUser(std::string &&name, std::string &&init_pass) noexcept
if (users.try_emplace_l( if (users.try_emplace_l(
std::move(name), [](User &) {}, std::move(init_pass))) std::move(name), [](User &) {}, std::move(init_pass)))
{ {
#if CONSERVATIVE_DISK_SAVE
#if MULTI_THREADED #if MULTI_THREADED
save_flag.SetChangesOn(); save_flag.SetChangesOn();
#else #else
save_flag = true; save_flag = true;
#endif
#endif #endif
return {k200OK, "User added!"}; return {k200OK, "User added!"};
} }
@ -197,10 +192,12 @@ BankResponse Bank::AdminAddUser(std::string &&name, uint32_t init_bal, std::stri
if (users.try_emplace_l( if (users.try_emplace_l(
std::move(name), [](User &) {}, init_bal, std::move(init_pass))) std::move(name), [](User &) {}, init_bal, std::move(init_pass)))
{ {
#if CONSERVATIVE_DISK_SAVE
#if MULTI_THREADED #if MULTI_THREADED
save_flag.SetChangesOn(); save_flag.SetChangesOn();
#else #else
save_flag = true; save_flag = true;
#endif
#endif #endif
return {k200OK, "User added!"}; return {k200OK, "User added!"};
} }
@ -214,23 +211,23 @@ BankResponse Bank::DelUser(const std::string &name) noexcept
std::shared_lock<std::shared_mutex> lock{size_l}; std::shared_lock<std::shared_mutex> lock{size_l};
#if RETURN_ON_DEL #if RETURN_ON_DEL
uint32_t bal; 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;
bal = u.balance; return true;
return true; }))
}))
#else #else
if (users.erase(name)) if (users.erase(name))
#endif #endif
{ {
#if RETURN_ON_DEL #if RETURN_ON_DEL
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 CONSERVATIVE_DISK_SAVE
#if MULTI_THREADED #if MULTI_THREADED
save_flag.SetChangesOn(); save_flag.SetChangesOn();
#else #else
save_flag = true; save_flag = true;
#endif
#endif #endif
return BankResponse(k200OK, "User deleted!"); return BankResponse(k200OK, "User deleted!");
} }
@ -253,8 +250,7 @@ void Bank::Save()
for (const auto &u : users) for (const auto &u : users)
{ {
//we know it contains this key but we call this func to grab mutex //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) users.if_contains(u.first, [&temp, &u](const User &u_val) { temp[u.first.data()] = u_val.Serialize(); });
{ temp[u.first.data()] = u_val.Serialize(); });
} }
} }
if (temp.isNull()) if (temp.isNull())