ChangeFlag class

This commit is contained in:
EntireTwix 2021-06-27 20:27:03 -07:00
parent 1ea1618228
commit ba20e461f6
5 changed files with 43 additions and 33 deletions

View file

@ -16,12 +16,13 @@ target_sources(${PROJECT_NAME} PRIVATE
src/admin_filter.cpp src/admin_filter.cpp
src/bank_api.cpp src/bank_api.cpp
src/bank.cpp src/bank.cpp
src/base64.c #temp
src/change_flag.cpp
src/log.cpp src/log.cpp
src/transaction.cpp src/transaction.cpp
src/user_filter.cpp src/user_filter.cpp
src/user.cpp src/user.cpp
src/xxhash.c src/xxhash.c
src/base64.c #temp
) )
target_include_directories(${PROJECT_NAME} PUBLIC include) target_include_directories(${PROJECT_NAME} PUBLIC include)

View file

@ -2,12 +2,15 @@
#include <iostream> //temporary #include <iostream> //temporary
#include <fstream> #include <fstream>
#include <shared_mutex> #include <shared_mutex>
#include <atomic>
#include <drogon/HttpTypes.h> #include <drogon/HttpTypes.h>
#include <parallel-hashmap/parallel_hashmap/phmap.h>
#include "error_responses.hpp" //temporary #include "error_responses.hpp" //temporary
#include "parallel-hashmap/parallel_hashmap/phmap.h"
#include "user.h" #include "user.h"
#if CONSERVATIVE_DISK_SAVE
#include "change_flag.h"
#endif
using BankResponse = std::pair<drogon::HttpStatusCode, Json::Value>; using BankResponse = std::pair<drogon::HttpStatusCode, Json::Value>;
class Bank class Bank
@ -22,9 +25,8 @@ private:
std::mutex> std::mutex>
users; users;
std::atomic<bool> change_flag = false; //if true changes have been made ChangeFlag save_flag;
private:
/** /**
* @brief size_l should be grabbed if the operation MODIFIES the size (shared), this is so that when save claims unique * @brief size_l should be grabbed if the operation MODIFIES the size (shared), this is so that when save claims unique
* *
@ -37,17 +39,10 @@ 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 ChangesSaved() noexcept; //called after saving
#endif
public: public:
std::string admin_pass; std::string admin_pass;
#if CONSERVATIVE_DISK_SAVE bool GetChangeState() const 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;

13
include/change_flag.h Normal file
View file

@ -0,0 +1,13 @@
#pragma once
#include <atomic>
class ChangeFlag
{
private:
std::atomic<bool> change_flag = false; //if true changes have been made
public:
void SetChangesOn() noexcept;
void SetChangesOff() noexcept;
bool GetChangeState() const noexcept;
};

View file

@ -2,20 +2,7 @@
using namespace drogon; using namespace drogon;
#if CONSERVATIVE_DISK_SAVE bool Bank::GetChangeState() const noexcept { return save_flag.GetChangeState(); }
void Bank::ChangesMade() noexcept
{
return change_flag.store(1, std::memory_order_release);
}
void Bank::ChangesSaved() noexcept
{
return change_flag.store(0, std::memory_order_release);
}
bool Bank::GetChangeState() noexcept
{
return change_flag.load(std::memory_order_acquire);
}
#endif
BankResponse Bank::GetBal(const std::string &name) const noexcept BankResponse Bank::GetBal(const std::string &name) const noexcept
{ {
@ -92,7 +79,7 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
b.log.AddTrans(std::move(temp)); b.log.AddTrans(std::move(temp));
}); });
#if CONSERVATIVE_DISK_SAVE #if CONSERVATIVE_DISK_SAVE
ChangesMade(); save_flag.SetChangesOn();
#endif #endif
} }
return state; return state;
@ -118,7 +105,7 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
b.balance += amount; b.balance += amount;
}); });
#if CONSERVATIVE_DISK_SAVE #if CONSERVATIVE_DISK_SAVE
ChangesMade(); save_flag.SetChangesOn();
#endif #endif
} }
return state; return state;
@ -139,7 +126,7 @@ void Bank::ChangePassword(const std::string &name, std::string &&new_pass) noexc
u.password = XXH3_64bits(new_pass.data(), new_pass.size()); u.password = XXH3_64bits(new_pass.data(), new_pass.size());
}); });
#if CONSERVATIVE_DISK_SAVE #if CONSERVATIVE_DISK_SAVE
ChangesMade(); save_flag.SetChangesOn();
#endif #endif
} }
BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept
@ -149,7 +136,7 @@ BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept
})) }))
{ {
#if CONSERVATIVE_DISK_SAVE #if CONSERVATIVE_DISK_SAVE
ChangesMade(); save_flag.SetChangesOn();
#endif #endif
return BankResponse(k200OK, "Balance set!"); return BankResponse(k200OK, "Balance set!");
} }
@ -271,7 +258,7 @@ bool Bank::AdminVerifyPass(const std::string &attempt) noexcept
void Bank::Save() void Bank::Save()
{ {
#if CONSERVATIVE_DISK_SAVE #if CONSERVATIVE_DISK_SAVE
if (GetChangeState()) if (save_flag.GetChangeState())
{ {
#endif #endif
Json::Value temp; Json::Value temp;
@ -300,7 +287,7 @@ void Bank::Save()
user_save.close(); user_save.close();
} }
#if CONSERVATIVE_DISK_SAVE #if CONSERVATIVE_DISK_SAVE
ChangesSaved(); save_flag.SetChangesOff();
} }
#endif #endif
} }

14
src/change_flag.cpp Normal file
View file

@ -0,0 +1,14 @@
#include "change_flag.h"
void ChangeFlag::SetChangesOn() noexcept
{
return change_flag.store(1, std::memory_order_release);
}
void ChangeFlag::SetChangesOff() noexcept
{
return change_flag.store(0, std::memory_order_release);
}
bool ChangeFlag::GetChangeState() const noexcept
{
return change_flag.load(std::memory_order_acquire);
}