mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-17 00:22:14 +11:00
🎨 improved CONSERVATIVE_DISK_SAVE flags usage
This commit is contained in:
parent
a4964d2742
commit
3028c6153a
2 changed files with 16 additions and 19 deletions
|
|
@ -37,12 +37,17 @@ private:
|
||||||
*/
|
*/
|
||||||
std::shared_mutex send_funds_l;
|
std::shared_mutex send_funds_l;
|
||||||
|
|
||||||
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
void ChangesMade() noexcept; //called after making changes
|
void ChangesMade() noexcept; //called after making changes
|
||||||
void ChangesSaved() noexcept; //called after saving
|
void ChangesSaved() noexcept; //called after saving
|
||||||
|
#endif
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::string admin_pass;
|
std::string admin_pass;
|
||||||
|
|
||||||
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
bool GetChangeState() noexcept;
|
bool GetChangeState() 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;
|
||||||
|
|
|
||||||
30
src/bank.cpp
30
src/bank.cpp
|
|
@ -2,31 +2,20 @@
|
||||||
|
|
||||||
using namespace drogon;
|
using namespace drogon;
|
||||||
|
|
||||||
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
void Bank::ChangesMade() noexcept
|
void Bank::ChangesMade() noexcept
|
||||||
{
|
{
|
||||||
if constexpr (CONSERVATIVE_DISK_SAVE)
|
return change_flag.store(1, std::memory_order_release);
|
||||||
{
|
|
||||||
return change_flag.store(1, std::memory_order_release);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
void Bank::ChangesSaved() noexcept
|
void Bank::ChangesSaved() noexcept
|
||||||
{
|
{
|
||||||
if constexpr (CONSERVATIVE_DISK_SAVE)
|
return change_flag.store(0, std::memory_order_release);
|
||||||
{
|
|
||||||
return change_flag.store(0, std::memory_order_release);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
bool Bank::GetChangeState() noexcept
|
bool Bank::GetChangeState() noexcept
|
||||||
{
|
{
|
||||||
if constexpr (CONSERVATIVE_DISK_SAVE)
|
return change_flag.load(std::memory_order_acquire);
|
||||||
{
|
|
||||||
return change_flag.load(std::memory_order_acquire);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
BankResponse Bank::GetBal(const std::string &name) const noexcept
|
BankResponse Bank::GetBal(const std::string &name) const noexcept
|
||||||
{
|
{
|
||||||
|
|
@ -76,9 +65,8 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
|
||||||
if constexpr (max_log_size > 0)
|
if constexpr (max_log_size > 0)
|
||||||
{
|
{
|
||||||
Transaction temp(a_name, b_name, amount);
|
Transaction temp(a_name, b_name, amount);
|
||||||
Transaction temp_copy(temp);
|
|
||||||
std::shared_lock<std::shared_mutex> lock{send_funds_l};
|
std::shared_lock<std::shared_mutex> lock{send_funds_l};
|
||||||
users.modify_if(a_name, [&temp_copy, &state, amount](User &a) {
|
users.modify_if(a_name, [&temp, &state, amount](User &a) {
|
||||||
//if A can afford it
|
//if A can afford it
|
||||||
if (a.balance < amount)
|
if (a.balance < amount)
|
||||||
{
|
{
|
||||||
|
|
@ -87,7 +75,7 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
a.balance -= amount;
|
a.balance -= amount;
|
||||||
a.log.AddTrans(std::move(temp_copy));
|
a.log.AddTrans(Transaction(temp));
|
||||||
state = {k200OK, "Transfer successful!"};
|
state = {k200OK, "Transfer successful!"};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -275,8 +263,10 @@ int_fast8_t Bank::SetBal(const std::string &name, const std::string &attempt, ui
|
||||||
|
|
||||||
void Bank::Save()
|
void Bank::Save()
|
||||||
{
|
{
|
||||||
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
if (GetChangeState())
|
if (GetChangeState())
|
||||||
{
|
{
|
||||||
|
#endif
|
||||||
Json::Value temp;
|
Json::Value temp;
|
||||||
|
|
||||||
//loading info into json temp
|
//loading info into json temp
|
||||||
|
|
@ -302,8 +292,10 @@ void Bank::Save()
|
||||||
writer->write(temp, &user_save);
|
writer->write(temp, &user_save);
|
||||||
user_save.close();
|
user_save.close();
|
||||||
}
|
}
|
||||||
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
ChangesSaved();
|
ChangesSaved();
|
||||||
}
|
}
|
||||||
|
#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