mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-17 00:22:14 +11:00
🚚🔥 removed save_flag as bool possibility & moved static declarations from bank_api.cpp
This commit is contained in:
parent
edee4f2e0d
commit
37d885d81d
1 changed files with 49 additions and 57 deletions
106
src/bank.cpp
106
src/bank.cpp
|
|
@ -1,13 +1,29 @@
|
||||||
#include "bank.h"
|
#include "bank.h"
|
||||||
|
|
||||||
|
#if MULTI_THREADED
|
||||||
|
phmap::parallel_flat_hash_map<
|
||||||
|
std::string, User,
|
||||||
|
xxHashStringGen,
|
||||||
|
phmap::priv::hash_default_eq<std::string>,
|
||||||
|
phmap::priv::Allocator<phmap::priv::Pair<const std::string, User>>,
|
||||||
|
4UL,
|
||||||
|
std::mutex>
|
||||||
|
Bank::users;
|
||||||
|
#else
|
||||||
|
phmap::parallel_flat_hash_map<std::string, User, xxHashStringGen> Bank::users;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
|
ChangeFlag<false> Bank::save_flag;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
std::shared_mutex Bank::iter_lock;
|
||||||
|
std::string Bank::admin_account;
|
||||||
|
|
||||||
using namespace drogon;
|
using namespace drogon;
|
||||||
|
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
#if MULTI_THREADED
|
|
||||||
#define SET_CHANGES_ON save_flag.SetChangesOn()
|
#define SET_CHANGES_ON save_flag.SetChangesOn()
|
||||||
#else
|
|
||||||
#define SET_CHANGES_ON save_flag = true
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
__attribute__((always_inline)) inline bool ValidUsername(const std::string &name) noexcept
|
__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
|
BankResponse Bank::GetBal(const std::string &name) noexcept
|
||||||
{
|
{
|
||||||
uint32_t res = 0;
|
uint32_t res = 0;
|
||||||
if (!ValidUsername(name) || !Bank::users.if_contains(name, [&res](const User &u)
|
if (!ValidUsername(name) || !Bank::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 +87,7 @@ BankResponse Bank::GetBal(const std::string &name) noexcept
|
||||||
BankResponse Bank::GetLogs(const std::string &name) noexcept
|
BankResponse Bank::GetLogs(const std::string &name) noexcept
|
||||||
{
|
{
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
if (!Bank::users.modify_if(name, [&res](User &u)
|
if (!Bank::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 +141,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
|
||||||
Bank::users.modify_if(b_name, [current_time, &a_name, &b_name, amount](User &b)
|
Bank::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
|
||||||
Bank::users.modify_if(b_name, [amount](User &b)
|
Bank::users.modify_if(b_name, [amount](User &b) { b.balance += amount; });
|
||||||
{ b.balance += amount; });
|
|
||||||
#endif
|
#endif
|
||||||
SET_CHANGES_ON;
|
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 Bank::VerifyPassword(const std::string &name, const std::string_view &attempt) noexcept
|
||||||
{
|
{
|
||||||
bool res = false;
|
bool res = false;
|
||||||
Bank::users.if_contains(name, [&res, &attempt](const User &u)
|
Bank::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;
|
||||||
Bank::users.modify_if(name, [&new_pass](User &u)
|
Bank::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) && Bank::users.modify_if(name, [amount](User &u)
|
if (ValidUsername(name) && Bank::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 +183,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) && Bank::users.modify_if(name, [&balance, amount](User &u)
|
if (ValidUsername(name) && Bank::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
|
||||||
|
|
@ -201,19 +209,17 @@ BankResponse Bank::PruneUsers(uint32_t threshold_bal) noexcept
|
||||||
for (const auto &u : users)
|
for (const auto &u : users)
|
||||||
{
|
{
|
||||||
#if RETURN_ON_DEL
|
#if RETURN_ON_DEL
|
||||||
if (Bank::users.erase_if(u.first, [threshold_time, threshold_bal, &bal, &deleted_count](User &u) -> bool
|
if (Bank::users.erase_if(u.first, [threshold_time, threshold_bal, &bal, &deleted_count](User &u) -> bool {
|
||||||
{
|
bal += u.balance;
|
||||||
bal += u.balance;
|
|
||||||
#else
|
#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
|
#endif
|
||||||
#if MAX_LOG_SIZE > 0
|
#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
|
#else
|
||||||
return (u.balance < threshold_bal);
|
return (u.balance < threshold_bal);
|
||||||
#endif
|
#endif
|
||||||
}))
|
}))
|
||||||
{
|
{
|
||||||
|
|
||||||
SET_CHANGES_ON;
|
SET_CHANGES_ON;
|
||||||
|
|
@ -223,8 +229,7 @@ BankResponse Bank::PruneUsers(uint32_t threshold_bal) noexcept
|
||||||
#if RETURN_ON_DEL
|
#if RETURN_ON_DEL
|
||||||
if (bal)
|
if (bal)
|
||||||
{
|
{
|
||||||
Bank::users.modify_if(return_account, [bal](User &u)
|
Bank::users.modify_if(return_account, [bal](User &u) { u.balance += bal; });
|
||||||
{ u.balance += bal; });
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return {k200OK, std::to_string(deleted_count)};
|
return {k200OK, std::to_string(deleted_count)};
|
||||||
|
|
@ -253,12 +258,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 (Bank::users.if_contains(name, [&bal](const User &u)
|
if (Bank::users.if_contains(name, [&bal](const User &u) { bal = u.balance; }) &&
|
||||||
{ bal = u.balance; }) &&
|
|
||||||
bal)
|
bal)
|
||||||
{
|
{
|
||||||
Bank::users.modify_if(return_account, [bal](User &u)
|
Bank::users.modify_if(return_account, [bal](User &u) { u.balance += bal; });
|
||||||
{ u.balance += bal; });
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if (ValidUsername(name) && Bank::users.erase(name))
|
if (ValidUsername(name) && Bank::users.erase(name))
|
||||||
|
|
@ -277,12 +280,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 (Bank::users.if_contains(name, [&bal](const User &u)
|
if (Bank::users.if_contains(name, [&bal](const User &u) { bal = u.balance; }) &&
|
||||||
{ bal = u.balance; }) &&
|
|
||||||
bal)
|
bal)
|
||||||
{
|
{
|
||||||
Bank::users.modify_if(return_account, [bal](User &u)
|
Bank::users.modify_if(return_account, [bal](User &u) { u.balance += bal; });
|
||||||
{ u.balance += bal; });
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
SET_CHANGES_ON;
|
SET_CHANGES_ON;
|
||||||
|
|
@ -292,13 +293,7 @@ void Bank::DelSelf(const std::string &name) noexcept
|
||||||
const char *Bank::Save()
|
const char *Bank::Save()
|
||||||
{
|
{
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
if (
|
if (save_flag.GetChangeState())
|
||||||
#if MULTI_THREADED
|
|
||||||
save_flag.GetChangeState()
|
|
||||||
#else
|
|
||||||
save_flag
|
|
||||||
#endif
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
#endif
|
#endif
|
||||||
std::ofstream users_save(users_location, std::ios::out | std::ios::binary);
|
std::ofstream users_save(users_location, std::ios::out | std::ios::binary);
|
||||||
|
|
@ -313,11 +308,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)
|
||||||
{
|
{
|
||||||
Bank::users.if_contains(u.first, [&users_copy, &u](const User &u_val)
|
Bank::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;
|
||||||
|
|
@ -334,17 +328,15 @@ const char *Bank::Save()
|
||||||
throw std::invalid_argument("Error occurred at writing\n");
|
throw std::invalid_argument("Error occurred at writing\n");
|
||||||
}
|
}
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
#if MULTI_THREADED
|
|
||||||
save_flag.SetChangesOff();
|
save_flag.SetChangesOff();
|
||||||
#else
|
|
||||||
save_flag = true;
|
|
||||||
#endif
|
|
||||||
return " to disk...\n";
|
return " to disk...\n";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return " no changes...\n";
|
return " no changes...\n";
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
return " to disk...\n";
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
//NOT THREAD SAFE, BY NO MEANS SHOULD THIS BE CALLED WHILE RECEIEVING REQUESTS
|
//NOT THREAD SAFE, BY NO MEANS SHOULD THIS BE CALLED WHILE RECEIEVING REQUESTS
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue