diff --git a/src/bank.cpp b/src/bank.cpp index 1a654be..ab6080e 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -1,13 +1,29 @@ #include "bank.h" +#if MULTI_THREADED +phmap::parallel_flat_hash_map< + std::string, User, + xxHashStringGen, + phmap::priv::hash_default_eq, + phmap::priv::Allocator>, + 4UL, + std::mutex> + Bank::users; +#else +phmap::parallel_flat_hash_map Bank::users; +#endif + +#if CONSERVATIVE_DISK_SAVE +ChangeFlag Bank::save_flag; +#endif + +std::shared_mutex Bank::iter_lock; +std::string Bank::admin_account; + using namespace drogon; #if CONSERVATIVE_DISK_SAVE -#if MULTI_THREADED #define SET_CHANGES_ON save_flag.SetChangesOn() -#else -#define SET_CHANGES_ON save_flag = true -#endif #endif __attribute__((always_inline)) inline bool ValidUsername(const std::string &name) noexcept @@ -58,8 +74,7 @@ size_t Bank::SumBal() noexcept BankResponse Bank::GetBal(const std::string &name) noexcept { uint32_t res = 0; - if (!ValidUsername(name) || !Bank::users.if_contains(name, [&res](const User &u) - { res = u.balance; })) + if (!ValidUsername(name) || !Bank::users.if_contains(name, [&res](const User &u) { res = u.balance; })) { return {k404NotFound, "\"User not found\""}; } @@ -72,8 +87,7 @@ BankResponse Bank::GetBal(const std::string &name) noexcept BankResponse Bank::GetLogs(const std::string &name) noexcept { BankResponse res; - if (!Bank::users.modify_if(name, [&res](User &u) - { res = {k200OK, u.log.GetLogs()}; })) + if (!Bank::users.modify_if(name, [&res](User &u) { res = {k200OK, u.log.GetLogs()}; })) { return {k404NotFound, "\"User not found\""}; } @@ -127,14 +141,12 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam if (res.first == k200OK) { #if MAX_LOG_SIZE > 0 - Bank::users.modify_if(b_name, [current_time, &a_name, &b_name, amount](User &b) - { - b.balance += amount; - b.log.AddTrans(a_name, b_name, amount, current_time); - }); + Bank::users.modify_if(b_name, [current_time, &a_name, &b_name, amount](User &b) { + b.balance += amount; + b.log.AddTrans(a_name, b_name, amount, current_time); + }); #else - Bank::users.modify_if(b_name, [amount](User &b) - { b.balance += amount; }); + Bank::users.modify_if(b_name, [amount](User &b) { b.balance += amount; }); #endif SET_CHANGES_ON; } @@ -143,21 +155,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) noexcept { bool res = false; - Bank::users.if_contains(name, [&res, &attempt](const User &u) - { res = (u.password == xxHashStringGen{}(attempt)); }); + Bank::users.if_contains(name, [&res, &attempt](const User &u) { res = (u.password == xxHashStringGen{}(attempt)); }); return res; } void Bank::ChangePassword(const std::string &name, const std::string &new_pass) noexcept { SET_CHANGES_ON; - Bank::users.modify_if(name, [&new_pass](User &u) - { u.password = xxHashStringGen{}(new_pass); }); + Bank::users.modify_if(name, [&new_pass](User &u) { u.password = xxHashStringGen{}(new_pass); }); } BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept { - if (ValidUsername(name) && Bank::users.modify_if(name, [amount](User &u) - { u.balance = amount; })) + if (ValidUsername(name) && Bank::users.modify_if(name, [amount](User &u) { u.balance = amount; })) { SET_CHANGES_ON; return {k204NoContent, std::nullopt}; //returns new balance @@ -174,8 +183,7 @@ BankResponse Bank::ImpactBal(const std::string &name, int64_t amount) noexcept return {k400BadRequest, "\"Amount cannot be 0\""}; } uint32_t balance; - if (ValidUsername(name) && Bank::users.modify_if(name, [&balance, amount](User &u) - { balance = (u.balance < (amount * -1) ? u.balance = 0 : u.balance += amount); })) + if (ValidUsername(name) && Bank::users.modify_if(name, [&balance, amount](User &u) { balance = (u.balance < (amount * -1) ? u.balance = 0 : u.balance += amount); })) { SET_CHANGES_ON; return {k200OK, std::to_string(balance)}; //may return new balance @@ -201,19 +209,17 @@ BankResponse Bank::PruneUsers(uint32_t threshold_bal) noexcept for (const auto &u : users) { #if RETURN_ON_DEL - if (Bank::users.erase_if(u.first, [threshold_time, threshold_bal, &bal, &deleted_count](User &u) -> bool - { - bal += u.balance; + if (Bank::users.erase_if(u.first, [threshold_time, threshold_bal, &bal, &deleted_count](User &u) -> bool { + bal += u.balance; #else - if (Bank::users.erase_if(u.first, [threshold_time, threshold_bal, &deleted_count](User &u) -> bool - { + if (Bank::users.erase_if(u.first, [threshold_time, threshold_bal, &deleted_count](User &u) -> bool { #endif #if MAX_LOG_SIZE > 0 - return ((!u.log.data.size() || u.log.data.back().time < threshold_time) && u.balance < threshold_bal); + return ((!u.log.data.size() || u.log.data.back().time < threshold_time) && u.balance < threshold_bal); #else - return (u.balance < threshold_bal); + return (u.balance < threshold_bal); #endif - })) + })) { SET_CHANGES_ON; @@ -223,8 +229,7 @@ BankResponse Bank::PruneUsers(uint32_t threshold_bal) noexcept #if RETURN_ON_DEL if (bal) { - Bank::users.modify_if(return_account, [bal](User &u) - { u.balance += bal; }); + Bank::users.modify_if(return_account, [bal](User &u) { u.balance += bal; }); } #endif return {k200OK, std::to_string(deleted_count)}; @@ -253,12 +258,10 @@ BankResponse Bank::DelUser(const std::string &name) noexcept std::shared_lock lock{iter_lock}; #if RETURN_ON_DEL uint32_t bal; - if (Bank::users.if_contains(name, [&bal](const User &u) - { bal = u.balance; }) && + if (Bank::users.if_contains(name, [&bal](const User &u) { bal = u.balance; }) && bal) { - Bank::users.modify_if(return_account, [bal](User &u) - { u.balance += bal; }); + Bank::users.modify_if(return_account, [bal](User &u) { u.balance += bal; }); } #endif if (ValidUsername(name) && Bank::users.erase(name)) @@ -277,12 +280,10 @@ void Bank::DelSelf(const std::string &name) noexcept std::shared_lock lock{iter_lock}; #if RETURN_ON_DEL uint32_t bal; - if (Bank::users.if_contains(name, [&bal](const User &u) - { bal = u.balance; }) && + if (Bank::users.if_contains(name, [&bal](const User &u) { bal = u.balance; }) && bal) { - Bank::users.modify_if(return_account, [bal](User &u) - { u.balance += bal; }); + Bank::users.modify_if(return_account, [bal](User &u) { u.balance += bal; }); } #endif SET_CHANGES_ON; @@ -292,13 +293,7 @@ void Bank::DelSelf(const std::string &name) noexcept const char *Bank::Save() { #if CONSERVATIVE_DISK_SAVE - if ( -#if MULTI_THREADED - save_flag.GetChangeState() -#else - save_flag -#endif - ) + if (save_flag.GetChangeState()) { #endif std::ofstream users_save(users_location, std::ios::out | std::ios::binary); @@ -313,11 +308,10 @@ const char *Bank::Save() std::unique_lock lock{iter_lock}; for (const auto &u : users) { - Bank::users.if_contains(u.first, [&users_copy, &u](const User &u_val) - { - users_copy.users.emplace_back(u_val.Encode()); - users_copy.keys.emplace_back(u.first); - }); + Bank::users.if_contains(u.first, [&users_copy, &u](const User &u_val) { + users_copy.users.emplace_back(u_val.Encode()); + users_copy.keys.emplace_back(u.first); + }); } } FBE::bank_dom::GlobalFinalModel writer; @@ -334,17 +328,15 @@ const char *Bank::Save() throw std::invalid_argument("Error occurred at writing\n"); } #if CONSERVATIVE_DISK_SAVE -#if MULTI_THREADED save_flag.SetChangesOff(); -#else - save_flag = true; -#endif return " to disk...\n"; } else { return " no changes...\n"; } +#else + return " to disk...\n"; #endif } //NOT THREAD SAFE, BY NO MEANS SHOULD THIS BE CALLED WHILE RECEIEVING REQUESTS