diff --git a/include/consts.hpp b/include/consts.hpp index d0b19f4..2a9c176 100644 --- a/include/consts.hpp +++ b/include/consts.hpp @@ -1,8 +1,8 @@ #pragma once // Setting both values to 0 does not compile logging (useful for if disk/memory is very valuable) -constexpr unsigned max_log_size = 100; -constexpr unsigned pre_log_size = 10; +#define MAX_LOG_SIZE 100 +#define PRE_LOG_SIZE 10 constexpr unsigned max_name_size = 50; diff --git a/include/log.h b/include/log.h index cd1cd8a..f56b70f 100644 --- a/include/log.h +++ b/include/log.h @@ -2,8 +2,8 @@ #include // to be removed later #include #include -#include "change_flag.h" #include "consts.hpp" +#include "change_flag.h" #include "transaction.h" struct Log diff --git a/include/user.h b/include/user.h index efec7b3..88e28fc 100644 --- a/include/user.h +++ b/include/user.h @@ -8,7 +8,9 @@ struct User { uint32_t balance = 0; uint64_t password; +#if MAX_LOG_SIZE > 0 Log log; +#endif User(const std::string &init_pass); User(uint32_t init_bal, const std::string &init_pass); diff --git a/main.cpp b/main.cpp index 42c571a..29c8e61 100644 --- a/main.cpp +++ b/main.cpp @@ -32,9 +32,9 @@ int main(int argc, char **argv) << "\nThreads : " << get_nprocs() + 1 << std::endl; //flushing before EventLoop - static_assert(bool(max_log_size) == bool(pre_log_size), "You must either utilize both or neither logging variables.\n"); - static_assert(max_log_size >= pre_log_size, "The maximum log size must be larger than or equal to the amount preallocated.\n"); - static_assert(!max_log_size || !(max_log_size % pre_log_size), "The maximum log size must be divisible by the preallocation size.\n"); + static_assert(bool(MAX_LOG_SIZE) == bool(PRE_LOG_SIZE), "You must either utilize both or neither logging variables.\n"); + static_assert(MAX_LOG_SIZE >= PRE_LOG_SIZE, "The maximum log size must be larger than or equal to the amount preallocated.\n"); + static_assert(!MAX_LOG_SIZE || !(MAX_LOG_SIZE % PRE_LOG_SIZE), "The maximum log size must be divisible by the preallocation size.\n"); if (argc != 3) { diff --git a/src/bank.cpp b/src/bank.cpp index a629c9c..7d2b05c 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -44,7 +44,7 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam } BankResponse state; - if constexpr (max_log_size > 0) + if constexpr (MAX_LOG_SIZE > 0) { Transaction temp(a_name, b_name, amount); std::shared_lock lock{send_funds_l}; @@ -304,7 +304,7 @@ void Bank::Load() user_save.close(); for (const auto &u : temp.getMemberNames()) { - if constexpr (max_log_size > 0) + if constexpr (MAX_LOG_SIZE > 0) { users.try_emplace(u, temp[u]["balance"].asUInt(), std::move(temp[u]["password"].asUInt64()), temp[u]["log"]); } diff --git a/src/bank_api.cpp b/src/bank_api.cpp index 15d6da7..66946fc 100644 --- a/src/bank_api.cpp +++ b/src/bank_api.cpp @@ -54,7 +54,7 @@ void api::GetBal(req_args, const std::string &name) const } void api::GetLog(req_args) { - if constexpr (max_log_size > 0) + if constexpr (MAX_LOG_SIZE > 0) { RESPONSE_PARSE(bank.GetLogs(NAME_PARAM)); } diff --git a/src/log.cpp b/src/log.cpp index 2650729..6137688 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -2,7 +2,7 @@ void Log::AddTrans(Transaction &&t) { - if (data.size() == max_log_size) // If we hit the max size + if (data.size() == MAX_LOG_SIZE) // If we hit the max size { for (uint32_t i = 1; i < data.size(); i++) // Make room at the back { @@ -13,7 +13,7 @@ void Log::AddTrans(Transaction &&t) } else if (data.size() == data.capacity()) // If we haven't hit the max but hit capacity { - data.reserve(data.capacity() + pre_log_size); // Reserve more memory + data.reserve(data.capacity() + PRE_LOG_SIZE); // Reserve more memory } data.push_back(std::move(t)); // In either case we have space under max length, move to new spot log_flag.SetChangesOn(); diff --git a/src/user.cpp b/src/user.cpp index 084c13c..3e13465 100644 --- a/src/user.cpp +++ b/src/user.cpp @@ -26,8 +26,8 @@ User::User(uint32_t init_bal, uint64_t init_pass, const Json::Value &log_j) : ba { if (log_j.size()) { - log.data.reserve(std::min(pre_log_size * ((log_j.size() / pre_log_size) + 1), max_log_size)); - for (uint32_t i = (log_j.size() - max_log_size) * (log_j.size() > max_log_size); i < log_j.size(); i++) + log.data.reserve(std::min((size_t)PRE_LOG_SIZE * ((log_j.size() / PRE_LOG_SIZE) + 1), (size_t)MAX_LOG_SIZE)); + for (uint32_t i = (log_j.size() - MAX_LOG_SIZE) * (log_j.size() > MAX_LOG_SIZE); i < log_j.size(); i++) { log.data.push_back(Transaction( log_j[i]["from"].asCString(), @@ -47,7 +47,7 @@ Json::Value User::Serialize() const Json::Value res; res["balance"] = (Json::UInt)balance; res["password"] = (Json::UInt64)password; - if constexpr (max_log_size > 0) + if constexpr (MAX_LOG_SIZE > 0) { res["log"] = log.Serialize(); }