🐎 made logs copy construct from thread_local

This commit is contained in:
EntireTwix 2021-07-11 00:55:24 -07:00
parent 4b7ef754c0
commit f7dbd4c3e0
3 changed files with 10 additions and 10 deletions

View file

@ -23,6 +23,6 @@ public:
#endif #endif
const std::string &GetLogs() noexcept; const std::string &GetLogs() noexcept;
void AddTrans(Transaction &&t) noexcept; void AddTrans(const Transaction &t) noexcept;
Json::Value Serialize() const; // to be removed later Json::Value Serialize() const; // to be removed later
}; };

View file

@ -10,7 +10,7 @@ bool ValidUsername(const std::string &name) noexcept
} }
for (const char &c : name) for (const char &c : name)
{ {
if (!((c > 96 && c < 122) || std::isdigit(c) || c == '_')) if (!((c >= 97 && c <= 122) || std::isdigit(c) || c == '_'))
{ {
return false; return false;
} }
@ -103,9 +103,9 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
BankResponse state; BankResponse state;
std::shared_lock<std::shared_mutex> lock{save_lock}; //about 10% of this function's cost std::shared_lock<std::shared_mutex> lock{save_lock}; //about 10% of this function's cost
#if MAX_LOG_SIZE > 0 #if MAX_LOG_SIZE > 0
Transaction temp(a_name, b_name, amount); static thread_local Transaction temp(a_name, b_name, amount);
#endif #endif
if (!users.modify_if(a_name, [&temp, &state, amount](User &a) { if (!users.modify_if(a_name, [&state, amount](User &a) {
//if A can afford it //if A can afford it
if (a.balance < amount) if (a.balance < amount)
{ {
@ -115,7 +115,7 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
{ {
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(temp); //about 40% of this function's cost
#endif #endif
state = BankResponse(k200OK, std::to_string(a.balance)); state = BankResponse(k200OK, std::to_string(a.balance));
} }
@ -126,9 +126,9 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
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, [amount](User &b) {
b.balance += amount; b.balance += amount;
b.log.AddTrans(std::move(temp)); b.log.AddTrans(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) { b.balance += amount; }); users.modify_if(b_name, [amount](User &b) { b.balance += amount; });

View file

@ -1,10 +1,10 @@
#include "log.h" #include "log.h"
void Log::AddTrans(Transaction &&t) noexcept void Log::AddTrans(const Transaction &t) noexcept
{ {
log_flag.SetChangesOn(); log_flag.SetChangesOn();
#if MAX_LOG_SIZE == 1 #if MAX_LOG_SIZE == 1
data = std::move(t); data = t;
#else #else
if (data.size() == MAX_LOG_SIZE) // If we hit the max size if (data.size() == MAX_LOG_SIZE) // If we hit the max size
{ {
@ -15,7 +15,7 @@ void Log::AddTrans(Transaction &&t) noexcept
data[data.size() - 1] = std::move(t); // Place new in opened spot data[data.size() - 1] = std::move(t); // Place new in opened spot
return; return;
} }
data.push_back(std::move(t)); // In either case we have space under max length, move to new spot data.push_back(t); // In either case we have space under max length, move to new spot
#endif #endif
} }